summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/ranges/Range-intersectsNode.html
blob: 97e10f6f0d893fb8ded1067c83c2b0b4262f796a (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
<!doctype html>
<title>Range.intersectsNode() 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";

// Will be filled in on the first run for that range
var testRangesCached = [];

for (var i = 0; i < testNodes.length; i++) {
  var node = eval(testNodes[i]);

  for (var j = 0; j < testRanges.length; j++) {
    test(function() {
      if (testRangesCached[j] === undefined) {
        try {
          testRangesCached[j] = rangeFromEndpoints(eval(testRanges[i]));
        } catch(e) {
          testRangesCached[j] = null;
        }
      }
      assert_not_equals(testRangesCached[j], null,
        "Setting up the range failed");

      var range = testRangesCached[j].cloneRange();

      // "If node's root is different from the context object's root,
      // return false and terminate these steps."
      if (furthestAncestor(node) !== furthestAncestor(range.startContainer)) {
        assert_equals(range.intersectsNode(node), false,
          "Must return false if node and range have different roots");
        return;
      }

      // "Let parent be node's parent."
      var parent_ = node.parentNode;

      // "If parent is null, return true and terminate these steps."
      if (!parent_) {
        assert_equals(range.intersectsNode(node), true,
          "Must return true if node's parent is null");
        return;
      }

      // "Let offset be node's index."
      var offset = indexOf(node);

      // "If (parent, offset) is before end and (parent, offset + 1) is
      // after start, return true and terminate these steps."
      if (getPosition(parent_, offset, range.endContainer, range.endOffset) === "before"
      && getPosition(parent_, offset + 1, range.startContainer, range.startOffset) === "after") {
        assert_equals(range.intersectsNode(node), true,
          "Must return true if (parent, offset) is before range end and (parent, offset + 1) is after range start");
        return;
      }

      // "Return false."
      assert_equals(range.intersectsNode(node), false,
        "Must return false if (parent, offset) is not before range end or (parent, offset + 1) is not after range start");
    }, "Node " + i + " " + testNodes[i] + ", range " + j + " " + testRanges[j]);
  }
}

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