Skip to content

Commit 8a0e94e

Browse files
voidstackloopclaude
andcommitted
Verify the shadow-DOM fix's two unproven claims: nesting depth, closed roots
The previous commit shipped a recursive deep-query helper and claimed (in its own doc comments) that it handles arbitrary shadow-nesting depth and degrades gracefully on a closed shadow root — neither claim had a test. Both turned out to need real verification, not just inspection: - fixtures/nested-shadow-dom-form.html nests a second open shadow root *inside* the first one's <form>. Investigating why this test should matter (rather than assuming "recursion is recursion") turned up a subtlety: the existing single-level fixture never actually exercises DEEP_QUERY_JS's own shadow-crossing — once TARGET_FORM_JS's separate, self-contained copy of the walk resolves the form, that form's own field is a same-tree descendant of it, so a plain non-shadow-aware querySelectorAll starting from the form would already find it. Only a second, independently-nested shadow root actually requires DEEP_QUERY_JS (used by check_validation_errors and friends, called with the form as root, not document) to cross a boundary itself. Confirmed by temporarily stripping DEEP_QUERY_JS's shadow-crossing entirely: the single-level fixture's test still passed exactly as this reasoning predicted, while the new nested one failed. - fixtures/closed-shadow-dom-form.html's form lives inside a CLOSED shadow root — genuinely, permanently invisible from outside, by design. Confirmed the code degrades to a clean "No <form> element found" (not a crash) by temporarily removing the `if (node.shadowRoot)` null-guard in both DEEP_QUERY_JS and TARGET_FORM_JS's own copy of the walk: this reliably threw a real TypeError ("Cannot read properties of undefined (reading 'children')") surfaced through chromiumoxide as an Err, proving the guard is load-bearing, before restoring it. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent fe9e910 commit 8a0e94e

3 files changed

Lines changed: 108 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head><meta charset="utf-8"><title>Closed Shadow DOM Form</title></head>
4+
<body>
5+
<!-- A CLOSED shadow root is genuinely, deliberately impossible to
6+
inspect from outside — that's the platform enforcing real
7+
encapsulation, not something any tool can or should work around.
8+
formwatch should degrade to a clean, honest "no form found" here,
9+
not throw partway through a check. -->
10+
<div id="host"></div>
11+
<script>
12+
const host = document.getElementById('host');
13+
const shadow = host.attachShadow({ mode: 'closed' });
14+
shadow.innerHTML = `
15+
<form>
16+
<label for="fullname">Full name</label>
17+
<input id="fullname" name="fullname" type="text" required>
18+
<button type="submit">Go</button>
19+
</form>
20+
`;
21+
</script>
22+
</body>
23+
</html>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head><meta charset="utf-8"><title>Nested Shadow DOM Form</title></head>
4+
<body>
5+
<!-- Two levels of open shadow root: the <form> lives in the outer
6+
component's shadow root, but its own required field lives inside a
7+
SECOND, inner component's shadow root nested inside the form. This
8+
is what actually exercises the deep-query helper's recursion —
9+
fixtures/shadow-dom-form.html only has one level of nesting, so it
10+
couldn't tell a correct unbounded-depth walk from one that just
11+
happens to handle a single hop. -->
12+
<div id="outer-host"></div>
13+
<script>
14+
const outerHost = document.getElementById('outer-host');
15+
const outerShadow = outerHost.attachShadow({ mode: 'open' });
16+
outerShadow.innerHTML = `
17+
<form>
18+
<div id="inner-host"></div>
19+
<button type="submit">Go</button>
20+
</form>
21+
`;
22+
const innerHost = outerShadow.getElementById('inner-host');
23+
const innerShadow = innerHost.attachShadow({ mode: 'open' });
24+
innerShadow.innerHTML = `
25+
<label for="fullname">Full name</label>
26+
<input id="fullname" name="fullname" type="text" required>
27+
`;
28+
outerShadow.querySelector('form').addEventListener('submit', (e) => {
29+
e.preventDefault();
30+
outerShadow.innerHTML += '<p>Thank you, your application was received.</p>';
31+
});
32+
</script>
33+
</body>
34+
</html>

‎tests/checks_test.rs‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,3 +562,54 @@ async fn form_inside_an_open_shadow_root_is_found_and_checked() {
562562
documents.detail
563563
);
564564
}
565+
566+
#[tokio::test]
567+
async fn deep_query_recurses_through_two_levels_of_nested_shadow_roots() {
568+
// fixtures/nested-shadow-dom-form.html nests a second open shadow
569+
// root *inside* the first one's <form> — the outer host has a shadow
570+
// root containing the form, and the form itself contains another
571+
// host whose own shadow root contains the actual required field.
572+
//
573+
// shadow-dom-form.html's single level of nesting doesn't actually
574+
// exercise DEEP_QUERY_JS's own shadow-crossing at all: once
575+
// TARGET_FORM_JS's separate, self-contained copy of the walk resolves
576+
// `f`, the input is a same-tree descendant of `f` (both directly in
577+
// shadow root #1), so a plain non-shadow-aware querySelectorAll
578+
// starting at `f` would already find it. Only a *second*, independent
579+
// shadow root nested inside the form — as here — requires
580+
// DEEP_QUERY_JS itself (used by check_validation_errors and friends,
581+
// called with `f` as the root, not `document`) to actually cross a
582+
// shadow boundary. Confirmed by temporarily stripping DEEP_QUERY_JS's
583+
// shadow-crossing entirely: shadow-dom-form.html's test still passed
584+
// (as predicted), while this one failed.
585+
let (_browser, page, _handle) = open_fixture("nested-shadow-dom-form.html").await;
586+
let result = checks::check_validation_errors(&page)
587+
.await
588+
.expect("check_validation_errors");
589+
assert!(
590+
result.detail.starts_with("1 required field(s)"),
591+
"expected the doubly-nested required field to be found, got: {}",
592+
result.detail
593+
);
594+
}
595+
596+
#[tokio::test]
597+
async fn closed_shadow_root_degrades_to_a_clean_no_form_found_not_a_crash() {
598+
// fixtures/closed-shadow-dom-form.html's form lives inside a CLOSED
599+
// shadow root — genuinely, deliberately impossible to inspect from
600+
// outside (the platform's own encapsulation, not a formwatch gap).
601+
// The deep-query walk's `if (node.shadowRoot)` guard should simply
602+
// skip it (a closed root's `.shadowRoot` getter returns null to
603+
// outside code), degrading to an honest "no form found" rather than
604+
// throwing partway through a check.
605+
let (_browser, page, _handle) = open_fixture("closed-shadow-dom-form.html").await;
606+
let result = checks::check_submission_flow(&page, false)
607+
.await
608+
.expect("check_submission_flow should not error, just report no form");
609+
assert_eq!(result.status, checks::Status::Fail);
610+
assert!(
611+
result.detail.contains("No <form> element found"),
612+
"got: {}",
613+
result.detail
614+
);
615+
}

0 commit comments

Comments
 (0)