-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathdom-tools.ts
124 lines (104 loc) · 3.11 KB
/
dom-tools.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/**
* Exported DOM accessor utility functions
*/
export const _dom = {
hasStyle,
getDistributedNodes,
getShadowRoot,
getText,
getStyle,
childNodes,
childNodesAsList,
hasClass,
hasAttribute,
getAttribute,
hasShadowRoot,
isCommentNode,
isElementNode,
isPresent,
isShadowRoot,
tagName,
lastElementChild
};
// ******************************************************************************************
// These functions are cloned from
// * @angular/platform-browser/src/browser/GenericBrowserDomAdapter
// and are to be used ONLY internally in custom-matchers.ts and Unit Tests
// ******************************************************************************************
function getStyle(element: any, stylename: string): string {
return element.style[stylename];
}
function hasStyle(
element: any,
styleName: string,
styleValue: string = null,
inlineOnly = true
): boolean {
let value = getStyle(element, styleName) || '';
if (!value && !inlineOnly) {
// Search stylesheets
value = getComputedStyle(element).getPropertyValue(styleName) || '';
}
return styleValue ? value == styleValue : value.length > 0;
}
function getDistributedNodes(el: HTMLElement): Node[] {
return (<any>el).getDistributedNodes();
}
function getShadowRoot(el: HTMLElement): DocumentFragment {
return (<any>el).shadowRoot;
}
function getText(el: Node): string {
return el.textContent;
}
function childNodesAsList(el: Node): any[] {
const list = el.childNodes;
const res = new Array(list.length);
for (let i = 0; i < list.length; i++) {
res[i] = list[i];
}
return res;
}
function hasClass(element: any, className: string): boolean {
return element.classList.contains(className);
}
function hasAttribute(element: any, attributeName: string): boolean {
return element.hasAttribute(attributeName);
}
function getAttribute(element: any, attributeName: string): string {
return element.getAttribute(attributeName);
}
function childNodes(el: any): Node[] {
return el.childNodes;
}
function hasShadowRoot(node: any): boolean {
return isPresent(node.shadowRoot) && node instanceof HTMLElement;
}
function isCommentNode(node: Node): boolean {
return node.nodeType === Node.COMMENT_NODE;
}
function isElementNode(node: Node): boolean {
return node.nodeType === Node.ELEMENT_NODE;
}
function isShadowRoot(node: any): boolean {
return node instanceof DocumentFragment;
}
function isPresent(obj: any): boolean {
return obj != null;
}
function tagName(element: any): string {
return element.tagName;
}
// ******************************************************************************************
// These functions are part of the DOM API
// and are to be used ONLY internally in custom-matchers.ts and Unit Tests
// ******************************************************************************************
function lastElementChild(element: any): Node | null {
return element.lastElementChild;
}