-
Notifications
You must be signed in to change notification settings - Fork 295
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
Provide getAttributeNames() which returns an Array<DOMString> #115
Comments
That sounds pretty nice, not unlike My biggest question mark is around namespaces, without which this would be a simple API. Because of them, one can't return an array of strings where each string points to a unique attribute: var e = document.createElement('span');
e.setAttributeNS('http://a.example.com/', 'foo', 'bar');
e.setAttributeNS('http://b.example.com/', 'foo', 'baz');
alert(e.getAttribute('foo'));
e.removeAttribute('foo');
alert(e.getAttribute('foo')); I'm sure you could come up with a crazy case involving prefixes too. Should the API throw an exception if any of the attributes are in a namespace, or return a tuple of strings instead of a string for those cases? |
I think we should just return the qualified names, those are the property names NamedNodeMap already exposes and what most algorithms operate on. If you just stick to namespaceless attributes as you should, there will be no issue. |
With qualified names you could still have the same string appear multiple times, do we just live with that? It would be nice if one didn't have to resort to |
Yeah, and to be fair, for mutation observers we do care about all attributes. Perhaps we should return an array of two-item-arrays ([namespace, localName])? |
That would certainly do the job, although it's unfortunate to waste memory on those arrays for the vast majority of attributes that aren't in a namespace. A mixture of strings and arrays isn't exactly perfect, either. Any preferences, @esprehn? |
The alternative is to continue with precedent: |
Joy indeed, but that would probably work. One would lose the order between the two sets of attributes, but I'm not sure if that could possibly matter. |
To clarify, with the |
The naming conflict issue is already there when using |
@annevk clarified |
I'd prefer just |
I'd be OK with that, if the spec has a note saying that it's not guaranteed that the values will be unique, so you can't use it to implement something like Live DOM Viewer. |
Yeah, this all sounds good. @smaug----, any concerns? |
So getAttributeNames() would return a new sequence each time? It is just that .attributes doesn't change, so fewer new objects there. Would something like But I don't object getAttributeNames(). |
I went with |
@annevk Return empty sequence when element has no any attributes is implicitly or should be explicitly defined in prose (like here: https://url.spec.whatwg.org/#dom-urlsearchparams-getall, https://fetch.spec.whatwg.org/#dom-headers-getall, https://xhr.spec.whatwg.org/#dom-formdata-getall)? |
Thank you! |
Element.prototype.attributes requires allocating all the Attr node instances and the NamedNodeMap itself which is expensive, lots of frameworks (ex. Polymer, Angular, others) just want to iterate the attributes at construction time of an element or when traversing the document and don't need these expensive persistent structures.
Instead we should provide getAttributeNames(), then authors can use that with getAttribute/setAttribute/removeAttribute which results in better memory usage and performance.
This would also get developers to stop using .attributes which means the usage of the exotic Node like thing would go down possibly allowing the removal of it in the future.
@domenic @foolip
The text was updated successfully, but these errors were encountered: