document.contains / Node.contains polyfill?
dy opened this issue · 3 comments
Is there a polyfill for Node.contains? IE doesn’t support it so far.
It may look something like this:
Node.prototype.contains = function(el){
while (el = el.parentNode) {
if (el === this) return true;
}
return false;
}
Good call. I'll put a polyfill together.
Prepare yourself for slight disappointment, because contains
returns true if a node is a descendant of a given node or the node itself (and they didn't mention that). It's silly, but document.contains(document)
is true, and document.body.contains(document.body)
is true. You can try it for yourself, in Chrome or IE.
Internet Explorer has supported something like Element.prototype.contains
since IE5. In fact, in IE11 document.body.contains === HTMLElement.prototype.contains
is true.
So, the polyfill for Node.prototype.contains
targets document.contains
. I'm mentioning this because the polyfill for older IE will actually target Element
and Document
. Older IE cannot polyfill Node
because text nodes can't be polyfilled.
Ok, thanks for research.
@dfcreative it's in. thanks so much for finding this.