From 36b2c8e1beb989e064ddb1f2668afa277ae5931e Mon Sep 17 00:00:00 2001 From: Martin Schitter Date: Mon, 11 Dec 2023 17:46:03 +0100 Subject: [PATCH] #1176@patch: Prevent custom element registration under a different name. --- .../happy-dom/src/custom-element/CustomElementRegistry.ts | 5 +++++ .../test/custom-element/CustomElementRegistry.test.ts | 5 +++++ 2 files changed, 10 insertions(+) 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);