summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/selection/selection-range-in-shadow-after-the-shadow-removed.tentative.html
blob: e9f63715a2f15453f3aec8624b2d8af098f0604e (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="variant" content="?mode=closed">
<meta name="variant" content="?mode=open">
<title>Selection range in shadow after removing the shadow</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script>
"use strict";

addEventListener("load", () => {
  const mode = (new URLSearchParams(document.location.search)).get("mode");
  const container = document.querySelector("div");
  test(() => {
    const host = document.createElement("div");
    host.id = "host";
    const root = host.attachShadow({mode});
    root.innerHTML = '<div id="in-shadow">ABC</div>';
    container.appendChild(host);
    getSelection().collapse(root.firstChild.firstChild, 2);
    const range = getSelection().getRangeAt(0);
    host.remove();
    // The range should be kept in the shadow because it's referred by JS as
    // a range.
    assert_equals(range.startContainer, root.firstChild.firstChild, "startContainer should not be changed");
    assert_equals(range.startOffset, 2, "startOffset should not be changed");
    // It may be reasonable to just remove the range in removed shadow.
    // This matches with move-selection-range-into-different-root.tentative.html
    assert_equals(getSelection().rangeCount, 0, "Selection should not have the range in removed shadow");
  }, "Selection range in shadow should not be as a selection range after the host is removed");

  test(() => {
    const wrapper = document.createElement("div");
    wrapper.id = "wrapper";
    const host = document.createElement("div");
    host.id = "host";
    const root = host.attachShadow({mode});
    root.innerHTML = '<div id="in-shadow">ABC</div>';
    wrapper.appendChild(host);
    container.appendChild(wrapper);
    getSelection().collapse(root.firstChild.firstChild, 2);
    const range = getSelection().getRangeAt(0);
    wrapper.remove();
    // The range should be kept in the shadow because it's referred by JS as
    // a range.
    assert_equals(range.startContainer, root.firstChild.firstChild, "startContainer should not be changed");
    assert_equals(range.startOffset, 2, "startOffset should not be changed");
    // It may be reasonable to just remove the range in removed shadow.
    // This matches with move-selection-range-into-different-root.tentative.html
    assert_equals(getSelection().rangeCount, 0, "Selection should not have the range in removed shadow");
  }, "Selection range in shadow should not be as a selection range after the host parent is removed");

  test(() => {
    const host = document.createElement("div");
    host.id = "host";
    const root = host.attachShadow({mode});
    root.innerHTML = '<div id="in-shadow">ABC</div>';
    container.appendChild(host);
    getSelection().collapse(root.firstChild.firstChild, 2);
    const range = getSelection().getRangeAt(0);
    host.replaceWith(host);
    // The range should be kept in the shadow because it's referred by JS as
    // a range.
    assert_equals(range.startContainer, root.firstChild.firstChild, "startContainer should not be changed");
    assert_equals(range.startOffset, 2, "startOffset should not be changed");
    // It may be reasonable to just remove the range in removed shadow.
    // This matches with move-selection-range-into-different-root.tentative.html
    assert_equals(getSelection().rangeCount, 0, "Selection should not have the range in removed shadow");
    host.remove();
  }, "Selection range in shadow should not be as a selection range after the host is replaced with itself (.replaceWith)");

  test(() => {
    const host = document.createElement("div");
    host.id = "host";
    const root = host.attachShadow({mode});
    root.innerHTML = '<div id="in-shadow">ABC</div>';
    container.appendChild(host);
    getSelection().collapse(root.firstChild.firstChild, 2);
    const range = getSelection().getRangeAt(0);
    container.replaceChild(host, host);
    // The range should be kept in the shadow because it's referred by JS as
    // a range.
    assert_equals(range.startContainer, root.firstChild.firstChild, "startContainer should not be changed");
    assert_equals(range.startOffset, 2, "startOffset should not be changed");
    // It may be reasonable to just remove the range in removed shadow.
    // This matches with move-selection-range-into-different-root.tentative.html
    assert_equals(getSelection().rangeCount, 0, "Selection should not have the range in removed shadow");
    host.remove();
  }, "Selection range in shadow should not be as a selection range after the host is replaced with itself (.replaceChild");
}, {once: true});
</script>
</head>
<body><div id="container"></div></body>
</html>