UNPKG

2.02 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", {
3 value: true
4});
5Object.defineProperty(exports, // Arbitrary values must contain balanced brackets (), [] and {}. Escaped
6// values don't count, and brackets inside quotes also don't count.
7//
8// E.g.: w-[this-is]w-[weird-and-invalid]
9// E.g.: w-[this-is\\]w-\\[weird-but-valid]
10// E.g.: content-['this-is-also-valid]-weirdly-enough']
11"default", {
12 enumerable: true,
13 get: ()=>isValidArbitraryValue
14});
15let matchingBrackets = new Map([
16 [
17 "{",
18 "}"
19 ],
20 [
21 "[",
22 "]"
23 ],
24 [
25 "(",
26 ")"
27 ]
28]);
29let inverseMatchingBrackets = new Map(Array.from(matchingBrackets.entries()).map(([k, v])=>[
30 v,
31 k
32 ]));
33let quotes = new Set([
34 '"',
35 "'",
36 "`"
37]);
38function isValidArbitraryValue(value) {
39 let stack = [];
40 let inQuotes = false;
41 for(let i = 0; i < value.length; i++){
42 let char = value[i];
43 if (char === ":" && !inQuotes && stack.length === 0) {
44 return false;
45 }
46 // Non-escaped quotes allow us to "allow" anything in between
47 if (quotes.has(char) && value[i - 1] !== "\\") {
48 inQuotes = !inQuotes;
49 }
50 if (inQuotes) continue;
51 if (value[i - 1] === "\\") continue; // Escaped
52 if (matchingBrackets.has(char)) {
53 stack.push(char);
54 } else if (inverseMatchingBrackets.has(char)) {
55 let inverse = inverseMatchingBrackets.get(char);
56 // Nothing to pop from, therefore it is unbalanced
57 if (stack.length <= 0) {
58 return false;
59 }
60 // Popped value must match the inverse value, otherwise it is unbalanced
61 if (stack.pop() !== inverse) {
62 return false;
63 }
64 }
65 }
66 // If there is still something on the stack, it is also unbalanced
67 if (stack.length > 0) {
68 return false;
69 }
70 // All good, totally balanced!
71 return true;
72}