diff --git a/packages/happy-dom/src/custom-element/CustomElementRegistry.ts b/packages/happy-dom/src/custom-element/CustomElementRegistry.ts index 4b37b481e..b7af253ae 100644 --- a/packages/happy-dom/src/custom-element/CustomElementRegistry.ts +++ b/packages/happy-dom/src/custom-element/CustomElementRegistry.ts @@ -65,6 +65,11 @@ export default class CustomElementRegistry { throw new DOMException(`Custom Element: "${localName}" already defined.`); } + const otherName = this.getName(elementClass); + if (otherName) { + throw new DOMException(`Custom Element already defined as "${otherName}".`); + } + this._registry[localName] = { elementClass, extends: options && options.extends ? options.extends.toLowerCase() : null diff --git a/packages/happy-dom/test/custom-element/CustomElementRegistry.test.ts b/packages/happy-dom/test/custom-element/CustomElementRegistry.test.ts index d6baf08fe..fc2b7020e 100644 --- a/packages/happy-dom/test/custom-element/CustomElementRegistry.test.ts +++ b/packages/happy-dom/test/custom-element/CustomElementRegistry.test.ts @@ -63,6 +63,11 @@ describe('CustomElementRegistry', () => { expect(() => customElements.define('custom-element', CustomElement)).toThrow(); }); + it('Throws an error if already registered under a different tag name.', () => { + customElements.define('custom-element', CustomElement); + expect(() => customElements.define('custom-element2', CustomElement)).toThrow(); + }); + it('Calls observed attributes and set _observedAttributes as a property on the element class.', () => { customElements.define('custom-element', CustomElement); expect(CustomElement.observedAttributesCallCount).toBe(1);