Bug 1829335 - Add customElements.getName. r=smaug

This adds the new customElements.getName method which takes a constructor
and returns string if the definition has been registered, otherwise null.

Differential Revision: https://phabricator.services.mozilla.com/D181646
This commit is contained in:
keithamus 2023-06-27 09:38:01 +00:00
parent d011b4ba61
commit b2af7cafb3
5 changed files with 18 additions and 10 deletions

View File

@ -1195,6 +1195,19 @@ void CustomElementRegistry::Get(
aRetVal.SetAsCustomElementConstructor() = data->mConstructor;
}
void CustomElementRegistry::GetName(JSContext* aCx,
CustomElementConstructor& aConstructor,
nsAString& aResult) {
CustomElementDefinition* aDefinition =
LookupCustomElementDefinition(aCx, aConstructor.CallableOrNull());
if (aDefinition) {
aDefinition->mType->ToString(aResult);
} else {
aResult.SetIsVoid(true);
}
}
already_AddRefed<Promise> CustomElementRegistry::WhenDefined(
const nsAString& aName, ErrorResult& aRv) {
// Define a function that lazily creates a Promise and perform some action on

View File

@ -557,6 +557,9 @@ class CustomElementRegistry final : public nsISupports, public nsWrapperCache {
void Get(const nsAString& name,
OwningCustomElementConstructorOrUndefined& aRetVal);
void GetName(JSContext* aCx, CustomElementConstructor& aConstructor,
nsAString& aResult);
already_AddRefed<Promise> WhenDefined(const nsAString& aName,
ErrorResult& aRv);

View File

@ -180,7 +180,7 @@ DOMInterfaces = {
},
'CustomElementRegistry': {
'implicitJSContext': ['define'],
'implicitJSContext': ['define', 'getName'],
},
'DebuggerNotification': {

View File

@ -11,6 +11,7 @@ interface CustomElementRegistry {
[ChromeOnly, Throws]
undefined setElementCreationCallback(DOMString name, CustomElementCreationCallback callback);
(CustomElementConstructor or undefined) get(DOMString name);
DOMString? getName(CustomElementConstructor constructor);
[Throws]
Promise<CustomElementConstructor> whenDefined(DOMString name);
[CEReactions] undefined upgrade(Node root);

View File

@ -1,9 +0,0 @@
[CustomElementRegistry.html]
[customElements.getName must return null when the registry does not contain an entry with the given constructor]
expected: FAIL
[customElements.getName returns the name of the entry with the given constructor when there is a matching entry.]
expected: FAIL
[customElements.getName returns the name of the entry with the given customized built in constructor when there is a matching entry.]
expected: FAIL