deleight
Version:
A library with 9 modules for writing more expressive web applications with traditional HTML, CSS and JavaScript.
42 lines (41 loc) • 1.07 kB
JavaScript
;
/**
* Utility functions for getting ancestor elements.
* These can be used for selecting ancestors whether or not
* there are shadow roots in-between.
*
* @module
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.selectParent = exports.parent = void 0;
/**
* Returns the parent node of a given element or shadow root.
* This function is necessary because shadow roots have no
* parents but instead have hosts which may have them.
*
* @example
*
* @param node
* @returns
*/
function parent(node) {
return node instanceof Element ? node.parentElement : node.host;
}
exports.parent = parent;
/**
* Returns the first ancestor that matches the specified selector.
* This function treates the host of a shadow root as its parent.
*
* @example
*
*
* @param element
* @param selector
*/
function selectParent(element, selector) {
let eParent = parent(element);
while (eParent && !eParent.matches(selector))
eParent = parent(eParent);
return eParent;
}
exports.selectParent = selectParent;