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.

refactor: support parsing font-family (#3849)

Bogdan Chadkin0b72f64b801a
diff --git a/packages/css-data/src/parse-css-value.test.ts b/packages/css-data/src/parse-css-value.test.ts
index 3342da1984..471345f1bc 100644
--- a/packages/css-data/src/parse-css-value.test.ts
+++ b/packages/css-data/src/parse-css-value.test.ts
@@ -885,3 +885,38 @@ describe("aspect-ratio", () => {
});
});
});
+
+describe("font-family", () => {
+ test("support single value", () => {
+ expect(parseCssValue("fontFamily", "sans-serif")).toEqual({
+ type: "fontFamily",
+ value: ["sans-serif"],
+ });
+ });
+
+ test("support multiple values", () => {
+ expect(parseCssValue("fontFamily", "serif, sans-serif")).toEqual({
+ type: "fontFamily",
+ value: ["serif", "sans-serif"],
+ });
+ });
+
+ test("support space separated values", () => {
+ expect(parseCssValue("fontFamily", "Song Ti, Hei Ti")).toEqual({
+ type: "fontFamily",
+ value: ["Song Ti", "Hei Ti"],
+ });
+ // only two keywords
+ expect(parseCssValue("fontFamily", "Song Ti")).toEqual({
+ type: "fontFamily",
+ value: ["Song Ti"],
+ });
+ });
+
+ test("support quoted values", () => {
+ expect(parseCssValue("fontFamily", "\"Song Ti\", 'Hei Ti'")).toEqual({
+ type: "fontFamily",
+ value: ["Song Ti", "Hei Ti"],
+ });
+ });
+});
diff --git a/packages/css-data/src/parse-css-value.ts b/packages/css-data/src/parse-css-value.ts
index 125f34eb6b..37b35276b5 100644
--- a/packages/css-data/src/parse-css-value.ts
+++ b/packages/css-data/src/parse-css-value.ts
@@ -399,6 +399,23 @@ export const parseCssValue = (
return invalidValue;
}
+ if (property === "fontFamily") {
+ const nodes = "children" in ast ? ast.children?.toArray() ?? [] : [ast];
+ return {
+ type: "fontFamily",
+ value: splitRepeated(nodes).map((nodes) => {
+ // unquote values
+ if (nodes.length === 1 && nodes[0].type === "String") {
+ return nodes[0].value;
+ }
+ return generate({
+ type: "Value",
+ children: new List<CssNode>().fromArray(nodes),
+ });
+ }),
+ };
+ }
+
// Probably a tuple like background-size or box-shadow
if (
ast.type === "Value" &&