-
Notifications
You must be signed in to change notification settings - Fork 22.8k
document.createElement returns an HTMLElement #7136
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
Conversation
Preview URLsFlawsURL:
External URLsURL: No new external URLs |
Chrome still returns |
Thanks @jpmedley - a quick sanity check and FF returns HTMLElement. The question is, how do we find out when browsers updated this? Maybe we add a note to BCD explaining that Chrome returns |
@Rumyra Here's the PR for that. |
The return type in Web IDL is Here's a snipped where the returned var doc = document.implementation.createDocument(null, null, null); // or new Document()
var el = doc.createElement('bla');
// Object.getPrototypeOf(el) is Element.prototype I believe this is ancient behavior. |
@@ -30,7 +30,7 @@ <h3 id="Parameters">Parameters</h3> | |||
|
|||
<h3 id="Return_value">Return value</h3> | |||
|
|||
<p>The new {{domxref("Element")}}.</p> | |||
<p>The new {{domxref("HTMLElement")}}.</p> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even though the return type is accurately described as Element
, it would be useful to document when an HTMLElement
is returned, and that this is the case that web developers will see 99.9% of the time.
@foolip Good catch. This is something we should document completely. |
@foolip Interesting. So what the spec says is actually not what practically all browser developers experience. They don't deal with different types of "documents", only It seems the spec covers cases for other engines that aren't Firefox, Chrome, Safari, or Edge (which ones?). This is so much the case, that even the official TypeScript type definition returns To follow the spec? Or to follow what we all see in practice? That is the question. (It seems the latter is more useful). |
This is a reference for web developers explaining what the web does, not a reference for browser implementors explaining what it should do. That's what specs are for. |
I'm not sure what would lead you to this conclusion. It's more likely that browser vendors haven't gotten around to updating their implementations for one reason or another. It's also possible that someone was supposed to update the spec and didn't. I would suggest opening an issue on the spec repo itself. The editors of the spec should be able to clear this up. |
Maybe there's more than meets the eye here, but AFAICT there isn't anything funny going on, I believe browsers match the spec. Since It seems to me the TypeScript bindings are wrong here. Maybe the return type could be made to depend on the type of |
Pinging @sandersn since he reviewed changes on this part of the TypeScript code base |
I would like to - as soon as I get 5 mins - close this PR in favour for the explanation @foolip has given to be included on this page. |
@orta has thought about this particular problem more than I have, but to quote two early comments:
Typescript aims for the 99.9% case here, not spec compatibility. That's why it intentionally returns HTMLElement even though it won't be correct sometimes. Edit: For Typescript, the penalty for returning a subclass is low: the people who find themselves in the 0.01% case will see extraneous HTMLElement properties in completions when they should see only the Element ones. |
Web developers don't come here to learn what's supposed to happen. They come here to learn what actually happens. |
@jpmedley this isn't a spec-fiction vs. browser-reality situation, it's an issue of With the code snippet in #7136 (comment), what actually happens is that Chrome, Firefox and Safari return an MDN could say that an |
Yeah, just confirming @sandersn - we chose HTMLElement for a few API cases like this which are specifically not conforming to the spec but conform to what happens nearly all the time. We have tried to switch down to Element but use generics to default to HTMLElement ( microsoft/TypeScript-DOM-lib-generator#885 ) but found that the results would really make JS folks lives worse microsoft/TypeScript#4689 (comment) IMO #7136 (comment) is probably the right take 👍🏻 |
Thanks for chiming in and providing the rationale behind the decisions made in TypeScript 🚀 |
Closing this PR in favour of this one #7971 which adds a note based on this discussion 👍 |
document.createElement
more accurately returns anHTMLElement
instances.document.createElementNS
is needed (unfortunately) for non-HTMLElement
types, likeSVGElement
.The built-in type definition in TypeScript also returns
HTMLElement
.