MCPcopy
hub / github.com/irbis-sh/zen-desktop / plan

Function plan

jslibs/extended-css/src/parse/selectorList/plan.ts:11–164  ·  view source on GitHub ↗
(tokens: IRToken[])

Source from the content-addressed store, hash-verified

9 * Builds a final, optimized query out of intermediate representation tokens.
10 */
11export function plan(tokens: IRToken[]): Selector {
12 if (tokens.length === 0) return [];
13
14 const steps: Selector = [];
15 let haveContextualStep = false; // true after we've emitted anything that can serve as context for requiresContext
16
17 const emitBridge = (comb: CombToken) => {
18 switch (comb.literal) {
19 case ' ':
20 steps.push(new Descendant());
21 break;
22 case '+':
23 steps.push(new NextSibling());
24 break;
25 case '~':
26 steps.push(new SubsequentSibling());
27 break;
28 case '>':
29 steps.push(new Child());
30 break;
31 default:
32 throw new Error(`Unknown combinator: "${comb.literal}"`);
33 }
34 haveContextualStep = true;
35 };
36
37 const emitExt = (name: keyof typeof extPseudoClasses, args: string) => {
38 const extClass = extPseudoClasses[name];
39 if (!extClass) {
40 throw new Error(`Unknown extended pseudo-class ":${name}"`);
41 }
42 if (extClass.requiresContext && !haveContextualStep) {
43 steps.push(new RawQuery('*'));
44 haveContextualStep = true;
45 }
46 steps.push(new extClass(args));
47 haveContextualStep = true;
48 };
49
50 // ---------- Relative selector (starts with a combinator) ----------
51 const startsWithCombinator = tokens[0].kind === 'comb';
52 if (startsWithCombinator) {
53 /**
54 * Special case: if the selector start with a combinator, then it's a relative selector.
55 * In this case we cannot optimize by merging adjacent "raw" runs; instead, we must rely on filtering via
56 * .matches at each step.
57 * This behavior is required to support the :has() pseudo-class.
58 * See: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_selectors/Selector_structure#relative_selector
59 */
60 for (let i = 0; i < tokens.length; i++) {
61 const t = tokens[i];
62
63 switch (t.kind) {
64 case 'comb': {
65 // Mirror default path invariants
66 const next = tokens[i + 1];
67 if (!next) {
68 throw new Error('Relative selector ends with a dangling combinator');

Callers

nothing calls this directly

Calls 4

emitBridgeFunction · 0.85
emitExtFunction · 0.85
pushMethod · 0.80
flushRawFunction · 0.70

Tested by

no test coverage detected