Skip to content
This repository has been archived by the owner on Sep 20, 2019. It is now read-only.

V1: instanceof operator isn't working for non-patched nodes. #623

Closed
blasten opened this issue Sep 19, 2016 · 7 comments
Closed

V1: instanceof operator isn't working for non-patched nodes. #623

blasten opened this issue Sep 19, 2016 · 7 comments
Assignees

Comments

@blasten
Copy link

blasten commented Sep 19, 2016

Using the v1, the following assertion fails:

assert.isTrue(document.documentElement instenceof HTMLElement);

/cc @justinfagnani

@blasten
Copy link
Author

blasten commented Sep 19, 2016

I also found that HTMLElement.ELEMENT_NODE is undefined.

@justinfagnani
Copy link
Contributor

The first issue is more or less unsolvable. Any element that has HTMLElement as its direct prototype can't be patched to make instanceof work.

The second issue is fixable and important. We need to copy statics down.

@shannonmoeller
Copy link

In case it's useful to others, my team came up with a workaround. It's far from perfect, but it worked well enough for our needs:

const div = document.createElement('div');

console.log((/^HTML\w*Element/).test(div.constructor.name));

Further, all of our custom element use a specific prefix, so we could extend the regex to:

/^(HTML|Foo)\w*Element/

@justinfagnani
Copy link
Contributor

For your custom elements, instanceof should work fine. The issue here is that the polyfilled HTMLElement's prototype isn't on the prototype chain of the built-in elements, but your custom element's prototype is fine.

Another option, which should be fine in basically all cases, is el.nodeType === Node.ELEMENT_NODE.

@justinfagnani justinfagnani self-assigned this Sep 21, 2016
@blasten
Copy link
Author

blasten commented Sep 22, 2016

thanks for the reply @justinfagnani

@notwaldorf
Copy link
Contributor

This sounds like a CustomElements polyfill issue so I'm moving it there, sorrynotsorry.

@notwaldorf
Copy link
Contributor

This issue was moved to webcomponents/custom-elements#48

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants