From 1665e056eaa982131383c4b8af31024b90f9d9dd Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Fri, 5 Aug 2022 12:26:01 -0400 Subject: [PATCH] Idempotent Custom Element registration If the package is accidentally imported a second time, the Custom Element registrations will raise an exception like: ``` NotSupportedError: Cannot define multiple custom elements with the same tag name ``` This commit adds a guard that checks whether or not the element has been registered through a call to [CustomElementRegistry.get][]. When present, the redundant call to [CustomElementRegistry.define][] is skipped. [CustomElementRegistry.get]: https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry/get [CustomElementRegistry.define]: https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry/define --- src/trix/core/helpers/custom_elements.coffee | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/trix/core/helpers/custom_elements.coffee b/src/trix/core/helpers/custom_elements.coffee index 2789a6c7b..3cef09007 100644 --- a/src/trix/core/helpers/custom_elements.coffee +++ b/src/trix/core/helpers/custom_elements.coffee @@ -78,7 +78,8 @@ registerElement = do -> Object.setPrototypeOf(constructor.prototype, HTMLElement.prototype) Object.setPrototypeOf(constructor, HTMLElement) Object.defineProperties(constructor.prototype, properties) - window.customElements.define(tagName, constructor) + unless window.customElements.get(tagName) + window.customElements.define(tagName, constructor) constructor else (tagName, properties) ->