utils: Avoid Node#contains for IE 11 support

Bug: T260061
Change-Id: Ifd1e9067ab16a65349a460e935bb95f5895432db
This commit is contained in:
Bartosz Dziewoński 2020-08-10 21:37:42 +02:00
parent 5c41597eb5
commit 34fb7c6497

View file

@ -19,6 +19,22 @@ function childIndexOf( child ) {
return i;
}
/**
* Check whether a Node contains (is an ancestor of) another Node (or is the same node)
*
* @param {Node} ancestor
* @param {Node} descendant
* @return {boolean}
*/
function contains( ancestor, descendant ) {
// Support: IE 11
// Node#contains is only supported on HTMLElement nodes. Otherwise we could just use
// `ancestor.contains( descendant )`.
return ancestor === descendant ||
// eslint-disable-next-line no-bitwise
ancestor.compareDocumentPosition( descendant ) & Node.DOCUMENT_POSITION_CONTAINED_BY;
}
/**
* Find closest ancestor element using one of the given tag names.
*
@ -140,12 +156,12 @@ function getCoveredSiblings( item ) {
end = siblings.length - 1;
// Find first of the siblings that contains the item
while ( !siblings[ start ].contains( range.startContainer ) ) {
while ( !contains( siblings[ start ], range.startContainer ) ) {
start++;
}
// Find last of the siblings that contains the item
while ( !siblings[ end ].contains( range.endContainer ) ) {
while ( !contains( siblings[ end ], range.endContainer ) ) {
end--;
}