Skip to content

Commit 70dde49

Browse files
authored
fix: [#1945] Elements should only be upgraded to a custom element when the element is in the document (#1945)
1 parent 6070199 commit 70dde49

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

packages/happy-dom/src/nodes/html-element/HTMLElement.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ export default class HTMLElement extends Element {
985985
* @override
986986
* @see https://html.spec.whatwg.org/multipage/dom.html#htmlelement
987987
*/
988-
public override [PropertySymbol.connectedToNode](): void {
988+
public override [PropertySymbol.connectedToDocument](): void {
989989
const window = this[PropertySymbol.window];
990990
const localName = this[PropertySymbol.localName]!;
991991
const allCallbacks = window.customElements[PropertySymbol.callbacks];
@@ -1005,13 +1005,13 @@ export default class HTMLElement extends Element {
10051005
}
10061006
}
10071007

1008-
super[PropertySymbol.connectedToNode]();
1008+
super[PropertySymbol.connectedToDocument]();
10091009
}
10101010

10111011
/**
10121012
* @override
10131013
*/
1014-
public override [PropertySymbol.disconnectedFromNode](): void {
1014+
public override [PropertySymbol.disconnectedFromDocument](): void {
10151015
const window = this[PropertySymbol.window];
10161016
const localName = this[PropertySymbol.localName]!;
10171017
const allCallbacks = window.customElements[PropertySymbol.callbacks];
@@ -1033,7 +1033,7 @@ export default class HTMLElement extends Element {
10331033
}
10341034
}
10351035

1036-
super[PropertySymbol.disconnectedFromNode]();
1036+
super[PropertySymbol.disconnectedFromDocument]();
10371037
}
10381038

10391039
/**

packages/happy-dom/test/nodes/html-element/HTMLElement.test.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -715,12 +715,17 @@ describe('HTMLElement', () => {
715715
});
716716

717717
describe('[PropertySymbol.connectNode]()', () => {
718-
it('Waits for a custom element to be defined and replace it when it is.', () => {
718+
it("Waits for a custom element to be defined and replace it when it's connected to document", () => {
719719
const element = <HTMLElement>document.createElement('custom-element');
720720
const parent = document.createElement('div');
721721

722722
parent.appendChild(element);
723723

724+
expect(parent.children[0] instanceof HTMLElement).toBe(true);
725+
expect(Object.keys(window.customElements[PropertySymbol.callbacks]).length).toBe(0);
726+
727+
document.body.appendChild(parent);
728+
724729
expect(window.customElements[PropertySymbol.callbacks].get('custom-element')?.length).toBe(1);
725730

726731
parent.removeChild(element);
@@ -729,15 +734,13 @@ describe('HTMLElement', () => {
729734

730735
parent.appendChild(element);
731736

737+
expect(window.customElements[PropertySymbol.callbacks].get('custom-element')?.length).toBe(1);
738+
732739
window.customElements.define('custom-element', CustomElement);
733740

734741
expect(parent.children.length).toBe(1);
735742

736743
expect(parent.children[0] instanceof CustomElement).toBe(true);
737-
expect(parent.children[0].shadowRoot?.children.length).toBe(0);
738-
739-
document.body.appendChild(parent);
740-
741744
expect(parent.children[0].shadowRoot?.children.length).toBe(2);
742745
});
743746

0 commit comments

Comments
 (0)