1 | import {COMMENT, RULESET, DECLARATION} from './Enum.js'
|
---|
2 | import {abs, charat, trim, from, sizeof, strlen, substr, append, replace, indexof} from './Utility.js'
|
---|
3 | import {node, char, prev, next, peek, caret, alloc, dealloc, delimit, whitespace, escaping, identifier, commenter} from './Tokenizer.js'
|
---|
4 |
|
---|
5 | /**
|
---|
6 | * @param {string} value
|
---|
7 | * @return {object[]}
|
---|
8 | */
|
---|
9 | export function compile (value) {
|
---|
10 | return dealloc(parse('', null, null, null, [''], value = alloc(value), 0, [0], value))
|
---|
11 | }
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * @param {string} value
|
---|
15 | * @param {object} root
|
---|
16 | * @param {object?} parent
|
---|
17 | * @param {string[]} rule
|
---|
18 | * @param {string[]} rules
|
---|
19 | * @param {string[]} rulesets
|
---|
20 | * @param {number[]} pseudo
|
---|
21 | * @param {number[]} points
|
---|
22 | * @param {string[]} declarations
|
---|
23 | * @return {object}
|
---|
24 | */
|
---|
25 | export function parse (value, root, parent, rule, rules, rulesets, pseudo, points, declarations) {
|
---|
26 | var index = 0
|
---|
27 | var offset = 0
|
---|
28 | var length = pseudo
|
---|
29 | var atrule = 0
|
---|
30 | var property = 0
|
---|
31 | var previous = 0
|
---|
32 | var variable = 1
|
---|
33 | var scanning = 1
|
---|
34 | var ampersand = 1
|
---|
35 | var character = 0
|
---|
36 | var type = ''
|
---|
37 | var props = rules
|
---|
38 | var children = rulesets
|
---|
39 | var reference = rule
|
---|
40 | var characters = type
|
---|
41 |
|
---|
42 | while (scanning)
|
---|
43 | switch (previous = character, character = next()) {
|
---|
44 | // (
|
---|
45 | case 40:
|
---|
46 | if (previous != 108 && charat(characters, length - 1) == 58) {
|
---|
47 | if (indexof(characters += replace(delimit(character), '&', '&\f'), '&\f', abs(index ? points[index - 1] : 0)) != -1)
|
---|
48 | ampersand = -1
|
---|
49 | break
|
---|
50 | }
|
---|
51 | // " ' [
|
---|
52 | case 34: case 39: case 91:
|
---|
53 | characters += delimit(character)
|
---|
54 | break
|
---|
55 | // \t \n \r \s
|
---|
56 | case 9: case 10: case 13: case 32:
|
---|
57 | characters += whitespace(previous)
|
---|
58 | break
|
---|
59 | // \
|
---|
60 | case 92:
|
---|
61 | characters += escaping(caret() - 1, 7)
|
---|
62 | continue
|
---|
63 | // /
|
---|
64 | case 47:
|
---|
65 | switch (peek()) {
|
---|
66 | case 42: case 47:
|
---|
67 | append(comment(commenter(next(), caret()), root, parent, declarations), declarations)
|
---|
68 | break
|
---|
69 | default:
|
---|
70 | characters += '/'
|
---|
71 | }
|
---|
72 | break
|
---|
73 | // {
|
---|
74 | case 123 * variable:
|
---|
75 | points[index++] = strlen(characters) * ampersand
|
---|
76 | // } ; \0
|
---|
77 | case 125 * variable: case 59: case 0:
|
---|
78 | switch (character) {
|
---|
79 | // \0 }
|
---|
80 | case 0: case 125: scanning = 0
|
---|
81 | // ;
|
---|
82 | case 59 + offset: if (ampersand == -1) characters = replace(characters, /\f/g, '')
|
---|
83 | if (property > 0 && (strlen(characters) - length))
|
---|
84 | append(property > 32 ? declaration(characters + ';', rule, parent, length - 1, declarations) : declaration(replace(characters, ' ', '') + ';', rule, parent, length - 2, declarations), declarations)
|
---|
85 | break
|
---|
86 | // @ ;
|
---|
87 | case 59: characters += ';'
|
---|
88 | // { rule/at-rule
|
---|
89 | default:
|
---|
90 | append(reference = ruleset(characters, root, parent, index, offset, rules, points, type, props = [], children = [], length, rulesets), rulesets)
|
---|
91 |
|
---|
92 | if (character === 123)
|
---|
93 | if (offset === 0)
|
---|
94 | parse(characters, root, reference, reference, props, rulesets, length, points, children)
|
---|
95 | else
|
---|
96 | switch (atrule === 99 && charat(characters, 3) === 110 ? 100 : atrule) {
|
---|
97 | // d l m s
|
---|
98 | case 100: case 108: case 109: case 115:
|
---|
99 | parse(value, reference, reference, rule && append(ruleset(value, reference, reference, 0, 0, rules, points, type, rules, props = [], length, children), children), rules, children, length, points, rule ? props : children)
|
---|
100 | break
|
---|
101 | default:
|
---|
102 | parse(characters, reference, reference, reference, [''], children, 0, points, children)
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | index = offset = property = 0, variable = ampersand = 1, type = characters = '', length = pseudo
|
---|
107 | break
|
---|
108 | // :
|
---|
109 | case 58:
|
---|
110 | length = 1 + strlen(characters), property = previous
|
---|
111 | default:
|
---|
112 | if (variable < 1)
|
---|
113 | if (character == 123)
|
---|
114 | --variable
|
---|
115 | else if (character == 125 && variable++ == 0 && prev() == 125)
|
---|
116 | continue
|
---|
117 |
|
---|
118 | switch (characters += from(character), character * variable) {
|
---|
119 | // &
|
---|
120 | case 38:
|
---|
121 | ampersand = offset > 0 ? 1 : (characters += '\f', -1)
|
---|
122 | break
|
---|
123 | // ,
|
---|
124 | case 44:
|
---|
125 | points[index++] = (strlen(characters) - 1) * ampersand, ampersand = 1
|
---|
126 | break
|
---|
127 | // @
|
---|
128 | case 64:
|
---|
129 | // -
|
---|
130 | if (peek() === 45)
|
---|
131 | characters += delimit(next())
|
---|
132 |
|
---|
133 | atrule = peek(), offset = length = strlen(type = characters += identifier(caret())), character++
|
---|
134 | break
|
---|
135 | // -
|
---|
136 | case 45:
|
---|
137 | if (previous === 45 && strlen(characters) == 2)
|
---|
138 | variable = 0
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | return rulesets
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * @param {string} value
|
---|
147 | * @param {object} root
|
---|
148 | * @param {object?} parent
|
---|
149 | * @param {number} index
|
---|
150 | * @param {number} offset
|
---|
151 | * @param {string[]} rules
|
---|
152 | * @param {number[]} points
|
---|
153 | * @param {string} type
|
---|
154 | * @param {string[]} props
|
---|
155 | * @param {string[]} children
|
---|
156 | * @param {number} length
|
---|
157 | * @param {object[]} siblings
|
---|
158 | * @return {object}
|
---|
159 | */
|
---|
160 | export function ruleset (value, root, parent, index, offset, rules, points, type, props, children, length, siblings) {
|
---|
161 | var post = offset - 1
|
---|
162 | var rule = offset === 0 ? rules : ['']
|
---|
163 | var size = sizeof(rule)
|
---|
164 |
|
---|
165 | for (var i = 0, j = 0, k = 0; i < index; ++i)
|
---|
166 | for (var x = 0, y = substr(value, post + 1, post = abs(j = points[i])), z = value; x < size; ++x)
|
---|
167 | if (z = trim(j > 0 ? rule[x] + ' ' + y : replace(y, /&\f/g, rule[x])))
|
---|
168 | props[k++] = z
|
---|
169 |
|
---|
170 | return node(value, root, parent, offset === 0 ? RULESET : type, props, children, length, siblings)
|
---|
171 | }
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * @param {number} value
|
---|
175 | * @param {object} root
|
---|
176 | * @param {object?} parent
|
---|
177 | * @param {object[]} siblings
|
---|
178 | * @return {object}
|
---|
179 | */
|
---|
180 | export function comment (value, root, parent, siblings) {
|
---|
181 | return node(value, root, parent, COMMENT, from(char()), substr(value, 2, -2), 0, siblings)
|
---|
182 | }
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * @param {string} value
|
---|
186 | * @param {object} root
|
---|
187 | * @param {object?} parent
|
---|
188 | * @param {number} length
|
---|
189 | * @param {object[]} siblings
|
---|
190 | * @return {object}
|
---|
191 | */
|
---|
192 | export function declaration (value, root, parent, length, siblings) {
|
---|
193 | return node(value, root, parent, DECLARATION, substr(value, 0, length), substr(value, length + 1, -1), length, siblings)
|
---|
194 | }
|
---|