Home | History | Annotate | Download | only in py-dom-xpath
      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>
     11 function 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 
     42 var doc = (new DOMParser).parseFromString(
     43     '' +
     44         '' +
     45             '
' + 46 '' + 47 '
'
+ 48 '' + 49 '' + 50 '
' + 51 '' + 52 '
'
+ 53 '
' + 54 '' + 55 '
'
+ 56 '
' + 57 '' + 58 '
'
+ 59 '' + 60 '' + 61 '
' + 62 '' + 63 '
'
+ 64 '' + 65 '', 66 'application/xml'); 67 68 var ROOT = doc.documentElement; 69 var CHAPTER1 = ROOT.firstChild; 70 var CHAPTER2 = CHAPTER1.nextSibling; 71 var CHAPTER3 = CHAPTER2.nextSibling; 72 var SECTION11 = CHAPTER1.firstChild; 73 var SECTION21 = CHAPTER2.firstChild; 74 var SECTION22 = SECTION21.nextSibling; 75 var SECTION23 = SECTION22.nextSibling; 76 var SECTION31 = CHAPTER3.firstChild; 77 var ITEM111 = SECTION11.firstChild; 78 var ITEM211 = SECTION21.firstChild; 79 var ITEM221 = SECTION22.firstChild; 80 var ITEM222 = ITEM221.nextSibling; 81 var ITEM223 = ITEM222.nextSibling; 82 var ITEM231 = SECTION23.firstChild; 83 var ITEM311 = SECTION31.firstChild; 84 85 test(doc, doc.documentElement, '//*[@id="2"]/child::*', [SECTION21, SECTION22, SECTION23]); 86 test(doc, doc.documentElement, '//*[@id="2.2"]/parent::*', [CHAPTER2]); 87 test(doc, doc.documentElement, '//*[@id="2.2"]/ancestor::*', [ROOT, CHAPTER2]); 88 test(doc, doc.documentElement, '//*[@id="2.2"]/following-sibling::*', [SECTION23]); 89 test(doc, doc.documentElement, '//*[@id="2.2"]/preceding-sibling::*', [SECTION21]); 90 test(doc, doc.documentElement, '//*[@id="2.2"]/following::*', [SECTION23, ITEM231, CHAPTER3, SECTION31, ITEM311]); 91 test(doc, doc.documentElement, '//*[@id="2.2"]/preceding::*', [CHAPTER1, SECTION11, ITEM111, SECTION21, ITEM211]); 92 test(doc, doc.documentElement, '//*[@id="2.2"]/attribute::*', [SECTION22.getAttributeNode("id")]); 93 test(doc, doc.documentElement, '//*[@id="2.2"]/self::*', [SECTION22]); 94 test(doc, doc.documentElement, '//*[@id="1"]/descendant-or-self::*', [CHAPTER1, SECTION11, ITEM111]); 95 test(doc, doc.documentElement, '//*[@id="2.2"]/ancestor-or-self::*', [ROOT, CHAPTER2, SECTION22]); 96 97 debug("Test that the ancestor, descendant, following, preceding, and self axes partition the document"); 98 var nodeCount = doc.evaluate("count(//*)", doc.documentElement, null, XPathResult.ANY_TYPE, null).numberValue; 99 shouldBe('nodeCount', '16'); 100 var allNodes = doc.evaluate("//*", doc.documentElement, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); 101 var allNodesSet = [] 102 for (i = 0; i < allNodes.snapshotLength; ++i) { 103 allNodesSet.push(allNodes.snapshotItem(i)); 104 } 105 for (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 121 var successfullyParsed = true; 122 123 </script> 124 <script src="../../js/resources/js-test-post.js"></script> 125 </body> 126 </html> 127