You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit d8158d9
Browse filesBrowse the repository at this point in the historyBrowse files
Read the last token of a selector that ends in a nested pseudo-class (#182)
Before testing candidates, collect() asks reOptimizer for the last simple token of a selector and uses it to fetch the candidates by tag, class or id. The parenthesized part of that pattern is '\x28[^\x29]+(?:\x29|$)', which stops at the first ')', so a final compound holding a nested functional pseudo-class does not match at all — and a selector the optimizer cannot read is answered by walking every element in the context.
'div:not(:nth-of-type(2n))' therefore tests every element in the document instead of the divs, and since ':not()' evaluates its argument through s.match() per element, each of those elements resolves nth-of-type. On a 6300-element page that is 6344 resolutions building 3911 sibling caches over 196312 steps, for a selector whose subject is a div.
The parenthesized part now tolerates two levels of nesting, which reaches ':not(:not(:not(span)))'. Deeper than that falls back to the unoptimized scan, as before. Both the old and new patterns stay linear on unbalanced input: 3200 unclosed parentheses match in 0.02ms.
div:not(:nth-of-type(2n)) 45.62ms -> 134.92us 338x div:not(:nth-child(3)) 9.42ms -> 123.94us 76x div:is(.example):not(:where(.x)) 2.65ms -> 39.45us 67x div:not(.x) 27.93us -> 27.75us -
Results are unchanged; the four above agree with the native engine.
References:
- Spec: https://drafts.csswg.org/css-syntax/#consume-simple-block — why a parenthesized part has to tolerate nesting
- Spec: https://drafts.csswg.org/selectors-4/#matches — the functional pseudo-classes that put parentheses inside a compound
Copy file name to clipboardExpand all lines: docs/api.md
+31-31Lines changed: 31 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,21 +11,21 @@ Query contexts default to the factory document when omitted. `closest()`, `first
11
11
12
12
| Method | Result |
13
13
| --- | --- |
14
-
|[`byClass(cls, context)`](../src/nwsapi.mts#L2972)| Returns elements with the class name. |
15
-
|[`byId(id, context)`](../src/nwsapi.mts#L2970)| Returns elements with the ID. Duplicate IDs are allowed by default. |
16
-
|[`byTag(tag, context)`](../src/nwsapi.mts#L2971)| Returns elements with the tag name. Use `*` for all elements. |
17
-
|[`closest(selectors, element, callback)`](../src/nwsapi.mts#L2978)| Returns the nearest match, starting with the element, or `null`. |
18
-
|[`compile(selector, mode, callback)`](../src/nwsapi.mts#L2980)| Compiles a selector into a resolver function. This is an advanced API. |
19
-
|[`configure(option, clear)`](../src/nwsapi.mts#L2981)| Reads or changes options. Pass `true` as the second argument to clear compiled selectors. |
20
-
|[`emit(message, proto)`](../src/nwsapi.mts#L2983)| Reports an error using the configured error policy. |
21
-
|[`first(selectors, context, callback)`](../src/nwsapi.mts#L2974)| Returns the first matching descendant, or `null`. |
22
-
|[`install(all)`](../src/nwsapi.mts#L2989)| Replaces native selector methods. Pass `true` to also replace collection methods. |
23
-
|[`match(selectors, element, callback)`](../src/nwsapi.mts#L2975)| Returns whether the element matches. |
24
-
|[`registerCombinator(combinator, resolver)`](../src/nwsapi.mts#L2996)| Adds a relationship between elements using trusted resolver code. |
25
-
|[`registerOperator(operator, resolver)`](../src/nwsapi.mts#L3021)| Adds an attribute operator using a resolver with `p1`, `p2`, and `p3` fields. |
26
-
|[`registerSelector(name, rexp, func)`](../src/nwsapi.mts#L3043)| Adds a selector pattern and a compiler callback that returns `source` and `status`. |
27
-
|[`select(selectors, context, callback)`](../src/nwsapi.mts#L2976)| Returns an array of matching descendants, or an empty array. |
28
-
|[`uninstall()`](../src/nwsapi.mts#L2990)| Restores the native methods saved by `install()`. |
14
+
|[`byClass(cls, context)`](../src/nwsapi.mts#L2982)| Returns elements with the class name. |
15
+
|[`byId(id, context)`](../src/nwsapi.mts#L2980)| Returns elements with the ID. Duplicate IDs are allowed by default. |
16
+
|[`byTag(tag, context)`](../src/nwsapi.mts#L2981)| Returns elements with the tag name. Use `*` for all elements. |
17
+
|[`closest(selectors, element, callback)`](../src/nwsapi.mts#L2988)| Returns the nearest match, starting with the element, or `null`. |
18
+
|[`compile(selector, mode, callback)`](../src/nwsapi.mts#L2990)| Compiles a selector into a resolver function. This is an advanced API. |
19
+
|[`configure(option, clear)`](../src/nwsapi.mts#L2991)| Reads or changes options. Pass `true` as the second argument to clear compiled selectors. |
20
+
|[`emit(message, proto)`](../src/nwsapi.mts#L2993)| Reports an error using the configured error policy. |
21
+
|[`first(selectors, context, callback)`](../src/nwsapi.mts#L2984)| Returns the first matching descendant, or `null`. |
22
+
|[`install(all)`](../src/nwsapi.mts#L2999)| Replaces native selector methods. Pass `true` to also replace collection methods. |
23
+
|[`match(selectors, element, callback)`](../src/nwsapi.mts#L2985)| Returns whether the element matches. |
24
+
|[`registerCombinator(combinator, resolver)`](../src/nwsapi.mts#L3006)| Adds a relationship between elements using trusted resolver code. |
25
+
|[`registerOperator(operator, resolver)`](../src/nwsapi.mts#L3031)| Adds an attribute operator using a resolver with `p1`, `p2`, and `p3` fields. |
26
+
|[`registerSelector(name, rexp, func)`](../src/nwsapi.mts#L3053)| Adds a selector pattern and a compiler callback that returns `source` and `status`. |
27
+
|[`select(selectors, context, callback)`](../src/nwsapi.mts#L2986)| Returns an array of matching descendants, or an empty array. |
28
+
|[`uninstall()`](../src/nwsapi.mts#L3000)| Restores the native methods saved by `install()`. |
29
29
30
30
<details>
31
31
<summary>Configuration</summary>
@@ -60,22 +60,22 @@ These exports support extensions and debugging. Prefer query methods and `config
60
60
61
61
| Member | Purpose |
62
62
| --- | --- |
63
-
|[`CFG`](../src/nwsapi.mts#L2958)| Contains the compiler syntax settings. |
64
-
|[`Config`](../src/nwsapi.mts#L2984)| Contains the active options. Use `configure()` to change them. |
65
-
|[`M_BODY`](../src/nwsapi.mts#L2961)| Contains the matching resolver body template. |
66
-
|[`M_TEST`](../src/nwsapi.mts#L2965)| Contains the matching resolver test template. |
67
-
|[`matchLambdas`](../src/nwsapi.mts#L2950)| Caches compiled matching functions, not DOM results. |
68
-
|[`matchResolvers`](../src/nwsapi.mts#L2953)| Caches matching plans, not DOM results. |
69
-
|[`N_BODY`](../src/nwsapi.mts#L2962)| Exposes the matching resolver body template. |
70
-
|[`N_TEST`](../src/nwsapi.mts#L2966)| Contains the alternate resolver test template. |
0 commit comments