Skip to content

Commit d8985af

Browse files
authored
🔀 Merge pull request #496 from FrostCo/watcher_multiple_selectors
Watcher multiple selectors
2 parents 99e9893 + e1b0f3c commit d8985af

File tree

1 file changed

+16
-38
lines changed

1 file changed

+16
-38
lines changed

src/script/lib/helper.ts

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -51,52 +51,24 @@ export function formatNumber(number: number): string {
5151
// Returns the element found by selector string
5252
// Supports querying through a shadow DOM using '>>>'
5353
export function getElement(selector: string, root: Document | ShadowRoot | HTMLElement = document): HTMLElement {
54-
let element;
55-
const selectors = selector.split('>>>');
56-
57-
// No shadowRoot in selector: return native querySelector
58-
if (selectors.length == 1) {
59-
return root.querySelector(selector);
60-
}
61-
62-
// shadowRoot in selector: return querySelector through shadowRoot(s)
63-
while (selectors.length) {
64-
if (root) {
65-
element = root.querySelector(selectors.shift().trim());
66-
67-
if (element) {
68-
if (selectors.length == 0) {
69-
return element;
70-
} else {
71-
root = element.shadowRoot;
72-
}
73-
} else {
74-
return null;
75-
}
76-
} else {
77-
return null;
78-
}
79-
}
54+
return getElementCore(selector, root, 'querySelector') as HTMLElement;
8055
}
8156

8257
// Returns the elements found by selector string
8358
// Supports querying through a shadow DOM using '>>>'
84-
export function getElements(selector: string, root: Document | HTMLElement | ShadowRoot = document): NodeListOf<HTMLElement> {
59+
function getElementCore(selector: string, root: Document | HTMLElement | ShadowRoot = document, queryMethod = 'querySelector'): HTMLElement | NodeListOf<HTMLElement> {
8560
let element;
86-
const selectors = selector.split('>>>');
61+
const domLayers = selector.split('>>>');
8762

88-
// No shadowRoot in selector: return native querySelectorAll
89-
if (selectors.length == 1) {
90-
return root.querySelectorAll(selector);
91-
}
63+
// No shadowRoot in selector: return native querySelector[All]
64+
if (domLayers.length == 1) return root[queryMethod](selector);
9265

93-
// shadowRoot in selector: return querySelectorAll through shadowRoot(s)
94-
while (selectors.length) {
66+
// shadowRoot in selector: return querySelector[All] through shadowRoot(s)
67+
while (domLayers.length) {
9568
if (root) {
96-
const currentSelector = selectors.shift().trim();
97-
98-
if (selectors.length == 0) {
99-
return root.querySelectorAll(currentSelector);
69+
const currentSelector = domLayers.shift().trim();
70+
if (domLayers.length == 0) {
71+
return root[queryMethod](currentSelector);
10072
} else {
10173
element = root.querySelector(currentSelector);
10274
if (element) {
@@ -111,6 +83,12 @@ export function getElements(selector: string, root: Document | HTMLElement | Sha
11183
}
11284
}
11385

86+
// Returns the elements found by selector string
87+
// Supports querying through a shadow DOM using '>>>'
88+
export function getElements(selector: string, root: Document | HTMLElement | ShadowRoot = document): NodeListOf<HTMLElement> {
89+
return getElementCore(selector, root, 'querySelectorAll') as NodeListOf<HTMLElement>;
90+
}
91+
11492
export function getParent(node: HTMLElement, level: number = 1): HTMLElement {
11593
if (!node) {
11694
return null;

0 commit comments

Comments
 (0)