Skip to content

Commit 259c9de

Browse files
committed
♻️ Refactor helper getElement functions
1 parent 68424d1 commit 259c9de

File tree

1 file changed

+12
-32
lines changed

1 file changed

+12
-32
lines changed

src/script/lib/helper.ts

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -51,50 +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;
8661
const selectors = selector.split('>>>');
8762

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

91-
// shadowRoot in selector: return querySelectorAll through shadowRoot(s)
66+
// shadowRoot in selector: return querySelector[All] through shadowRoot(s)
9267
while (selectors.length) {
9368
if (root) {
9469
const currentSelector = selectors.shift().trim();
95-
9670
if (selectors.length == 0) {
97-
return root.querySelectorAll(currentSelector);
71+
return root[queryMethod](currentSelector);
9872
} else {
9973
element = root.querySelector(currentSelector);
10074
if (element) {
@@ -109,6 +83,12 @@ export function getElements(selector: string, root: Document | HTMLElement | Sha
10983
}
11084
}
11185

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+
11292
export function getParent(node: HTMLElement, level: number = 1): HTMLElement {
11393
if (!node) {
11494
return null;

0 commit comments

Comments
 (0)