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
refactor: move shadow parser into value parser (#3723)
Bogdan Chadkin0373c52a533e
diff --git a/packages/css-data/src/parse-css-value.test.ts b/packages/css-data/src/parse-css-value.test.tsindex 43d765632c..8a3a6d3636 100644--- a/packages/css-data/src/parse-css-value.test.ts+++ b/packages/css-data/src/parse-css-value.test.ts@@ -619,3 +619,170 @@ test("support custom properties var reference in custom property", () => {fallbacks: [{ type: "unparsed", value: "red" }],});});++describe("parse shadows", () => {+ test("parses value and returns invalid when used a invalid boxShadow is passed", () => {+ expect(parseCssValue("boxShadow", `10px 10px 5px foo`)).toEqual({+ type: "invalid",+ value: "10px 10px 5px foo",+ });+ });++ test("parses value and returns invalid when a invalid textShadow is passed", () => {+ expect(parseCssValue("textShadow", `10px 10px 5px foo`)).toEqual({+ type: "invalid",+ value: "10px 10px 5px foo",+ });+ });++ test("throws error when passed a value without a unit", () => {+ expect(parseCssValue("boxShadow", `10 10px 5px red`)).toEqual({+ type: "invalid",+ value: "10 10px 5px red",+ });+ });++ test("parses values and returns a layer when a valid textShadow is passes", () => {+ expect(parseCssValue("textShadow", "1px 1px 2px black")).toEqual({+ type: "layers",+ value: [+ {+ type: "tuple",+ value: [+ { type: "unit", unit: "px", value: 1 },+ { type: "unit", unit: "px", value: 1 },+ { type: "unit", unit: "px", value: 2 },+ { type: "keyword", value: "black" },+ ],+ },+ ],+ });+ });++ test("inset and color values can be interchanged", () => {+ expect(parseCssValue("boxShadow", `inset 10px 10px 5px black`)).toEqual({+ type: "layers",+ value: [+ {+ type: "tuple",+ value: [+ { type: "keyword", value: "inset" },+ { type: "unit", unit: "px", value: 10 },+ { type: "unit", unit: "px", value: 10 },+ { type: "unit", unit: "px", value: 5 },+ { type: "keyword", value: "black" },+ ],+ },+ ],+ });+ });++ test("parses value when inset is used but missing blur-radius", () => {+ expect(parseCssValue("boxShadow", `inset 5em 1em gold`)).toEqual({+ type: "layers",+ value: [+ {+ type: "tuple",+ value: [+ { type: "keyword", value: "inset" },+ { type: "unit", unit: "em", value: 5 },+ { type: "unit", unit: "em", value: 1 },+ { type: "keyword", value: "gold" },+ ],+ },+ ],+ });+ });++ test("parses value when offsetX and offsetY are used", () => {+ expect(parseCssValue("boxShadow", `60px -16px teal`)).toEqual({+ type: "layers",+ value: [+ {+ type: "tuple",+ value: [+ { type: "unit", unit: "px", value: 60 },+ { type: "unit", unit: "px", value: -16 },+ { type: "keyword", value: "teal" },+ ],+ },+ ],+ });+ });++ test("parses value from figma", () => {+ expect(+ parseCssValue(+ "boxShadow",+ "0 60px 80px rgba(0,0,0,0.60), 0 45px 26px rgba(0,0,0,0.14)"+ )+ ).toEqual({+ type: "layers",+ value: [+ {+ type: "tuple",+ value: [+ { type: "unit", unit: "number", value: 0 },+ { type: "unit", unit: "px", value: 60 },+ { type: "unit", unit: "px", value: 80 },+ { alpha: 0.6, b: 0, g: 0, r: 0, type: "rgb" },+ ],+ },+ {+ type: "tuple",+ value: [+ { type: "unit", unit: "number", value: 0 },+ { type: "unit", unit: "px", value: 45 },+ { type: "unit", unit: "px", value: 26 },+ { alpha: 0.14, b: 0, g: 0, r: 0, type: "rgb" },+ ],+ },+ ],+ });+ });++ test(`parses multiple layers of box-shadow property`, () => {+ expect(+ parseCssValue(+ "boxShadow",+ `+ 0 0 5px rgba(0, 0, 0, 0.2),+ inset 0 0 10px rgba(0, 0, 0, 0.3),+ 0 0 15px rgba(0, 0, 0, 0.4)+ `+ )+ ).toEqual({+ type: "layers",+ value: [+ {+ type: "tuple",+ value: [+ { type: "unit", unit: "number", value: 0 },+ { type: "unit", unit: "number", value: 0 },+ { type: "unit", unit: "px", value: 5 },+ { alpha: 0.2, b: 0, g: 0, r: 0, type: "rgb" },+ ],+ },+ {+ type: "tuple",+ value: [+ { type: "keyword", value: "inset" },+ { type: "unit", unit: "number", value: 0 },+ { type: "unit", unit: "number", value: 0 },+ { type: "unit", unit: "px", value: 10 },+ { alpha: 0.3, b: 0, g: 0, r: 0, type: "rgb" },+ ],+ },+ {+ type: "tuple",+ value: [+ { type: "unit", unit: "number", value: 0 },+ { type: "unit", unit: "number", value: 0 },+ { type: "unit", unit: "px", value: 15 },+ { alpha: 0.4, b: 0, g: 0, r: 0, type: "rgb" },+ ],+ },+ ],+ });+ });+});diff --git a/packages/css-data/src/parse-css-value.ts b/packages/css-data/src/parse-css-value.tsindex c2b3e60252..2ccff13e3d 100644--- a/packages/css-data/src/parse-css-value.ts+++ b/packages/css-data/src/parse-css-value.ts@@ -23,7 +23,6 @@ import { parseTranslate } from "./property-parsers/translate";import { parseTransform } from "./property-parsers/transform";import { parseScale } from "./property-parsers/scale";import { parseFilter } from "./property-parsers/filter";-import { parseShadow } from "./property-parsers/shadows";export const cssTryParseValue = (input: string) => {try {@@ -124,8 +123,12 @@ const repeatedProps = new Set<StyleProperty>(["transitionDelay","transitionTimingFunction","transitionBehavior",+ "boxShadow",+ "textShadow",]);+const tupleProps = new Set<StyleProperty>(["boxShadow", "textShadow"]);+const availableUnits = new Set<string>(Object.values(units).flat());const parseColor = (colorString: string): undefined | RgbValue => {@@ -281,10 +284,6 @@ export const parseCssValue = (return parseFilter(property, input);}- if (property === "boxShadow" || property === "textShadow") {- return parseShadow(property, input);- }-// prevent infinite splitting into layers for itemsif (repeatedProps.has(property) && topLevel) {const nodes = "children" in ast ? ast.children?.toArray() ?? [] : [ast];@@ -360,8 +359,11 @@ export const parseCssValue = (}}- // Probably a tuple like background-size- if (ast.type === "Value" && ast.children.size > 1) {+ // Probably a tuple like background-size or box-shadow+ if (+ ast.type === "Value" &&+ (ast.children.size > 1 || tupleProps.has(property))+ ) {const tuple: TupleValue = {type: "tuple",value: [],diff --git a/packages/css-data/src/property-parsers/shadow-properties-extractor.test.ts b/packages/css-data/src/property-parsers/shadow-properties-extractor.test.tsindex f860ab978d..585066e287 100644--- a/packages/css-data/src/property-parsers/shadow-properties-extractor.test.ts+++ b/packages/css-data/src/property-parsers/shadow-properties-extractor.test.ts@@ -1,7 +1,7 @@import { describe, expect, test } from "@jest/globals";import { extractShadowProperties } from "./shadow-properties-extractor";-import { parseShadow } from "./shadows";-import type { TupleValue } from "@webstudio-is/css-engine";+import type { LayersValue, TupleValue } from "@webstudio-is/css-engine";+import { parseCssValue } from "../parse-css-value";describe("Extract Box-Shadow Properties", () => {test("tokenize and extracts values from 5em 5em 2em 2em #0000", () => {@@ -126,10 +126,10 @@ describe("Extract Box-Shadow Properties", () => {});test("tokenize and extract values from 5em 5em 0em 5em inset #ffff", () => {- const layers = parseShadow("boxShadow", "5em 5em 0em 5em inset #ffff");- if (layers.type === "invalid") {- throw new Error(`Failed in parsing box-shadow string`);- }+ const layers = parseCssValue(+ "boxShadow",+ "5em 5em 0em 5em inset #ffff"+ ) as LayersValue;const layer = layers.value.find((layer): layer is TupleValue => layer.type === "tuple"@@ -153,7 +153,8 @@ describe("Extract Box-Shadow Properties", () => {describe("Extract Text-Shadow Properties", () => {test("tokenize and extracts values from 5em 10em 2em #0000", () => {const { offsetX, offsetY, blur, color } = extractShadowProperties(- parseShadow("textShadow", "5em 10em 2em #0000")?.value[0] as TupleValue+ (parseCssValue("textShadow", "5em 10em 2em #0000") as LayersValue)+ ?.value[0] as TupleValue);expect(offsetY).toBeDefined();@@ -164,7 +165,8 @@ describe("Extract Text-Shadow Properties", () => {test("tokenize and extracts values from 5em 10em", () => {const { offsetX, offsetY, blur, color } = extractShadowProperties(- parseShadow("textShadow", "5em 10em")?.value[0] as TupleValue+ (parseCssValue("textShadow", "5em 10em") as LayersValue)+ ?.value[0] as TupleValue);expect(offsetY?.value).toBe(10);@@ -173,16 +175,4 @@ describe("Extract Text-Shadow Properties", () => {expect(color).not.toBeDefined();expect(blur).not.toBeDefined();});-- test("tokenize and extracts values from box-shadow: red 60px -16px;", () => {- const { offsetX, offsetY, blur, color } = extractShadowProperties(- parseShadow("textShadow", "text-shadow: red 60px -16px;")- ?.value[0] as TupleValue- );-- expect(offsetX?.value).toBe(60);- expect(offsetY?.value).toBe(-16);- expect(color?.type).toBe("keyword");- expect(blur).not.toBeDefined();- });});diff --git a/packages/css-data/src/property-parsers/shadows.test.ts b/packages/css-data/src/property-parsers/shadows.test.tsdeleted file mode 100644index e366b0134b..0000000000--- a/packages/css-data/src/property-parsers/shadows.test.ts+++ /dev/null@@ -1,345 +0,0 @@-import { describe, expect, test } from "@jest/globals";--import { parseShadow } from "./shadows";--describe("parse shadows", () => {- test("parses value and returns invalid when used a invalid boxShadow is passed", () => {- expect(parseShadow("boxShadow", `10px 10px 5px foo`))- .toMatchInlineSnapshot(`- {- "type": "invalid",- "value": "10px 10px 5px foo",- }- `);- });-- test("parses value and returns invalid when a invalid textShadow is passed", () => {- expect(parseShadow("textShadow", `10px 10px 5px foo`))- .toMatchInlineSnapshot(`- {- "type": "invalid",- "value": "10px 10px 5px foo",- }- `);- });-- test("throws error when passed a value without a unit", () => {- expect(parseShadow("boxShadow", `10 10px 5px red`)).toMatchInlineSnapshot(`- {- "type": "invalid",- "value": "10 10px 5px red",- }- `);- });-- test("parses values and returns a layer when a valid textShadow is passes", () => {- expect(parseShadow("textShadow", "text-shadow: 1px 1px 2px black;"))- .toMatchInlineSnapshot(`-{- "type": "layers",- "value": [- {- "type": "tuple",- "value": [- {- "type": "unit",- "unit": "px",- "value": 1,- },- {- "type": "unit",- "unit": "px",- "value": 1,- },- {- "type": "unit",- "unit": "px",- "value": 2,- },- {- "type": "keyword",- "value": "black",- },- ],- },- ],-}-`);- });-- test("inset and color values can be interchanged", () => {- expect(parseShadow("boxShadow", `inset 10px 10px 5px black;`))- .toMatchInlineSnapshot(`- {- "type": "layers",- "value": [- {- "type": "tuple",- "value": [- {- "type": "keyword",- "value": "inset",- },- {- "type": "unit",- "unit": "px",- "value": 10,- },- {- "type": "unit",- "unit": "px",- "value": 10,- },- {- "type": "unit",- "unit": "px",- "value": 5,- },- {- "type": "keyword",- "value": "black",- },- ],- },- ],- }- `);- });-- test("parses value when inset is used but missing blur-radius", () => {- expect(parseShadow("boxShadow", `box-shadow: inset 5em 1em gold`))- .toMatchInlineSnapshot(`- {- "type": "layers",- "value": [- {- "type": "tuple",- "value": [- {- "type": "keyword",- "value": "inset",- },- {- "type": "unit",- "unit": "em",- "value": 5,- },- {- "type": "unit",- "unit": "em",- "value": 1,- },- {- "type": "keyword",- "value": "gold",- },- ],- },- ],- }- `);- });-- test("parses value when offsetX and offsetY are used", () => {- expect(parseShadow("boxShadow", `box-shadow: 60px -16px teal;`))- .toMatchInlineSnapshot(`- {- "type": "layers",- "value": [- {- "type": "tuple",- "value": [- {- "type": "unit",- "unit": "px",- "value": 60,- },- {- "type": "unit",- "unit": "px",- "value": -16,- },- {- "type": "keyword",- "value": "teal",- },- ],- },- ],- }- `);- });-- test("parses value from figma", () => {- expect(- parseShadow(- "boxShadow",- "box-shadow: 0 60px 80px rgba(0,0,0,0.60), 0 45px 26px rgba(0,0,0,0.14);"- )- ).toMatchInlineSnapshot(`- {- "type": "layers",- "value": [- {- "type": "tuple",- "value": [- {- "type": "unit",- "unit": "number",- "value": 0,- },- {- "type": "unit",- "unit": "px",- "value": 60,- },- {- "type": "unit",- "unit": "px",- "value": 80,- },- {- "alpha": 0.6,- "b": 0,- "g": 0,- "r": 0,- "type": "rgb",- },- ],- },- {- "type": "tuple",- "value": [- {- "type": "unit",- "unit": "number",- "value": 0,- },- {- "type": "unit",- "unit": "px",- "value": 45,- },- {- "type": "unit",- "unit": "px",- "value": 26,- },- {- "alpha": 0.14,- "b": 0,- "g": 0,- "r": 0,- "type": "rgb",- },- ],- },- ],- }- `);- });-- test(`parses multiple layers of box-shadow property`, () => {- expect(- parseShadow(- "boxShadow",- `box-shadow:- 0 0 5px rgba(0, 0, 0, 0.2),- inset 0 0 10px rgba(0, 0, 0, 0.3),- 0 0 15px rgba(0, 0, 0, 0.4);- `- )- ).toMatchInlineSnapshot(`- {- "type": "layers",- "value": [- {- "type": "tuple",- "value": [- {- "type": "unit",- "unit": "number",- "value": 0,- },- {- "type": "unit",- "unit": "number",- "value": 0,- },- {- "type": "unit",- "unit": "px",- "value": 5,- },- {- "alpha": 0.2,- "b": 0,- "g": 0,- "r": 0,- "type": "rgb",- },- ],- },- {- "type": "tuple",- "value": [- {- "type": "keyword",- "value": "inset",- },- {- "type": "unit",- "unit": "number",- "value": 0,- },- {- "type": "unit",- "unit": "number",- "value": 0,- },- {- "type": "unit",- "unit": "px",- "value": 10,- },- {- "alpha": 0.3,- "b": 0,- "g": 0,- "r": 0,- "type": "rgb",- },- ],- },- {- "type": "tuple",- "value": [- {- "type": "unit",- "unit": "number",- "value": 0,- },- {- "type": "unit",- "unit": "number",- "value": 0,- },- {- "type": "unit",- "unit": "px",- "value": 15,- },- {- "alpha": 0.4,- "b": 0,- "g": 0,- "r": 0,- "type": "rgb",- },- ],- },- ],- }- `);- });-});diff --git a/packages/css-data/src/property-parsers/shadows.ts b/packages/css-data/src/property-parsers/shadows.tsdeleted file mode 100644index 51faf37683..0000000000--- a/packages/css-data/src/property-parsers/shadows.ts+++ /dev/null@@ -1,137 +0,0 @@-import * as csstree from "css-tree";-import { colord } from "colord";-import type {- LayersValue,- TupleValue,- InvalidValue,- TupleValueItem,- Unit,-} from "@webstudio-is/css-engine";-import { cssTryParseValue } from "../parse-css-value";--const ShadowProperties = {- boxShadow: { cleanupKeywords: ["box-shadow:"], propertyName: "box-shadow" },- textShadow: {- cleanupKeywords: ["text-shadow:"],- propertyName: "text-shadow",- },-};--export const parseShadow = (- property: keyof typeof ShadowProperties,- shadow: string-): LayersValue | InvalidValue => {- let tokenStream = shadow.trim();- tokenStream = tokenStream.endsWith(";")- ? tokenStream.slice(0, -1)- : tokenStream;- const { cleanupKeywords, propertyName } = ShadowProperties[property];-- for (const cleanupKeyword of cleanupKeywords) {- tokenStream = tokenStream.startsWith(cleanupKeyword)- ? tokenStream.slice(cleanupKeyword.length).trim()- : tokenStream;- }-- const cssAst = cssTryParseValue(tokenStream);- if (cssAst === undefined) {- return {- type: "invalid",- value: shadow,- };- }-- const parsed = csstree.lexer.matchProperty(propertyName, cssAst);- if (parsed.error) {- return {- type: "invalid",- value: shadow,- };- }-- const layers: TupleValue[] = [];-- csstree.walk(cssAst, (node) => {- if (node.type === "Value") {- const children = node.children;- let layer: csstree.CssNode[] = [];-- for (const child of children) {- if (children.last === child) {- layer.push(child);- }-- if (child.type === "Operator" || children.last === child) {- const shadow: TupleValueItem[] = [];-- /**- * https://developer.mozilla.org/en-US/docs/Web/CSS/box-shadow#syntax- * `inset` and color can be at the start or end and their sequence can be anywhere.- * The rest need to follow the sequence all the time- */-- for (let index = 0; index < layer.length; index++) {- const item = layer[index];-- if (item === undefined) {- throw new Error(`Layer at index ${index} is undefined`);- }-- if (item.type === "Identifier") {- shadow.push({- type: "keyword",- value: item.name,- });- }-- if (item.type === "Function" || item.type === "Hash") {- const colorValue = colord(csstree.generate(item));- if (colorValue.isValid() === true) {- const rgb = colorValue.toRgb();- shadow.push({- type: "rgb",- alpha: rgb.a,- r: rgb.r,- g: rgb.g,- b: rgb.b,- });- }- }-- if (item.type === "Dimension") {- shadow.push({- type: "unit",- value: Number(item.value),- unit: item.unit as Unit,- });- }-- if (item.type === "Number") {- shadow.push({- type: "unit",- value: Number(item.value),- unit: "number",- });- }- }-- layers.push({- type: "tuple",- value: shadow,- });- layer = [];- continue;- }-- layer.push(child);- }- }- });-- return layers.length > 0- ? {- type: "layers",- value: layers,- }- : { type: "invalid", value: shadow };-};