@webstudio-is/css-data 0.66.0 → 0.67.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.
Files changed (36) hide show
  1. package/lib/cjs/index.js +3 -0
  2. package/lib/cjs/parse-css-value.js +152 -0
  3. package/lib/cjs/parse-css.js +125 -0
  4. package/lib/cjs/property-parsers/background.js +105 -0
  5. package/lib/cjs/property-parsers/index.js +18 -0
  6. package/lib/cjs/property-parsers/parsers.js +24 -0
  7. package/lib/cjs/property-parsers/to-longhand.js +24 -0
  8. package/lib/index.js +3 -0
  9. package/lib/parse-css-value.js +122 -0
  10. package/lib/parse-css.js +95 -0
  11. package/lib/property-parsers/background.js +75 -0
  12. package/lib/property-parsers/index.js +1 -0
  13. package/lib/property-parsers/parsers.js +4 -0
  14. package/lib/property-parsers/to-longhand.js +4 -0
  15. package/lib/types/src/index.d.ts +3 -0
  16. package/lib/types/src/parse-css-value.d.ts +3 -0
  17. package/lib/types/src/parse-css-value.test.d.ts +1 -0
  18. package/lib/types/src/parse-css.d.ts +9 -0
  19. package/lib/types/src/parse-css.test.d.ts +1 -0
  20. package/lib/types/src/property-parsers/background.d.ts +11 -0
  21. package/lib/types/src/property-parsers/background.test.d.ts +1 -0
  22. package/lib/types/src/property-parsers/index.d.ts +1 -0
  23. package/lib/types/src/property-parsers/parsers.d.ts +1 -0
  24. package/lib/types/src/property-parsers/to-longhand.d.ts +1 -0
  25. package/package.json +12 -4
  26. package/src/index.ts +6 -0
  27. package/src/parse-css-value.test.ts +136 -0
  28. package/src/parse-css-value.ts +157 -0
  29. package/src/parse-css.test.ts +101 -0
  30. package/src/parse-css.ts +124 -0
  31. package/src/property-parsers/README.md +11 -0
  32. package/src/property-parsers/background.test.ts +184 -0
  33. package/src/property-parsers/background.ts +132 -0
  34. package/src/property-parsers/index.ts +1 -0
  35. package/src/property-parsers/parsers.ts +4 -0
  36. package/src/property-parsers/to-longhand.ts +4 -0
package/lib/cjs/index.js CHANGED
@@ -37,6 +37,9 @@ var exportedHtml = __toESM(require("./html"), 1);
37
37
  __reExport(src_exports, require("./__generated__/keyword-values"), module.exports);
38
38
  __reExport(src_exports, require("./__generated__/units"), module.exports);
39
39
  __reExport(src_exports, require("./schema"), module.exports);
40
+ __reExport(src_exports, require("./property-parsers/index"), module.exports);
41
+ __reExport(src_exports, require("./parse-css-value"), module.exports);
42
+ __reExport(src_exports, require("./parse-css"), module.exports);
40
43
  var import_properties = require("./__generated__/properties");
41
44
  const html = exportedHtml;
42
45
  const properties = import_properties.properties;
@@ -0,0 +1,152 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var parse_css_value_exports = {};
30
+ __export(parse_css_value_exports, {
31
+ isValidDeclaration: () => isValidDeclaration,
32
+ parseCssValue: () => parseCssValue
33
+ });
34
+ module.exports = __toCommonJS(parse_css_value_exports);
35
+ var import_colord = require("colord");
36
+ var csstree = __toESM(require("css-tree"), 1);
37
+ var import_hyphenate_style_name = __toESM(require("hyphenate-style-name"), 1);
38
+ var import_warn_once = __toESM(require("warn-once"), 1);
39
+ var import_keyword_values = require("./__generated__/keyword-values");
40
+ var import_units = require("./__generated__/units");
41
+ const cssTryParseValue = (input) => {
42
+ try {
43
+ const ast = csstree.parse(input, { context: "value" });
44
+ return ast;
45
+ } catch {
46
+ return void 0;
47
+ }
48
+ };
49
+ const isValidDeclaration = (property, value) => {
50
+ const ast = cssTryParseValue(value);
51
+ if (ast == null) {
52
+ return false;
53
+ }
54
+ const cssPropertyName = (0, import_hyphenate_style_name.default)(property);
55
+ const matchResult = csstree.lexer.matchProperty(cssPropertyName, ast);
56
+ const isValidDeclaration2 = matchResult.matched != null;
57
+ if (isValidDeclaration2 && typeof CSSStyleValue !== "undefined") {
58
+ try {
59
+ CSSStyleValue.parse(cssPropertyName, value);
60
+ } catch {
61
+ (0, import_warn_once.default)(
62
+ true,
63
+ `Css property "${property}" with value "${value}" is invalid according to CSSStyleValue.parse
64
+ but valid according to csstree.lexer.matchProperty.`
65
+ );
66
+ return false;
67
+ }
68
+ }
69
+ return isValidDeclaration2;
70
+ };
71
+ const parseCssValue = (property, input) => {
72
+ const invalidValue = {
73
+ type: "invalid",
74
+ value: input
75
+ };
76
+ if (input.length === 0) {
77
+ return invalidValue;
78
+ }
79
+ if (!isValidDeclaration(property, input)) {
80
+ return invalidValue;
81
+ }
82
+ const ast = cssTryParseValue(input);
83
+ if (ast == null) {
84
+ (0, import_warn_once.default)(
85
+ true,
86
+ `Can't parse css property "${property}" with value "${input}"`
87
+ );
88
+ return invalidValue;
89
+ }
90
+ if (ast != null && ast.type === "Value" && ast.children.first === ast.children.last) {
91
+ const first = ast.children.first;
92
+ if (first?.type === "Number") {
93
+ return {
94
+ type: "unit",
95
+ unit: "number",
96
+ value: Number(first.value)
97
+ };
98
+ }
99
+ if (first?.type === "Dimension") {
100
+ const unit = first.unit;
101
+ for (const unitGroup of Object.values(import_units.units)) {
102
+ if (unitGroup.includes(unit)) {
103
+ return {
104
+ type: "unit",
105
+ unit,
106
+ value: Number(first.value)
107
+ };
108
+ }
109
+ }
110
+ return invalidValue;
111
+ }
112
+ if (first?.type === "Percentage") {
113
+ return {
114
+ type: "unit",
115
+ unit: "%",
116
+ value: Number(first.value)
117
+ };
118
+ }
119
+ if (first?.type === "Identifier") {
120
+ const values = import_keyword_values.keywordValues[property];
121
+ const lettersRegex = /[^a-zA-Z]+/g;
122
+ const searchValues = values.map(
123
+ (value) => value.replace(lettersRegex, "").toLowerCase()
124
+ );
125
+ const keywordInput = input.replace(lettersRegex, "").toLowerCase();
126
+ const index = searchValues.indexOf(keywordInput);
127
+ if (index > -1) {
128
+ return {
129
+ type: "keyword",
130
+ value: values[index]
131
+ };
132
+ }
133
+ }
134
+ }
135
+ if (property.toLocaleLowerCase().includes("color")) {
136
+ const mayBeColor = (0, import_colord.colord)(input);
137
+ if (mayBeColor.isValid()) {
138
+ const rgb = mayBeColor.toRgb();
139
+ return {
140
+ type: "rgb",
141
+ alpha: rgb.a,
142
+ r: rgb.r,
143
+ g: rgb.g,
144
+ b: rgb.b
145
+ };
146
+ }
147
+ }
148
+ return {
149
+ type: "unparsed",
150
+ value: input
151
+ };
152
+ };
@@ -0,0 +1,125 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var parse_css_exports = {};
30
+ __export(parse_css_exports, {
31
+ parseCss: () => parseCss
32
+ });
33
+ module.exports = __toCommonJS(parse_css_exports);
34
+ var csstree = __toESM(require("css-tree"), 1);
35
+ var import_parse_css_value = require("./parse-css-value");
36
+ var parsers = __toESM(require("./property-parsers/parsers"), 1);
37
+ var toLonghand = __toESM(require("./property-parsers/to-longhand"), 1);
38
+ var import_schema = require("./schema");
39
+ const parseCssValue = function parseCssValue2(property, value) {
40
+ const unwrap = toLonghand[property];
41
+ if (typeof unwrap === "function") {
42
+ const longhands = unwrap(value);
43
+ return Object.fromEntries(
44
+ Object.entries(longhands).map(([property2, value2]) => {
45
+ const valueParser = parsers[property2];
46
+ if (typeof valueParser === "function") {
47
+ return [property2, valueParser(value2)];
48
+ }
49
+ if (Array.isArray(value2)) {
50
+ return [
51
+ property2,
52
+ {
53
+ type: "invalid",
54
+ value: value2.join("")
55
+ }
56
+ ];
57
+ }
58
+ if (!value2) {
59
+ return [property2, { type: "invalid", value: "" }];
60
+ }
61
+ return [
62
+ property2,
63
+ (0, import_parse_css_value.parseCssValue)(property2, value2)
64
+ ];
65
+ })
66
+ );
67
+ }
68
+ return {
69
+ [property]: (0, import_parse_css_value.parseCssValue)(property, value)
70
+ };
71
+ };
72
+ const parseCss = function cssToWS(css) {
73
+ const ast = csstree.parse(css);
74
+ let selectors = [];
75
+ const styles = {};
76
+ csstree.walk(ast, (node, item) => {
77
+ if (node.type === "SelectorList") {
78
+ selectors = [];
79
+ }
80
+ if (node.type === "ClassSelector") {
81
+ if (!item.prev && !item.next) {
82
+ selectors.push(node.name);
83
+ }
84
+ return;
85
+ }
86
+ if (node.type === "Declaration") {
87
+ const stringValue = csstree.generate(node.value);
88
+ const parsedCss = parseCssValue(
89
+ node.property,
90
+ stringValue
91
+ );
92
+ Object.entries(parsedCss).forEach(
93
+ ([property, value]) => {
94
+ try {
95
+ import_schema.StyleValue.parse(value);
96
+ selectors.forEach((selector) => {
97
+ if (Array.isArray(styles[selector])) {
98
+ styles[selector].push({
99
+ property,
100
+ value
101
+ });
102
+ } else {
103
+ styles[selector] = [{ property, value }];
104
+ }
105
+ });
106
+ } catch (error) {
107
+ if (true) {
108
+ console.warn(
109
+ true,
110
+ `Declaration parsing for \`${selectors.join(", ")}.${node.property}: ${stringValue}\` failed:
111
+
112
+ ${JSON.stringify(
113
+ parsedCss,
114
+ null,
115
+ 2
116
+ )}`
117
+ );
118
+ }
119
+ }
120
+ }
121
+ );
122
+ }
123
+ });
124
+ return styles;
125
+ };
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var background_exports = {};
30
+ __export(background_exports, {
31
+ backgroundToLonghand: () => backgroundToLonghand,
32
+ gradientNames: () => gradientNames,
33
+ parseBackground: () => parseBackground,
34
+ parseBackgroundImage: () => parseBackgroundImage
35
+ });
36
+ module.exports = __toCommonJS(background_exports);
37
+ var csstree = __toESM(require("css-tree"), 1);
38
+ var import_parse_css_value = require("../parse-css-value");
39
+ const gradientNames = [
40
+ "conic-gradient",
41
+ "linear-gradient",
42
+ "radial-gradient",
43
+ "repeating-conic-gradient",
44
+ "repeating-linear-gradient",
45
+ "repeating-radial-gradient"
46
+ ];
47
+ const parseBackground = (background) => {
48
+ const { backgroundImage, backgroundColor: backgroundColorRaw } = backgroundToLonghand(background);
49
+ const backgroundColor = backgroundColorRaw ? (0, import_parse_css_value.parseCssValue)("backgroundColor", backgroundColorRaw) : void 0;
50
+ return {
51
+ backgroundImage: parseBackgroundImage(backgroundImage),
52
+ backgroundColor: backgroundColor && backgroundColor.type === "rgb" ? backgroundColor : void 0
53
+ };
54
+ };
55
+ const backgroundToLonghand = (background) => {
56
+ const layers = [];
57
+ let tokenStream = background.trim();
58
+ tokenStream = tokenStream.endsWith(";") ? tokenStream.slice(0, -1) : tokenStream;
59
+ const cleanupKeywords = ["background:", "background-image:"];
60
+ for (const cleanupKeyword of cleanupKeywords) {
61
+ tokenStream = tokenStream.startsWith(cleanupKeyword) ? tokenStream.slice(cleanupKeyword.length).trim() : tokenStream;
62
+ }
63
+ const cssAst = csstree.parse(tokenStream, { context: "value" });
64
+ let backgroundColorRaw;
65
+ let nestingLevel = 0;
66
+ csstree.walk(cssAst, {
67
+ enter: (node, item, list) => {
68
+ if (node.type === "Function") {
69
+ if (gradientNames.includes(node.name)) {
70
+ layers.push(csstree.generate(node));
71
+ }
72
+ if (item.next === null && nestingLevel === 0) {
73
+ backgroundColorRaw = csstree.generate(node);
74
+ }
75
+ nestingLevel++;
76
+ }
77
+ if (node.type === "Hash" && item.next === null && nestingLevel === 0) {
78
+ backgroundColorRaw = csstree.generate(node);
79
+ }
80
+ },
81
+ leave: (node, item, list) => {
82
+ if (node.type === "Function") {
83
+ nestingLevel--;
84
+ }
85
+ }
86
+ });
87
+ return {
88
+ backgroundImage: layers,
89
+ backgroundColor: backgroundColorRaw
90
+ };
91
+ };
92
+ const parseBackgroundImage = (layers) => {
93
+ const backgroundImages = [];
94
+ for (const layer of layers) {
95
+ if (gradientNames.some((gradientName) => layer.startsWith(gradientName)) === false) {
96
+ break;
97
+ }
98
+ const layerStyle = (0, import_parse_css_value.parseCssValue)("backgroundImage", layer);
99
+ if (layerStyle.type !== "unparsed") {
100
+ break;
101
+ }
102
+ backgroundImages.push(layerStyle);
103
+ }
104
+ return backgroundImages.length > 0 ? { type: "layers", value: backgroundImages } : { type: "invalid", value: layers.join(",") };
105
+ };
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __copyProps = (to, from, except, desc) => {
7
+ if (from && typeof from === "object" || typeof from === "function") {
8
+ for (let key of __getOwnPropNames(from))
9
+ if (!__hasOwnProp.call(to, key) && key !== except)
10
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
+ }
12
+ return to;
13
+ };
14
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
15
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
16
+ var property_parsers_exports = {};
17
+ module.exports = __toCommonJS(property_parsers_exports);
18
+ __reExport(property_parsers_exports, require("./background"), module.exports);
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var parsers_exports = {};
20
+ __export(parsers_exports, {
21
+ backgroundImage: () => import_background.parseBackgroundImage
22
+ });
23
+ module.exports = __toCommonJS(parsers_exports);
24
+ var import_background = require("./background");
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var to_longhand_exports = {};
20
+ __export(to_longhand_exports, {
21
+ background: () => import_background.backgroundToLonghand
22
+ });
23
+ module.exports = __toCommonJS(to_longhand_exports);
24
+ var import_background = require("./background");
package/lib/index.js CHANGED
@@ -3,6 +3,9 @@ const html = exportedHtml;
3
3
  export * from "./__generated__/keyword-values";
4
4
  export * from "./__generated__/units";
5
5
  export * from "./schema";
6
+ export * from "./property-parsers/index";
7
+ export * from "./parse-css-value";
8
+ export * from "./parse-css";
6
9
  import { properties as generatedProperties } from "./__generated__/properties";
7
10
  const properties = generatedProperties;
8
11
  export {
@@ -0,0 +1,122 @@
1
+ import { colord } from "colord";
2
+ import * as csstree from "css-tree";
3
+ import hyphenate from "hyphenate-style-name";
4
+ import warnOnce from "warn-once";
5
+ import { keywordValues } from "./__generated__/keyword-values";
6
+ import { units } from "./__generated__/units";
7
+ const cssTryParseValue = (input) => {
8
+ try {
9
+ const ast = csstree.parse(input, { context: "value" });
10
+ return ast;
11
+ } catch {
12
+ return void 0;
13
+ }
14
+ };
15
+ const isValidDeclaration = (property, value) => {
16
+ const ast = cssTryParseValue(value);
17
+ if (ast == null) {
18
+ return false;
19
+ }
20
+ const cssPropertyName = hyphenate(property);
21
+ const matchResult = csstree.lexer.matchProperty(cssPropertyName, ast);
22
+ const isValidDeclaration2 = matchResult.matched != null;
23
+ if (isValidDeclaration2 && typeof CSSStyleValue !== "undefined") {
24
+ try {
25
+ CSSStyleValue.parse(cssPropertyName, value);
26
+ } catch {
27
+ warnOnce(
28
+ true,
29
+ `Css property "${property}" with value "${value}" is invalid according to CSSStyleValue.parse
30
+ but valid according to csstree.lexer.matchProperty.`
31
+ );
32
+ return false;
33
+ }
34
+ }
35
+ return isValidDeclaration2;
36
+ };
37
+ const parseCssValue = (property, input) => {
38
+ const invalidValue = {
39
+ type: "invalid",
40
+ value: input
41
+ };
42
+ if (input.length === 0) {
43
+ return invalidValue;
44
+ }
45
+ if (!isValidDeclaration(property, input)) {
46
+ return invalidValue;
47
+ }
48
+ const ast = cssTryParseValue(input);
49
+ if (ast == null) {
50
+ warnOnce(
51
+ true,
52
+ `Can't parse css property "${property}" with value "${input}"`
53
+ );
54
+ return invalidValue;
55
+ }
56
+ if (ast != null && ast.type === "Value" && ast.children.first === ast.children.last) {
57
+ const first = ast.children.first;
58
+ if (first?.type === "Number") {
59
+ return {
60
+ type: "unit",
61
+ unit: "number",
62
+ value: Number(first.value)
63
+ };
64
+ }
65
+ if (first?.type === "Dimension") {
66
+ const unit = first.unit;
67
+ for (const unitGroup of Object.values(units)) {
68
+ if (unitGroup.includes(unit)) {
69
+ return {
70
+ type: "unit",
71
+ unit,
72
+ value: Number(first.value)
73
+ };
74
+ }
75
+ }
76
+ return invalidValue;
77
+ }
78
+ if (first?.type === "Percentage") {
79
+ return {
80
+ type: "unit",
81
+ unit: "%",
82
+ value: Number(first.value)
83
+ };
84
+ }
85
+ if (first?.type === "Identifier") {
86
+ const values = keywordValues[property];
87
+ const lettersRegex = /[^a-zA-Z]+/g;
88
+ const searchValues = values.map(
89
+ (value) => value.replace(lettersRegex, "").toLowerCase()
90
+ );
91
+ const keywordInput = input.replace(lettersRegex, "").toLowerCase();
92
+ const index = searchValues.indexOf(keywordInput);
93
+ if (index > -1) {
94
+ return {
95
+ type: "keyword",
96
+ value: values[index]
97
+ };
98
+ }
99
+ }
100
+ }
101
+ if (property.toLocaleLowerCase().includes("color")) {
102
+ const mayBeColor = colord(input);
103
+ if (mayBeColor.isValid()) {
104
+ const rgb = mayBeColor.toRgb();
105
+ return {
106
+ type: "rgb",
107
+ alpha: rgb.a,
108
+ r: rgb.r,
109
+ g: rgb.g,
110
+ b: rgb.b
111
+ };
112
+ }
113
+ }
114
+ return {
115
+ type: "unparsed",
116
+ value: input
117
+ };
118
+ };
119
+ export {
120
+ isValidDeclaration,
121
+ parseCssValue
122
+ };
@@ -0,0 +1,95 @@
1
+ import * as csstree from "css-tree";
2
+ import { parseCssValue as parseCssValueLonghand } from "./parse-css-value";
3
+ import * as parsers from "./property-parsers/parsers";
4
+ import * as toLonghand from "./property-parsers/to-longhand";
5
+ import { StyleValue } from "./schema";
6
+ const parseCssValue = function parseCssValue2(property, value) {
7
+ const unwrap = toLonghand[property];
8
+ if (typeof unwrap === "function") {
9
+ const longhands = unwrap(value);
10
+ return Object.fromEntries(
11
+ Object.entries(longhands).map(([property2, value2]) => {
12
+ const valueParser = parsers[property2];
13
+ if (typeof valueParser === "function") {
14
+ return [property2, valueParser(value2)];
15
+ }
16
+ if (Array.isArray(value2)) {
17
+ return [
18
+ property2,
19
+ {
20
+ type: "invalid",
21
+ value: value2.join("")
22
+ }
23
+ ];
24
+ }
25
+ if (!value2) {
26
+ return [property2, { type: "invalid", value: "" }];
27
+ }
28
+ return [
29
+ property2,
30
+ parseCssValueLonghand(property2, value2)
31
+ ];
32
+ })
33
+ );
34
+ }
35
+ return {
36
+ [property]: parseCssValueLonghand(property, value)
37
+ };
38
+ };
39
+ const parseCss = function cssToWS(css) {
40
+ const ast = csstree.parse(css);
41
+ let selectors = [];
42
+ const styles = {};
43
+ csstree.walk(ast, (node, item) => {
44
+ if (node.type === "SelectorList") {
45
+ selectors = [];
46
+ }
47
+ if (node.type === "ClassSelector") {
48
+ if (!item.prev && !item.next) {
49
+ selectors.push(node.name);
50
+ }
51
+ return;
52
+ }
53
+ if (node.type === "Declaration") {
54
+ const stringValue = csstree.generate(node.value);
55
+ const parsedCss = parseCssValue(
56
+ node.property,
57
+ stringValue
58
+ );
59
+ Object.entries(parsedCss).forEach(
60
+ ([property, value]) => {
61
+ try {
62
+ StyleValue.parse(value);
63
+ selectors.forEach((selector) => {
64
+ if (Array.isArray(styles[selector])) {
65
+ styles[selector].push({
66
+ property,
67
+ value
68
+ });
69
+ } else {
70
+ styles[selector] = [{ property, value }];
71
+ }
72
+ });
73
+ } catch (error) {
74
+ if (true) {
75
+ console.warn(
76
+ true,
77
+ `Declaration parsing for \`${selectors.join(", ")}.${node.property}: ${stringValue}\` failed:
78
+
79
+ ${JSON.stringify(
80
+ parsedCss,
81
+ null,
82
+ 2
83
+ )}`
84
+ );
85
+ }
86
+ }
87
+ }
88
+ );
89
+ }
90
+ });
91
+ return styles;
92
+ };
93
+ export {
94
+ parseCss
95
+ };