-
-
Notifications
You must be signed in to change notification settings - Fork 435
/
Copy pathdom.ts
37 lines (35 loc) · 1.12 KB
/
dom.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { notEmpty } from '@theia/core/lib/common/objects';
/**
* Finds the closest child HTMLButtonElement representing a Theia button.
* A button is a Theia button if it's a `<button>` element and has the `"theia-button"` class.
* If an element has multiple Theia button children, this function prefers `"main"` over `"secondary"` button.
*/
export function findChildTheiaButton(
element: HTMLElement,
recursive = false
): HTMLButtonElement | undefined {
let button: HTMLButtonElement | undefined = undefined;
const children = Array.from(element.children);
for (const child of children) {
if (
child instanceof HTMLButtonElement &&
child.classList.contains('theia-button')
) {
if (child.classList.contains('main')) {
return child;
}
button = child;
}
}
if (!button && recursive) {
button = children
.filter(isHTMLElement)
.map((childElement) => findChildTheiaButton(childElement, true))
.filter(notEmpty)
.shift();
}
return button;
}
function isHTMLElement(element: Element): element is HTMLElement {
return element instanceof HTMLElement;
}