Skip to content
This repository was archived by the owner on Sep 20, 2019. It is now read-only.

Commit feef188

Browse files
committed
Fix bug in IE10 with MutationObserver
There is a difference in behavior between MutationObserver and this shim in IE10, causing the target for a MutationRecord to be different from the expect value. This happens because when you append a node with children in IE10 it'll call the DOMNodeInserted for the node and its children seperately. The test has an if statement because the behavior is different between IE10 and chrome, making both outcomes valid.
1 parent e2bac0f commit feef188

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

src/MutationObserver/MutationObserver.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,6 @@
524524
// Fall through.
525525
case 'DOMNodeInserted':
526526
// http://dom.spec.whatwg.org/#concept-mo-queue-childlist
527-
var target = e.relatedNode;
528527
var changedNode = e.target;
529528
var addedNodes, removedNodes;
530529
if (e.type === 'DOMNodeInserted') {
@@ -539,13 +538,13 @@
539538
var nextSibling = changedNode.nextSibling;
540539

541540
// 1.
542-
var record = getRecord('childList', target);
541+
var record = getRecord('childList', e.target.parentNode);
543542
record.addedNodes = addedNodes;
544543
record.removedNodes = removedNodes;
545544
record.previousSibling = previousSibling;
546545
record.nextSibling = nextSibling;
547546

548-
forEachAncestorAndObserverEnqueueRecord(target, function(options) {
547+
forEachAncestorAndObserverEnqueueRecord(e.relatedNode, function(options) {
549548
// 2.1, 3.2
550549
if (!options.childList)
551550
return;

tests/MutationObservers/childList.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,4 +378,25 @@ suite('JsMutationObserver childList', function() {
378378
});
379379
});
380380

381+
test('Append child in child', function() {
382+
var div = testDiv.appendChild(document.createElement('div'));
383+
384+
var observer = new JsMutationObserver(function() {});
385+
observer.observe(div, {
386+
childList: true,
387+
subtree: true
388+
});
389+
var div2 = document.createElement('div')
390+
var div3 = div2.appendChild(document.createElement('div'));
391+
div.appendChild(div2);
392+
var records = observer.takeRecords();
393+
394+
if(records.length == 1) {
395+
assert.strictEqual(records[0].target, div);
396+
assert.strictEqual(records[0].addedNodes[0].firstChild, div3);
397+
} else {
398+
assert.strictEqual(records[0].target, div);
399+
assert.strictEqual(records[1].target, div2);
400+
}
401+
});
381402
});

tests/MutationObservers/runner.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<script src="../tools/mocha-htmltest.js"></script>
1818

1919
<!-- MutationObserver -->
20+
<script src="../../src/WeakMap/WeakMap.js"></script>
2021
<script src="../../src/MutationObserver/MutationObserver.js"></script>
2122
<script>
2223

0 commit comments

Comments
 (0)