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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
experimental: expand text shorthands (#3545)
Bogdan Chadkinc5438cf6e349
diff --git a/packages/css-data/src/shorthands.test.ts b/packages/css-data/src/shorthands.test.tsindex 5f1840c931..a245e35f7c 100644--- a/packages/css-data/src/shorthands.test.ts+++ b/packages/css-data/src/shorthands.test.ts@@ -376,6 +376,69 @@ test("expand place properties", () => {]);});+test("expand font", () => {+ expect(+ expandShorthands([+ [+ "font",+ `ultra-condensed small-caps bold italic 1.2em "Fira Sans", sans-serif`,+ ],+ ])+ ).toEqual([+ ["font-style", "italic"],+ ["font-variant", "small-caps"],+ ["font-weight", "bold"],+ ["font-width", "ultra-condensed"],+ ["font-size", "1.2em"],+ ["line-height", "initial"],+ ["font-family", '"Fira Sans",sans-serif'],+ ]);+ expect(+ expandShorthands([["font", `1.2em/2 "Fira Sans", sans-serif`]])+ ).toEqual([+ ["font-style", "initial"],+ ["font-variant", "initial"],+ ["font-weight", "initial"],+ ["font-width", "initial"],+ ["font-size", "1.2em"],+ ["line-height", "2"],+ ["font-family", '"Fira Sans",sans-serif'],+ ]);+});++test("expand text-decoration", () => {+ expect(expandShorthands([["text-decoration", `underline`]])).toEqual([+ ["text-decoration-line", "underline"],+ ["text-decoration-style", "initial"],+ ["text-decoration-color", "initial"],+ ]);+ expect(expandShorthands([["text-decoration", `underline dotted`]])).toEqual([+ ["text-decoration-line", "underline"],+ ["text-decoration-style", "dotted"],+ ["text-decoration-color", "initial"],+ ]);+ expect(+ expandShorthands([["text-decoration", `green wavy underline`]])+ ).toEqual([+ ["text-decoration-line", "underline"],+ ["text-decoration-style", "wavy"],+ ["text-decoration-color", "green"],+ ]);+});++test("expand text-emphasis", () => {+ expect(+ expandShorthands([["text-emphasis", "filled double-circle #ffb703"]])+ ).toEqual([+ ["text-emphasis-style", "filled double-circle"],+ ["text-emphasis-color", "#ffb703"],+ ]);+ expect(expandShorthands([["text-emphasis", "none"]])).toEqual([+ ["text-emphasis-style", "none"],+ ["text-emphasis-color", "initial"],+ ]);+});+test.todo("animation");test.todo("container");test.todo("columns");@@ -383,9 +446,6 @@ test.todo("column-rule");test.todo("contain-intrinsic-size");test.todo("flex");test.todo("flex-flow");-test.todo("font");-test.todo("font-synthesis");-test.todo("font-variant");test.todo("grid");test.todo("grid-area");test.todo("grid-column");@@ -398,20 +458,22 @@ test.todo("offset");test.todo("scroll-margin");test.todo("scroll-padding");test.todo("scroll-timeline");-test.todo("text-decoration");-test.todo("text-emphasis");test.todo("transition");-test.todo("all");+test.todo("font-synthesis - only shorthand is supported in webstudio");+test.todo(+ "font-variant - both shorthand and longhands are supported in webstudio"+);+test.todo("white-space - not a shorthand in webflow");+test.todo("text-wrap - not a shorthand in webflow");++test.todo("all - can negatively affect build size");test.todo("background - not used in webflow");test.todo("background-position-x - we use shorthand");test.todo("background-position-y - we use shorthand");test.todo("overflow - used in webflow");test.todo("overflow-x - we use shorthand");test.todo("overflow-y - we use shorthand");-test.todo("text-wrap - webflow use shorthand");-test.todo("white-space");-test.todo("white-space-collapse");test.todo("translate - are these directly mappable to transform");test.todo("rotate");test.todo("scale");diff --git a/packages/css-data/src/shorthands.ts b/packages/css-data/src/shorthands.tsindex 84f674440d..6a8a393c6a 100644--- a/packages/css-data/src/shorthands.ts+++ b/packages/css-data/src/shorthands.ts@@ -12,7 +12,7 @@ const createValueNode = (data?: CssNode[]): Value => ({children: new List<CssNode>().fromArray(data ?? []),});-const createInitialValueNode = (): Value => ({+const createInitialNode = (): Value => ({type: "Value",children: new List<CssNode>().appendData({type: "Identifier",@@ -25,7 +25,7 @@ const getValueList = (value: CssNode): CssNode[] => {return children ?? [value];};-const splitBySlash = (list: List<CssNode>) => {+const splitBySlash = (list: List<CssNode> | CssNode[]) => {const lists: Array<undefined | CssNode[]> = [[]];for (const node of list) {if (node.type === "Operator" && node.value === "/") {@@ -76,7 +76,10 @@ const parseUnordered = (syntaxes: string[], value: CssNode) => {}lastCursor = nextCursor;}- return syntaxes.map((syntax) => matched.get(syntax));+ return [+ ...syntaxes.map((syntax) => matched.get(syntax)),+ createValueNode(unprocessedNodes),+ ];};/**@@ -102,9 +105,9 @@ const expandBorder = function* (property: string, value: CssNode) {["<line-width>", "<line-style>", "<color>"],value);- yield [`${property}-width`, width ?? createInitialValueNode()] as const;- yield [`${property}-style`, style ?? createInitialValueNode()] as const;- yield [`${property}-color`, color ?? createInitialValueNode()] as const;+ yield [`${property}-width`, width ?? createInitialNode()] as const;+ yield [`${property}-style`, style ?? createInitialNode()] as const;+ yield [`${property}-color`, color ?? createInitialNode()] as const;break;}default:@@ -251,9 +254,9 @@ const expandBorderImage = function* (property: string, value: CssNode) {],value);- let slice = createInitialValueNode();- let width = createInitialValueNode();- let outset = createInitialValueNode();+ let slice = createInitialNode();+ let width = createInitialNode();+ let outset = createInitialNode();if (config) {const [sliceNodes, widthNodes, outsetNodes] = splitBySlash(config.children);if (sliceNodes && sliceNodes.length > 0) {@@ -266,11 +269,11 @@ const expandBorderImage = function* (property: string, value: CssNode) {outset = createValueNode(outsetNodes);}}- yield ["border-image-source", source ?? createInitialValueNode()] as const;+ yield ["border-image-source", source ?? createInitialNode()] as const;yield ["border-image-slice", slice] as const;yield ["border-image-width", width] as const;yield ["border-image-outset", outset] as const;- yield ["border-image-repeat", repeat ?? createInitialValueNode()] as const;+ yield ["border-image-repeat", repeat ?? createInitialNode()] as const;};const expandGap = function* (property: string, value: CssNode) {@@ -317,6 +320,95 @@ const expandPlace = function* (property: string, value: CssNode) {yield [property, value] as const;}};+/**+ *+ * font =+ * [ <'font-style'> || <font-variant-css2> || <'font-weight'> || <font-width-css3> ]?+ * <'font-size'> [ / <'line-height'> ]? <'font-family'>+ *+ * text-decoration =+ * <'text-decoration-line'> ||+ * <'text-decoration-style'> ||+ * <'text-decoration-color'>+ *+ * text-emphasis =+ * <'text-emphasis-style'> ||+ * <'text-emphasis-color'>+ */+const expandText = function* (property: string, value: CssNode) {+ switch (property) {+ case "font": {+ // const [before, after] = splitBySlash(getValueList(value));+ const [fontStyle, fontVariant, fontWeight, fontWidth, config] =+ parseUnordered(+ [+ "<'font-style'>",+ // <font-variant-css2> is unsupported by csstree+ "[ normal | small-caps ]",+ "<'font-weight'>",+ // <font-width-css3> is unsupported by csstree+ "[ normal | ultra-condensed | extra-condensed | condensed | semi-condensed | semi-expanded | expanded | extra-expanded | ultra-expanded ]",+ ],+ value+ );+ let fontSize: CssNode = createInitialNode();+ let lineHeight: CssNode = createInitialNode();+ let fontFamily: CssNode = createInitialNode();+ if (config) {+ if (+ lexer.match("<'font-size'> / <'line-height'> <'font-family'>", config)+ .matched+ ) {+ const [fontSizeNode, _slashNode, lineHeightNode, ...fontFamilyNodes] =+ getValueList(config);+ fontSize = fontSizeNode;+ lineHeight = lineHeightNode;+ fontFamily = createValueNode(fontFamilyNodes);+ } else {+ const [fontSizeNode, ...fontFamilyNodes] = getValueList(config);+ fontSize = fontSizeNode;+ fontFamily = createValueNode(fontFamilyNodes);+ }+ }+ yield ["font-style", fontStyle ?? createInitialNode()] as const;+ yield ["font-variant", fontVariant ?? createInitialNode()] as const;+ yield ["font-weight", fontWeight ?? createInitialNode()] as const;+ yield ["font-width", fontWidth ?? createInitialNode()] as const;+ yield ["font-size", fontSize] as const;+ yield ["line-height", lineHeight] as const;+ yield ["font-family", fontFamily] as const;+ break;+ }++ case "text-decoration": {+ const [line, style, color] = parseUnordered(+ [+ "<'text-decoration-line'>",+ "<'text-decoration-style'>",+ "<'text-decoration-color'>",+ ],+ value+ );+ yield ["text-decoration-line", line ?? createInitialNode()] as const;+ yield ["text-decoration-style", style ?? createInitialNode()] as const;+ yield ["text-decoration-color", color ?? createInitialNode()] as const;+ break;+ }++ case "text-emphasis": {+ const [style, color] = parseUnordered(+ ["<'text-emphasis-style'>", "<'text-emphasis-color'>"],+ value+ );+ yield ["text-emphasis-style", style ?? createInitialNode()] as const;+ yield ["text-emphasis-color", color ?? createInitialNode()] as const;+ break;+ }++ default:+ yield [property, value] as const;+ }+};const parseValue = function* (property: string, value: string) {try {@@ -353,7 +445,11 @@ export const expandShorthands = (const generator = expandPlace(property, value);for (const [property, value] of generator) {- longhands.push([property, generate(value)]);+ const generator = expandText(property, value);++ for (const [property, value] of generator) {+ longhands.push([property, generate(value)]);+ }}}}