summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/ranges/Range-selectNode.html
blob: fe9b1f7860b1a297e1583e64242329d759d1dc41 (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
<!doctype html>
<title>Range.selectNode() and .selectNodeContents() tests</title>
<link rel="author" title="Aryeh Gregor" href=ayg@aryeh.name>
<meta name=timeout content=long>
<div id=log></div>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=../common.js></script>
<script>
"use strict";

function testSelectNode(range, node) {
    try {
        range.collapsed;
    } catch (e) {
        // Range is detached
        assert_throws_dom("INVALID_STATE_ERR", function () {
            range.selectNode(node);
        }, "selectNode() on a detached node must throw INVALID_STATE_ERR");
        assert_throws_dom("INVALID_STATE_ERR", function () {
            range.selectNodeContents(node);
        }, "selectNodeContents() on a detached node must throw INVALID_STATE_ERR");
        return;
    }

    if (!node.parentNode) {
        assert_throws_dom("INVALID_NODE_TYPE_ERR", function() {
            range.selectNode(node);
        }, "selectNode() on a node with no parent must throw INVALID_NODE_TYPE_ERR");
    } else {
        var index = 0;
        while (node.parentNode.childNodes[index] != node) {
            index++;
        }

        range.selectNode(node);
        assert_equals(range.startContainer, node.parentNode,
            "After selectNode(), startContainer must equal parent node");
        assert_equals(range.endContainer, node.parentNode,
            "After selectNode(), endContainer must equal parent node");
        assert_equals(range.startOffset, index,
            "After selectNode(), startOffset must be index of node in parent (" + index + ")");
        assert_equals(range.endOffset, index + 1,
            "After selectNode(), endOffset must be one plus index of node in parent (" + (index + 1) + ")");
    }

    if (node.nodeType == Node.DOCUMENT_TYPE_NODE) {
        assert_throws_dom("INVALID_NODE_TYPE_ERR", function () {
            range.selectNodeContents(node);
        }, "selectNodeContents() on a doctype must throw INVALID_NODE_TYPE_ERR");
    } else {
        range.selectNodeContents(node);
        assert_equals(range.startContainer, node,
            "After selectNodeContents(), startContainer must equal node");
        assert_equals(range.endContainer, node,
            "After selectNodeContents(), endContainer must equal node");
        assert_equals(range.startOffset, 0,
            "After selectNodeContents(), startOffset must equal 0");
        var len = nodeLength(node);
        assert_equals(range.endOffset, len,
            "After selectNodeContents(), endOffset must equal node length (" + len + ")");
    }
}

var range = document.createRange();
var foreignRange = foreignDoc.createRange();
var xmlRange = xmlDoc.createRange();
var detachedRange = document.createRange();
detachedRange.detach();
var tests = [];
function testTree(root, marker) {
    if (root.nodeType == Node.ELEMENT_NODE && root.id == "log") {
        // This is being modified during the tests, so let's not test it.
        return;
    }
    tests.push([marker + ": " + root.nodeName.toLowerCase() + " node, current doc's range, type " + root.nodeType, range, root]);
    tests.push([marker + ": " + root.nodeName.toLowerCase() + " node, foreign doc's range, type " + root.nodeType, foreignRange, root]);
    tests.push([marker + ": " + root.nodeName.toLowerCase() + " node, XML doc's range, type " + root.nodeType, xmlRange, root]);
    tests.push([marker + ": " + root.nodeName.toLowerCase() + " node, detached range, type " + root.nodeType, detachedRange, root]);
    for (var i = 0; i < root.childNodes.length; i++) {
        testTree(root.childNodes[i], marker + "[" + i + "]");
    }
}
testTree(document, "current doc");
testTree(foreignDoc, "foreign doc");
testTree(detachedDiv, "detached div in current doc");

var otherTests = ["xmlDoc", "xmlElement", "detachedTextNode",
"foreignTextNode", "xmlTextNode", "processingInstruction", "comment",
"foreignComment", "xmlComment", "docfrag", "foreignDocfrag", "xmlDocfrag"];

for (var i = 0; i < otherTests.length; i++) {
    testTree(window[otherTests[i]], otherTests[i]);
}

generate_tests(testSelectNode, tests);

testDiv.style.display = "none";
</script>