-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom-get.ts
63 lines (56 loc) · 1.88 KB
/
dom-get.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// TODO: need to be checked if app is running in browser
let localContext: Document | null = typeof document !== "undefined" ? document : null;
export class DomGet {
/**
*
* @param context document context
*/
public static setContext(context: Document): void {
localContext = context;
}
/**
*
* @param className name of class
* @param context searched context
* @returns collection of found elements
*/
public static byClass(className: string, context: Document = localContext as Document): HTMLCollectionOf<Element> {
return context.getElementsByClassName(className);
}
/**
*
* @param link name of link
* @param context searched context
* @returns nodeList of found elements
*/
public static byLink(link: string, context: Document = localContext as Document): NodeListOf<HTMLElementTagNameMap[keyof HTMLElementTagNameMap]> {
return context.querySelectorAll(`a[attr="${link}"]`);
}
/**
*
* @param id searched ID
* @param context searched context
* @returns found element or null
*/
public static byId(id: string, context: Document = localContext as Document): HTMLElement | null {
return context.getElementById(id);
}
/**
*
* @param name elements name
* @param context searched context
* @returns nodeList of found elements
*/
public static byName(name: string, context: Document = localContext as Document): NodeListOf<HTMLElement> {
return context.getElementsByName(name);
}
/**
*
* @param tagName elements tagName
* @param context searched context
* @returns nodeList of found elements
*/
public static byTag(tagName: string, context: Document = localContext as Document): NodeListOf<Element> {
return context.getElementsByTagName(tagName) as any;
}
}