001.xml (1836B)
1 <html xmlns="http://www.w3.org/1999/xhtml"> 2 <head> 3 <title>DOM Traversal: NodeIterator: Basics</title> 4 <script type="text/javascript"> <![CDATA[ 5 function doTest() { 6 var iterator = document.createNodeIterator(document, NodeFilter.SHOW_ALL, null, false); 7 var expected = new Array(9, // document 8 1, // html 9 3, 1, // head 10 3, 1, 3, // title 11 3, 1, 3, 4, // script and CDATA block 12 3, 3, 1, // body 13 3, 1, 3, // pre 14 3, // </body> 15 3, 8, // <!-- --> 16 3, 7, // <? ?>, 17 3, 4, 3); // CDATA 18 var found = new Array(); 19 20 // walk document 21 var node; 22 while (node = iterator.nextNode()) 23 found.push(node.nodeType); 24 25 // check results 26 var errors = 0; 27 var s = ''; 28 var length = (found.length > expected.length) ? found.length : expected.length; 29 s += 'EXPECTED FOUND\n'; 30 for (var i = 0; i < length; i += 1) { 31 s += ' ' + (expected[i] ? expected[i] : '-') + 32 ' ' + (found[i] ? found[i] : '-'); 33 if (found[i] != expected[i]) { 34 s += ' MISMATCH'; 35 errors += 1; 36 } 37 s += '\n'; 38 } 39 var p = document.getElementsByTagNameNS('http://www.w3.org/1999/xhtml', 'pre')[0]; 40 if (errors) 41 p.firstChild.data = 'FAIL: ' + errors + ' errors found:\n\n' + s; 42 else 43 p.firstChild.data = 'PASS'; 44 } 45 ]]></script> 46 </head> 47 <body onload="doTest()"> 48 <pre id="result">FAIL: Script failed to run.</pre> 49 </body> 50 <!-- some more nodes to test this: --> 51 <?test node?> 52 <![CDATA[]]> 53 </html>