File: xpath-shadow-dom.html

package info (click to toggle)
firefox 149.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,767,760 kB
  • sloc: cpp: 7,416,064; javascript: 6,752,859; ansic: 3,774,850; python: 1,250,473; xml: 641,578; asm: 439,191; java: 186,617; sh: 56,634; makefile: 18,856; objc: 13,092; perl: 12,763; pascal: 5,960; yacc: 4,583; cs: 3,846; lex: 1,720; ruby: 1,002; php: 436; lisp: 258; awk: 105; sql: 66; sed: 53; csh: 10; exp: 6
file content (49 lines) | stat: -rw-r--r-- 1,941 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!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>