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.

Parse tuple in css-data (#1548)

Bogdan Chadkin403edf652301
diff --git a/packages/css-data/bin/mdn-data.ts b/packages/css-data/bin/mdn-data.ts
index 52c5f27ee8..46d988ca26 100755
--- a/packages/css-data/bin/mdn-data.ts
+++ b/packages/css-data/bin/mdn-data.ts
@@ -1,5 +1,5 @@
/* eslint-disable import/no-internal-modules */
-import { parse, definitionSyntax, type DSNode } from "css-tree";
+import { parse, definitionSyntax, type DSNode, type CssNode } from "css-tree";
import properties from "mdn-data/css/properties.json";
import syntaxes from "mdn-data/css/syntaxes.json";
import data from "css-tree/dist/data";
@@ -7,7 +7,13 @@ import { popularityIndex } from "../src/popularity-index";
import camelCase from "camelcase";
import * as fs from "fs";
import * as path from "path";
-import type { StyleValue, Unit } from "../src";
+import type {
+ KeywordValue,
+ StyleValue,
+ Unit,
+ UnitValue,
+ UnparsedValue,
+} from "../src/schema";
const units = {
number: [],
@@ -66,29 +72,12 @@ const beautifyKeyword = (property: string, keyword: string) => {
return keyword;
};
-const parseInitialValue = (
+const convertToStyleValue = (
+ node: CssNode,
property: string,
value: string,
unitGroups: Set<string>
-): StyleValue => {
- // Our default values hardcoded because no single standard
- if (property in normalizedValues) {
- return normalizedValues[property as keyof typeof normalizedValues];
- }
- const ast = parse(value, { context: "value" });
- if (ast.type !== "Value") {
- throw Error(`Unknown parsed type ${ast.type}`);
- }
-
- // more than 2 values consider as keyword
- if (ast.children.first !== ast.children.last) {
- return {
- type: "keyword",
- value: beautifyKeyword(property, value),
- };
- }
-
- const node = ast.children.first;
+): undefined | UnitValue | KeywordValue | UnparsedValue => {
if (node?.type === "Identifier") {
return {
type: "keyword",
@@ -127,6 +116,49 @@ const parseInitialValue = (
value: Number(node.value),
};
}
+};
+
+const parseInitialValue = (
+ property: string,
+ value: string,
+ unitGroups: Set<string>
+): StyleValue => {
+ // Our default values hardcoded because no single standard
+ if (property in normalizedValues) {
+ return normalizedValues[property as keyof typeof normalizedValues];
+ }
+ const ast = parse(value, { context: "value" });
+ if (ast.type !== "Value") {
+ throw Error(`Unknown parsed type ${ast.type}`);
+ }
+
+ // more than 2 values consider as keyword
+ if (ast.children.first !== ast.children.last) {
+ return {
+ type: "tuple",
+ value: ast.children.toArray().map((node) => {
+ const styleValue = convertToStyleValue(
+ node,
+ property,
+ value,
+ unitGroups
+ );
+ if (styleValue !== undefined) {
+ return styleValue;
+ }
+ throw Error(`Cannot find initial for ${property}`);
+ }),
+ };
+ }
+
+ const node = ast.children.first;
+ let styleValue: undefined | StyleValue;
+ if (node) {
+ styleValue = convertToStyleValue(node, property, value, unitGroups);
+ }
+ if (styleValue !== undefined) {
+ return styleValue;
+ }
throw Error(`Cannot find initial for ${property}`);
};
diff --git a/packages/css-data/package.json b/packages/css-data/package.json
index d7e0abbcaa..cd654fa7ba 100644
--- a/packages/css-data/package.json
+++ b/packages/css-data/package.json
@@ -23,6 +23,7 @@
"html-tags": "^3.2.0",
"mdn-data": "2.0.30",
"tsx": "^3.12.6",
+ "type-fest": "^3.7.1",
"typescript": "5.0.3",
"zod": "^3.19.1"
},
diff --git a/packages/css-data/src/__generated__/properties.ts b/packages/css-data/src/__generated__/properties.ts
index 9a1d9f4c44..da224431c9 100644
--- a/packages/css-data/src/__generated__/properties.ts
+++ b/packages/css-data/src/__generated__/properties.ts
@@ -257,8 +257,19 @@ export const properties = {
unitGroups: ["length", "percentage"],
inherited: false,
initial: {
- type: "keyword",
- value: "0% 0%",
+ type: "tuple",
+ value: [
+ {
+ type: "unit",
+ unit: "%",
+ value: 0,
+ },
+ {
+ type: "unit",
+ unit: "%",
+ value: 0,
+ },
+ ],
},
popularity: 0.76724967,
appliesTo: "allElements",
@@ -1344,8 +1355,17 @@ export const properties = {
unitGroups: [],
inherited: true,
initial: {
- type: "keyword",
- value: "weight style",
+ type: "tuple",
+ value: [
+ {
+ type: "keyword",
+ value: "weight",
+ },
+ {
+ type: "keyword",
+ value: "style",
+ },
+ ],
},
popularity: 0.00686166,
appliesTo: "allElements",
@@ -2239,8 +2259,19 @@ export const properties = {
unitGroups: ["length", "percentage"],
inherited: true,
initial: {
- type: "keyword",
- value: "50% 50%",
+ type: "tuple",
+ value: [
+ {
+ type: "unit",
+ unit: "%",
+ value: 50,
+ },
+ {
+ type: "unit",
+ unit: "%",
+ value: 50,
+ },
+ ],
},
popularity: 0.08374319,
appliesTo: "replacedElements",
@@ -2643,8 +2674,19 @@ export const properties = {
unitGroups: ["length", "percentage"],
inherited: false,
initial: {
- type: "keyword",
- value: "50% 50%",
+ type: "tuple",
+ value: [
+ {
+ type: "unit",
+ unit: "%",
+ value: 50,
+ },
+ {
+ type: "unit",
+ unit: "%",
+ value: 50,
+ },
+ ],
},
popularity: 0.03597136,
appliesTo: "transformableElements",
@@ -3175,8 +3217,17 @@ export const properties = {
unitGroups: [],
inherited: false,
initial: {
- type: "keyword",
- value: "over right",
+ type: "tuple",
+ value: [
+ {
+ type: "keyword",
+ value: "over",
+ },
+ {
+ type: "keyword",
+ value: "right",
+ },
+ ],
},
popularity: 0,
appliesTo: "allElements",
@@ -3337,8 +3388,24 @@ export const properties = {
unitGroups: ["length", "percentage"],
inherited: false,
initial: {
- type: "keyword",
- value: "50% 50% 0",
+ type: "tuple",
+ value: [
+ {
+ type: "unit",
+ unit: "%",
+ value: 50,
+ },
+ {
+ type: "unit",
+ unit: "%",
+ value: 50,
+ },
+ {
+ type: "unit",
+ unit: "px",
+ value: 0,
+ },
+ ],
},
popularity: 0.54179205,
appliesTo: "transformableElements",
diff --git a/packages/css-data/src/index.ts b/packages/css-data/src/index.ts
index 0c9a684170..a1bfe9592f 100644
--- a/packages/css-data/src/index.ts
+++ b/packages/css-data/src/index.ts
@@ -1,8 +1,15 @@
+import type { WritableDeep } from "type-fest";
import type { Html } from "./html";
import * as exportedHtml from "./html";
export const html: Html = exportedHtml;
export * from "./__generated__/keyword-values";
-export * from "./__generated__/properties";
export * from "./__generated__/units";
export * from "./schema";
+
+import { properties as generatedProperties } from "./__generated__/properties";
+
+// convert to writable to avoid conflicts with schema type
+export const properties = generatedProperties as WritableDeep<
+ typeof generatedProperties
+>;
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 4207ad5454..de026f88e5 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -479,6 +479,9 @@ importers:
tsx:
specifier: ^3.12.6
version: 3.12.6
+ type-fest:
+ specifier: ^3.7.1
+ version: 3.7.1
typescript:
specifier: 5.0.3
version: 5.0.3