Skip to content

fix(parser): inline declarative shadow roots so rules see their controls #922 - #1346

Closed
nc9 wants to merge 3 commits into
mainfrom
fix/shadow-root-922
Closed

nc9 wants to merge 3 commits into
mainfrom
fix/shadow-root-922

Conversation

@nc9

@nc9 nc9 commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Closes #922

linkedom keeps the content of <template shadowrootmode> inside the template's inert content fragment, so querySelectorAll never reached controls a browser renders inside a declarative shadow root. parseHTML (the single parser entry) now places that content where the template was, only when the HTML mentions shadowroot (one regex test otherwise). Browser rules kept: first shadow template of a host only, only elements that can host a shadow root (a hyphenated custom element or the standard list), plain <template> untouched, nested roots handled (bounded: 16 rounds, 5000 templates).

Tests: packages/rules/tests/declarative-shadow-root-922.test.ts: unnamed button in open and closed roots fails like the light DOM, plain template inert, named button passes, links/images/headings and nested roots reachable, first-template-only, table host inert, 20,000-template page bounded.

Docs: new "Content inside shadow roots" section in docs/rules/index.mdx; CHANGELOG under Unreleased. Skill: none.

@claude

claude Bot commented Oct 30, 2026

Copy link
Copy Markdown

Review of #1346 (declarative shadow root flattening in parseHTML).

The approach is sound: it is gated by a cheap regex, bounded in rounds and templates, only touches template[shadowrootmode], and uses sensible host rules. I read the diff only and did not run the tests.

  • (medium) packages/parser/src/dom.ts:65-70: flattening only runs on the first branch, where documentElement exists. When linkedom returns no root element (a fragment-only or markup-free page), the fallback re-parse wraps the HTML in <html><body> and returns without calling flattenDeclarativeShadowRoots. A shadow template on such a page stays inert, so the bug from Rules do not see controls inside a declarative shadow root (<template shadowrootmode>) #922 persists there. Run the flatten on the wrapped result too, and add a test with no <html> wrapper (the current tests all use a full document).
  • (low) dom.ts: the 5000 budget is spent on skipped templates too (a second template on the same host, or a non-host element). It is also re-spent every round, because those templates are re-queried. A page with many ineligible templates can exhaust the budget before legitimate ones are inlined. That is acceptable as a hostile-input bound, but it is worth a comment.
  • (low) dom.ts: the light DOM children stay next to the inlined shadow content. A browser renders only slotted light children. Text-based rules (word count, duplicate content, heading order) may therefore see both. The doc comment acknowledges this, but the docs page claims the rules see the page "the same way" as a browser. Consider softening that wording.
  • (nit) The bounded-input test asserts elapsed time under 5s. AGENTS.md prefers asserting an effect: for example, that exactly 5000 templates were inlined and the rest remain, which would show the cap works.
  • (nit) The new code in dom.ts and the test file have very long lines. Per AGENTS.md, check the changed files with the pinned oxfmt --check.
  • (nit) The test sits in packages/rules/tests but mostly exercises the parser. That is fine, but a parser-level test alongside attribute-case.test.ts would sit closer to the code under test.

The CHANGELOG entry and the docs section look good.

@nc9
nc9 force-pushed the fix/shadow-root-922 branch from 9ecc231 to 325a9c5 Compare October 30, 2026 08:36
@claude

claude Bot commented Oct 30, 2026

Copy link
Copy Markdown

Review of the declarative shadow DOM flattening in packages/parser/src/dom.ts. I read the diff and did not run the tests. CI was still pending when I looked.

  • (medium) dom.ts scopeIdsToRoot: ids in a root get a __srN suffix, but href="#id" is not in ID_REFERENCE_ATTRIBUTES. A link or SVG <use href="#x"> inside a root that targets an id in the same root keeps #x, while the target becomes x__sr1. links/broken-anchors and links/page-anchors could then report a false broken anchor. I did not confirm how those rules read ids, so please add a test with an in-root <a href="#x"> and id="x". Either rewrite href fragments that point inside the root or document the limit.
  • (low) dropReplacedSlotFallback: the light-children scan includes the other (second, inert) <template> siblings. They are nodeType 1 with no slot attribute, so they mark the default slot as supplied and its fallback gets dropped wrongly. Skip template elements when building supplied.
  • (low) parseHTML: the retry path (documentElement missing, re-parse wrapped in <body>) returns without calling flattenDeclarativeShadowRoots. Fragments or odd documents containing shadow templates therefore stay inert on that path, which is inconsistent with the first path.
  • (low) The flatten loop re-runs document.querySelectorAll("template[shadowrootmode]") each round. Skipped templates, such as those under li or table, are not counted against the 5,000 budget and are re-iterated every round. That is bounded by 16 rounds times page size, so the cost is acceptable. A WeakSet of already-skipped templates would avoid the rescans.
  • (nit) declarative-shadow-root-922.test.ts: buttonName duplicates runRule; use runRule(buttonNameRule, ...).
  • (nit) The timing assertion < 5000 ms in the hostile-page test is very loose and could be flaky. The AGENTS.md guidance is to assert an effect, not elapsed time. The later budget test covers the bound already.
  • (nit) Several lines in dom.ts are very long and not oxfmt-formatted. Packages are not format-checked in CI, so this is only a style point.
  • (nit) The docs say only the first shadow template counts. A test that the second template stays an inert <template> (not removed) would make that explicit.

Missing from the PR: the CHANGELOG and docs are in place, but there is no check that the template-invariance fixtures or the golden pins are unaffected. The flattening runs on every page containing "shadowroot", so run the golden suite to confirm.

…light style/script children no longer replace slot fallback #922

Signed-off-by: Nik Cubrilovic <[email protected]>
@claude

claude Bot commented Oct 30, 2026

Copy link
Copy Markdown

Reviewed the diff only. I did not run the tests.

  • (medium) packages/parser/src/dom.ts (parseHTML fallback): flattening only runs in the branch where documentElement exists. When the input has no root element, the code reparses the wrapped <html><body>… and returns that result without calling flattenDeclarativeShadowRoots. A fragment-style document containing <template shadowrootmode> therefore stays inert, which contradicts the docs. Flatten the wrapped parse too, and add a test with a fragment input.
  • (low) stripShadowIdScopes removes every __sr<digits> suffix from check strings once the document is marked scoped. A real light-DOM id such as foo__sr1 on the same page gets mangled in findings. This is unlikely, but a rarer marker would avoid the collision.
  • (low) stripShadowIdScopes runs only in the page-rule finalize. Other consumers of the parsed DOM (extractors that record ids or anchors, site-rule output) keep the suffixed ids. Confirm none of them surface ids in the report, or strip there too.
  • (low) The reference-attribute rewrite does a plain split(/\s+/) over site-supplied values. Cap the value length or token count, as AGENTS.md asks for site text.
  • (low) Each of up to 16 rounds re-runs querySelectorAll("template[shadowrootmode]") over the whole document. It is bounded, so it is acceptable, but collecting nested templates from the inlined content would avoid the rescan.
  • (nit) The long one-line signatures and constants in dom.ts (SHADOW_HOSTS, ID_REFERENCE_ATTRIBUTES, TemplateLike) will not pass oxfmt. Run the formatter on this file only, as AGENTS.md describes.
  • (nit) In declarative-shadow-root-922.test.ts, buttonName duplicates runRule. The "bounded" test asserts wall-clock time (< 5s), which AGENTS.md says to avoid; the later test already asserts the effect.
  • (nit) Confirm template-verdict-inputs.test.ts and the golden suite still pass. I could not verify them here.

The design is sound overall. It follows browser semantics (first template only, restricted host list, slot fallback), it is bounded, and the tests cover the main cases.

@nc9

nc9 commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Landed in v0.0.144 as 206e8e42 7d7f353d 6c1a0fd6 via #1356. Release: https://github.com/squirrelscan/squirrelscan/releases/tag/v0.0.144. Closing this original.

nik

@nc9 nc9 closed this Oct 30, 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.

Rules do not see controls inside a declarative shadow root (<template shadowrootmode>)

1 participant