UNPKG

4.51 kBJavaScriptView Raw
1Object.defineProperty(exports, "__esModule", { value: true });
2exports.cssTreeParse = void 0;
3var css_tree_1 = require("css-tree");
4function mapSelectors(selector) {
5 if (!selector) {
6 return [];
7 }
8 return selector.split(/\s*(?![^(]*\)),\s*/).map(function (s) { return s.replace(/\u200C/g, ','); });
9}
10function mapPosition(node, css) {
11 var res = {
12 start: {
13 line: node.loc.start.line,
14 column: node.loc.start.column,
15 },
16 end: {
17 line: node.loc.end.line,
18 column: node.loc.end.column,
19 },
20 content: css,
21 };
22 if (node.loc.source && node.loc.source !== '<unknown>') {
23 res.source = node.loc.source;
24 }
25 return res;
26}
27function transformAst(node, css, type) {
28 if (type === void 0) { type = null; }
29 if (!node) {
30 return;
31 }
32 if (node.type === 'StyleSheet') {
33 return {
34 type: 'stylesheet',
35 stylesheet: {
36 rules: node.children
37 .map(function (child) { return transformAst(child, css); })
38 .filter(function (child) { return child !== null; })
39 .toArray(),
40 parsingErrors: [],
41 },
42 };
43 }
44 if (node.type === 'Atrule') {
45 var atrule = {
46 type: node.name,
47 };
48 if (node.name === 'supports' || node.name === 'media') {
49 atrule[node.name] = node.prelude.value;
50 atrule.rules = transformAst(node.block, css);
51 }
52 else if (node.name === 'page') {
53 atrule.selectors = node.prelude ? mapSelectors(node.prelude.value) : [];
54 atrule.declarations = transformAst(node.block, css);
55 }
56 else if (node.name === 'document') {
57 atrule.document = node.prelude ? node.prelude.value : '';
58 atrule.vendor = '';
59 atrule.rules = transformAst(node.block, css);
60 }
61 else if (node.name === 'font-face') {
62 atrule.declarations = transformAst(node.block, css);
63 }
64 else if (node.name === 'import' || node.name === 'charset' || node.name === 'namespace') {
65 atrule[node.name] = node.prelude ? node.prelude.value : '';
66 }
67 else if (node.name === 'keyframes') {
68 atrule.name = node.prelude ? node.prelude.value : '';
69 atrule.keyframes = transformAst(node.block, css, 'keyframe');
70 atrule.vendor = undefined;
71 }
72 else {
73 atrule.rules = transformAst(node.block, css);
74 }
75 atrule.position = mapPosition(node, css);
76 return atrule;
77 }
78 if (node.type === 'Block') {
79 return node.children
80 .map(function (child) { return transformAst(child, css, type); })
81 .filter(function (child) { return child !== null; })
82 .toArray();
83 }
84 if (node.type === 'Rule') {
85 var value = node.prelude.value;
86 var res = {
87 type: type != null ? type : 'rule',
88 declarations: transformAst(node.block, css),
89 position: mapPosition(node, css),
90 };
91 if (type === 'keyframe') {
92 res.values = mapSelectors(value);
93 }
94 else {
95 res.selectors = mapSelectors(value);
96 }
97 return res;
98 }
99 if (node.type === 'Comment') {
100 return {
101 type: 'comment',
102 comment: node.value,
103 position: mapPosition(node, css),
104 };
105 }
106 if (node.type === 'Declaration') {
107 return {
108 type: 'declaration',
109 property: node.property,
110 value: node.value.value ? node.value.value.trim() : '',
111 position: mapPosition(node, css),
112 };
113 }
114 if (node.type === 'Raw') {
115 return null;
116 }
117 throw Error("Unknown node type ".concat(node.type));
118}
119function cssTreeParse(css, source) {
120 var errors = [];
121 var ast = (0, css_tree_1.parse)(css, {
122 parseValue: false,
123 parseAtrulePrelude: false,
124 parseRulePrelude: false,
125 positions: true,
126 filename: source,
127 onParseError: function (error) {
128 errors.push("".concat(source, ":").concat(error.line, ":").concat(error.column, ": ").concat(error.formattedMessage));
129 },
130 });
131 if (errors.length > 0) {
132 throw new Error(errors[0]);
133 }
134 return transformAst(ast, css);
135}
136exports.cssTreeParse = cssTreeParse;
137//# sourceMappingURL=css-tree-parser.js.map
\No newline at end of file