1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.parseCss = parseCss;
|
7 | exports.cleanCss = cleanCss;
|
8 | exports.ruleTypes = exports.default = void 0;
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | function parseCss() {
|
44 | var cssText = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : '';
|
45 | var cleaned = cleanCss(cssText);
|
46 | return parseLexed(lex(cleaned), cleaned);
|
47 | }
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | var ruleTypes = {
|
54 | style: 1,
|
55 | keyframes: 7,
|
56 | media: 4
|
57 | };
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | exports.ruleTypes = ruleTypes;
|
66 |
|
67 | function cleanCss() {
|
68 | var text = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : '';
|
69 |
|
70 | return text.replace(/\/\*[^*]*\*+([^/*][^*]*\*+)*\//gim, '').replace(/@import[^;]*;/gim, '');
|
71 | }
|
72 |
|
73 | function lex(text) {
|
74 | var rootNode = {
|
75 | start: 0,
|
76 | end: text.length
|
77 | };
|
78 | var node = rootNode;
|
79 | var chars = text.split('');
|
80 | chars.forEach(function (char, i) {
|
81 | switch (char) {
|
82 | case '{':
|
83 | {
|
84 | if (!node.rules) {
|
85 | node.rules = [];
|
86 | }
|
87 |
|
88 | var parent = node;
|
89 | var previous = parent.rules[parent.rules.length - 1];
|
90 | node = {
|
91 | start: i + 1,
|
92 | parent: parent,
|
93 | previous: previous
|
94 | };
|
95 | parent.rules.push(node);
|
96 | break;
|
97 | }
|
98 |
|
99 | case '}':
|
100 | {
|
101 | node.end = i + 1;
|
102 | node = node.parent || rootNode;
|
103 | break;
|
104 | }
|
105 |
|
106 | default:
|
107 | {
|
108 | break;
|
109 | }
|
110 | }
|
111 | });
|
112 | return rootNode;
|
113 | }
|
114 |
|
115 | function parseSelector(node, text) {
|
116 | var start = node.previous ? node.previous.end : node.parent.start;
|
117 | var end = node.start - 1;
|
118 | var selector = text.substring(start, end);
|
119 | selector = selector.replace(/\s+/g, ' ');
|
120 | selector = selector.substring(selector.lastIndexOf(';') + 1);
|
121 | return selector.trim();
|
122 | }
|
123 |
|
124 | function parseRuleType(selector) {
|
125 | if (selector.indexOf('@') === 0) {
|
126 | if (selector.indexOf('@media') === 0) {
|
127 | return ruleTypes.media;
|
128 | } else if (selector.match(/^@[^\s]*keyframes/)) {
|
129 | return ruleTypes.keyframes;
|
130 | }
|
131 | } else {
|
132 | return ruleTypes.style;
|
133 | }
|
134 | }
|
135 |
|
136 | function parseLexed(node) {
|
137 | var text = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : '';
|
138 |
|
139 |
|
140 | if (node.parent) {
|
141 | node.selector = parseSelector(node, text);
|
142 | node.type = parseRuleType(node.selector);
|
143 | }
|
144 |
|
145 | node.cssText = text.substring(node.start, node.end - 1).trim();
|
146 |
|
147 | if (node.rules && node.rules.length > 0) {
|
148 | node.rules = node.rules.map(function (rule) {
|
149 | return parseLexed(rule, text);
|
150 | });
|
151 | }
|
152 |
|
153 |
|
154 |
|
155 | return node;
|
156 | }
|
157 |
|
158 | var _default = parseCss;
|
159 | exports.default = _default; |
\ | No newline at end of file |