test_bug628938.html (6594B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=628938 5 --> 6 <head> 7 <title>Test for Bug 628938</title> 8 <script src="/tests/SimpleTest/SimpleTest.js"></script> 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 10 </head> 11 <body> 12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=628938">Mozilla Bug 628938</a> 13 <p id="display"></p> 14 <foo id="content" style="display: none"> 15 <div><div>foo</div></div> 16 <span> bar </span> 17 <div>tulip<span>bar</span></div> 18 <span></span> 19 <div>foo</div> 20 <span></span> 21 <div>bar</div> 22 <span></span> 23 <div>foobar</div> 24 </foo> 25 <pre id="test"> 26 <script type="application/javascript"> 27 28 /** Test for Bug 628938 */ 29 30 var gResults = []; 31 var gIdx = 0; 32 33 function walkTree(walker) 34 { 35 if(walker.firstChild()) { 36 do { 37 if (walker.currentNode.nodeType == Node.ELEMENT_NODE) { 38 ok(gResults[gIdx][0], "current node should be an element"); 39 is(walker.currentNode.nodeName, gResults[gIdx][1], 40 "current node name should be " + gResults[gIdx][1]); 41 } else { 42 ok(!gResults[gIdx][0], "current node shouldn't be an element"); 43 is(walker.currentNode.nodeValue, gResults[gIdx][1], 44 "current node value should be " + gResults[gIdx][1]); 45 } 46 gIdx++; 47 // Recursively walk the rest of the sub-tree. 48 walkTree(walker); 49 } while(walker.nextSibling()); 50 51 // don't forget to return the treewalker to it's previous state 52 // before exiting the function 53 walker.parentNode(); 54 } 55 } 56 57 function regularWalk() 58 { 59 gResults = [ 60 [ false, "\n " ], 61 [ true, "DIV" ], 62 [ true, "DIV" ], 63 [ false, "foo" ], 64 [ false, "\n " ], 65 [ true, "SPAN" ], 66 [ false, " bar " ], 67 [ false, "\n " ], 68 [ true, "DIV" ], 69 [ false, "tulip" ], 70 [ true, "SPAN" ], 71 [ false, "bar" ], 72 [ false, "\n " ], 73 [ true, "SPAN" ], 74 [ false, "\n " ], 75 [ true, "DIV" ], 76 [ false, "foo" ], 77 [ false, "\n " ], 78 [ true, "SPAN" ], 79 [ false, "\n " ], 80 [ true, "DIV" ], 81 [ false, "bar" ], 82 [ false, "\n " ], 83 [ true, "SPAN" ], 84 [ false, "\n " ], 85 [ true, "DIV" ], 86 [ false, "foobar" ], 87 [ false, "\n" ], 88 ]; 89 var walker = document.createTreeWalker(document.getElementById('content'), 90 NodeFilter.SHOW_ALL, null); 91 92 walkTree(walker); 93 94 gIdx = 0; 95 } 96 97 function noWhiteSpaceWalk() 98 { 99 gResults = [ 100 [ true, "DIV" ], 101 [ true, "DIV" ], 102 [ false, "foo" ], 103 [ true, "SPAN" ], 104 [ false, " bar " ], 105 [ true, "DIV" ], 106 [ false, "tulip" ], 107 [ true, "SPAN" ], 108 [ false, "bar" ], 109 [ true, "SPAN" ], 110 [ true, "DIV" ], 111 [ false, "foo" ], 112 [ true, "SPAN" ], 113 [ true, "DIV" ], 114 [ false, "bar" ], 115 [ true, "SPAN" ], 116 [ true, "DIV" ], 117 [ false, "foobar" ], 118 ]; 119 var walker = document.createTreeWalker(document.getElementById('content'), 120 NodeFilter.SHOW_ALL, 121 { 122 acceptNode(node) { 123 if (node.nodeType == Node.TEXT_NODE && 124 !(/[^\t\n\r ]/.test(node.nodeValue))) 125 return NodeFilter.FILTER_REJECT; 126 return NodeFilter.FILTER_ACCEPT; 127 } 128 }); 129 130 walkTree(walker); 131 132 gIdx = 0; 133 } 134 135 function onlyElementsWalk() 136 { 137 gResults = [ 138 [ true, "DIV" ], 139 [ true, "DIV" ], 140 [ true, "SPAN" ], 141 [ true, "DIV" ], 142 [ true, "SPAN" ], 143 [ true, "SPAN" ], 144 [ true, "DIV" ], 145 [ true, "SPAN" ], 146 [ true, "DIV" ], 147 [ true, "SPAN" ], 148 [ true, "DIV" ], 149 ]; 150 var walker = document.createTreeWalker(document.getElementById('content'), 151 NodeFilter.SHOW_ELEMENT, null); 152 153 walkTree(walker); 154 155 gIdx = 0; 156 } 157 158 function onlyDivSubTreeWalk() 159 { 160 gResults = [ 161 [ true, "DIV" ], 162 [ true, "DIV" ], 163 [ false, "foo" ], 164 [ true, "DIV" ], 165 [ false, "tulip" ], 166 [ true, "SPAN" ], 167 [ false, "bar" ], 168 [ true, "DIV" ], 169 [ false, "foo" ], 170 [ true, "DIV" ], 171 [ false, "bar" ], 172 [ true, "DIV" ], 173 [ false, "foobar" ], 174 ]; 175 var walker = document.createTreeWalker(document.getElementById('content'), 176 NodeFilter.SHOW_ALL, 177 { 178 acceptNode(node) { 179 if (node.nodeType == Node.TEXT_NODE && 180 !(/[^\t\n\r ]/.test(node.nodeValue))) 181 return NodeFilter.FILTER_REJECT; 182 183 while (node) { 184 if (node.nodeName == "DIV") 185 return NodeFilter.FILTER_ACCEPT; 186 node = node.parentNode; 187 } 188 return NodeFilter.FILTER_SKIP; 189 } 190 }); 191 192 walkTree(walker); 193 194 gIdx = 0; 195 } 196 197 function onlyDivDataWalk() 198 { 199 gResults = [ 200 [ true, "DIV" ], 201 [ true, "DIV" ], 202 [ false, "foo" ], 203 [ true, "DIV" ], 204 [ false, "tulip" ], 205 [ true, "SPAN" ], 206 [ true, "DIV" ], 207 [ false, "foo" ], 208 [ true, "DIV" ], 209 [ false, "bar" ], 210 [ true, "DIV" ], 211 [ false, "foobar" ], 212 ]; 213 var walker = document.createTreeWalker(document.getElementById('content'), 214 NodeFilter.SHOW_ALL, 215 { 216 acceptNode(node) { 217 if (node.nodeName == "DIV" || 218 (node.parentNode && 219 node.parentNode.nodeName == "DIV")) 220 return NodeFilter.FILTER_ACCEPT; 221 return NodeFilter.FILTER_SKIP; 222 } 223 }); 224 225 walkTree(walker); 226 227 gIdx = 0; 228 } 229 230 regularWalk(); 231 noWhiteSpaceWalk(); 232 onlyElementsWalk(); 233 onlyDivSubTreeWalk(); 234 onlyDivDataWalk(); 235 236 </script> 237 </pre> 238 </body> 239 </html>