Skip to content

fix(parser): read anchor text up to a nested anchor, linear parse on nested links #1844 - #1847

Merged
nc9 merged 1 commit into
mainfrom
boat/nested-anchors-1844
Nov 14, 2026
Merged

nc9 merged 1 commit into
mainfrom
boat/nested-anchors-1844

Conversation

@nc9

@nc9 nc9 commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

Closes #1844

Cause

The quadratic step is link extraction, in the anchor text read (anchorTextResult, packages/parser/src/extractors/anchor-text.ts). It is not the DOM build or an ancestor walk: those were made linear in repo#3356. linkedom/htmlparser2 keeps <a> inside <a>, and each anchor's text walk descended into every nested anchor, so anchor i read the text of all N-i anchors below it. A CPU profile (bun --cpu-prof, N=10,000) put 10.98 s of the 11.17 s parse in anchorTextResult, called from extractLinks. The output was quadratic too: N=5,000 (128 KB of HTML) stored 62 MB of link text, about 1 GB at N=20,000, so no linear algorithm could give identical output.

Fix

Anchor text now stops at a nested <a>, which matches the HTML spec: a browser never builds an anchor inside an anchor, because the parser's adoption agency algorithm closes the open one when the next starts. The inner link's text belongs to the inner link alone. A skipped nested anchor separates the outer text on either side of it, like a block, so the words are not fused. Every text node is now read once per page, so the parse is linear in input size (AC option 1, with no budget needed and no content dropped: every link is still found with its own text). Both link extractors and the a11y/link-text DOM re-read share this function, so they stay consistent. Pages without nested anchors are unchanged.

Before / after (parsePage, <a href=/pI>lI nested N deep; AMD EPYC 9254, 48 cores, bun 1.4.4)

N before after
2,500 0.70 s 0.05 s
5,000 2.88 s 0.07 s
10,000 11.54 s 0.11 s
20,000 45.45 s 0.19 s

Acceptance criteria

  • Curve before/after above; 20,000 now 0.19 s (under 2 s), machine stated.
  • Cause named: anchor-text read in link extraction (profile above).
  • Normal-page output unchanged: 333 parser tests pass (27 files), 237 audit-engine golden tests pass (24 *golden* files), 214 rules tests pass (anchor/link-text/integrity-budget-784 files), and there are no snapshot changes (git status shows only the source file, the new test and the CHANGELOG).
  • Bounded: the parse is linear in input size (CHANGELOG documents the text semantics).
  • packages/parser/tests/nested-anchors-parse-time-1844.test.ts: parses 20,000 nested anchors with a 5 s bound (25x headroom), checks all 20,000 links each carry their own text (both extractors), and pins the semantics on small shapes. 7 tests pass.

Gates

Root bun run format:check, bun run lint and bun run typecheck all exit 0. scripts/check-released-changelog.sh origin/main passes.

Notes

  • CHANGELOG: the v0.0.182 release commit had cut the header bullet short and dropped the ## [Unreleased] heading. This PR restores both (text from the previous revision) and adds a Fixed line.
  • Agent skill (squirrelscan/skills): no change needed.

…nested links #1844

parsePage was quadratic on directly nested anchors: the parse keeps <a> inside
<a>, and anchorTextResult read every descendant anchor's text into each
ancestor, so 20,000 nested links stored about 1 GB of link text and took 45 s.
A browser never nests anchors (the parser closes the open one when the next
starts), so anchor text now stops at a nested anchor. Each text node is read
once per page: 0.19 s at 20,000.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the boat/nested-anchors-1844 branch from 37330a3 to 428373a Compare November 14, 2026 07:19
@nc9
nc9 merged commit f8bd5e3 into main Nov 14, 2026
16 checks passed
@nc9
nc9 deleted the boat/nested-anchors-1844 branch November 14, 2026 07:41
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.

parsePage is quadratic on deeply nested anchors (45 s at 20,000)

1 participant