-
Notifications
You must be signed in to change notification settings - Fork 12.8k
XML getElementsByTagName returns Node #3613
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
Comments
As per the W3C DOM Level 3, NodeList is an array-like structure containing items of Node. And Node does not contain The easiest way to deal with this, because you know something that can't be derived from the code, is to use a cast: let xml: XMLDocument;
let element = <Element> xml.getElementsByTagName('test')[0];
element.getAttribute('attr'); /* no error */ |
@kitsonk Oh, man, I know how it's per spec, but I am a bit tired of such answers here. It's not a question how to solve this problem, I understand how to cast types. Problem is that Plus do not miss this:
|
And please, as per spec, document.getElementsByTagName('a')[0] instanceof Node &&
document.getElementsByTagName('a')[0] instanceof Element So returning |
If you think I am affiliated with TypeScript, you are wrong. I am trying to point out though that your expectations do not align to the specs, so I am surprised you think that TypeScript shouldn't follow the specs and do what you think it should do, versus what the spec says. I even gave you a working piece of code that gets around the issue. So to be honest, I am "tired of such" attitude for just trying to help. Why do you think TypeScript should magically ignore the spec for you? Yes, you are right that There are several other issues that talk about the usability of the As far as |
Note that the DOM spec is written in a language called WebIDL. The mapping of WebIDL to ECMAScript is defined in WebIDL spec, but is undefined for other languages. So it is up to TS to decide how to map from DOM spec. Also note that some WebIDL features are not possible in TS currently, such as overriding a member in a sub interface. As to |
@duanyao that is a fair point about As far as the WebIDL, if I understand correctly, lib.d.ts is auto-generated from the internal implementation of IE's ECMAScript engine, which is based on Chakra's implementation of the DOM spec. Ergo, I suspect Chakra is returning a |
@kitsonk Note that WebIDL has no generics. AFIW |
@kitsonk You know, after your answers here I really do not want to report any bugs here to TypeScript. If you are tired here, then please, do not answer here, it's simple. You was wrong here but was arguing with such rage like only you can be right. It's so obvious that getElementsByTagName returns Elements. Treating the spec so literally it's like junior Police man (or any such position, like moderator somewhere) treats rules literally word-to-word. It's of course wrong, their job is to be people, so please, be people here. |
If you don't mind, I'd like to leave this open so that we can fix the bug? |
@zhengbli can you take a look. |
I agree that it is safe to assume that |
and |
I agree this should not be throwing an error. Example code
Error thrown Property 'classList' does not exist on type 'Node'.
I understand this is because it is not defined in the lib.d.ts file but this is the is related to the issue from OP. |
@NekR give the latest library a test and let us know if the issue has been solved. |
@mhegazy it seems to work fine. Thanks! |
Hi,
I understand all concerns around HTML/SVG elements with
querySelector()
, etc. But why method which should returnElement
cannot even returnElement
but returnsNode
? Do you think it's possible to query processing instructing withgetElementsByTagName()
? I know it'sNodeList
returned by that method, but NodeList does not mean that it always should contains onlyNode
class, not its subclasses.This code won't work:
Also, fine thing here is that
Node
has methodhasAttributes()
.The text was updated successfully, but these errors were encountered: