Skip to content

perf(rules): a11y table rules and link-in-text-block walk the page once - #1136

Merged
nc9 merged 3 commits into
mainfrom
perf/3355-a11y-nested-tables
Oct 26, 2026
Merged

nc9 merged 3 commits into
mainfrom
perf/3355-a11y-nested-tables

Conversation

@nc9

@nc9 nc9 commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Four a11y rules scanned a subtree once per matched element, so nested markup multiplied the work (squirrelscan/repo#3355).

  • The three table rules (a11y/th-has-data-cells, a11y/td-headers-attr, a11y/table-headers) read each table's rows and cells with table.querySelectorAll, which also returns those of every nested table.
    • Every outer table worked through everything below it.
    • For th-has-data-cells it was also a correctness bug: a nested table's cells were judged as the outer table's.
  • a11y/link-in-text-block took each text block's links, so a link in nested blocks was counted and reported once per enclosing block.

Changes

  • shared/own-table-cells.ts does one walk of the page. Each row goes to its nearest table and each cell to its nearest row and table, as in the HTML table model.
    • Elements are the ones the document-wide selectors return. What counts as a table, row or cell is unchanged, and template content still counts as none.
  • th-has-data-cells builds each table's grid from that table's own rows and cells.
    • Row and column "has a td" are now read once per table, not rescanned for each header.
    • The free-column search starts at the previous cell rather than at column 0.
    • Output is the same for tables without nesting.
  • td-headers-attr checks each cell against the header cells of its own table, once.
  • table-headers reports exactly as before. A table counts the headers of the tables nested in it, so a layout table wrapping a data table still passes, as axe reads it. It now gets that from one walk, propagating from nested tables up to their parents.
  • link-in-text-block makes one walk, counting each link once, in document order.
  • Spans (crash fix): colspan="Infinity" (or 1e999) made the th-has-data-cells grid fill loop run until the process ran out of memory, from a page of about 60 bytes. A span that is not a finite number of at least 1 now counts as 1, a span past the HTML limits (1,000 columns, 65,534 rows) as the limit, and a table's grid stops at 1,000,000 slots (judged on the cells placed before it). Finite spans up to the limits read as before.
  • Item text for the at most ten shown items is read only for those ten. On nested markup an element's text is its whole subtree's.

What changes in what users see:

  • In th-has-data-cells, an outer header no longer gets data cells from a nested table. A row nested inside a cell (<th>B<div><tr>, which a browser closes the cell for) is now a row of its own.
  • In td-headers-attr, a bad headers reference inside a nested table is reported once, and a cell may reference only its own table's headers.
  • In link-in-text-block, a link in nested blocks is counted once.
  • The rule docs for th-has-data-cells, td-headers-attr and link-in-text-block say so.

Results

Before and after were measured minutes apart at the same load.

rule nesting before after
a11y/th-has-data-cells 200 1,430 ms 1 ms
a11y/th-has-data-cells 1,000 not run (minutes) 5 ms
a11y/td-headers-attr 5,000 3,624 ms 9 ms
a11y/table-headers 5,000 1,003 ms 6 ms
a11y/link-in-text-block 5,000 827 ms 3 ms

Method and the 20,000-level rows are in benchmarks/2026-10-a11y-nested-tables.md. The harness is benchmarks/harness/a11y-nested-tables.ts, and --before runs the previous rules. Parsing now grows faster than the rules do (squirrelscan/repo#3356). Cutting a row span at the end of its row group, as browsers do, is left to squirrelscan/repo#3383.

Tests

The tests are in packages/rules/tests/a11y-nested-tables-3355.test.ts. The previous implementations are kept as oracles in tests/fixtures/reference-3355/; the link oracle has a one-line counted set added.

Parity with the previous implementations, each on 600 seeded random pages:

  • The three table rules give identical results on pages without nested tables. These pages include spans, thead/tbody, caption, presentation roles, cross-table headers, cells with no row, a cell in a wrapper, and a td[headers] outside any table.
  • table-headers gives identical results on pages of nested tables.
  • link-in-text-block gives identical results on pages of nested blocks, counting each link once.

Nested verdicts: one test per rule, and one for a row inside a cell. Each shows both the old and the new result.

Cost:

  • Time budgets: 20,000 nested tables for each table rule and 5,000 nested blocks for the link rule, each under 1 s.
  • Wide rows: one row of 20,000 headers, and 500 cells of 1,000 columns each, each under 1 s.

Spans:

  • Infinity, 1e999, -Infinity, NaN, junk, 0, negative and 0.5 read as 1; 1.5 reads as before.
  • A span past the limit reads as the limit, pinned by a verdict.
  • A 65-million-slot cell stops at the slot cap in under 1 s.
  • The parity generator now draws fractional and junk spans (1.5, 2.7, 0, -1, abc, 2px, 2, 1e1) and an uppercase ROWSPAN.
  • A deterministic check: none of the four calls querySelectorAll on an element, while a spy counts the old rules' calls per table and per block.

Mutations caught:

  • removing the row reset at a nested table;
  • dropping row-less cells;
  • dropping the inherited in-block flag.

Two independent review rounds (one fuzzing 4,000 seeds against the oracles) found nothing blocking; their findings are fixed here.

Other suites, all passing: the existing aria suites, item-cap, template-verdict, rule-verdict-scope, and the audit-engine golden baseline (518 pages, findings unchanged).

Docs and skill

The rule docs and the CHANGELOG are updated. No skill change.

Gates

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

@nc9
nc9 marked this pull request as ready for review October 26, 2026 02:44
@claude

claude Bot commented Oct 26, 2026

Copy link
Copy Markdown

Reviewed the source changes in packages/rules. I didn't run the tests or CI.

  • (low) th-has-data-cells.ts: when a table passes MAX_GRID_SLOTS, buildGrid returns a partial grid and the rest of the table is silently left unjudged. That is a reasonable trade-off. A short comment, or a details note on the check, would tell a reader that a large table was only partly checked.
  • (nit) th-has-data-cells.ts: the cursor shortcut assumes every slot left of the previous cell's column is filled. This holds for the integer and ceil'd spans used here. The comment states the assumption, so a regression test with fractional spans in a row with rowspan carry-over would lock it in, if one isn't there already.
  • (nit) The nested-table, link-in-text-block and span handling is a clear improvement. The own-table-cells walk is iterative, so deep nesting can't overflow the stack. The Infinity colspan fix is a good catch.

No blocking issues found.

nc9 added 3 commits October 26, 2026 15:14
… nested table is its own table

a11y/th-has-data-cells, td-headers-attr and table-headers read each table's
rows and cells with table.querySelectorAll, which also returns those of every
nested table: a nested table's cells were judged as the outer table's, and
nested tables made the rules quadratic or worse (200 levels: 1.7 s). One walk
now assigns each row and cell to its nearest table. link-in-text-block counted
a link once per enclosing text block; it now counts each link once.

squirrelscan/repo#3355

Signed-off-by: Nik Cubrilovic <[email protected]>
… checks, item text for the shown ten

table-headers keeps counting the headers of nested tables (a table wrapping a
data table is a layout table, as axe reads it), now from one walk.
th-has-data-cells reads each row's and column's td once per table and starts
each free-column search at the previous cell. Item text is read only for the
ten items shown. Tests: table-headers parity on nested tables, a row inside a
cell, 20,000-level budgets, and a check that no rule queries a subtree.

squirrelscan/repo#3355

Signed-off-by: Nik Cubrilovic <[email protected]>
…ans and grid slots

colspan="Infinity" (or 1e999) made the grid fill loop run until the process
ran out of memory, from a page of about 60 bytes. A span that is not a finite
number of at least 1 now counts as 1, a span past the HTML limits (1,000
columns, 65,534 rows) as the limit, and a table's grid stops at 1,000,000
slots. Tests for those spans, junk and fractional spans in the parity
generator, and wide-row time budgets.

squirrelscan/repo#3355

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the perf/3355-a11y-nested-tables branch from d77a4bc to 6f12cd2 Compare October 26, 2026 04:14
@claude

claude Bot commented Oct 26, 2026

Copy link
Copy Markdown

Review of the nested-table and text-block rewrite. I read the diff and did not run the tests. CI was still pending when I looked.

The approach is sound. The shared ownTableCells walk is iterative, so it can't overflow the stack on 20k-deep markup. The span clamping and the MAX_GRID_SLOTS cap close a real memory blowup. The seeded parity tests against the reference implementations are a good way to pin behaviour.

  • (medium) CHANGELOG.md, ## [Unreleased]: the PR adds three near-duplicate bullets. They look like successive drafts, and they disagree: one says 1.7 s to 2 ms and 734 ms to 22 ms, the other two say 1.4 s to 1 ms and 827 ms to 3 ms. The third also mentions the Infinity span fix. Keep one bullet, probably the last, and make sure its numbers match benchmarks/2026-10-a11y-nested-tables.md. The bullet should also end with an issue reference as (#123), per AGENTS.md. It currently has none.
  • (low) packages/rules/src/a11y/th-has-data-cells.ts buildGrid: the span clamp and the MAX_GRID_SLOTS truncation change behaviour beyond the issue. A table over 1M slots is now judged only on the cells placed before the cap, so headers past it can be reported as orphaned. This is a reasonable trade-off, but I couldn't find a test for the truncation path or for MAX_GRID_SLOTS in the part of the test file I read. Please confirm one exists.
  • (low) packages/rules/src/a11y/table-headers.ts: the withHeaders propagation relies on own.values() being in document order, so inner tables are visited before the outer ones. That holds because the Map is filled in walk order, but it's a subtle invariant. A short comment on ownTableCells guaranteeing the order would protect it.
  • (low) packages/rules/tests/fixtures/reference-3355/* and benchmarks/harness/a11y-nested-tables.ts: this adds about 1.3k lines of frozen copies of the old rules, and the harness imports them from tests/fixtures. That works, but it is a maintenance burden: if the rules change later, the reference copies drift. Consider noting in the test header that they are frozen on purpose. Also check that scripts/test-suites.ts --check doesn't pick the fixtures up as test files.
  • (nit) link-in-text-block.ts: the new walk skips <template> by localName, which matches what querySelectorAll does in linkedom. This is fine, but the comment could say that is the reason.
  • (nit) The docs wording is fine. th-has-data-cells.mdx states the new span limits, so it matches the code.

I found nothing critical or high-severity. Untrusted-input handling is good: the walk is iterative and the grid is bounded.

@nc9
nc9 merged commit f74cd0c into main Oct 26, 2026
15 checks passed
@nc9
nc9 deleted the perf/3355-a11y-nested-tables branch October 26, 2026 04:29
nc9 added a commit that referenced this pull request Oct 26, 2026
The squash of #1136 kept all three drafts of its entry; the last one, which names the span caps, stays.

Signed-off-by: Nik Cubrilovic <[email protected]>
nc9 added a commit that referenced this pull request Oct 26, 2026
The squash of #1136 kept all three drafts of its entry; the last one, which names the span caps, stays.

Signed-off-by: Nik Cubrilovic <[email protected]>
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