Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

experimental: support mask and mask-border shorthands (#3577)

Bogdan Chadkin3ded54d59b64
diff --git a/packages/css-data/src/shorthands.test.ts b/packages/css-data/src/shorthands.test.ts
index c22b318164..e566c2113f 100644
--- a/packages/css-data/src/shorthands.test.ts
+++ b/packages/css-data/src/shorthands.test.ts
@@ -594,6 +594,83 @@ test("expand transition", () => {
]);
});
+test("expand mask", () => {
+ expect(expandShorthands([["mask", `none`]])).toEqual([
+ ["mask-image", "none"],
+ ["mask-position", "0% 0%"],
+ ["mask-size", "auto"],
+ ["mask-repeat", "repeat"],
+ ["mask-origin", "border-box"],
+ ["mask-clip", "border-box"],
+ ["mask-composite", "add"],
+ ["mask-mode", "match-source"],
+ ]);
+ expect(expandShorthands([["mask", `url(mask.png)`]])).toEqual([
+ ["mask-image", "url(mask.png)"],
+ ["mask-position", "0% 0%"],
+ ["mask-size", "auto"],
+ ["mask-repeat", "repeat"],
+ ["mask-origin", "border-box"],
+ ["mask-clip", "border-box"],
+ ["mask-composite", "add"],
+ ["mask-mode", "match-source"],
+ ]);
+ expect(
+ expandShorthands([["mask", `url(masks.svg#star) 0 0/50px 50px`]])
+ ).toEqual([
+ ["mask-image", "url(masks.svg#star)"],
+ ["mask-position", "0 0"],
+ ["mask-size", "50px 50px"],
+ ["mask-repeat", "repeat"],
+ ["mask-origin", "border-box"],
+ ["mask-clip", "border-box"],
+ ["mask-composite", "add"],
+ ["mask-mode", "match-source"],
+ ]);
+ expect(
+ expandShorthands([
+ [
+ "mask",
+ `url(masks.svg#star) left / 16px repeat-y, url(masks.svg#circle) right / 16px repeat-y`,
+ ],
+ ])
+ ).toEqual([
+ ["mask-image", "url(masks.svg#star),url(masks.svg#circle)"],
+ ["mask-position", "left,right"],
+ ["mask-size", "16px,16px"],
+ ["mask-repeat", "repeat-y,repeat-y"],
+ ["mask-origin", "border-box,border-box"],
+ ["mask-clip", "border-box,border-box"],
+ ["mask-composite", "add,add"],
+ ["mask-mode", "match-source,match-source"],
+ ]);
+});
+
+test("expand mask-border", () => {
+ expect(
+ expandShorthands([["mask-border", `url("border-mask.png") 25`]])
+ ).toEqual([
+ ["mask-border-source", "url(border-mask.png)"],
+ ["mask-border-slice", "25"],
+ ["mask-border-width", "initial"],
+ ["mask-border-outset", "initial"],
+ ["mask-border-repeat", "initial"],
+ ["mask-border-mode", "initial"],
+ ]);
+ expect(
+ expandShorthands([
+ ["mask-border", `url("border-mask.png") 25 / 35px / 12px space alpha`],
+ ])
+ ).toEqual([
+ ["mask-border-source", "url(border-mask.png)"],
+ ["mask-border-slice", "25"],
+ ["mask-border-width", "35px"],
+ ["mask-border-outset", "12px"],
+ ["mask-border-repeat", "space"],
+ ["mask-border-mode", "alpha"],
+ ]);
+});
+
test.todo("container");
test.todo("contain-intrinsic-size");
test.todo("grid");
@@ -601,8 +678,6 @@ test.todo("grid-area");
test.todo("grid-column");
test.todo("grid-row");
test.todo("grid-template");
-test.todo("mask");
-test.todo("mask-border");
test.todo("offset");
test.todo("scroll-margin");
test.todo("scroll-padding");
diff --git a/packages/css-data/src/shorthands.ts b/packages/css-data/src/shorthands.ts
index 61b6e6a85e..7a6c1b370e 100644
--- a/packages/css-data/src/shorthands.ts
+++ b/packages/css-data/src/shorthands.ts
@@ -116,6 +116,9 @@ const parseUnordered = (syntaxes: string[], value: CssNode) => {
*
* border = <line-width> || <line-style> || <color>
*
+ * should also reset border-image but would bloat
+ * resulting data for very rare usecase
+ *
*/
const expandBorder = function* (property: string, value: CssNode) {
switch (property) {
@@ -309,7 +312,6 @@ const expandBorderImage = function* (value: CssNode) {
*
*/
const expandFont = function* (value: CssNode) {
- // const [before, after] = splitBySlash(getValueList(value));
const [fontStyle, fontVariant, fontWeight, fontWidth, config] =
parseUnordered(
[
@@ -525,6 +527,142 @@ const expandTransition = function* (value: CssNode) {
] as const;
};
+/**
+ *
+ * mask = <mask-layer>#
+ *
+ * <mask-layer> =
+ * <mask-reference> ||
+ * <position> [ / <bg-size> ]? ||
+ * <repeat-style> ||
+ * <geometry-box> ||
+ * [ <geometry-box> | no-clip ] ||
+ * <compositing-operator> ||
+ * <masking-mode>
+ *
+ * should also reset mask-border but would bloat
+ * resulting data for very rare usecase
+ *
+ */
+const expandMask = function* (value: CssNode) {
+ const references: CssNode[] = [];
+ const positions: CssNode[] = [];
+ const bgSizes: CssNode[] = [];
+ const repeatStyles: CssNode[] = [];
+ const origins: CssNode[] = [];
+ const clips: CssNode[] = [];
+ const compositionOperators: CssNode[] = [];
+ const modes: CssNode[] = [];
+ for (const animationNodes of splitByOperator(getValueList(value), ",")) {
+ const [
+ reference,
+ positionAndSize,
+ repeatStyle,
+ origin,
+ clip,
+ compositingOperator,
+ mode,
+ ] = parseUnordered(
+ [
+ "<mask-reference>",
+ "<position> [ / <bg-size> ]?",
+ "<repeat-style>",
+ "<geometry-box>",
+ "[ <geometry-box> | no-clip ]",
+ "<compositing-operator>",
+ "<masking-mode>",
+ ],
+ createValueNode(animationNodes)
+ );
+ references.push(reference ?? createIdentifier("none"));
+ let position = createValueNode([
+ { type: "Dimension", value: "0", unit: "%" },
+ { type: "Dimension", value: "0", unit: "%" },
+ ]);
+ let bgSize = createIdentifier("auto");
+ if (positionAndSize) {
+ const [positionNodes, bgSizeNodes] = splitByOperator(
+ getValueList(positionAndSize),
+ "/"
+ );
+ position = createValueNode(positionNodes);
+ bgSize = bgSizeNodes ? createValueNode(bgSizeNodes) : position;
+ }
+ positions.push(position);
+ bgSizes.push(bgSize);
+ repeatStyles.push(repeatStyle ?? createIdentifier("repeat"));
+ origins.push(origin ?? createIdentifier("border-box"));
+ clips.push(clip ?? origin ?? createIdentifier("border-box"));
+ compositionOperators.push(compositingOperator ?? createIdentifier("add"));
+ modes.push(mode ?? createIdentifier("match-source"));
+ }
+ yield [
+ "mask-image",
+ createValueNode(joinByOperator(references, ",")),
+ ] as const;
+ yield [
+ "mask-position",
+ createValueNode(joinByOperator(positions, ",")),
+ ] as const;
+ yield ["mask-size", createValueNode(joinByOperator(bgSizes, ","))] as const;
+ yield [
+ "mask-repeat",
+ createValueNode(joinByOperator(repeatStyles, ",")),
+ ] as const;
+ yield ["mask-origin", createValueNode(joinByOperator(origins, ","))] as const;
+ yield ["mask-clip", createValueNode(joinByOperator(clips, ","))] as const;
+ yield [
+ "mask-composite",
+ createValueNode(joinByOperator(compositionOperators, ",")),
+ ] as const;
+ yield ["mask-mode", createValueNode(joinByOperator(modes, ","))] as const;
+};
+
+/**
+ *
+ * mask-border =
+ * <'mask-border-source'> ||
+ * <'mask-border-slice'> [ / <'mask-border-width'>? [ / <'mask-border-outset'> ]? ]? ||
+ * <'mask-border-repeat'> ||
+ * <'mask-border-mode'>
+ *
+ */
+const expandMaskBorder = function* (value: CssNode) {
+ const [source, config, repeat, mode] = parseUnordered(
+ [
+ "<'mask-border-source'>",
+ "<'mask-border-slice'> [ / <'mask-border-width'>? [ / <'mask-border-outset'> ]? ]?",
+ "<'mask-border-repeat'>",
+ "<'mask-border-mode'>",
+ ],
+ value
+ );
+ let slice: undefined | CssNode;
+ let width: undefined | CssNode;
+ let outset: undefined | CssNode;
+ if (config) {
+ const [sliceNodes, widthNodes, outsetNodes] = splitByOperator(
+ config.children,
+ "/"
+ );
+ if (sliceNodes && sliceNodes.length > 0) {
+ slice = createValueNode(sliceNodes);
+ }
+ if (widthNodes && widthNodes.length > 0) {
+ width = createValueNode(widthNodes);
+ }
+ if (outsetNodes && outsetNodes.length > 0) {
+ outset = createValueNode(outsetNodes);
+ }
+ }
+ yield ["mask-border-source", source ?? createInitialNode()] as const;
+ yield ["mask-border-slice", slice ?? createInitialNode()] as const;
+ yield ["mask-border-width", width ?? createInitialNode()] as const;
+ yield ["mask-border-outset", outset ?? createInitialNode()] as const;
+ yield ["mask-border-repeat", repeat ?? createInitialNode()] as const;
+ yield ["mask-border-mode", mode ?? createInitialNode()] as const;
+};
+
const expandShorthand = function* (property: string, value: CssNode) {
switch (property) {
case "font":
@@ -564,6 +702,14 @@ const expandShorthand = function* (property: string, value: CssNode) {
yield* expandBorderImage(value);
break;
+ case "mask":
+ yield* expandMask(value);
+ break;
+
+ case "mask-border":
+ yield* expandMaskBorder(value);
+ break;
+
case "gap":
case "grid-gap": {
const [rowGap, columnGap] = getValueList(value);