summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/ranges/Range-isPointInRange-shadowdom.tentative.html
blob: a90ddcf584785d0b3c2fd2ecba0a9479ba8567c5 (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<!doctype html>
<title>Range.isPointInRange() with ShadowDOM selection tests</title>
<link rel="author" title="Sean Feng" href=sefeng@mozilla.com>
<div id=log></div>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<span id="start">Start</span>
<div id="host">
  <template shadowrootmode="open">
    <span id="inner1">Inner1</span>
    <span id="inner2">Inner2</span>
  </template>
</div>
<span id="end">End</span>
<script>
"use strict";

test(function() {
  assert_implements(window.getSelection().getComposedRanges, "GetComposedRanges is not supported");
  const start = document.getElementById("start");
  const shadowRoot = document.getElementById("host").shadowRoot;

  const end = shadowRoot.getElementById("inner2");
  const inner1 = shadowRoot.getElementById("inner1");

  window.getSelection().setBaseAndExtent(start.firstChild, 3, end.firstChild, 3);

  const composedRange = window.getSelection().getComposedRanges(shadowRoot)[0];
  // Sanity check to make sure we have selected something across the shadow boundary.
  assert_true(composedRange.startContainer == start.firstChild);
  assert_true(composedRange.startOffset == 3);
  assert_true(composedRange.endContainer == end.firstChild);
  assert_true(composedRange.endOffset == 3);

  assert_true(window.getSelection().isCollapsed, "Selection should be collapsed");

  const range = window.getSelection().getRangeAt(0);
  assert_false(range.isPointInRange(inner1, 0), "inner1 is in the shadow tree, should not be in the range");
  assert_true(range.comparePoint(inner1, 0) == -1, "inner1 is in the shadow tree, should return -1 for comparison");
}, "isPointInRange() test for collapsed selection");

test(function() {
  assert_implements(window.getSelection().getComposedRanges, "GetComposedRanges is not supported");
  const start = document.getElementById("start");
  const shadowRoot = document.getElementById("host").shadowRoot;

  const end = document.getElementById("end");
  const inner1 = shadowRoot.getElementById("inner1");

  window.getSelection().setBaseAndExtent(start.firstChild, 3, end.firstChild, 3);

  const composedRange = window.getSelection().getRangeAt(0);
  // Sanity check to make sure we have selected something
  assert_true(composedRange.startContainer == start.firstChild);
  assert_true(composedRange.startOffset == 3);
  assert_true(composedRange.endContainer == end.firstChild);
  assert_true(composedRange.endOffset == 3);

  assert_false(window.getSelection().isCollapsed, "Range should not be collapsed");

  const range = window.getSelection().getRangeAt(0);

  assert_false(range.isPointInRange(inner1, 0), "inner1 is in the shadow tree, should not be in the range");

  // The selection is not collapsed so inner1 is not in the same tree as the selection.
  assert_throws_dom("WrongDocumentError", function() {
    range.comparePoint(inner1, 0);
  });

  const host = document.getElementById("host");
  assert_true(range.isPointInRange(host, 0), "host is not in the shadow tree, should be in the range");
  assert_true(range.comparePoint(host, 0) == 0, "host is not in the shadow tree, should return 0 for comparison");
}, "isPointInRange() test for non-collapsed selection");

</script>