Last change
on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
4.5 KB
|
Line | |
---|
1 | /**
|
---|
2 | * @license
|
---|
3 | * Copyright Google LLC All Rights Reserved.
|
---|
4 | *
|
---|
5 | * Use of this source code is governed by an MIT-style license that can be
|
---|
6 | * found in the LICENSE file at https://angular.io/license
|
---|
7 | */
|
---|
8 | /** IE 11 compatible closest implementation that is able to start from non-Element Nodes. */
|
---|
9 | export function closest(element, selector) {
|
---|
10 | if (!(element instanceof Node)) {
|
---|
11 | return null;
|
---|
12 | }
|
---|
13 | let curr = element;
|
---|
14 | while (curr != null && !(curr instanceof Element)) {
|
---|
15 | curr = curr.parentNode;
|
---|
16 | }
|
---|
17 | return curr && (hasNativeClosest ?
|
---|
18 | curr.closest(selector) : polyfillClosest(curr, selector));
|
---|
19 | }
|
---|
20 | /** Polyfill for browsers without Element.closest. */
|
---|
21 | function polyfillClosest(element, selector) {
|
---|
22 | let curr = element;
|
---|
23 | while (curr != null && !(curr instanceof Element && matches(curr, selector))) {
|
---|
24 | curr = curr.parentNode;
|
---|
25 | }
|
---|
26 | return (curr || null);
|
---|
27 | }
|
---|
28 | const hasNativeClosest = typeof Element != 'undefined' && !!Element.prototype.closest;
|
---|
29 | /** IE 11 compatible matches implementation. */
|
---|
30 | function matches(element, selector) {
|
---|
31 | return element.matches ?
|
---|
32 | element.matches(selector) :
|
---|
33 | element['msMatchesSelector'](selector);
|
---|
34 | }
|
---|
35 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicG9seWZpbGwuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi8uLi9zcmMvY2RrL2ExMXkvZm9jdXMtdHJhcC9wb2x5ZmlsbC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTs7Ozs7O0dBTUc7QUFFSCw0RkF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