source: imaps-frontend/node_modules/stylis/src/Parser.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 6.2 KB
RevLine 
[d565449]1import {COMMENT, RULESET, DECLARATION} from './Enum.js'
2import {abs, charat, trim, from, sizeof, strlen, substr, append, replace, indexof} from './Utility.js'
[0c6b92a]3import {node, char, prev, next, peek, token, caret, alloc, dealloc, delimit, whitespace, escaping, identifier, commenter} from './Tokenizer.js'
[d565449]4
5/**
6 * @param {string} value
7 * @return {object[]}
8 */
9export 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 */
25export 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)
[0c6b92a]68 if ((token(previous || 1) == 5 || token(peek() || 1) == 5) && strlen(characters) && substr(characters, -1, void 0) !== ' ') characters += ' '
[d565449]69 break
70 default:
71 characters += '/'
72 }
73 break
74 // {
75 case 123 * variable:
76 points[index++] = strlen(characters) * ampersand
77 // } ; \0
78 case 125 * variable: case 59: case 0:
79 switch (character) {
80 // \0 }
81 case 0: case 125: scanning = 0
82 // ;
83 case 59 + offset: if (ampersand == -1) characters = replace(characters, /\f/g, '')
[0c6b92a]84 if (property > 0 && (strlen(characters) - length || (variable === 0 && previous === 47)))
[d565449]85 append(property > 32 ? declaration(characters + ';', rule, parent, length - 1, declarations) : declaration(replace(characters, ' ', '') + ';', rule, parent, length - 2, declarations), declarations)
86 break
87 // @ ;
88 case 59: characters += ';'
89 // { rule/at-rule
90 default:
91 append(reference = ruleset(characters, root, parent, index, offset, rules, points, type, props = [], children = [], length, rulesets), rulesets)
92
93 if (character === 123)
94 if (offset === 0)
95 parse(characters, root, reference, reference, props, rulesets, length, points, children)
[79a0317]96 else {
97 switch (atrule) {
98 // c(ontainer)
99 case 99:
100 if (charat(characters, 3) === 110) break
101 // l(ayer)
102 case 108:
103 if (charat(characters, 2) === 97) break
[d565449]104 default:
[79a0317]105 offset = 0
106 // d(ocument) m(edia) s(upports)
107 case 100: case 109: case 115:
[d565449]108 }
[79a0317]109 if (offset) 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)
110 else parse(characters, reference, reference, reference, [''], children, 0, points, children)
111 }
[d565449]112 }
113
114 index = offset = property = 0, variable = ampersand = 1, type = characters = '', length = pseudo
115 break
116 // :
117 case 58:
118 length = 1 + strlen(characters), property = previous
119 default:
120 if (variable < 1)
121 if (character == 123)
122 --variable
123 else if (character == 125 && variable++ == 0 && prev() == 125)
124 continue
125
126 switch (characters += from(character), character * variable) {
127 // &
128 case 38:
129 ampersand = offset > 0 ? 1 : (characters += '\f', -1)
130 break
131 // ,
132 case 44:
133 points[index++] = (strlen(characters) - 1) * ampersand, ampersand = 1
134 break
135 // @
136 case 64:
137 // -
138 if (peek() === 45)
139 characters += delimit(next())
140
141 atrule = peek(), offset = length = strlen(type = characters += identifier(caret())), character++
142 break
143 // -
144 case 45:
145 if (previous === 45 && strlen(characters) == 2)
146 variable = 0
147 }
148 }
149
150 return rulesets
151}
152
153/**
154 * @param {string} value
155 * @param {object} root
156 * @param {object?} parent
157 * @param {number} index
158 * @param {number} offset
159 * @param {string[]} rules
160 * @param {number[]} points
161 * @param {string} type
162 * @param {string[]} props
163 * @param {string[]} children
164 * @param {number} length
165 * @param {object[]} siblings
166 * @return {object}
167 */
168export function ruleset (value, root, parent, index, offset, rules, points, type, props, children, length, siblings) {
169 var post = offset - 1
170 var rule = offset === 0 ? rules : ['']
171 var size = sizeof(rule)
172
173 for (var i = 0, j = 0, k = 0; i < index; ++i)
174 for (var x = 0, y = substr(value, post + 1, post = abs(j = points[i])), z = value; x < size; ++x)
175 if (z = trim(j > 0 ? rule[x] + ' ' + y : replace(y, /&\f/g, rule[x])))
176 props[k++] = z
177
178 return node(value, root, parent, offset === 0 ? RULESET : type, props, children, length, siblings)
179}
180
181/**
182 * @param {number} value
183 * @param {object} root
184 * @param {object?} parent
185 * @param {object[]} siblings
186 * @return {object}
187 */
188export function comment (value, root, parent, siblings) {
189 return node(value, root, parent, COMMENT, from(char()), substr(value, 2, -2), 0, siblings)
190}
191
192/**
193 * @param {string} value
194 * @param {object} root
195 * @param {object?} parent
196 * @param {number} length
197 * @param {object[]} siblings
198 * @return {object}
199 */
200export function declaration (value, root, parent, length, siblings) {
201 return node(value, root, parent, DECLARATION, substr(value, 0, length), substr(value, length + 1, -1), length, siblings)
202}
Note: See TracBrowser for help on using the repository browser.