MCPcopy Create free account
hub / github.com/CanadaHonk/shadow / parseSelector

Method parseSelector

engine/cssparser.js:47–108  ·  view source on GitHub ↗
(input)

Source from the content-addressed store, hash-verified

45 properties = {};
46
47 static parseSelector(input) {
48 input = input.trim()
49 .replaceAll('\r\n', '\n'); // normalize newlines
50
51 const out = [];
52
53 for (let x of input.split(',')) {
54 x = x.trim();
55
56 let cType = CombinatorType.Descendant;
57 let sType = SelectorType.Tag;
58 let text = '';
59
60 const sels = [];
61 let conds = [];
62
63 const pushCond = () => {
64 if (text || sType === SelectorType.Universal) conds.push({ type: sType, text });
65 sType = SelectorType.Tag;
66 text = '';
67 };
68
69 const isCombinator = c => [' ', '>'].includes(c);
70
71 for (let i = 0; i < x.length; i++) {
72 const c = x[i];
73
74 if (isCombinator(c) && text && (c !== ' ' || (!isCombinator(x[i + 1]) && !isspace(x[i + 1])))) {
75 pushCond();
76
77 sels.push({ type: cType, conds });
78 conds = [];
79
80 if (c === ' ') cType = CombinatorType.Descendant;
81 if (c === '>') cType = CombinatorType.Child;
82
83 continue;
84 }
85
86 if (['#', '.', '*', ':'].includes(c) || i === x.length - 1) {
87 if (i === x.length - 1) text += c;
88
89 if (c === '*') sType = SelectorType.Universal;
90 pushCond();
91
92 if (c === '#') sType = SelectorType.Id;
93 if (c === '.') sType = SelectorType.Class;
94 if (c === ':') sType = SelectorType.Pseudo;
95
96 continue;
97 }
98
99 if (!isspace(c)) text += c;
100 }
101
102 if (conds.length > 0) sels.push({ type: cType, conds });
103
104 out.push(sels);

Callers 3

querySelectorMethod · 0.80
querySelectorAllMethod · 0.80
constructorMethod · 0.80

Calls 1

isspaceFunction · 0.85

Tested by

no test coverage detected