| OLD | NEW |
| (Empty) |
| 1 <html> | |
| 2 <link rel="import" href="../resources/chai.html" /> | |
| 3 <link rel="import" href="../resources/mocha.html" /> | |
| 4 <script> | |
| 5 describe('MutationObserver.observe on childList', function() { | |
| 6 it('should handle basic observation', function(done) { | |
| 7 var div; | |
| 8 var observer; | |
| 9 var mutations; | |
| 10 var removedDiv1, removedDiv2; | |
| 11 | |
| 12 function start() { | |
| 13 div = document.createElement('div'); | |
| 14 observer = new MutationObserver(function(m) { | |
| 15 mutations = m; | |
| 16 }); | |
| 17 | |
| 18 observer.observe(div, { childList: true }); | |
| 19 removedDiv1 = div.appendChild(document.createElement('div')); | |
| 20 setTimeout(checkDisconnectAndMutate, 0); | |
| 21 } | |
| 22 | |
| 23 function checkDisconnectAndMutate() { | |
| 24 // ...can childList changes be observed at all. | |
| 25 | |
| 26 assert.equal(mutations.length, 1); | |
| 27 assert.equal(mutations[0].type, "childList"); | |
| 28 assert.equal(mutations[0].addedNodes.length, 1); | |
| 29 assert.equal(mutations[0].addedNodes[0], removedDiv1); | |
| 30 | |
| 31 mutations = null; | |
| 32 observer.disconnect(); | |
| 33 removedDiv1 = div.appendChild(document.createElement('div')); | |
| 34 setTimeout(checkNotDeliveredAndMutateMultiple, 0); | |
| 35 } | |
| 36 | |
| 37 function checkNotDeliveredAndMutateMultiple() { | |
| 38 // ...observer.disconnect() should prevent further delivery of mutations. | |
| 39 | |
| 40 assert.equal(mutations, null); | |
| 41 observer.observe(div, { childList: true }); | |
| 42 removedDiv1 = div.removeChild(div.firstChild); | |
| 43 removedDiv2 = div.removeChild(div.firstChild); | |
| 44 setTimeout(finish); | |
| 45 } | |
| 46 | |
| 47 function finish() { | |
| 48 // ...re-observing after disconnect works with the same observer. | |
| 49 | |
| 50 assert.equal(mutations.length, 2); | |
| 51 assert.equal(mutations[0].type, "childList"); | |
| 52 assert.equal(mutations[0].removedNodes.length, 1); | |
| 53 assert.equal(mutations[0].removedNodes[0], removedDiv1); | |
| 54 assert.equal(mutations[1].type, "childList"); | |
| 55 assert.equal(mutations[1].removedNodes.length, 1); | |
| 56 assert.equal(mutations[1].removedNodes[0], removedDiv2); | |
| 57 observer.disconnect(); | |
| 58 done(); | |
| 59 } | |
| 60 | |
| 61 start(); | |
| 62 }); | |
| 63 | |
| 64 it('should handle observing without specifying "childList" does not result in
hearing about childList changes', function(done) { | |
| 65 var div; | |
| 66 var observer; | |
| 67 var mutations; | |
| 68 | |
| 69 function start() { | |
| 70 div = document.createElement('div'); | |
| 71 observer = new MutationObserver(function(m) { | |
| 72 mutations = m; | |
| 73 }); | |
| 74 | |
| 75 observer.observe(div, { attributes: true, characterData: true }); | |
| 76 div.appendChild(document.createElement('div')); | |
| 77 setTimeout(finish, 0); | |
| 78 } | |
| 79 | |
| 80 function finish() { | |
| 81 assert.equal(mutations, null); | |
| 82 observer.disconnect(); | |
| 83 done(); | |
| 84 } | |
| 85 | |
| 86 start(); | |
| 87 }); | |
| 88 | |
| 89 it('should handle re-observing the same node with the same observer has the ef
fect of resetting the options', function(done) { | |
| 90 var div; | |
| 91 var observer; | |
| 92 var mutations; | |
| 93 var calls = 0; | |
| 94 | |
| 95 function start() { | |
| 96 div = document.createElement('div'); | |
| 97 observer = new MutationObserver(function(m) { | |
| 98 mutations = m; | |
| 99 calls++; | |
| 100 }); | |
| 101 | |
| 102 observer.observe(div, { childList: true, characterData: true }); | |
| 103 observer.observe(div, { childList: true }); | |
| 104 div.appendChild(document.createElement('div')); | |
| 105 setTimeout(checkDisconnectAndMutate, 0); | |
| 106 } | |
| 107 | |
| 108 function checkDisconnectAndMutate() { | |
| 109 assert.equal(calls, 1); | |
| 110 assert.equal(mutations.length, 1); | |
| 111 assert.equal(mutations[0].type, "childList"); | |
| 112 mutations = null; | |
| 113 observer.observe(div, { childList: true, characterData: true }); | |
| 114 observer.observe(div, { attributes: true }); | |
| 115 div.appendChild(document.createElement('div')); | |
| 116 setTimeout(finish, 0); | |
| 117 } | |
| 118 | |
| 119 function finish() { | |
| 120 assert.equal(mutations, null); | |
| 121 observer.disconnect(); | |
| 122 done(); | |
| 123 } | |
| 124 | |
| 125 start(); | |
| 126 }); | |
| 127 | |
| 128 it('should handle multiple observers can be registered to a given node and bot
h receive mutations', function(done) { | |
| 129 var div; | |
| 130 var observer; | |
| 131 var observer2; | |
| 132 var mutations; | |
| 133 var mutations2; | |
| 134 | |
| 135 function start() { | |
| 136 div = document.createElement('div'); | |
| 137 observer = new MutationObserver(function(m) { | |
| 138 mutations = m; | |
| 139 }); | |
| 140 observer2 = new MutationObserver(function(m) { | |
| 141 mutations2 = m; | |
| 142 }); | |
| 143 observer.observe(div, { childList: true }); | |
| 144 observer2.observe(div, { childList: true }); | |
| 145 div.appendChild(document.createElement('div')); | |
| 146 setTimeout(finish, 0); | |
| 147 } | |
| 148 | |
| 149 function finish() { | |
| 150 assert.equal(mutations.length, 1); | |
| 151 assert.equal(mutations[0].type, "childList"); | |
| 152 assert.equal(mutations2.length, 1); | |
| 153 assert.equal(mutations2[0].type, "childList"); | |
| 154 observer.disconnect(); | |
| 155 observer2.disconnect(); | |
| 156 done(); | |
| 157 } | |
| 158 | |
| 159 start(); | |
| 160 }); | |
| 161 | |
| 162 it('should create minimal mutations for replaceChild', function(done) { | |
| 163 var div; | |
| 164 var observer; | |
| 165 var fragment; | |
| 166 var mutations; | |
| 167 var addedDiv1; | |
| 168 var addedDiv2; | |
| 169 var removedDiv1; | |
| 170 | |
| 171 function start() { | |
| 172 div = document.createElement('div'); | |
| 173 | |
| 174 var first = document.createElement('span'); | |
| 175 first.textContent = 'Foo'; | |
| 176 div.appendChild(first); | |
| 177 | |
| 178 var second = document.createElement('div'); | |
| 179 second.textContent = 'Bar'; | |
| 180 div.appendChild(second); | |
| 181 | |
| 182 removedDiv1 = div.firstChild; | |
| 183 | |
| 184 observer = new MutationObserver(function(m) { | |
| 185 mutations = m; | |
| 186 }); | |
| 187 | |
| 188 observer.observe(div, { childList: true }); | |
| 189 addedDiv1 = document.createElement('div'); | |
| 190 div.replaceChild(addedDiv1, div.firstChild); | |
| 191 setTimeout(checkReplaceWithNode, 0); | |
| 192 } | |
| 193 | |
| 194 function checkReplaceWithNode() { | |
| 195 // ...simple replace child | |
| 196 | |
| 197 assert.equal(mutations.length, 1); | |
| 198 assert.equal(mutations[0].type, "childList"); | |
| 199 assert.equal(mutations[0].addedNodes.length, 1); | |
| 200 assert.equal(mutations[0].addedNodes[0], addedDiv1); | |
| 201 assert.equal(mutations[0].removedNodes.length, 1); | |
| 202 assert.equal(mutations[0].removedNodes[0], removedDiv1); | |
| 203 | |
| 204 mutations = null; | |
| 205 fragment = document.createDocumentFragment(); | |
| 206 addedDiv1 = fragment.appendChild(document.createElement('div')); | |
| 207 addedDiv2 = fragment.appendChild(document.createElement('div')); | |
| 208 removedDiv1 = div.firstChild; | |
| 209 | |
| 210 div.replaceChild(fragment, removedDiv1); | |
| 211 | |
| 212 setTimeout(finish, 0); | |
| 213 } | |
| 214 | |
| 215 function finish() { | |
| 216 // ...replace with DocumentFragment. | |
| 217 | |
| 218 assert.equal(mutations.length, 1); | |
| 219 assert.equal(mutations[0].type, "childList"); | |
| 220 assert.equal(mutations[0].addedNodes.length, 2); | |
| 221 assert.equal(mutations[0].addedNodes[0], addedDiv1); | |
| 222 assert.equal(mutations[0].addedNodes[1], addedDiv2); | |
| 223 assert.equal(mutations[0].removedNodes.length, 1); | |
| 224 assert.equal(mutations[0].removedNodes[0], removedDiv1); | |
| 225 | |
| 226 observer.disconnect(); | |
| 227 done(); | |
| 228 } | |
| 229 | |
| 230 start(); | |
| 231 }); | |
| 232 | |
| 233 it('should create minimal mutations for insertBefore', function(done) { | |
| 234 var div; | |
| 235 var observer; | |
| 236 var fragment; | |
| 237 var mutations; | |
| 238 var addedDiv1; | |
| 239 var addedDiv2; | |
| 240 | |
| 241 function start() { | |
| 242 div = document.createElement('div'); | |
| 243 | |
| 244 var first = document.createElement('span'); | |
| 245 first.textContent = 'Foo'; | |
| 246 div.appendChild(first); | |
| 247 | |
| 248 fragment = document.createDocumentFragment(); | |
| 249 addedDiv1 = fragment.appendChild(document.createElement('div')); | |
| 250 addedDiv2 = fragment.appendChild(document.createElement('div')); | |
| 251 | |
| 252 observer = new MutationObserver(function(m) { | |
| 253 mutations = m; | |
| 254 }); | |
| 255 | |
| 256 observer.observe(div, { childList: true }); | |
| 257 div.insertBefore(fragment, div.firstChild); | |
| 258 setTimeout(finish, 0); | |
| 259 } | |
| 260 | |
| 261 | |
| 262 function finish() { | |
| 263 assert.equal(mutations.length, 1); | |
| 264 assert.equal(mutations[0].type, "childList"); | |
| 265 assert.equal(mutations[0].addedNodes.length, 2); | |
| 266 assert.equal(mutations[0].addedNodes[0], addedDiv1); | |
| 267 assert.equal(mutations[0].addedNodes[1], addedDiv2); | |
| 268 assert.equal(mutations[0].removedNodes.length, 0); | |
| 269 | |
| 270 observer.disconnect(); | |
| 271 done(); | |
| 272 } | |
| 273 | |
| 274 start(); | |
| 275 }); | |
| 276 | |
| 277 it('should create minimal mutations for appendChild', function(done) { | |
| 278 var div; | |
| 279 var observer; | |
| 280 var fragment; | |
| 281 var mutations; | |
| 282 var addedDiv1; | |
| 283 var addedDiv2; | |
| 284 | |
| 285 function start() { | |
| 286 div = document.createElement('div'); | |
| 287 | |
| 288 var first = document.createElement('span'); | |
| 289 first.textContent = 'Foo'; | |
| 290 div.appendChild(first); | |
| 291 | |
| 292 fragment = document.createDocumentFragment(); | |
| 293 addedDiv1 = fragment.appendChild(document.createElement('div')); | |
| 294 addedDiv2 = fragment.appendChild(document.createElement('div')); | |
| 295 | |
| 296 observer = new MutationObserver(function(m) { | |
| 297 mutations = m; | |
| 298 }); | |
| 299 | |
| 300 observer.observe(div, { childList: true }); | |
| 301 div.appendChild(fragment); | |
| 302 setTimeout(finish, 0); | |
| 303 } | |
| 304 | |
| 305 function finish() { | |
| 306 assert.equal(mutations.length, 1); | |
| 307 assert.equal(mutations[0].type, "childList"); | |
| 308 assert.equal(mutations[0].addedNodes.length, 2); | |
| 309 assert.equal(mutations[0].addedNodes[0], addedDiv1); | |
| 310 assert.equal(mutations[0].addedNodes[1], addedDiv2); | |
| 311 assert.equal(mutations[0].removedNodes.length, 0); | |
| 312 | |
| 313 observer.disconnect(); | |
| 314 done(); | |
| 315 } | |
| 316 | |
| 317 start(); | |
| 318 }); | |
| 319 }); | |
| 320 </script> | |
| 321 </body> | |
| 322 </html> | |
| OLD | NEW |