|
1 | 1 | <!DOCTYPE html> |
2 | 2 | <script src="../testing.js"></script> |
| 3 | + |
| 4 | +<!-- Test fixture --> |
| 5 | +<div id="container"> |
| 6 | + <!-- comment1 --> |
| 7 | + <div id="outer"> |
| 8 | + <!-- comment2 --> |
| 9 | + <span id="inner"> |
| 10 | + <!-- comment3 --> |
| 11 | + Text content |
| 12 | + <!-- comment4 --> |
| 13 | + </span> |
| 14 | + <!-- comment5 --> |
| 15 | + </div> |
| 16 | + <!-- comment6 --> |
| 17 | +</div> |
| 18 | + |
3 | 19 | <script id=nodeFilter> |
4 | 20 | testing.expectEqual(1, NodeFilter.FILTER_ACCEPT); |
5 | 21 | testing.expectEqual(2, NodeFilter.FILTER_REJECT); |
6 | 22 | testing.expectEqual(3, NodeFilter.FILTER_SKIP); |
7 | 23 | testing.expectEqual(4294967295, NodeFilter.SHOW_ALL); |
8 | 24 | testing.expectEqual(129, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_COMMENT); |
9 | 25 | </script> |
| 26 | + |
| 27 | +<script id=treeWalkerComments> |
| 28 | + { |
| 29 | + const container = $('#container'); |
| 30 | + const walker = document.createTreeWalker( |
| 31 | + container, |
| 32 | + NodeFilter.SHOW_COMMENT, |
| 33 | + null, |
| 34 | + false |
| 35 | + ); |
| 36 | + |
| 37 | + const comments = []; |
| 38 | + let node; |
| 39 | + while (node = walker.nextNode()) { |
| 40 | + comments.push(node.data.trim()); |
| 41 | + } |
| 42 | + |
| 43 | + // Should find all 6 comments, including those nested inside elements |
| 44 | + testing.expectEqual(6, comments.length); |
| 45 | + testing.expectEqual('comment1', comments[0]); |
| 46 | + testing.expectEqual('comment2', comments[1]); |
| 47 | + testing.expectEqual('comment3', comments[2]); |
| 48 | + testing.expectEqual('comment4', comments[3]); |
| 49 | + testing.expectEqual('comment5', comments[4]); |
| 50 | + testing.expectEqual('comment6', comments[5]); |
| 51 | + } |
| 52 | +</script> |
| 53 | + |
| 54 | +<script id=treeWalkerElements> |
| 55 | + { |
| 56 | + const container = $('#container'); |
| 57 | + const walker = document.createTreeWalker( |
| 58 | + container, |
| 59 | + NodeFilter.SHOW_ELEMENT, |
| 60 | + null, |
| 61 | + false |
| 62 | + ); |
| 63 | + |
| 64 | + const elements = []; |
| 65 | + let node; |
| 66 | + while (node = walker.nextNode()) { |
| 67 | + if (node.id) { |
| 68 | + elements.push(node.id); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Should find the 2 nested elements (outer and inner) |
| 73 | + testing.expectEqual(2, elements.length); |
| 74 | + testing.expectEqual('outer', elements[0]); |
| 75 | + testing.expectEqual('inner', elements[1]); |
| 76 | + } |
| 77 | +</script> |
| 78 | + |
| 79 | +<script id=treeWalkerAll> |
| 80 | + { |
| 81 | + const container = $('#container'); |
| 82 | + const walker = document.createTreeWalker( |
| 83 | + container, |
| 84 | + NodeFilter.SHOW_ALL, |
| 85 | + null, |
| 86 | + false |
| 87 | + ); |
| 88 | + |
| 89 | + let commentCount = 0; |
| 90 | + let elementCount = 0; |
| 91 | + let textCount = 0; |
| 92 | + |
| 93 | + let node; |
| 94 | + while (node = walker.nextNode()) { |
| 95 | + if (node.nodeType === 8) commentCount++; // Comment |
| 96 | + else if (node.nodeType === 1) elementCount++; // Element |
| 97 | + else if (node.nodeType === 3) textCount++; // Text |
| 98 | + } |
| 99 | + |
| 100 | + testing.expectEqual(6, commentCount); |
| 101 | + testing.expectEqual(2, elementCount); |
| 102 | + testing.expectEqual(true, textCount > 0); |
| 103 | + } |
| 104 | +</script> |
| 105 | + |
| 106 | +<script id=treeWalkerCombined> |
| 107 | + { |
| 108 | + const container = $('#container'); |
| 109 | + const walker = document.createTreeWalker( |
| 110 | + container, |
| 111 | + NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_COMMENT, |
| 112 | + null, |
| 113 | + false |
| 114 | + ); |
| 115 | + |
| 116 | + let commentCount = 0; |
| 117 | + let elementCount = 0; |
| 118 | + |
| 119 | + let node; |
| 120 | + while (node = walker.nextNode()) { |
| 121 | + if (node.nodeType === 8) commentCount++; // Comment |
| 122 | + else if (node.nodeType === 1) elementCount++; // Element |
| 123 | + } |
| 124 | + |
| 125 | + // Should find 6 comments and 2 elements, but no text nodes |
| 126 | + testing.expectEqual(6, commentCount); |
| 127 | + testing.expectEqual(2, elementCount); |
| 128 | + } |
| 129 | +</script> |
| 130 | + |
| 131 | +<script id=treeWalkerCustomFilter> |
| 132 | + { |
| 133 | + const container = $('#container'); |
| 134 | + |
| 135 | + // Filter that accepts only elements with id |
| 136 | + const walker = document.createTreeWalker( |
| 137 | + container, |
| 138 | + NodeFilter.SHOW_ELEMENT, |
| 139 | + { |
| 140 | + acceptNode: function(node) { |
| 141 | + return node.id ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP; |
| 142 | + } |
| 143 | + }, |
| 144 | + false |
| 145 | + ); |
| 146 | + |
| 147 | + const elements = []; |
| 148 | + let node; |
| 149 | + while (node = walker.nextNode()) { |
| 150 | + elements.push(node.id); |
| 151 | + } |
| 152 | + |
| 153 | + // Should find only elements with id (outer and inner) |
| 154 | + testing.expectEqual(2, elements.length); |
| 155 | + testing.expectEqual('outer', elements[0]); |
| 156 | + testing.expectEqual('inner', elements[1]); |
| 157 | + } |
| 158 | +</script> |
| 159 | + |
| 160 | +<script id=nodeIteratorComments> |
| 161 | + { |
| 162 | + const container = $('#container'); |
| 163 | + const iterator = document.createNodeIterator( |
| 164 | + container, |
| 165 | + NodeFilter.SHOW_COMMENT, |
| 166 | + null, |
| 167 | + false |
| 168 | + ); |
| 169 | + |
| 170 | + const comments = []; |
| 171 | + let node; |
| 172 | + while (node = iterator.nextNode()) { |
| 173 | + comments.push(node.data.trim()); |
| 174 | + } |
| 175 | + |
| 176 | + // Should find all 6 comments, including those nested inside elements |
| 177 | + testing.expectEqual(6, comments.length); |
| 178 | + testing.expectEqual('comment1', comments[0]); |
| 179 | + testing.expectEqual('comment2', comments[1]); |
| 180 | + testing.expectEqual('comment3', comments[2]); |
| 181 | + testing.expectEqual('comment4', comments[3]); |
| 182 | + testing.expectEqual('comment5', comments[4]); |
| 183 | + testing.expectEqual('comment6', comments[5]); |
| 184 | + } |
| 185 | +</script> |
| 186 | + |
| 187 | +<script id=reactLikeScenario> |
| 188 | + { |
| 189 | + // Test a React-like scenario with comment markers |
| 190 | + const div = document.createElement('div'); |
| 191 | + div.innerHTML = ` |
| 192 | + <a href="/"> |
| 193 | + <!--$--> |
| 194 | + <svg viewBox="0 0 10 10"> |
| 195 | + <path d="M0,0 L10,10" /> |
| 196 | + </svg> |
| 197 | + <!--/$--> |
| 198 | + </a> |
| 199 | + `; |
| 200 | + |
| 201 | + const walker = document.createTreeWalker( |
| 202 | + div, |
| 203 | + NodeFilter.SHOW_COMMENT, |
| 204 | + null, |
| 205 | + false |
| 206 | + ); |
| 207 | + |
| 208 | + const comments = []; |
| 209 | + let node; |
| 210 | + while (node = walker.nextNode()) { |
| 211 | + comments.push(node.data); |
| 212 | + } |
| 213 | + |
| 214 | + // Should find both React markers even though they're nested inside <a> |
| 215 | + testing.expectEqual(2, comments.length); |
| 216 | + testing.expectEqual('$', comments[0]); |
| 217 | + testing.expectEqual('/$', comments[1]); |
| 218 | + } |
| 219 | +</script> |
0 commit comments