-
Notifications
You must be signed in to change notification settings - Fork 378
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
Permit retrieval of registered classes from window.customElements #445
Comments
A couple questions:
|
It should probably also return the callbacks stored by the user agent... Or at least we should have the option to do that if we think it's useful at some point. |
I don't want to support |
To answer @domenic's question: I think the iterator would be generally useful for tools/extensions that want to list the elements which have been registered. But I don't have a pressing need for it right now. If the iterator is hard, it's by no means required for v1. |
Let's consider an example for the iterator: // somewhere else were defined MyElement1, MyElement2... and so on
window.customElements.define('my-element1', MyElement1);
window.customElements.define('my-element2', MyElement2);
// then, let's play with Iterator...
var alreadyDefined = window.customElements; //
for (var customElement of alreadyDefined) {
console.dir(customElement); // should I see here MyElement1, MyElement2?
} What about if somewhere else we've imported a custom element that registers some new custom elements? <link rel="import" href="myExamples.html"> <!-- here we define more custom elements? --> Remember that all imports don't have context. What are the pros and cons if have a table with all registered custom elements? var defined = window.customElements.definitions;
for (var customElement in defined) {
console.dir(customElement);
} And if check if an element already was registered. if (!window.customElements.definitions.getItem('my-element1')) {
// ...
} The above is following the convention given for Attributes. |
I don't think we can consider HTML imports as a use case given that nobody but Chrome wants to implement it. |
@annevk Let's not consider HTML imports, but let's consider what @JanMiksovsky proposed: |
@annevk Thanks a lot. |
I don't see any reason to create anything as complex as |
@domenic I see you noticed that
Then,
I think that to expose the list of defined custom elements should be used an extra attribute... something like window.customElements.definitions
// or
window.customElements.elements
// or something similar But...
|
I am supremely uninterested in the API shape. What I am interested in is use cases. It's probably not worth your time bikeshedding the former. |
Ok. The use cases will drive us to this conversation at some point. |
In case every standard element gets its own constructor as discussed in whatwg/html#896, wouldn't it make more sense to have a registry that includes both standard and custom elements? Sample use cases: const SUPPORTS_PICTURE = document.elements.has("picture");
const SUPPORTS_SMIL = document.elements.has("animate");
const SUPPORTS_MATHML = document.elements.has("math");
const SUPPORTS_MESH_GRADIENTS = document.elements.has("mesh"); |
@jarek-foksa can you explain what the use case for that is? If it's about figuring out support, you can do that just as easily by testing for |
@domenic This looks rather inconsistent to me: if (window.HTMLPictureElement) {
let picture = document.createElement("picture");
} I would prefer to have a choice of either constructor-based or name-based APIs that I could use consistently: // Name-based approach
if (document.elements.has("picture")) {
let picture = document.elements.create("picture");
}
// Constructor-based approach
if (window.HTMLPictureElement) {
let picture = new HTMLPictureElement();
} |
I don't see any need to introduce an entire new registry API surface just to fix some aesthetic inconsistency. |
I think that @domenic desires to see a very short way to check if a custom element was defined or not. What about if var check = document.querySelector('my-custom-element');
if (check) {
console.log('my-custom-element already defined');
} |
Is this needed for v1? |
It seems like this is a v2 feature although it has a very high benefit-to-cost ratio so I'd consider adding this earlier than other v2 features. |
Untagging needs-consensus as it seems like this has consensus at least in its basic form. I'll spec |
Please, let's summary. I'd like to understand the thread 😄 The current documentation states:
So,
Should you consider to name that function in a more specific way? window.getComputedStyle;
window.getSelection;
window.getMatchedCSSRules; Does have sense to name the function to |
It's window.customElements, not document or window. There's no getDefinition for now, just get. |
Ah 😄 excelente! |
This is included in whatwg/html#1012. |
\o/ |
Following up on issue #431, I'd like to propose that window.customElements allow a dev to retrieve information about the custom elements which have been registered. It sounds like @domenic was supportive of this idea, so as a strawman, I propose adding the following:
window.customElements.get(name)
. Returns the class which was previously registered withdefine(name)
, orundefined
if no element has yet been registered with that name.window.customElements[@@iterator]()
. This returns a newIterator
object that enumerates the[name, class]
pairs registered. I don't particularly care in what order the entries are returned. For consistency withMap.prototype[@@iterator]()
, it would be nice to return these in the order in which they were defined.This, at least would meet the needs I've encountered, chiefly being able to: 1) easily verify that a particular custom element has in fact been registered, 2) conditionally define an element if it hasn't already been registered. E.g.:
The text was updated successfully, but these errors were encountered: