Skip to content

perf(rules): aria-text and aria-hidden-focus walk the page once - #1132

Closed
nc9 wants to merge 3 commits into
mainfrom
perf/3066-a11y-subtree-scan
Closed

nc9 wants to merge 3 commits into
mainfrom
perf/3066-a11y-subtree-scan

Conversation

@nc9

@nc9 nc9 commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

a11y/aria-text ran querySelectorAll("*") once for every role="text" element, and a11y/aria-hidden-focus did the same for every aria-hidden container, also walking up the ancestors of each descendant. A page that nests those elements made the first rule quadratic and the second cubic in the nesting depth. At 1,000 nested aria-hidden containers, one page took 56 s. Both rules now walk the page once. squirrelscan/repo#3066.

Changes

  • aria-text counts each element's focusable descendants as the difference of a running counter between entering and leaving the element.
  • aria-hidden-focus does one pre-order walk seeded from doc.children, so it also covers a page with no <html> element and top-level elements after </html>. Each container's findings come from the run of reportable elements between its entry and exit positions. Template content is skipped, as the old document-wide queries never entered it. A honeypot input that is itself aria-hidden keeps its label.
  • static-hidden.ts: the unconfirmed-hidden message moves into a shared helper.

Results

rule nesting before after
a11y/aria-text 1,000 1,259 ms 8 ms
a11y/aria-text 5,000 35,328 ms 23 ms
a11y/aria-hidden-focus 1,000 55,628 ms 22 ms
a11y/aria-hidden-focus 5,000 not run (cubic) 150 ms

Method and the 20,000-level rows are in benchmarks/2026-10-a11y-nesting.md (harness benchmarks/harness/a11y-nesting.ts). The parser has its own depth cost, tracked in squirrelscan/repo#3356.

Tests

  • packages/rules/tests/a11y-nesting-parity-3066.test.ts runs copies of the previous implementations (tests/fixtures/reference-3066/) as an oracle on 600 seeded random pages plus fixed edge cases: no <html>, content after </html>, two <html> elements, templates (also inside SVG), SVG, noscript, tables and honeypot inputs. Both previous rule versions fail the generator, and the new ones pass. An independent mutation run killed 19 of 20 mutants, and the leaf added in the last commit kills the 20th.
  • The existing aria-hidden-focus suites (css-hidden, focusability, honeypot), item-cap and template-verdict pass.
  • The audit-engine golden baseline (518 pages, 309 rules) is unchanged.

Docs and skill

No docs or skill change: findings are the same, only faster.

Gates

  • tsgo on packages/rules
  • tests run by path
  • scripts/test-suites.ts --check
  • detect-secrets on the changed files

nc9 added 3 commits October 26, 2026 10:57
…once per nested element

a11y/aria-text ran querySelectorAll("*") per role="text" element, and
a11y/aria-hidden-focus did so per aria-hidden container plus an ancestor walk
per descendant: quadratic and cubic in nesting depth (5,000 nested role="text":
35 s; 1,000 nested aria-hidden: 56 s). aria-text now counts each element's
focusable descendants from a running counter between entering and leaving it;
aria-hidden-focus takes each container's findings from the run of reportable
elements between its entry and exit positions in one pre-order walk, with the
same order and multiplicity. After: 23 ms and 150 ms at 5,000 levels.

The previous implementations are kept as a test oracle; a parity test compares
both on 600 seeded random pages, and a budget test pins 5,000 levels under 1 s.
Benchmark record and harness in benchmarks/.

Fixes squirrelscan/repo#3066.

Signed-off-by: Nik Cubrilovic <[email protected]>
- aria-hidden-focus walks every top-level element, not only documentElement
  (a page with no <html> tag, a warning printed ahead of the doctype, content
  after </html>); a self-focusable aria-hidden honeypot keeps its honeypot label
- neither walk enters a <template>, as the old document-wide queries did not;
  a role="text" template counts its own content as before
- the parity generator adds those document shapes, templates, aria-hidden
  controls and SVG links (both previous versions fail it); 30 s test timeout

Refs squirrelscan/repo#3066.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 marked this pull request as ready for review October 26, 2026 00:04
@claude

claude Bot commented Oct 26, 2026

Copy link
Copy Markdown

Review of the aria-text and aria-hidden-focus single-walk rewrite.

I read the rule diffs and traced the logic. I did not run the tests or the benchmarks.

No blocking issues found.

  • The invariant in the aria-hidden-focus comment holds. A container is skipped when an inert or hidden ancestor covers it, so a reportable descendant of an unskipped container is never covered.

  • The run-between-entry-and-exit counting, the per-container multiplicity, the self-focusable continue and the honeypot label for a self-focusable container all match the old behaviour.

  • Template skipping and the doc.children seeding match the old document-wide querySelectorAll semantics.

  • The aria-text running counter is correct: each element's own focusability is added before its entered snapshot, so it counts for ancestors only.

  • Using the 600-page oracle test against copies of the old implementations is a strong way to guard this refactor.

  • (nit) aria-hidden-focus.ts: the Frame union relies on "leave" in frame for discrimination. A kind tag would be clearer, but this is purely stylistic.

  • (nit) aria-hidden-focus.ts: containers is created with exit: pos as a placeholder. If the walk ever ended early the exit would be wrong, but it is always set when the leave frame pops, so this is fine as is.

  • (low) aria-text.ts: a role="text" template recomputes with querySelectorAll on its own content. Deeply nested templates of that shape could still be quadratic, but that is very unlikely. It could be noted in a comment if desired.

Confirm bun run scripts/test-suites.ts --check passes for the new test and fixture files, and that the detect-secrets baseline is still in sync.

@nc9 nc9 mentioned this pull request Oct 26, 2026
@nc9

nc9 commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

Landed in the v0.0.134 batch (#1149), main commits 8923536..6cdc892.

@nc9 nc9 closed this Oct 26, 2026
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