Skip to content

Commit

Permalink
Re implment the CustomElementRegistry interface
Browse files Browse the repository at this point in the history
  • Loading branch information
startracex committed Dec 27, 2024
1 parent 10f8e07 commit 9a963a0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/custom-elements/ts_src/CustomElementRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ export default class CustomElementRegistry {
return undefined;
}

getName(constructor: ElementConstructor): string | null {
const definition = this._constructorToDefinition.get(constructor);
return definition ? definition.localName : null;
}

whenDefined(localName: string): Promise<void> {
if (!Utilities.isValidCustomElementName(localName)) {
return Promise.reject(
Expand Down Expand Up @@ -363,4 +368,6 @@ CustomElementRegistry.prototype['polyfillDefineLazy'] =
CustomElementRegistry.prototype.polyfillDefineLazy;
CustomElementRegistry.prototype['polyfillWrapFlushCallback'] =
CustomElementRegistry.prototype.polyfillWrapFlushCallback;
CustomElementRegistry.prototype['getName'] =
CustomElementRegistry.prototype.getName;
/* eslint-enable no-self-assign */
17 changes: 17 additions & 0 deletions packages/tests/custom-elements/html/registry.test.html
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,23 @@
});
});

suite('getName', function () {
test('returns the local name for a defined custom element constructor', function () {
class XGetName extends HTMLElement {}
customElements.define('x-get-name', XGetName);

var name = customElements.getName(XGetName);
assert.equal(name, 'x-get-name');
});

test('returns null for an undefined custom element constructor', function () {
class XUndefinedElement extends HTMLElement {}

var name = customElements.getName(XUndefinedElement);
assert.isNull(name);
});
});

suite('whenDefined', function () {
test('resolves when a tag is defined', function () {
var promise = customElements
Expand Down

0 comments on commit 9a963a0

Please sign in to comment.