@webstudio-is/css-data 0.71.0 → 0.73.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/parse-css.ts CHANGED
@@ -61,12 +61,25 @@ const parseCssValue = function parseCssValue(
61
61
  };
62
62
  };
63
63
 
64
+ const cssTreeTryParse = (input: string) => {
65
+ try {
66
+ const ast = csstree.parse(input);
67
+ return ast;
68
+ } catch {
69
+ return undefined;
70
+ }
71
+ };
72
+
64
73
  export const parseCss = function cssToWS(css: string) {
65
- const ast = csstree.parse(css);
74
+ const ast = cssTreeTryParse(css);
66
75
 
67
76
  let selectors: Selector[] = [];
68
77
  const styles: Styles = {};
69
78
 
79
+ if (ast === undefined) {
80
+ return styles;
81
+ }
82
+
70
83
  csstree.walk(ast, (node, item) => {
71
84
  if (node.type === "SelectorList") {
72
85
  selectors = [];
@@ -38,6 +38,15 @@ export const parseBackground = (
38
38
  };
39
39
  };
40
40
 
41
+ const cssTreeTryParseValue = (input: string) => {
42
+ try {
43
+ const ast = csstree.parse(input, { context: "value" });
44
+ return ast;
45
+ } catch {
46
+ return undefined;
47
+ }
48
+ };
49
+
41
50
  export const backgroundToLonghand = (
42
51
  background: string
43
52
  ): {
@@ -62,7 +71,14 @@ export const backgroundToLonghand = (
62
71
  : tokenStream;
63
72
  }
64
73
 
65
- const cssAst = csstree.parse(tokenStream, { context: "value" });
74
+ const cssAst = cssTreeTryParseValue(tokenStream);
75
+
76
+ if (cssAst === undefined) {
77
+ return {
78
+ backgroundImage: [],
79
+ backgroundColor: undefined,
80
+ };
81
+ }
66
82
 
67
83
  let backgroundColorRaw: string | undefined;
68
84