UNPKG

within-element

Version:

check if an element is within the element

27 lines (21 loc) 808 B
/** * Check if the DOM element `child` is within the given `parent` DOM element. * * @param {DOMElement|Range} child - the DOM element or Range to check if it's within `parent` * @param {DOMElement} parent - the parent node that `child` could be inside of * @return {Boolean} True if `child` is within `parent`. False otherwise. * @public */ module.exports = function within (child, parent) { // don't throw if `child` is null if (!child) return false; // Range support if (child.commonAncestorContainer) child = child.commonAncestorContainer; else if (child.endContainer) child = child.endContainer; // traverse up the `parentNode` properties until `parent` is found var node = child; while (node = node.parentNode) { if (node == parent) return true; } return false; };