summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/selection/dir-manual.html
blob: 39cf6555234b7b2e189412e97aed5a93b19ccea2 (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
<!doctype html>
<title>Selection direction tests</title>
<meta charset=utf-8>
<div id=test>
    <p>This is a manual test, since there's no way to synthesize keyboard or
    mouse input.  Click after the letter "c" in the following paragraph and
    drag backwards so that both the "b" and the "c" are highlighted, then click
    the "Test" button:

    <p>abcd <button onclick=testDirection()>Test</button>

    <p>efghi
</div>
<div id=log></div>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script>
setup({explicit_done: true});

function testDirection() {
    var testDiv = document.getElementById("test");
    var p = testDiv.getElementsByTagName("p")[1].firstChild;
    var selection = getSelection();
    var range = selection.getRangeAt(0);
    test(function() {
        assert_equals(range.toString(), "bc");
    }, "The expected range is selected");
    test(function() {
        assert_equals(selection.anchorNode, p);
        assert_equals(selection.focusNode, p);
    }, "Expected node is initially selected");
    test(function() {
        assert_array_equals([selection.anchorOffset, selection.focusOffset].sort(), [1, 3]);
    }, "Expected offsets are initially selected (maybe not in order)");
    test(function() {
        assert_equals(selection.anchorOffset, 3);
        assert_equals(selection.focusOffset, 1);
    }, "Offsets are backwards for initial selection"),
    test(function() {
        assert_equals(selection.anchorNode, range.endContainer);
        assert_equals(selection.anchorOffset, range.endOffset);
        assert_equals(selection.focusNode, range.startContainer);
        assert_equals(selection.focusOffset, range.startOffset);
    }, "Offsets match the range for initial selection");

    // Per spec, the direction of the selection remains even if you zap a range
    // and add a new one.
    test(function() {
        selection.removeRange(range);
        range = document.createRange();
        p = testDiv.getElementsByTagName("p")[0].firstChild;
        range.setStart(p, 0);
        range.setEnd(p, 4);
        assert_equals(range.toString(), "This");
        selection.addRange(range);
    }, "removeRange()/addRange() successful");
    test(function() {
        assert_equals(selection.anchorNode, p);
        assert_equals(selection.focusNode, p);
    }, "Expected node is selected after remove/addRange()");
    test(function() {
        assert_array_equals([selection.anchorOffset, selection.focusOffset].sort(), [0, 4]);
    }, "Expected offsets are selected after remove/addRange() (maybe not in order)");
    test(function() {
        assert_equals(selection.anchorOffset, 4);
        assert_equals(selection.focusOffset, 0);
    }, "Offsets are backwards after remove/addRange()"),
    test(function() {
        assert_equals(selection.anchorNode, range.endContainer);
        assert_equals(selection.anchorOffset, range.endOffset);
        assert_equals(selection.focusNode, range.startContainer);
        assert_equals(selection.focusOffset, range.startOffset);
    }, "Offsets match the range after remove/addRange()");

    // But if you call removeAllRanges(), the direction should reset to
    // forwards.
    test(function() {
        selection.removeAllRanges();
        range = document.createRange();
        p = testDiv.getElementsByTagName("p")[2].firstChild;
        range.setStart(p, 2);
        range.setEnd(p, 5);
        assert_equals(range.toString(), "ghi");
        selection.addRange(range);
    }, "removeAllRanges() successful");
    test(function() {
        assert_equals(selection.anchorNode, p);
        assert_equals(selection.focusNode, p);
    }, "Expected node is selected after removeAllRanges()");
    test(function() {
        assert_array_equals([selection.anchorOffset, selection.focusOffset].sort(), [2, 5]);
    }, "Expected offsets are selected after removeAllRanges() (maybe not in order)");
    test(function() {
        assert_equals(selection.anchorOffset, 2);
        assert_equals(selection.focusOffset, 5);
    }, "Offsets are forwards after removeAllRanges()");
    test(function() {
        assert_equals(selection.anchorNode, range.startContainer);
        assert_equals(selection.anchorOffset, range.startOffset);
        assert_equals(selection.focusNode, range.endContainer);
        assert_equals(selection.focusOffset, range.endOffset);
    }, "Offsets match the range after removeAllRanges()");

    done();
}
</script>