summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/shadow-dom/selection-getComposedRanges.tentative.html
blob: 983328693c6c32b5b0ca8454b626017550868bc4 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<!DOCTYPE html>
<html>
<body>
<meta name="author" title="Ryosuke Niwa" href="mailto:rniwa@webkit.org">
<meta name="assert" content="Selection's getComposedRanges should return a sequence of static ranges">
<link rel="help" href="https://w3c.github.io/selection-api/#dom-selection-getcomposedrange">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="container"></div>
<script>

test(() => {
    getSelection().removeAllRanges();
    assert_array_equals(getSelection().getComposedRanges(), []);
}, 'getComposedRanges returns an empty sequence when there is no selection');

test(() => {
    container.innerHTML = 'hello, world';
    getSelection().setBaseAndExtent(container.firstChild, 0, container.firstChild, 5);
    const ranges = getSelection().getComposedRanges();
    assert_equals(ranges.length, 1);
    assert_equals(ranges[0].startContainer, container.firstChild);
    assert_equals(ranges[0].startOffset, 0);
    assert_equals(ranges[0].endContainer, container.firstChild);
    assert_equals(ranges[0].endOffset, 5);
}, 'getComposedRanges returns a sequence with a static range when there is a forward-direction selection in the document tree');

test(() => {
    container.innerHTML = 'hello, world';
    getSelection().setBaseAndExtent(container.firstChild, 4, container.firstChild, 3);
    const ranges = getSelection().getComposedRanges();
    assert_equals(ranges.length, 1);
    assert_equals(ranges[0].startContainer, container.firstChild);
    assert_equals(ranges[0].startOffset, 3);
    assert_equals(ranges[0].endContainer, container.firstChild);
    assert_equals(ranges[0].endOffset, 4);
}, 'getComposedRanges returns a sequence with a static range when there is a backward-direction selection in the document tree');

test(() => {
    container.innerHTML = 'a<div id="host"></div>b';
    const shadowRoot = host.attachShadow({mode: 'closed'});
    shadowRoot.innerHTML = 'hello, world';
    getSelection().setBaseAndExtent(shadowRoot.firstChild, 0, shadowRoot.firstChild, 5);
    const ranges = getSelection().getComposedRanges(shadowRoot);
    assert_equals(ranges.length, 1);
    assert_equals(ranges[0].startContainer, shadowRoot.firstChild);
    assert_equals(ranges[0].startOffset, 0);
    assert_equals(ranges[0].endContainer, shadowRoot.firstChild);
    assert_equals(ranges[0].endOffset, 5);
}, 'getComposedRanges returns a sequence with a static range pointing to a shadaw tree when there is a selection in the shadow tree and the shadow tree is specified as an argument');

test(() => {
    container.innerHTML = 'a<div id="host"></div>b';
    const shadowRoot = host.attachShadow({mode: 'closed'});
    shadowRoot.innerHTML = 'hello, world';
    getSelection().setBaseAndExtent(shadowRoot.firstChild, 0, shadowRoot.firstChild, 5);
    const ranges = getSelection().getComposedRanges();
    assert_equals(ranges.length, 1);
    assert_equals(ranges[0].startContainer, container);
    assert_equals(ranges[0].startOffset, 1);
    assert_equals(ranges[0].endContainer, container);
    assert_equals(ranges[0].endOffset, 2);
}, 'getComposedRanges returns a sequence with a static range pointing to the shadow host when there is a selection in a shadow tree and the shadow tree is not specified as an argument');

test(() => {
    container.innerHTML = 'a<div id="host"></div>b';
    const shadowRoot = host.attachShadow({mode: 'closed'});
    shadowRoot.innerHTML = 'hello, world';
    getSelection().setBaseAndExtent(shadowRoot.firstChild, 7, container, 2);
    const ranges = getSelection().getComposedRanges();
    assert_equals(ranges.length, 1);
    assert_equals(ranges[0].startContainer, container);
    assert_equals(ranges[0].startOffset, 1);
    assert_equals(ranges[0].endContainer, container);
    assert_equals(ranges[0].endOffset, 2);
}, 'getComposedRanges a sequence with a static range pointing to the shadow host when there is a forward selection that crosses shadow boundaries and the shadow tree is not specified as an argument');

test(() => {
    container.innerHTML = 'a<div id="host"></div>b';
    const shadowRoot = host.attachShadow({mode: 'closed'});
    shadowRoot.innerHTML = 'hello, world';
    getSelection().setBaseAndExtent(shadowRoot.firstChild, 7, container, 2);
    const ranges = getSelection().getComposedRanges(shadowRoot);
    assert_equals(ranges.length, 1);
    assert_equals(ranges[0].startContainer, shadowRoot.firstChild);
    assert_equals(ranges[0].startOffset, 7);
    assert_equals(ranges[0].endContainer, container);
    assert_equals(ranges[0].endOffset, 2);
}, 'getComposedRanges a sequence with a static range that crosses shadow boundaries when there is a forward selection that crosses shadow boundaries and the shadow tree is specified as an argument');

test(() => {
    container.innerHTML = 'a<div id="outerHost"></div>b';
    const outerShadowRoot = outerHost.attachShadow({mode: 'closed'});
    outerShadowRoot.innerHTML = '<div id="innerHost">hello</div><div>world</div>';
    const innerHost = outerShadowRoot.getElementById('innerHost');
    const innerShadowRoot = innerHost.attachShadow({mode: 'closed'});
    innerShadowRoot.innerHTML = 'some text';
    getSelection().setBaseAndExtent(innerShadowRoot.firstChild, 5, innerShadowRoot.firstChild, 9);
    const ranges = getSelection().getComposedRanges();
    assert_equals(ranges.length, 1);
    assert_equals(ranges[0].startContainer, container);
    assert_equals(ranges[0].startOffset, 1);
    assert_equals(ranges[0].endContainer, container);
    assert_equals(ranges[0].endOffset, 2);
}, 'getComposedRanges returns a sequence with a static range pointing to the outer shadow host when there is a selection in an inner shadow tree and no shadow tree is specified as an argument');

test(() => {
    container.innerHTML = 'a<div id="outerHost"></div>b';
    const outerShadowRoot = outerHost.attachShadow({mode: 'closed'});
    outerShadowRoot.innerHTML = '<div id="innerHost">hello</div><div>world</div>';
    const innerHost = outerShadowRoot.getElementById('innerHost');
    const innerShadowRoot = innerHost.attachShadow({mode: 'closed'});
    innerShadowRoot.innerHTML = 'some text';
    getSelection().setBaseAndExtent(innerShadowRoot.firstChild, 5, innerShadowRoot.firstChild, 9);
    const ranges = getSelection().getComposedRanges(innerShadowRoot);
    assert_equals(ranges.length, 1);
    assert_equals(ranges[0].startContainer, innerShadowRoot.firstChild);
    assert_equals(ranges[0].startOffset, 5);
    assert_equals(ranges[0].endContainer, innerShadowRoot.firstChild);
    assert_equals(ranges[0].endOffset, 9);
}, 'getComposedRanges returns a sequence with a static range pointing to the inner shadow tree when there is a selection in an inner shadow tree and the inner shadow tree is specified as an argument');

test(() => {
    container.innerHTML = 'a<div id="outerHost"></div>b';
    const outerShadowRoot = outerHost.attachShadow({mode: 'closed'});
    outerShadowRoot.innerHTML = '<div id="innerHost">hello</div><div>world</div>';
    const innerHost = outerShadowRoot.getElementById('innerHost');
    const innerShadowRoot = innerHost.attachShadow({mode: 'closed'});
    innerShadowRoot.innerHTML = 'some text';
    getSelection().setBaseAndExtent(innerShadowRoot.firstChild, 5, innerShadowRoot.firstChild, 9);
    const ranges = getSelection().getComposedRanges(outerShadowRoot);
    assert_equals(ranges.length, 1);
    assert_equals(ranges[0].startContainer, outerShadowRoot);
    assert_equals(ranges[0].startOffset, 0);
    assert_equals(ranges[0].endContainer, outerShadowRoot);
    assert_equals(ranges[0].endOffset, 1);
}, 'getComposedRanges returns a sequence with a static range pointing to the outer shadow tree when there is a selection in an inner shadow tree and the outer shadow tree is specified as an argument');

</script>
</body>
</html>