|
1 | 1 | import * as boolbase from "boolbase"; |
| 2 | +import { parse } from "css-what"; |
2 | 3 | import getNCheck from "nth-check"; |
3 | 4 | import { cacheParentResults } from "../helpers/cache.js"; |
4 | 5 | import { getElementParent } from "../helpers/querying.js"; |
5 | | -import type { CompiledQuery, InternalOptions } from "../types.js"; |
| 6 | +import type { CompiledQuery, CompileToken, InternalOptions } from "../types.js"; |
6 | 7 |
|
7 | | -type Filter = <Node, ElementNode extends Node>( |
| 8 | +/** @see {@link https://www.w3.org/TR/selectors-4/#the-nth-child-pseudo} */ |
| 9 | +const nthOfRegex = /^(.+?)\s+of\s+(.+)$/is; |
| 10 | + |
| 11 | +/** A pre-compiled pseudo filter. */ |
| 12 | +export type Filter = <Node, ElementNode extends Node>( |
8 | 13 | next: CompiledQuery<ElementNode>, |
9 | 14 | text: string, |
10 | 15 | options: InternalOptions<Node, ElementNode>, |
11 | 16 | context?: Node[], |
| 17 | + compileToken?: CompileToken<Node, ElementNode>, |
12 | 18 | ) => CompiledQuery<ElementNode>; |
13 | 19 |
|
14 | | -/** |
15 | | - * Pre-compiled pseudo filters. |
16 | | - */ |
17 | | -export const filters: Record<string, Filter> = { |
18 | | - contains(next, text, options) { |
19 | | - const { getText } = options.adapter; |
| 20 | +function compileNth(reverse: boolean, ofType: boolean): Filter { |
| 21 | + return function nth(next, rule, options, _context, compileToken) { |
| 22 | + const { adapter, equals } = options; |
| 23 | + const ofMatch = ofType ? null : rule.match(nthOfRegex); |
| 24 | + const nthCheck = getNCheck(ofMatch ? ofMatch[1].trim() : rule); |
20 | 25 |
|
21 | | - return cacheParentResults(next, options, (element) => |
22 | | - getText(element).includes(text), |
23 | | - ); |
24 | | - }, |
25 | | - icontains(next, text, options) { |
26 | | - const itext = text.toLowerCase(); |
27 | | - const { getText } = options.adapter; |
| 26 | + if (nthCheck === boolbase.falseFunc) return boolbase.falseFunc; |
28 | 27 |
|
29 | | - return cacheParentResults(next, options, (element) => |
30 | | - getText(element).toLowerCase().includes(itext), |
31 | | - ); |
32 | | - }, |
| 28 | + const ofSelector = |
| 29 | + ofMatch && compileToken |
| 30 | + ? compileToken(parse(ofMatch[2].trim()), options) |
| 31 | + : undefined; |
33 | 32 |
|
34 | | - // Location specific methods |
35 | | - "nth-child"(next, rule, { adapter, equals }) { |
36 | | - const nthCheck = getNCheck(rule); |
| 33 | + if (ofSelector === boolbase.falseFunc) return boolbase.falseFunc; |
37 | 34 |
|
38 | | - if (nthCheck === boolbase.falseFunc) { |
39 | | - return boolbase.falseFunc; |
40 | | - } |
41 | | - if (nthCheck === boolbase.trueFunc) { |
| 35 | + if (nthCheck === boolbase.trueFunc && !ofSelector) { |
42 | 36 | return (element) => |
43 | 37 | getElementParent(element, adapter) !== null && next(element); |
44 | 38 | } |
45 | 39 |
|
46 | | - return function nthChild(element) { |
47 | | - const siblings = adapter.getSiblings(element); |
48 | | - let pos = 0; |
49 | | - |
50 | | - for (const sibling of siblings) { |
51 | | - if (equals(element, sibling)) { |
52 | | - break; |
53 | | - } |
54 | | - if (adapter.isTag(sibling)) { |
55 | | - pos++; |
| 40 | + type ElementNode = Parameters<typeof next>[0]; |
| 41 | + |
| 42 | + const shouldCount = ofSelector |
| 43 | + ? (_element: ElementNode, sibling: ElementNode) => |
| 44 | + ofSelector(sibling) |
| 45 | + : ofType |
| 46 | + ? (element: ElementNode, sibling: ElementNode) => |
| 47 | + adapter.getName(sibling) === adapter.getName(element) |
| 48 | + : boolbase.trueFunc; |
| 49 | + |
| 50 | + if (reverse) { |
| 51 | + return function nthLast(element) { |
| 52 | + if (ofSelector && !ofSelector(element)) return false; |
| 53 | + const siblings = adapter.getSiblings(element); |
| 54 | + let pos = 0; |
| 55 | + for (let index = siblings.length - 1; index >= 0; index--) { |
| 56 | + const sibling = siblings[index]; |
| 57 | + if (equals(element, sibling)) break; |
| 58 | + if (adapter.isTag(sibling) && shouldCount(element, sibling)) |
| 59 | + pos++; |
56 | 60 | } |
57 | | - } |
58 | | - |
59 | | - return nthCheck(pos) && next(element); |
60 | | - }; |
61 | | - }, |
62 | | - "nth-last-child"(next, rule, { adapter, equals }) { |
63 | | - const nthCheck = getNCheck(rule); |
64 | | - |
65 | | - if (nthCheck === boolbase.falseFunc) { |
66 | | - return boolbase.falseFunc; |
67 | | - } |
68 | | - if (nthCheck === boolbase.trueFunc) { |
69 | | - return (element) => |
70 | | - getElementParent(element, adapter) !== null && next(element); |
| 61 | + return nthCheck(pos) && next(element); |
| 62 | + }; |
71 | 63 | } |
72 | 64 |
|
73 | | - return function nthLastChild(element) { |
| 65 | + return function nth(element) { |
| 66 | + if (ofSelector && !ofSelector(element)) return false; |
74 | 67 | const siblings = adapter.getSiblings(element); |
75 | 68 | let pos = 0; |
76 | | - |
77 | | - for (let index = siblings.length - 1; index >= 0; index--) { |
78 | | - if (equals(element, siblings[index])) { |
79 | | - break; |
80 | | - } |
81 | | - if (adapter.isTag(siblings[index])) { |
| 69 | + for (const sibling of siblings) { |
| 70 | + if (equals(element, sibling)) break; |
| 71 | + if (adapter.isTag(sibling) && shouldCount(element, sibling)) |
82 | 72 | pos++; |
83 | | - } |
84 | 73 | } |
85 | | - |
86 | 74 | return nthCheck(pos) && next(element); |
87 | 75 | }; |
88 | | - }, |
89 | | - "nth-of-type"(next, rule, { adapter, equals }) { |
90 | | - const nthCheck = getNCheck(rule); |
91 | | - |
92 | | - if (nthCheck === boolbase.falseFunc) { |
93 | | - return boolbase.falseFunc; |
94 | | - } |
95 | | - if (nthCheck === boolbase.trueFunc) { |
96 | | - return (element) => |
97 | | - getElementParent(element, adapter) !== null && next(element); |
98 | | - } |
99 | | - |
100 | | - return function nthOfType(element) { |
101 | | - const siblings = adapter.getSiblings(element); |
102 | | - let pos = 0; |
| 76 | + }; |
| 77 | +} |
103 | 78 |
|
104 | | - for (const currentSibling of siblings) { |
105 | | - if (equals(element, currentSibling)) { |
106 | | - break; |
107 | | - } |
108 | | - if ( |
109 | | - adapter.isTag(currentSibling) && |
110 | | - adapter.getName(currentSibling) === adapter.getName(element) |
111 | | - ) { |
112 | | - pos++; |
113 | | - } |
114 | | - } |
| 79 | +/** |
| 80 | + * Pre-compiled pseudo filters. |
| 81 | + */ |
| 82 | +export const filters: Record<string, Filter> = { |
| 83 | + contains(next, text, options) { |
| 84 | + const { getText } = options.adapter; |
115 | 85 |
|
116 | | - return nthCheck(pos) && next(element); |
117 | | - }; |
| 86 | + return cacheParentResults(next, options, (element) => |
| 87 | + getText(element).includes(text), |
| 88 | + ); |
118 | 89 | }, |
119 | | - "nth-last-of-type"(next, rule, { adapter, equals }) { |
120 | | - const nthCheck = getNCheck(rule); |
121 | | - |
122 | | - if (nthCheck === boolbase.falseFunc) { |
123 | | - return boolbase.falseFunc; |
124 | | - } |
125 | | - if (nthCheck === boolbase.trueFunc) { |
126 | | - return (element) => |
127 | | - getElementParent(element, adapter) !== null && next(element); |
128 | | - } |
129 | | - |
130 | | - return function nthLastOfType(element) { |
131 | | - const siblings = adapter.getSiblings(element); |
132 | | - let pos = 0; |
133 | | - |
134 | | - for (let index = siblings.length - 1; index >= 0; index--) { |
135 | | - const currentSibling = siblings[index]; |
136 | | - if (equals(element, currentSibling)) { |
137 | | - break; |
138 | | - } |
139 | | - if ( |
140 | | - adapter.isTag(currentSibling) && |
141 | | - adapter.getName(currentSibling) === adapter.getName(element) |
142 | | - ) { |
143 | | - pos++; |
144 | | - } |
145 | | - } |
| 90 | + icontains(next, text, options) { |
| 91 | + const itext = text.toLowerCase(); |
| 92 | + const { getText } = options.adapter; |
146 | 93 |
|
147 | | - return nthCheck(pos) && next(element); |
148 | | - }; |
| 94 | + return cacheParentResults(next, options, (element) => |
| 95 | + getText(element).toLowerCase().includes(itext), |
| 96 | + ); |
149 | 97 | }, |
150 | 98 |
|
| 99 | + // Location specific methods |
| 100 | + "nth-child": compileNth(false, false), |
| 101 | + "nth-last-child": compileNth(true, false), |
| 102 | + "nth-of-type": compileNth(false, true), |
| 103 | + "nth-last-of-type": compileNth(true, true), |
| 104 | + |
151 | 105 | // TODO determine the actual root element |
152 | 106 | root(next, _rule, { adapter }) { |
153 | 107 | return (element) => |
|
0 commit comments