UNPKG

1.11 kBJavaScriptView Raw
1"use strict";
2/**
3 * Copyright (c) 2017, Philip Walton <philip@philipwalton.com>
4 */
5Object.defineProperty(exports, "__esModule", { value: true });
6var matches_1 = require("./matches");
7var parents_1 = require("./parents");
8/**
9 * Gets the closest parent element that matches the passed selector.
10 * @param {Element} element The element whose parents to check.
11 * @param {string} selector The CSS selector to match against.
12 * @param {boolean=} shouldCheckSelf True if the selector should test against
13 * the passed element itself.
14 * @return {Element|undefined} The matching element or undefined.
15 */
16function closest(element, selector, shouldCheckSelf) {
17 if (shouldCheckSelf === void 0) { shouldCheckSelf = false; }
18 if (!(element && element.nodeType === 1 && selector))
19 return;
20 var parentElements = (shouldCheckSelf ? [element] : []).concat(parents_1.parents(element));
21 for (var i = 0, parent_1; (parent_1 = parentElements[i]); i++) {
22 if (matches_1.matches(parent_1, selector))
23 return parent_1;
24 }
25}
26exports.closest = closest;
27//# sourceMappingURL=closest.js.map
\No newline at end of file