js-web-tools
Version:
Tools for Javascript develpers
85 lines (63 loc) • 1.95 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.closest = closest;
exports.contains = contains;
exports.getActiveElement = getActiveElement;
exports.querySelectorAll = querySelectorAll;
var _domUtils = require("./domUtils");
var matchesImpl = null;
function matches(node, selector) {
if (!matchesImpl) {
var body = document.body;
/* istanbul ignore next */
var nativeMatch = body.matches || body.matchesSelector || body.webkitMatchesSelector || body.mozMatchesSelector || body.msMatchesSelector;
matchesImpl = function matchesImpl(n, s) {
return nativeMatch.call(n, s);
};
}
return matchesImpl(node, selector);
}
function closest(node, selector, stopAt) {
if (node.closest && !stopAt) {
return node.closest(selector);
}
var nextNode = node;
do {
if (matches(nextNode, selector)) {
return nextNode;
}
nextNode = nextNode.parentElement;
} while (nextNode && nextNode !== stopAt && nextNode.nodeType === document.ELEMENT_NODE);
return null;
}
function contains(context, node) {
/* istanbul ignore else */
if (context.contains) {
return context.contains(node);
}
/* istanbul ignore else */
if (context.compareDocumentPosition) {
return context === node || !!(context.compareDocumentPosition(node) & 16);
}
}
function getActiveElement() {
var doc = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _domUtils.ownerDocument)();
try {
var active = doc.activeElement;
/* istanbul ignore else */
if (!active || !active.nodeName) {
return null;
}
return active;
} catch (e) {
// TODO: How to test this codes coverage?
/* istanbul ignore next */
return doc.body;
}
}
var toArray = Function.prototype.bind.call(Function.prototype.call, [].slice);
function querySelectorAll(element, selector) {
return toArray(element.querySelectorAll(selector));
}