axes.html revision 5f90462bbf4efb0ac7bb65a852d5559d0ab30f0b
1<html>
2<head>
3<link rel="stylesheet" href="/js/resources/js-test-style.css">
4<script src="/js/resources/js-test-pre.js"></script>
5<script src="/xpath-test-pre.js"></script>
6</head>
7<body>
8<div id="console"></div>
9
10<script>
11function arraysAreEqual(array1, array2) {
12   var temp = new Array();
13   if ( (!array1[0]) || (!array2[0]) ) { // If either is not an array
14      return false;
15   }
16   if (array1.length != array2.length) {
17      return false;
18   }
19   // Put all the elements from array1 into a "tagged" array
20   for (var i = 0; i < array1.length; i++) {
21      key = (typeof array1[i]) + "~" + array1[i];
22   // Use "typeof" so a number 1 isn't equal to a string "1".
23      if (temp[key]) { temp[key]++; } else { temp[key] = 1; }
24   // temp[key] = # of occurrences of the value (so an element could appear multiple times)
25   }
26   // Go through array2 - if same tag missing in "tagged" array, not equal
27   for (var i = 0; i < array2.length; i++) {
28      key = (typeof array2[i]) + "~" + array2[i];
29      if (temp[key]) {
30         if (temp[key] == 0) { return false; } else { temp[key]--; }
31      // Subtract to keep track of # of appearances in array2
32      } else { // Key didn't appear in array1, arrays are not equal.
33         return false;
34      }
35   }
36   // If we get to this point, then every generated key in array1 showed up the exact same
37   // number of times in array2, so the arrays are equal.
38   return true;
39}
40
41
42var doc = (new DOMParser).parseFromString(
43    '<doc id="0">' +
44        '<chapter id="1">' +
45            '<section id="1.1">' +
46                '<item id="1.1.1" />' +
47            '</section>' +
48        '</chapter>' +
49        '<chapter id="2">' +
50            '<section id="2.1">' +
51                '<item id="2.1.1" />' +
52            '</section>' +
53            '<section id="2.2">' +
54                '<item id="2.2.1" /><item id="2.2.2" /><item id="2.2.3" />' +
55            '</section>' +
56            '<section id="2.3">' +
57                '<item id="2.3.1" />' +
58            '</section>' +
59        '</chapter>' +
60        '<chapter id="3">' +
61            '<section id="3.1">' +
62                '<item id="3.1.1" />' +
63            '</section>' +
64        '</chapter>' +
65    '</doc>',
66    'application/xml');
67
68var ROOT = doc.documentElement;
69var CHAPTER1 = ROOT.firstChild;
70var CHAPTER2 = CHAPTER1.nextSibling;
71var CHAPTER3 = CHAPTER2.nextSibling;
72var SECTION11 = CHAPTER1.firstChild;
73var SECTION21 = CHAPTER2.firstChild;
74var SECTION22 = SECTION21.nextSibling;
75var SECTION23 = SECTION22.nextSibling;
76var SECTION31 = CHAPTER3.firstChild;
77var ITEM111 = SECTION11.firstChild;
78var ITEM211 = SECTION21.firstChild;
79var ITEM221 = SECTION22.firstChild;
80var ITEM222 = ITEM221.nextSibling;
81var ITEM223 = ITEM222.nextSibling;
82var ITEM231 = SECTION23.firstChild;
83var ITEM311 = SECTION31.firstChild;
84
85test(doc, doc.documentElement, '//*[@id="2"]/child::*', [SECTION21, SECTION22, SECTION23]);
86test(doc, doc.documentElement, '//*[@id="2.2"]/parent::*', [CHAPTER2]);
87test(doc, doc.documentElement, '//*[@id="2.2"]/ancestor::*', [ROOT, CHAPTER2]);
88test(doc, doc.documentElement, '//*[@id="2.2"]/following-sibling::*', [SECTION23]);
89test(doc, doc.documentElement, '//*[@id="2.2"]/preceding-sibling::*', [SECTION21]);
90test(doc, doc.documentElement, '//*[@id="2.2"]/following::*', [SECTION23, ITEM231, CHAPTER3, SECTION31, ITEM311]);
91test(doc, doc.documentElement, '//*[@id="2.2"]/preceding::*', [CHAPTER1, SECTION11, ITEM111, SECTION21, ITEM211]);
92test(doc, doc.documentElement, '//*[@id="2.2"]/attribute::*', [SECTION22.getAttributeNode("id")]);
93test(doc, doc.documentElement, '//*[@id="2.2"]/self::*', [SECTION22]);
94test(doc, doc.documentElement, '//*[@id="1"]/descendant-or-self::*', [CHAPTER1, SECTION11, ITEM111]);
95test(doc, doc.documentElement, '//*[@id="2.2"]/ancestor-or-self::*', [ROOT, CHAPTER2, SECTION22]);
96
97debug("Test that the ancestor, descendant, following, preceding, and self axes partition the document");
98var nodeCount = doc.evaluate("count(//*)", doc.documentElement, null, XPathResult.ANY_TYPE, null).numberValue;
99shouldBe('nodeCount', '16');
100var allNodes = doc.evaluate("//*", doc.documentElement, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
101var allNodesSet = []
102for (i = 0; i < allNodes.snapshotLength; ++i) {
103    allNodesSet.push(allNodes.snapshotItem(i));
104}
105for (i = 0; i < allNodes.snapshotLength; ++i) {
106    var node = allNodes.snapshotItem(i);
107    var resultNodes = [];
108    var axes = ['ancestor','descendant','following','preceding','self'];
109    for (axis in axes) {
110        var res = doc.evaluate(axes[axis] + "::*", node, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
111        while (n = res.iterateNext()) {
112            resultNodes.push(n);
113        }
114    }
115    if (arraysAreEqual(resultNodes, allNodesSet))
116        testPassed(node.getAttribute("id"));
117    else
118        testFailed(node.getAttribute("id"));
119}
120
121var successfullyParsed = true;
122
123</script>
124<script src="/js/resources/js-test-post.js"></script>
125</body>
126</html>
127