Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of customElements.getName() #254

Merged
merged 3 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cjs/interface/custom-element-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,17 @@ class CustomElementRegistry {
const info = this.registry.get(localName);
return info && info.Class;
}

/**
* @param {Function} Class **Class** of custom element
* @returns {string?} found tag name or null
*/
getName(Class) {
if (Classes.has(Class)) {
const { localName } = Classes.get(Class);
return localName;
}
return null;
}
}
exports.CustomElementRegistry = CustomElementRegistry
12 changes: 12 additions & 0 deletions esm/interface/custom-element-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,16 @@ export class CustomElementRegistry {
const info = this.registry.get(localName);
return info && info.Class;
}

/**
* @param {Function} Class **Class** of custom element
* @returns {string?} found tag name or null
*/
getName(Class) {
Copy link
Owner

@WebReflection WebReflection Dec 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is also good but I wasn't clear ... see indentation compared to get(...)

Screenshot from 2023-12-12 13-33-21

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O.k. -- I finally got it! :)

Indentation should be fixed now.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is also good but I wasn't clear ... see indentation compared to get(...)

Screenshot from 2023-12-12 13-33-21

if (Classes.has(Class)) {
const { localName } = Classes.get(Class);
return localName;
}
return null;
}
}
6 changes: 6 additions & 0 deletions test/interface/custom-element-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ catch (ok) {}
customElements.define('c-e', CE);
assert(customElements.get('c-e'), CE, 'correctly defined');

assert(customElements.getName(CE), 'c-e', 'correctly got Name');

class CEn extends HTMLElement {}

assert(customElements.getName(CEn), null, 'correctly report didn\'t find');

try {
customElements.define('c-e', class extends HTMLElement {});
assert(false, 'if a name has been taken, it cannot be redefined');
Expand Down
5 changes: 5 additions & 0 deletions types/esm/interface/custom-element-registry.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ export class CustomElementRegistry implements globalThis.CustomElementRegistry {
* @returns {Function?} the custom element **Class**, if any
*/
get(localName: string): Function | null;
/**
* @param {Function} Class **Class** of custom element
* @returns {string?} found tag name or null
*/
getName(Class: Function): string | null;
}
5 changes: 5 additions & 0 deletions types/interface/custom-element-registry.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ export class CustomElementRegistry implements globalThis.CustomElementRegistry {
* @returns {Function?} the custom element **Class**, if any
*/
get(localName: string): Function | null;
/**
* @param {Function} Class custom element **Class**
* @returns {string?} found custom element tag name or `null`
*/
getName(Class: Function): string | null;
}