summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/nodes/Node-compareDocumentPosition.html
blob: afae60aad1e5f9543ede1d97f86cf83bf1df9d5d (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
<!doctype html>
<title>Node.compareDocumentPosition() tests</title>
<link rel=author title="Aryeh Gregor" href=ayg@aryeh.name>
<div id=log></div>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=../common.js></script>
<script>
"use strict";

testNodes.forEach(function(referenceName) {
  var reference = eval(referenceName);
  testNodes.forEach(function(otherName) {
    var other = eval(otherName);
    test(function() {
      var result = reference.compareDocumentPosition(other);

      // "If other and reference are the same object, return zero and
      // terminate these steps."
      if (other === reference) {
        assert_equals(result, 0);
        return;
      }

      // "If other and reference are not in the same tree, return the result of
      // adding DOCUMENT_POSITION_DISCONNECTED,
      // DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, and either
      // DOCUMENT_POSITION_PRECEDING or DOCUMENT_POSITION_FOLLOWING, with the
      // constraint that this is to be consistent, together and terminate these
      // steps."
      if (furthestAncestor(reference) !== furthestAncestor(other)) {
        // TODO: Test that it's consistent.
        assert_in_array(result, [Node.DOCUMENT_POSITION_DISCONNECTED +
                                 Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC +
                                 Node.DOCUMENT_POSITION_PRECEDING,
                                 Node.DOCUMENT_POSITION_DISCONNECTED +
                                 Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC +
                                 Node.DOCUMENT_POSITION_FOLLOWING]);
        return;
      }

      // "If other is an ancestor of reference, return the result of
      // adding DOCUMENT_POSITION_CONTAINS to DOCUMENT_POSITION_PRECEDING
      // and terminate these steps."
      var ancestor = reference.parentNode;
      while (ancestor && ancestor !== other) {
        ancestor = ancestor.parentNode;
      }
      if (ancestor === other) {
        assert_equals(result, Node.DOCUMENT_POSITION_CONTAINS +
                              Node.DOCUMENT_POSITION_PRECEDING);
        return;
      }

      // "If other is a descendant of reference, return the result of adding
      // DOCUMENT_POSITION_CONTAINED_BY to DOCUMENT_POSITION_FOLLOWING and
      // terminate these steps."
      ancestor = other.parentNode;
      while (ancestor && ancestor !== reference) {
        ancestor = ancestor.parentNode;
      }
      if (ancestor === reference) {
        assert_equals(result, Node.DOCUMENT_POSITION_CONTAINED_BY +
                              Node.DOCUMENT_POSITION_FOLLOWING);
        return;
      }

      // "If other is preceding reference return DOCUMENT_POSITION_PRECEDING
      // and terminate these steps."
      var prev = previousNode(reference);
      while (prev && prev !== other) {
        prev = previousNode(prev);
      }
      if (prev === other) {
        assert_equals(result, Node.DOCUMENT_POSITION_PRECEDING);
        return;
      }

      // "Return DOCUMENT_POSITION_FOLLOWING."
      assert_equals(result, Node.DOCUMENT_POSITION_FOLLOWING);
    }, referenceName + ".compareDocumentPosition(" + otherName + ")");
  });
});

testDiv.parentNode.removeChild(testDiv);
</script>
<!-- vim: set expandtab tabstop=2 shiftwidth=2: -->