Files
sousa-gecko/testing/web-platform/tests/domxpath/xpath-shadow-dom.html
T
Mason Freed bf3fe9f099 Bug 2013463 [wpt PR 57435] - Fix absolute XPath queries failing in Shadow DOM, a=testonly
Automatic update from web-platform-tests
Fix absolute XPath queries failing in Shadow DOM

Absolute XPath queries (starting with / or //) previously jumped to the
owner document root even when evaluated on a node within a shadow tree.
Since shadow DOM content is not reachable from the document root via
standard light-tree traversal, these queries would fail to find
elements.

This change updates the root node resolution in LocationPath::Evaluate
to treat the ShadowRoot as the root node when the evaluation context is
within a shadow tree.

A new runtime enabled feature, XPathShadowDOMSupport, is added to
gate this behavior.

Fixed: 440874372
Change-Id: I686c66b0ffe2b1ebc3493c3d0122024b79bad381
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7534177
Auto-Submit: Mason Freed <masonf@chromium.org>
Commit-Queue: Joey Arhar <jarhar@chromium.org>
Reviewed-by: Joey Arhar <jarhar@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1577039}

--

wpt-commits: 24d22fd1a42b6ef60982f70f61cfe6b172ef5c09
wpt-pr: 57435
2026-02-05 18:13:17 +00:00

50 lines
1.9 KiB
HTML

<!DOCTYPE html>
<meta charset="utf-8">
<link rel="author" href="mailto:masonf@chromium.org">
<link rel="help" href="https://crbug.com/440874372">
<link rel="help" href="https://dom.spec.whatwg.org/#xpath">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="host">
<template shadowrootmode="open">
<span id="target"></span>
</template>
</div>
<script>
test(function() {
const host = document.getElementById('host');
const span = host.shadowRoot.getElementById('target');
assert_true(!!span, "Span should exist in shadow DOM");
// evaluate xpath after attaching to shadow
const evaluator = new XPathEvaluator();
const result = evaluator.evaluate('//span', span, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE);
assert_equals(result.snapshotLength, 1, "Should find the span element");
assert_equals(result.snapshotItem(0), span, "Should find the correct span element");
}, "XPath //span should find element in Declarative Shadow DOM when evaluated on the element itself");
test(function() {
class MyCustomElement extends HTMLElement {
connectedCallback(){
const shadow = this.attachShadow({mode: 'open'});
const span = document.createElement('span');
shadow.appendChild(span);
// evaluate xpath after attaching to shadow
const evaluator = new XPathEvaluator();
const result = evaluator.evaluate('//span', span, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE);
assert_equals(result.snapshotLength, 1, "Should find the span element");
assert_equals(result.snapshotItem(0), span, "Should find the correct span element");
}
}
customElements.define('my-custom-element', MyCustomElement);
const el = document.createElement('my-custom-element');
document.body.appendChild(el);
}, "XPath //span should find element in Imperative Shadow DOM when evaluated on the element itself");
</script>