Repository navigation
fix: avoid reusing descendant results across :has anchors #1940
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Aschenbath
wants to merge
1
commit into
fb55:master
Choose a base branch
from
Aschenbath:fix/mutable-has-descendant-cache-1929
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import type { AnyNode, Element } from "domhandler"; | ||
| import { DomUtils, parseDocument } from "htmlparser2"; | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import * as CSSselect from "../src/index.js"; | ||
|
|
||
| const html = | ||
| '<div class="c" id="o"><div class="b c" id="m">' + | ||
| '<span class="b c" id="t"><input class="b" disabled>needle</span></div></div>'; | ||
|
|
||
| describe("descendant caching inside :has", () => { | ||
| const selectors = [ | ||
| ":has(:is(.c .b) :disabled)", | ||
| ":has(:where(.c .b) :disabled)", | ||
| ":has(:matches(.c .b) :disabled)", | ||
| ":has(:not(:not(.c .b)) :disabled)", | ||
| ':has(:is(.c .b):contains("needle") :disabled)', | ||
| ':has(:is(.c .b):icontains("NEEDLE") :disabled)', | ||
| ':has(:not(:not(.c .b)):contains("needle") :disabled)', | ||
| ":has(> :is(.c .b) :disabled)", | ||
| ":has(:is(:is(.c .b) :disabled))", | ||
| ]; | ||
|
|
||
| it.each(selectors)( | ||
| "does not reuse another anchor's answers for %s", | ||
| (selector) => { | ||
| const document = parseDocument(html); | ||
| const elements = DomUtils.findAll( | ||
| (element) => !!element.attribs["id"], | ||
| document.children, | ||
| ); | ||
| // eslint-disable-next-line unicorn/no-array-reverse -- The project targets ES2022. | ||
| for (const ordered of [elements, [...elements].reverse()]) { | ||
| const expected = ordered.filter( | ||
| CSSselect.compile(selector, { cacheResults: false }), | ||
| ); | ||
| const compiled = CSSselect.compile(selector); | ||
| expect(ordered.filter(compiled)).toStrictEqual(expected); | ||
| expect(ordered.filter(compiled)).toStrictEqual(expected); | ||
| } | ||
| expect(CSSselect.selectAll(selector, document)).toStrictEqual( | ||
| CSSselect.selectAll(selector, document, { | ||
| cacheResults: false, | ||
| }), | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| it("matches the reported tree independently of evaluation order", () => { | ||
| const document = parseDocument(html); | ||
| const selector = ":has(:is(.c .b) :disabled)"; | ||
| expect( | ||
| CSSselect.selectAll<AnyNode, Element>(selector, document).map( | ||
| (element) => element.attribs["id"], | ||
| ), | ||
| ).toStrictEqual(["o", "m"]); | ||
| const elements = CSSselect.selectAll<AnyNode, Element>( | ||
| "[id]", | ||
| document, | ||
| ); | ||
| elements.reverse(); | ||
| const compiled = CSSselect.compile(selector); | ||
| expect( | ||
| elements.filter(compiled).map((element) => element.attribs["id"]), | ||
| ).toStrictEqual(["m", "o"]); | ||
| }); | ||
|
|
||
| it("retains descendant caching with a stable query context", () => { | ||
| const document = parseDocument(html); | ||
| const elements = CSSselect.selectAll<AnyNode, Element>( | ||
| "[id]", | ||
| document, | ||
| ); | ||
| const input = CSSselect.selectOne<AnyNode, Element>("input", document); | ||
| if (input === null) throw new Error("Missing test input"); | ||
|
|
||
| for (const context of [undefined, elements[0], [elements[0]]]) { | ||
| for (const shouldCache of [true, false]) { | ||
| const marker = vi.fn(() => true); | ||
| const compiled = CSSselect.compile<AnyNode, Element>( | ||
| ":is(.c .b):marker :disabled", | ||
| { pseudos: { marker }, cacheResults: shouldCache }, | ||
| context, | ||
| ); | ||
| expect(compiled(input)).toBe(true); | ||
| const calls = marker.mock.calls.length; | ||
| expect(calls).toBeGreaterThan(0); | ||
| expect(compiled(input)).toBe(true); | ||
| if (shouldCache) { | ||
| expect(marker).toHaveBeenCalledTimes(calls); | ||
| } else { | ||
| expect(marker.mock.calls.length).toBeGreaterThan(calls); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: fb55/css-select
Length of output: 924
Script executed:
Repository: fb55/css-select
Length of output: 25969
Script executed:
Repository: fb55/css-select
Length of output: 42480
Script executed:
Repository: fb55/css-select
Length of output: 19732
Preserve descendant caching within each
:hasanchor.For deep trees with many candidates, disabling this cache repeats the same ancestor walk for each candidate. For example,
:has(> :contains("needle") c)can perform millions of extra parent checks when no text matches. Scope cached results to the current anchor to retain caching without reusing stale answers.