Skip to content

Commit 5e830a1

Browse files
authored
fix: [#1803] Adds previousSibling and nextSibling to MutationObserver records when a child is removed
1 parent b257aa1 commit 5e830a1

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

packages/happy-dom/src/nodes/node/Node.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -548,10 +548,6 @@ export default class Node extends EventTarget {
548548
node = node[PropertySymbol.proxy];
549549
}
550550

551-
node[PropertySymbol.parentNode] = null;
552-
553-
node[PropertySymbol.clearCache]();
554-
555551
const index = this[PropertySymbol.nodeArray].indexOf(node);
556552

557553
if (index === -1) {
@@ -560,6 +556,13 @@ export default class Node extends EventTarget {
560556
);
561557
}
562558

559+
const previousSibling = node.previousSibling;
560+
const nextSibling = node.nextSibling;
561+
562+
node[PropertySymbol.parentNode] = null;
563+
564+
node[PropertySymbol.clearCache]();
565+
563566
this[PropertySymbol.nodeArray].splice(index, 1);
564567

565568
if (node[PropertySymbol.nodeType] === NodeTypeEnum.elementNode) {
@@ -590,7 +593,9 @@ export default class Node extends EventTarget {
590593
new MutationRecord({
591594
target: this[PropertySymbol.proxy] || this,
592595
type: MutationTypeEnum.childList,
593-
removedNodes: [node]
596+
removedNodes: [node],
597+
previousSibling,
598+
nextSibling
594599
})
595600
);
596601

packages/happy-dom/test/mutation-observer/MutationObserver.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,25 @@ describe('MutationObserver', () => {
196196
const div = document.createElement('div');
197197
const span = document.createElement('span');
198198
const article = document.createElement('article');
199+
const h1 = document.createElement('h1');
200+
const h2 = document.createElement('h2');
201+
const h3 = document.createElement('h3');
202+
const h4 = document.createElement('h4');
199203
const text = document.createTextNode('old');
200204
const observer = new window.MutationObserver((mutationRecords) => {
201205
records.push(mutationRecords);
202206
});
203207
observer.observe(div, { subtree: true, childList: true });
204208
div.appendChild(text);
205209
div.appendChild(span);
210+
article.appendChild(h1);
211+
article.appendChild(h2);
212+
article.appendChild(h3);
213+
article.appendChild(h4);
206214
span.appendChild(article);
215+
article.removeChild(h2);
216+
article.removeChild(h4);
217+
article.removeChild(h1);
207218
span.removeChild(article);
208219

209220
await new Promise((resolve) => setTimeout(resolve, 1));
@@ -243,6 +254,39 @@ describe('MutationObserver', () => {
243254
target: span,
244255
type: 'childList'
245256
},
257+
{
258+
addedNodes: [],
259+
attributeName: null,
260+
attributeNamespace: null,
261+
nextSibling: h3,
262+
oldValue: null,
263+
previousSibling: h1,
264+
removedNodes: [h2],
265+
target: article,
266+
type: 'childList'
267+
},
268+
{
269+
addedNodes: [],
270+
attributeName: null,
271+
attributeNamespace: null,
272+
nextSibling: null,
273+
oldValue: null,
274+
previousSibling: h3,
275+
removedNodes: [h4],
276+
target: article,
277+
type: 'childList'
278+
},
279+
{
280+
addedNodes: [],
281+
attributeName: null,
282+
attributeNamespace: null,
283+
nextSibling: h3,
284+
oldValue: null,
285+
previousSibling: null,
286+
removedNodes: [h1],
287+
target: article,
288+
type: 'childList'
289+
},
246290
{
247291
addedNodes: [],
248292
attributeName: null,

0 commit comments

Comments
 (0)