https://github.com/web-platform-tests/wpt
Raw File
Tip revision: 96a24630129573978168b40892e9128f4e1861df authored by Mikhail Pozdnyakov on 27 March 2018, 18:32:09 UTC
Enable WPT tests for the Generic Sensor classes
Tip revision: 96a2463
dir-manual.html
<!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>
back to top