1 | var openParentheses = "(".charCodeAt(0);
|
---|
2 | var closeParentheses = ")".charCodeAt(0);
|
---|
3 | var singleQuote = "'".charCodeAt(0);
|
---|
4 | var doubleQuote = '"'.charCodeAt(0);
|
---|
5 | var backslash = "\\".charCodeAt(0);
|
---|
6 | var slash = "/".charCodeAt(0);
|
---|
7 | var comma = ",".charCodeAt(0);
|
---|
8 | var colon = ":".charCodeAt(0);
|
---|
9 | var star = "*".charCodeAt(0);
|
---|
10 | var uLower = "u".charCodeAt(0);
|
---|
11 | var uUpper = "U".charCodeAt(0);
|
---|
12 | var plus = "+".charCodeAt(0);
|
---|
13 | var isUnicodeRange = /^[a-f0-9?-]+$/i;
|
---|
14 |
|
---|
15 | var parse$1 = function(input) {
|
---|
16 | var tokens = [];
|
---|
17 | var value = input;
|
---|
18 |
|
---|
19 | var next,
|
---|
20 | quote,
|
---|
21 | prev,
|
---|
22 | token,
|
---|
23 | escape,
|
---|
24 | escapePos,
|
---|
25 | whitespacePos,
|
---|
26 | parenthesesOpenPos;
|
---|
27 | var pos = 0;
|
---|
28 | var code = value.charCodeAt(pos);
|
---|
29 | var max = value.length;
|
---|
30 | var stack = [{ nodes: tokens }];
|
---|
31 | var balanced = 0;
|
---|
32 | var parent;
|
---|
33 |
|
---|
34 | var name = "";
|
---|
35 | var before = "";
|
---|
36 | var after = "";
|
---|
37 |
|
---|
38 | while (pos < max) {
|
---|
39 | // Whitespaces
|
---|
40 | if (code <= 32) {
|
---|
41 | next = pos;
|
---|
42 | do {
|
---|
43 | next += 1;
|
---|
44 | code = value.charCodeAt(next);
|
---|
45 | } while (code <= 32);
|
---|
46 | token = value.slice(pos, next);
|
---|
47 |
|
---|
48 | prev = tokens[tokens.length - 1];
|
---|
49 | if (code === closeParentheses && balanced) {
|
---|
50 | after = token;
|
---|
51 | } else if (prev && prev.type === "div") {
|
---|
52 | prev.after = token;
|
---|
53 | prev.sourceEndIndex += token.length;
|
---|
54 | } else if (
|
---|
55 | code === comma ||
|
---|
56 | code === colon ||
|
---|
57 | (code === slash &&
|
---|
58 | value.charCodeAt(next + 1) !== star &&
|
---|
59 | (!parent ||
|
---|
60 | (parent && parent.type === "function" && parent.value !== "calc")))
|
---|
61 | ) {
|
---|
62 | before = token;
|
---|
63 | } else {
|
---|
64 | tokens.push({
|
---|
65 | type: "space",
|
---|
66 | sourceIndex: pos,
|
---|
67 | sourceEndIndex: next,
|
---|
68 | value: token
|
---|
69 | });
|
---|
70 | }
|
---|
71 |
|
---|
72 | pos = next;
|
---|
73 |
|
---|
74 | // Quotes
|
---|
75 | } else if (code === singleQuote || code === doubleQuote) {
|
---|
76 | next = pos;
|
---|
77 | quote = code === singleQuote ? "'" : '"';
|
---|
78 | token = {
|
---|
79 | type: "string",
|
---|
80 | sourceIndex: pos,
|
---|
81 | quote: quote
|
---|
82 | };
|
---|
83 | do {
|
---|
84 | escape = false;
|
---|
85 | next = value.indexOf(quote, next + 1);
|
---|
86 | if (~next) {
|
---|
87 | escapePos = next;
|
---|
88 | while (value.charCodeAt(escapePos - 1) === backslash) {
|
---|
89 | escapePos -= 1;
|
---|
90 | escape = !escape;
|
---|
91 | }
|
---|
92 | } else {
|
---|
93 | value += quote;
|
---|
94 | next = value.length - 1;
|
---|
95 | token.unclosed = true;
|
---|
96 | }
|
---|
97 | } while (escape);
|
---|
98 | token.value = value.slice(pos + 1, next);
|
---|
99 | token.sourceEndIndex = token.unclosed ? next : next + 1;
|
---|
100 | tokens.push(token);
|
---|
101 | pos = next + 1;
|
---|
102 | code = value.charCodeAt(pos);
|
---|
103 |
|
---|
104 | // Comments
|
---|
105 | } else if (code === slash && value.charCodeAt(pos + 1) === star) {
|
---|
106 | next = value.indexOf("*/", pos);
|
---|
107 |
|
---|
108 | token = {
|
---|
109 | type: "comment",
|
---|
110 | sourceIndex: pos,
|
---|
111 | sourceEndIndex: next + 2
|
---|
112 | };
|
---|
113 |
|
---|
114 | if (next === -1) {
|
---|
115 | token.unclosed = true;
|
---|
116 | next = value.length;
|
---|
117 | token.sourceEndIndex = next;
|
---|
118 | }
|
---|
119 |
|
---|
120 | token.value = value.slice(pos + 2, next);
|
---|
121 | tokens.push(token);
|
---|
122 |
|
---|
123 | pos = next + 2;
|
---|
124 | code = value.charCodeAt(pos);
|
---|
125 |
|
---|
126 | // Operation within calc
|
---|
127 | } else if (
|
---|
128 | (code === slash || code === star) &&
|
---|
129 | parent &&
|
---|
130 | parent.type === "function" &&
|
---|
131 | parent.value === "calc"
|
---|
132 | ) {
|
---|
133 | token = value[pos];
|
---|
134 | tokens.push({
|
---|
135 | type: "word",
|
---|
136 | sourceIndex: pos - before.length,
|
---|
137 | sourceEndIndex: pos + token.length,
|
---|
138 | value: token
|
---|
139 | });
|
---|
140 | pos += 1;
|
---|
141 | code = value.charCodeAt(pos);
|
---|
142 |
|
---|
143 | // Dividers
|
---|
144 | } else if (code === slash || code === comma || code === colon) {
|
---|
145 | token = value[pos];
|
---|
146 |
|
---|
147 | tokens.push({
|
---|
148 | type: "div",
|
---|
149 | sourceIndex: pos - before.length,
|
---|
150 | sourceEndIndex: pos + token.length,
|
---|
151 | value: token,
|
---|
152 | before: before,
|
---|
153 | after: ""
|
---|
154 | });
|
---|
155 | before = "";
|
---|
156 |
|
---|
157 | pos += 1;
|
---|
158 | code = value.charCodeAt(pos);
|
---|
159 |
|
---|
160 | // Open parentheses
|
---|
161 | } else if (openParentheses === code) {
|
---|
162 | // Whitespaces after open parentheses
|
---|
163 | next = pos;
|
---|
164 | do {
|
---|
165 | next += 1;
|
---|
166 | code = value.charCodeAt(next);
|
---|
167 | } while (code <= 32);
|
---|
168 | parenthesesOpenPos = pos;
|
---|
169 | token = {
|
---|
170 | type: "function",
|
---|
171 | sourceIndex: pos - name.length,
|
---|
172 | value: name,
|
---|
173 | before: value.slice(parenthesesOpenPos + 1, next)
|
---|
174 | };
|
---|
175 | pos = next;
|
---|
176 |
|
---|
177 | if (name === "url" && code !== singleQuote && code !== doubleQuote) {
|
---|
178 | next -= 1;
|
---|
179 | do {
|
---|
180 | escape = false;
|
---|
181 | next = value.indexOf(")", next + 1);
|
---|
182 | if (~next) {
|
---|
183 | escapePos = next;
|
---|
184 | while (value.charCodeAt(escapePos - 1) === backslash) {
|
---|
185 | escapePos -= 1;
|
---|
186 | escape = !escape;
|
---|
187 | }
|
---|
188 | } else {
|
---|
189 | value += ")";
|
---|
190 | next = value.length - 1;
|
---|
191 | token.unclosed = true;
|
---|
192 | }
|
---|
193 | } while (escape);
|
---|
194 | // Whitespaces before closed
|
---|
195 | whitespacePos = next;
|
---|
196 | do {
|
---|
197 | whitespacePos -= 1;
|
---|
198 | code = value.charCodeAt(whitespacePos);
|
---|
199 | } while (code <= 32);
|
---|
200 | if (parenthesesOpenPos < whitespacePos) {
|
---|
201 | if (pos !== whitespacePos + 1) {
|
---|
202 | token.nodes = [
|
---|
203 | {
|
---|
204 | type: "word",
|
---|
205 | sourceIndex: pos,
|
---|
206 | sourceEndIndex: whitespacePos + 1,
|
---|
207 | value: value.slice(pos, whitespacePos + 1)
|
---|
208 | }
|
---|
209 | ];
|
---|
210 | } else {
|
---|
211 | token.nodes = [];
|
---|
212 | }
|
---|
213 | if (token.unclosed && whitespacePos + 1 !== next) {
|
---|
214 | token.after = "";
|
---|
215 | token.nodes.push({
|
---|
216 | type: "space",
|
---|
217 | sourceIndex: whitespacePos + 1,
|
---|
218 | sourceEndIndex: next,
|
---|
219 | value: value.slice(whitespacePos + 1, next)
|
---|
220 | });
|
---|
221 | } else {
|
---|
222 | token.after = value.slice(whitespacePos + 1, next);
|
---|
223 | token.sourceEndIndex = next;
|
---|
224 | }
|
---|
225 | } else {
|
---|
226 | token.after = "";
|
---|
227 | token.nodes = [];
|
---|
228 | }
|
---|
229 | pos = next + 1;
|
---|
230 | token.sourceEndIndex = token.unclosed ? next : pos;
|
---|
231 | code = value.charCodeAt(pos);
|
---|
232 | tokens.push(token);
|
---|
233 | } else {
|
---|
234 | balanced += 1;
|
---|
235 | token.after = "";
|
---|
236 | token.sourceEndIndex = pos + 1;
|
---|
237 | tokens.push(token);
|
---|
238 | stack.push(token);
|
---|
239 | tokens = token.nodes = [];
|
---|
240 | parent = token;
|
---|
241 | }
|
---|
242 | name = "";
|
---|
243 |
|
---|
244 | // Close parentheses
|
---|
245 | } else if (closeParentheses === code && balanced) {
|
---|
246 | pos += 1;
|
---|
247 | code = value.charCodeAt(pos);
|
---|
248 |
|
---|
249 | parent.after = after;
|
---|
250 | parent.sourceEndIndex += after.length;
|
---|
251 | after = "";
|
---|
252 | balanced -= 1;
|
---|
253 | stack[stack.length - 1].sourceEndIndex = pos;
|
---|
254 | stack.pop();
|
---|
255 | parent = stack[balanced];
|
---|
256 | tokens = parent.nodes;
|
---|
257 |
|
---|
258 | // Words
|
---|
259 | } else {
|
---|
260 | next = pos;
|
---|
261 | do {
|
---|
262 | if (code === backslash) {
|
---|
263 | next += 1;
|
---|
264 | }
|
---|
265 | next += 1;
|
---|
266 | code = value.charCodeAt(next);
|
---|
267 | } while (
|
---|
268 | next < max &&
|
---|
269 | !(
|
---|
270 | code <= 32 ||
|
---|
271 | code === singleQuote ||
|
---|
272 | code === doubleQuote ||
|
---|
273 | code === comma ||
|
---|
274 | code === colon ||
|
---|
275 | code === slash ||
|
---|
276 | code === openParentheses ||
|
---|
277 | (code === star &&
|
---|
278 | parent &&
|
---|
279 | parent.type === "function" &&
|
---|
280 | parent.value === "calc") ||
|
---|
281 | (code === slash &&
|
---|
282 | parent.type === "function" &&
|
---|
283 | parent.value === "calc") ||
|
---|
284 | (code === closeParentheses && balanced)
|
---|
285 | )
|
---|
286 | );
|
---|
287 | token = value.slice(pos, next);
|
---|
288 |
|
---|
289 | if (openParentheses === code) {
|
---|
290 | name = token;
|
---|
291 | } else if (
|
---|
292 | (uLower === token.charCodeAt(0) || uUpper === token.charCodeAt(0)) &&
|
---|
293 | plus === token.charCodeAt(1) &&
|
---|
294 | isUnicodeRange.test(token.slice(2))
|
---|
295 | ) {
|
---|
296 | tokens.push({
|
---|
297 | type: "unicode-range",
|
---|
298 | sourceIndex: pos,
|
---|
299 | sourceEndIndex: next,
|
---|
300 | value: token
|
---|
301 | });
|
---|
302 | } else {
|
---|
303 | tokens.push({
|
---|
304 | type: "word",
|
---|
305 | sourceIndex: pos,
|
---|
306 | sourceEndIndex: next,
|
---|
307 | value: token
|
---|
308 | });
|
---|
309 | }
|
---|
310 |
|
---|
311 | pos = next;
|
---|
312 | }
|
---|
313 | }
|
---|
314 |
|
---|
315 | for (pos = stack.length - 1; pos; pos -= 1) {
|
---|
316 | stack[pos].unclosed = true;
|
---|
317 | stack[pos].sourceEndIndex = value.length;
|
---|
318 | }
|
---|
319 |
|
---|
320 | return stack[0].nodes;
|
---|
321 | };
|
---|
322 |
|
---|
323 | var walk$1 = function walk(nodes, cb, bubble) {
|
---|
324 | var i, max, node, result;
|
---|
325 |
|
---|
326 | for (i = 0, max = nodes.length; i < max; i += 1) {
|
---|
327 | node = nodes[i];
|
---|
328 | if (!bubble) {
|
---|
329 | result = cb(node, i, nodes);
|
---|
330 | }
|
---|
331 |
|
---|
332 | if (
|
---|
333 | result !== false &&
|
---|
334 | node.type === "function" &&
|
---|
335 | Array.isArray(node.nodes)
|
---|
336 | ) {
|
---|
337 | walk(node.nodes, cb, bubble);
|
---|
338 | }
|
---|
339 |
|
---|
340 | if (bubble) {
|
---|
341 | cb(node, i, nodes);
|
---|
342 | }
|
---|
343 | }
|
---|
344 | };
|
---|
345 |
|
---|
346 | function stringifyNode(node, custom) {
|
---|
347 | var type = node.type;
|
---|
348 | var value = node.value;
|
---|
349 | var buf;
|
---|
350 | var customResult;
|
---|
351 |
|
---|
352 | if (custom && (customResult = custom(node)) !== undefined) {
|
---|
353 | return customResult;
|
---|
354 | } else if (type === "word" || type === "space") {
|
---|
355 | return value;
|
---|
356 | } else if (type === "string") {
|
---|
357 | buf = node.quote || "";
|
---|
358 | return buf + value + (node.unclosed ? "" : buf);
|
---|
359 | } else if (type === "comment") {
|
---|
360 | return "/*" + value + (node.unclosed ? "" : "*/");
|
---|
361 | } else if (type === "div") {
|
---|
362 | return (node.before || "") + value + (node.after || "");
|
---|
363 | } else if (Array.isArray(node.nodes)) {
|
---|
364 | buf = stringify$1(node.nodes, custom);
|
---|
365 | if (type !== "function") {
|
---|
366 | return buf;
|
---|
367 | }
|
---|
368 | return (
|
---|
369 | value +
|
---|
370 | "(" +
|
---|
371 | (node.before || "") +
|
---|
372 | buf +
|
---|
373 | (node.after || "") +
|
---|
374 | (node.unclosed ? "" : ")")
|
---|
375 | );
|
---|
376 | }
|
---|
377 | return value;
|
---|
378 | }
|
---|
379 |
|
---|
380 | function stringify$1(nodes, custom) {
|
---|
381 | var result, i;
|
---|
382 |
|
---|
383 | if (Array.isArray(nodes)) {
|
---|
384 | result = "";
|
---|
385 | for (i = nodes.length - 1; ~i; i -= 1) {
|
---|
386 | result = stringifyNode(nodes[i], custom) + result;
|
---|
387 | }
|
---|
388 | return result;
|
---|
389 | }
|
---|
390 | return stringifyNode(nodes, custom);
|
---|
391 | }
|
---|
392 |
|
---|
393 | var stringify_1 = stringify$1;
|
---|
394 |
|
---|
395 | var unit;
|
---|
396 | var hasRequiredUnit;
|
---|
397 |
|
---|
398 | function requireUnit () {
|
---|
399 | if (hasRequiredUnit) return unit;
|
---|
400 | hasRequiredUnit = 1;
|
---|
401 | var minus = "-".charCodeAt(0);
|
---|
402 | var plus = "+".charCodeAt(0);
|
---|
403 | var dot = ".".charCodeAt(0);
|
---|
404 | var exp = "e".charCodeAt(0);
|
---|
405 | var EXP = "E".charCodeAt(0);
|
---|
406 |
|
---|
407 | // Check if three code points would start a number
|
---|
408 | // https://www.w3.org/TR/css-syntax-3/#starts-with-a-number
|
---|
409 | function likeNumber(value) {
|
---|
410 | var code = value.charCodeAt(0);
|
---|
411 | var nextCode;
|
---|
412 |
|
---|
413 | if (code === plus || code === minus) {
|
---|
414 | nextCode = value.charCodeAt(1);
|
---|
415 |
|
---|
416 | if (nextCode >= 48 && nextCode <= 57) {
|
---|
417 | return true;
|
---|
418 | }
|
---|
419 |
|
---|
420 | var nextNextCode = value.charCodeAt(2);
|
---|
421 |
|
---|
422 | if (nextCode === dot && nextNextCode >= 48 && nextNextCode <= 57) {
|
---|
423 | return true;
|
---|
424 | }
|
---|
425 |
|
---|
426 | return false;
|
---|
427 | }
|
---|
428 |
|
---|
429 | if (code === dot) {
|
---|
430 | nextCode = value.charCodeAt(1);
|
---|
431 |
|
---|
432 | if (nextCode >= 48 && nextCode <= 57) {
|
---|
433 | return true;
|
---|
434 | }
|
---|
435 |
|
---|
436 | return false;
|
---|
437 | }
|
---|
438 |
|
---|
439 | if (code >= 48 && code <= 57) {
|
---|
440 | return true;
|
---|
441 | }
|
---|
442 |
|
---|
443 | return false;
|
---|
444 | }
|
---|
445 |
|
---|
446 | // Consume a number
|
---|
447 | // https://www.w3.org/TR/css-syntax-3/#consume-number
|
---|
448 | unit = function(value) {
|
---|
449 | var pos = 0;
|
---|
450 | var length = value.length;
|
---|
451 | var code;
|
---|
452 | var nextCode;
|
---|
453 | var nextNextCode;
|
---|
454 |
|
---|
455 | if (length === 0 || !likeNumber(value)) {
|
---|
456 | return false;
|
---|
457 | }
|
---|
458 |
|
---|
459 | code = value.charCodeAt(pos);
|
---|
460 |
|
---|
461 | if (code === plus || code === minus) {
|
---|
462 | pos++;
|
---|
463 | }
|
---|
464 |
|
---|
465 | while (pos < length) {
|
---|
466 | code = value.charCodeAt(pos);
|
---|
467 |
|
---|
468 | if (code < 48 || code > 57) {
|
---|
469 | break;
|
---|
470 | }
|
---|
471 |
|
---|
472 | pos += 1;
|
---|
473 | }
|
---|
474 |
|
---|
475 | code = value.charCodeAt(pos);
|
---|
476 | nextCode = value.charCodeAt(pos + 1);
|
---|
477 |
|
---|
478 | if (code === dot && nextCode >= 48 && nextCode <= 57) {
|
---|
479 | pos += 2;
|
---|
480 |
|
---|
481 | while (pos < length) {
|
---|
482 | code = value.charCodeAt(pos);
|
---|
483 |
|
---|
484 | if (code < 48 || code > 57) {
|
---|
485 | break;
|
---|
486 | }
|
---|
487 |
|
---|
488 | pos += 1;
|
---|
489 | }
|
---|
490 | }
|
---|
491 |
|
---|
492 | code = value.charCodeAt(pos);
|
---|
493 | nextCode = value.charCodeAt(pos + 1);
|
---|
494 | nextNextCode = value.charCodeAt(pos + 2);
|
---|
495 |
|
---|
496 | if (
|
---|
497 | (code === exp || code === EXP) &&
|
---|
498 | ((nextCode >= 48 && nextCode <= 57) ||
|
---|
499 | ((nextCode === plus || nextCode === minus) &&
|
---|
500 | nextNextCode >= 48 &&
|
---|
501 | nextNextCode <= 57))
|
---|
502 | ) {
|
---|
503 | pos += nextCode === plus || nextCode === minus ? 3 : 2;
|
---|
504 |
|
---|
505 | while (pos < length) {
|
---|
506 | code = value.charCodeAt(pos);
|
---|
507 |
|
---|
508 | if (code < 48 || code > 57) {
|
---|
509 | break;
|
---|
510 | }
|
---|
511 |
|
---|
512 | pos += 1;
|
---|
513 | }
|
---|
514 | }
|
---|
515 |
|
---|
516 | return {
|
---|
517 | number: value.slice(0, pos),
|
---|
518 | unit: value.slice(pos)
|
---|
519 | };
|
---|
520 | };
|
---|
521 | return unit;
|
---|
522 | }
|
---|
523 |
|
---|
524 | var parse = parse$1;
|
---|
525 | var walk = walk$1;
|
---|
526 | var stringify = stringify_1;
|
---|
527 |
|
---|
528 | function ValueParser(value) {
|
---|
529 | if (this instanceof ValueParser) {
|
---|
530 | this.nodes = parse(value);
|
---|
531 | return this;
|
---|
532 | }
|
---|
533 | return new ValueParser(value);
|
---|
534 | }
|
---|
535 |
|
---|
536 | ValueParser.prototype.toString = function() {
|
---|
537 | return Array.isArray(this.nodes) ? stringify(this.nodes) : "";
|
---|
538 | };
|
---|
539 |
|
---|
540 | ValueParser.prototype.walk = function(cb, bubble) {
|
---|
541 | walk(this.nodes, cb, bubble);
|
---|
542 | return this;
|
---|
543 | };
|
---|
544 |
|
---|
545 | ValueParser.unit = requireUnit();
|
---|
546 |
|
---|
547 | ValueParser.walk = walk;
|
---|
548 |
|
---|
549 | ValueParser.stringify = stringify;
|
---|
550 |
|
---|
551 | var lib = ValueParser;
|
---|
552 |
|
---|
553 | export { lib as l };
|
---|