Skip to content

Selectors: Match exact selectors without css-select - #516

Open
kepano wants to merge 3 commits into
mainfrom
pr/1-selectors
Open

kepano wants to merge 3 commits into
mainfrom
pr/1-selectors

Conversation

@kepano

@kepano kepano commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Extraction is 19% faster on linkedom and 23% faster on JSDOM.

removeBySelector ran querySelectorAll(EXACT_SELECTORS_JOINED), so linkedom's css-select tested every element against 166 selectors, many of them attribute tests like [class*="pagination" i]. Its removal loop also ran FOOTNOTE_LIST_SELECTORS through matches, querySelector and closest for every element it was about to remove.

src/utils/selector-matcher.ts reads EXACT_SELECTORS from src/constants.ts when it loads. Plain tag, id and class selectors go into Sets. Compound selectors with attribute tests and :not() become JS tests indexed by tag, attribute or class. The three :has() selectors run matches() only on audio, video and header, and the three with combinators go to one querySelectorAll. The removal loop collects footnote lists and their ancestors once.

I kept the matcher small rather than writing a full selector compiler. It follows two rules that are easy to miss: values of HTML attributes such as type and rel match case-insensitively, and quirks-mode pages, where browsers and JSDOM match classes case-insensitively, use querySelectorAll. tests/selector-matcher.test.ts compares the matcher with querySelectorAll for seven lists from src/constants.ts on every fixture.

Medians: linkedom 1,880 → 1,523 ms, JSDOM 6,732 → 5,202 ms, both outside the spread. It adds 1.3 KB gzipped to the core bundle and 1.4 KB to the full one, and the core budget goes from 94 to 96 KB. +228 / −5 lines.

Output is byte-identical to main for all 250 fixtures, at each fixture's own URL and at https://example.com/page, with no options, separateMarkdown and markdown, under linkedom and JSDOM (1,500 runs each). tsc --noEmit, lint:unused, lint:innerhtml, npm run size and both Vitest suites pass. Timing alternates fresh processes of each branch over 0.19.11's 248 fixtures, 9 pairs on linkedom and 7 on JSDOM. I haven't run it in a browser, happy-dom or the Cloudflare Worker, and the output check doesn't cover debug mode.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant