source: imaps-frontend/node_modules/stylis/src/Parser.js@ 0c6b92a

main
Last change on this file since 0c6b92a was 0c6b92a, checked in by stefan toskovski <stefantoska84@…>, 5 weeks ago

Pred finalna verzija

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