summaryrefslogtreecommitdiffstats
path: root/LayoutTests/fast/xpath/py-dom-xpath/axes.html
blob: 1417c7e47ee0407d1f962adea8e2e984de9fbd05 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<html>
<head>
<link rel="stylesheet" href="../../js/resources/js-test-style.css">
<script src="../../js/resources/js-test-pre.js"></script>
<script src="../xpath-test-pre.js"></script>
</head>
<body>
<div id="console"></div>

<script>
function arraysAreEqual(array1, array2) {
   var temp = new Array();
   if ( (!array1[0]) || (!array2[0]) ) { // If either is not an array
      return false;
   }
   if (array1.length != array2.length) {
      return false;
   }
   // Put all the elements from array1 into a "tagged" array
   for (var i = 0; i < array1.length; i++) {
      key = (typeof array1[i]) + "~" + array1[i];
   // Use "typeof" so a number 1 isn't equal to a string "1".
      if (temp[key]) { temp[key]++; } else { temp[key] = 1; }
   // temp[key] = # of occurrences of the value (so an element could appear multiple times)
   }
   // Go through array2 - if same tag missing in "tagged" array, not equal
   for (var i = 0; i < array2.length; i++) {
      key = (typeof array2[i]) + "~" + array2[i];
      if (temp[key]) {
         if (temp[key] == 0) { return false; } else { temp[key]--; }
      // Subtract to keep track of # of appearances in array2
      } else { // Key didn't appear in array1, arrays are not equal.
         return false;
      }
   }
   // If we get to this point, then every generated key in array1 showed up the exact same
   // number of times in array2, so the arrays are equal.
   return true;
}


var doc = (new DOMParser).parseFromString(
    '<doc id="0">' +
        '<chapter id="1">' +
            '<section id="1.1">' +
                '<item id="1.1.1" />' +
            '</section>' +
        '</chapter>' +
        '<chapter id="2">' +
            '<section id="2.1">' +
                '<item id="2.1.1" />' +
            '</section>' +
            '<section id="2.2">' +
                '<item id="2.2.1" /><item id="2.2.2" /><item id="2.2.3" />' +
            '</section>' +
            '<section id="2.3">' +
                '<item id="2.3.1" />' +
            '</section>' +
        '</chapter>' +
        '<chapter id="3">' +
            '<section id="3.1">' +
                '<item id="3.1.1" />' +
            '</section>' +
        '</chapter>' +
    '</doc>',
    'application/xml');

var ROOT = doc.documentElement;
var CHAPTER1 = ROOT.firstChild;
var CHAPTER2 = CHAPTER1.nextSibling;
var CHAPTER3 = CHAPTER2.nextSibling;
var SECTION11 = CHAPTER1.firstChild;
var SECTION21 = CHAPTER2.firstChild;
var SECTION22 = SECTION21.nextSibling;
var SECTION23 = SECTION22.nextSibling;
var SECTION31 = CHAPTER3.firstChild;
var ITEM111 = SECTION11.firstChild;
var ITEM211 = SECTION21.firstChild;
var ITEM221 = SECTION22.firstChild;
var ITEM222 = ITEM221.nextSibling;
var ITEM223 = ITEM222.nextSibling;
var ITEM231 = SECTION23.firstChild;
var ITEM311 = SECTION31.firstChild;

test(doc, doc.documentElement, '//*[@id="2"]/child::*', [SECTION21, SECTION22, SECTION23]);
test(doc, doc.documentElement, '//*[@id="2.2"]/parent::*', [CHAPTER2]);
test(doc, doc.documentElement, '//*[@id="2.2"]/ancestor::*', [ROOT, CHAPTER2]);
test(doc, doc.documentElement, '//*[@id="2.2"]/following-sibling::*', [SECTION23]);
test(doc, doc.documentElement, '//*[@id="2.2"]/preceding-sibling::*', [SECTION21]);
test(doc, doc.documentElement, '//*[@id="2.2"]/following::*', [SECTION23, ITEM231, CHAPTER3, SECTION31, ITEM311]);
test(doc, doc.documentElement, '//*[@id="2.2"]/preceding::*', [CHAPTER1, SECTION11, ITEM111, SECTION21, ITEM211]);
test(doc, doc.documentElement, '//*[@id="2.2"]/attribute::*', [SECTION22.getAttributeNode("id")]);
test(doc, doc.documentElement, '//*[@id="2.2"]/self::*', [SECTION22]);
test(doc, doc.documentElement, '//*[@id="1"]/descendant-or-self::*', [CHAPTER1, SECTION11, ITEM111]);
test(doc, doc.documentElement, '//*[@id="2.2"]/ancestor-or-self::*', [ROOT, CHAPTER2, SECTION22]);

debug("Test that the ancestor, descendant, following, preceding, and self axes partition the document");
var nodeCount = doc.evaluate("count(//*)", doc.documentElement, null, XPathResult.ANY_TYPE, null).numberValue;
shouldBe('nodeCount', '16');
var allNodes = doc.evaluate("//*", doc.documentElement, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
var allNodesSet = []
for (i = 0; i < allNodes.snapshotLength; ++i) {
    allNodesSet.push(allNodes.snapshotItem(i));
}
for (i = 0; i < allNodes.snapshotLength; ++i) {
    var node = allNodes.snapshotItem(i);
    var resultNodes = [];
    var axes = ['ancestor','descendant','following','preceding','self'];
    for (axis in axes) {
        var res = doc.evaluate(axes[axis] + "::*", node, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
        while (n = res.iterateNext()) {
            resultNodes.push(n);
        }
    }
    if (arraysAreEqual(resultNodes, allNodesSet))
        testPassed(node.getAttribute("id"));
    else
        testFailed(node.getAttribute("id"));
}

var successfullyParsed = true;

</script>
<script src="../../js/resources/js-test-post.js"></script>
</body>
</html>