summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/shadow-dom/selection-collapse-and-extend.tentative.html
blob: 36cc036b3849f867fdcf7e9b9e1c2e3e25ddc53c (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
<!DOCTYPE html>
<html>
<body>
<meta name="author" title="Ryosuke Niwa" href="mailto:rniwa@webkit.org">
<meta name="assert" content="Selection's collapse and extend should abort only when the node's root is not connected">
<link rel="help" href="https://w3c.github.io/selection-api/#dom-selection-collapse">
<link rel="help" href="https://w3c.github.io/selection-api/#dom-selection-extend">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="container"></div>
<script>

test(() => {
    const host = document.createElement('div');
    container.appendChild(host);
    const shadowRoot = host.attachShadow({mode: 'closed'});
    shadowRoot.innerHTML = '<div contenteditable><p>hello, world</p></div>';
    const textNode = shadowRoot.querySelector('p').firstChild;
    getSelection().removeAllRanges();
    getSelection().collapse(textNode, 5);
    const ranges = getSelection().getComposedRanges(shadowRoot);
    assert_equals(ranges.length, 1);
    assert_equals(ranges[0].startContainer, textNode);
    assert_equals(ranges[0].startOffset, 5);
    assert_equals(ranges[0].endContainer, textNode);
    assert_equals(ranges[0].endOffset, 5);
    assert_true(ranges[0].collapsed);
}, 'collapse can set selection to a node inside a shadow tree');

test(() => {
    const host = document.createElement('div');
    const shadowRoot = host.attachShadow({mode: 'closed'});
    shadowRoot.innerHTML = '<div contenteditable><p>hello, world</p></div>';
    const textNode = shadowRoot.querySelector('p').firstChild;
    getSelection().removeAllRanges();
    getSelection().collapse(textNode, 5);
    const ranges = getSelection().getComposedRanges(shadowRoot);
    assert_equals(ranges.length, 0);
}, 'collapse abort steps when called with a disconnected node inside a shadow tree');

test(() => {
    const host = document.createElement('div');
    container.appendChild(host);
    const shadowRoot = host.attachShadow({mode: 'closed'});
    shadowRoot.innerHTML = '<div contenteditable><p>hello, world</p></div>';
    const textNode = shadowRoot.querySelector('p').firstChild;
    getSelection().removeAllRanges();
    getSelection().collapse(textNode, 5);
    let ranges = getSelection().getComposedRanges(shadowRoot);
    assert_equals(ranges.length, 1);
    getSelection().extend(textNode, 11);
    ranges = getSelection().getComposedRanges(shadowRoot);
    assert_equals(ranges.length, 1);
    assert_equals(ranges[0].startContainer, textNode);
    assert_equals(ranges[0].startOffset, 5);
    assert_equals(ranges[0].endContainer, textNode);
    assert_equals(ranges[0].endOffset, 11);
    assert_false(ranges[0].collapsed);
}, 'extend can set selection to a node inside a shadow tree');

test(() => {
    const host = document.createElement('div');
    const shadowRoot = host.attachShadow({mode: 'closed'});
    shadowRoot.innerHTML = '<div contenteditable><p>hello, world</p></div>';
    const textNode = shadowRoot.querySelector('p').firstChild;
    getSelection().removeAllRanges();
    getSelection().collapse(container, 0);
    getSelection().extend(textNode, 5);
    const ranges = getSelection().getComposedRanges(shadowRoot);
    assert_equals(ranges.length, 1);
    assert_equals(ranges[0].startContainer, container);
    assert_equals(ranges[0].startOffset, 0);
    assert_equals(ranges[0].endContainer, container);
    assert_equals(ranges[0].endOffset, 0);
    assert_true(ranges[0].collapsed);
}, 'extend abort steps when called with a disconnected node inside a shadow tree');

container.remove();

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