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: support flex shorthands (#3551)
Bogdan Chadkin7fcef25e3f96
diff --git a/packages/css-data/src/shorthands.test.ts b/packages/css-data/src/shorthands.test.tsindex a245e35f7c..8df94e16a4 100644--- a/packages/css-data/src/shorthands.test.ts+++ b/packages/css-data/src/shorthands.test.ts@@ -439,13 +439,59 @@ test("expand text-emphasis", () => {]);});+test("expand flex", () => {+ expect(expandShorthands([["flex", "initial"]])).toEqual([+ ["flex-grow", "0"],+ ["flex-shrink", "1"],+ ["flex-basis", "auto"],+ ]);+ expect(expandShorthands([["flex", "auto"]])).toEqual([+ ["flex-grow", "1"],+ ["flex-shrink", "1"],+ ["flex-basis", "auto"],+ ]);+ expect(expandShorthands([["flex", "none"]])).toEqual([+ ["flex-grow", "0"],+ ["flex-shrink", "0"],+ ["flex-basis", "auto"],+ ]);+ expect(expandShorthands([["flex", "10px"]])).toEqual([+ ["flex-grow", "1"],+ ["flex-shrink", "1"],+ ["flex-basis", "10px"],+ ]);+ expect(expandShorthands([["flex", "2"]])).toEqual([+ ["flex-grow", "2"],+ ["flex-shrink", "1"],+ ["flex-basis", "0"],+ ]);+ expect(expandShorthands([["flex", "2 3"]])).toEqual([+ ["flex-grow", "2"],+ ["flex-shrink", "3"],+ ["flex-basis", "0"],+ ]);+});++test("expand flex-flow", () => {+ expect(expandShorthands([["flex-flow", "row"]])).toEqual([+ ["flex-direction", "row"],+ ["flex-wrap", "initial"],+ ]);+ expect(expandShorthands([["flex-flow", "nowrap"]])).toEqual([+ ["flex-direction", "initial"],+ ["flex-wrap", "nowrap"],+ ]);+ expect(expandShorthands([["flex-flow", "row nowrap"]])).toEqual([+ ["flex-direction", "row"],+ ["flex-wrap", "nowrap"],+ ]);+});+test.todo("animation");test.todo("container");test.todo("columns");test.todo("column-rule");test.todo("contain-intrinsic-size");-test.todo("flex");-test.todo("flex-flow");test.todo("grid");test.todo("grid-area");test.todo("grid-column");diff --git a/packages/css-data/src/shorthands.ts b/packages/css-data/src/shorthands.tsindex 6a8a393c6a..a5c3ef9c9e 100644--- a/packages/css-data/src/shorthands.ts+++ b/packages/css-data/src/shorthands.ts@@ -296,89 +296,84 @@ const expandGap = function* (property: string, value: CssNode) {}};-const expandPlace = function* (property: string, value: CssNode) {- switch (property) {- case "place-content": {- const [align, justify] = getValueList(value);- yield ["align-content", align] as const;- yield ["justify-content", justify ?? align] as const;- break;- }- case "place-items": {- const [align, justify] = getValueList(value);- yield ["align-items", align] as const;- yield ["justify-items", justify ?? align] as const;- break;- }- case "place-self": {- const [align, justify] = getValueList(value);- yield ["align-self", align] as const;- yield ["justify-self", justify ?? align] as const;- break;- }- default:- 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) {+const expandFont = function* (value: CssNode) {+ // 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;+};++const expandFlex = function* (value: CssNode) {+ const zero = createValueNode([{ type: "Number", value: "0" }]);+ const one = createValueNode([{ type: "Number", value: "1" }]);+ const auto = createValueNode([{ type: "Identifier", name: "auto" }]);+ let grow: undefined | Value;+ let shrink: undefined | Value;+ let basis: undefined | Value;+ if (lexer.match("initial", value).matched) {+ [grow, shrink, basis] = [zero, one, auto];+ } else if (lexer.match("auto", value).matched) {+ [grow, shrink, basis] = [one, one, auto];+ } else if (lexer.match("none", value).matched) {+ [grow, shrink, basis] = [zero, zero, auto];+ } else {+ [grow, shrink, basis] = parseUnordered(+ ["<'flex-grow'>", "<'flex-shrink'>", "<'flex-basis'>"],+ value+ );+ }+ yield ["flex-grow", grow ?? one] as const;+ yield ["flex-shrink", shrink ?? one] as const;+ yield ["flex-basis", basis ?? zero] as const;+};++const expandShorthandsAst = 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;+ case "font":+ yield* expandFont(value);break;- }case "text-decoration": {const [line, style, color] = parseUnordered(@@ -405,6 +400,41 @@ const expandText = function* (property: string, value: CssNode) {break;}+ case "flex":+ yield* expandFlex(value);+ break;++ case "flex-flow": {+ const [direction, wrap] = parseUnordered(+ ["<'flex-direction'>", "<'flex-wrap'>"],+ value+ );+ yield ["flex-direction", direction ?? createInitialNode()] as const;+ yield ["flex-wrap", wrap ?? createInitialNode()] as const;+ break;+ }++ case "place-content": {+ const [align, justify] = getValueList(value);+ yield ["align-content", align] as const;+ yield ["justify-content", justify ?? align] as const;+ break;+ }++ case "place-items": {+ const [align, justify] = getValueList(value);+ yield ["align-items", align] as const;+ yield ["justify-items", justify ?? align] as const;+ break;+ }++ case "place-self": {+ const [align, justify] = getValueList(value);+ yield ["align-self", align] as const;+ yield ["justify-self", justify ?? align] as const;+ break;+ }+default:yield [property, value] as const;}@@ -442,14 +472,10 @@ export const expandShorthands = (const generator = expandGap(property, value);for (const [property, value] of generator) {- const generator = expandPlace(property, value);+ const generator = expandShorthandsAst(property, value);for (const [property, value] of generator) {- const generator = expandText(property, value);-- for (const [property, value] of generator) {- longhands.push([property, generate(value)]);- }+ longhands.push([property, generate(value)]);}}}