1 |
|
2 |
|
3 |
|
4 |
|
5 | import { JS } from '@aws-amplify/core';
|
6 |
|
7 | const proto =
|
8 | JS.browserOrNode().isBrowser && window['Element']
|
9 | ? window['Element'].prototype
|
10 | : null;
|
11 |
|
12 | const nativeMatches = proto
|
13 | ? proto.matches ||
|
14 |
|
15 | proto.matchesSelector ||
|
16 |
|
17 | proto.webkitMatchesSelector ||
|
18 |
|
19 | proto.mozMatchesSelector ||
|
20 |
|
21 | proto.msMatchesSelector ||
|
22 |
|
23 | proto.oMatchesSelector
|
24 | : null;
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | export function matches(element, test) {
|
34 |
|
35 | if (element && element.nodeType === 1 && test) {
|
36 |
|
37 | if (typeof test === 'string' || test.nodeType === 1) {
|
38 | return (
|
39 | element === test || matchesSelector(element, test)
|
40 | );
|
41 | } else if ('length' in test) {
|
42 |
|
43 |
|
44 | for (let i = 0, item; (item = test[i]); i++) {
|
45 | if (element === item || matchesSelector(element, item)) return true;
|
46 | }
|
47 | }
|
48 | }
|
49 |
|
50 | return false;
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | function matchesSelector(element, selector) {
|
61 | if (typeof selector !== 'string') return false;
|
62 | if (nativeMatches) return nativeMatches.call(element, selector);
|
63 | const nodes = element.parentNode.querySelectorAll(selector);
|
64 | for (let i = 0, node; (node = nodes[i]); i++) {
|
65 | if (node === element) return true;
|
66 | }
|
67 | return false;
|
68 | }
|