Skip to content

Commit 273ad6c

Browse files
lukeedcapricorn86
andauthored
fix: Node.replaceWith does not throw w/o parent (#1969)
* fix: Node.replaceWith does not throw w/o parent ```js let div = document.createElement("div"); let span = document.createElement("span"); div.parentNode; // null span.parentNode; // null div.replaceWith(span); // ok ``` * chore: remove unused DOMException import * chore: [#0] Adds unit test --------- Co-authored-by: David Ortner <david@ortner.se>
1 parent 7c44f48 commit 273ad6c

2 files changed

Lines changed: 5 additions & 2 deletions

File tree

packages/happy-dom/src/nodes/child-node/ChildNodeUtility.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import DOMException from '../../exception/DOMException.js';
21
import * as PropertySymbol from '../../PropertySymbol.js';
32
import Node from '../node/Node.js';
43
import type IParentNode from '../parent-node/IParentNode.js';
@@ -29,7 +28,7 @@ export default class ChildNodeUtility {
2928
const parent = <IParentNode>childNode[PropertySymbol.parentNode];
3029

3130
if (!parent) {
32-
throw new DOMException('This element has no parent node.');
31+
return;
3332
}
3433

3534
for (const node of nodes) {

packages/happy-dom/test/nodes/element/Element.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,10 @@ describe('Element', () => {
12031203
element.replaceWith(node1, node2);
12041204
expect(isCalled).toBe(true);
12051205
});
1206+
1207+
it('Should not throw when there is no parent node.', () => {
1208+
document.createElement('div').replaceWith(document.createElement('div'));
1209+
});
12061210
});
12071211

12081212
describe('before()', () => {

0 commit comments

Comments
 (0)