-
Notifications
You must be signed in to change notification settings - Fork 673
Expand file tree
/
Copy pathdomUtils.ts
More file actions
159 lines (120 loc) · 4.61 KB
/
Copy pathdomUtils.ts
File metadata and controls
159 lines (120 loc) · 4.61 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { ClientFunction } from 'testcafe';
const STYLESHEET_RULES_ID = 'stylesheetRules';
function createElement(
tagName: string,
id: string,
style: Partial<CSSStyleDeclaration>,
): HTMLElement {
const element = document.createElement(tagName);
element.setAttribute('id', id);
Object.keys(style).forEach((key) => { element.style[key] = style[key]; });
return element;
}
export const setAttribute = ClientFunction((selector, attribute, value) => {
const element = document.querySelector(selector);
element.setAttribute(attribute, value);
});
export const removeAttribute = ClientFunction((selector, attribute) => {
const element = document.querySelector(selector);
element.removeAttribute(attribute);
});
export const getStyleAttribute = ClientFunction((selector) => {
const element = selector();
return element.getAttribute('style');
});
export const setStyleAttribute = ClientFunction((selector, styleValue) => {
const element = selector();
const styles = element.getAttribute('style') ?? '';
const updatedStyles = `${styles} ${styleValue}`;
element.setAttribute('style', updatedStyles);
});
export const setClassAttribute = ClientFunction((selector, styleValue) => {
const element = selector();
const styles = element.getAttribute('class') ?? '';
const updatedClasses = `${styles} ${styleValue}`;
element.setAttribute('class', updatedClasses);
});
export const removeClassAttribute = ClientFunction((selector, styleValue) => {
const element = selector();
const styles = element.getAttribute('class') ?? '';
const updatedClasses = `${styles.replace(styleValue, '')}`;
element.setAttribute('class', updatedClasses);
});
export const insertStylesheetRulesToPage = ClientFunction((
rule: string,
): void => {
const styleEl = document.createElement('style');
styleEl.setAttribute('id', STYLESHEET_RULES_ID);
styleEl.innerHTML = rule;
document.head.appendChild(styleEl);
}, { dependencies: { STYLESHEET_RULES_ID } });
export const removeStylesheetRulesFromPage = ClientFunction((): void => {
const stylesheetRulesEl = document.querySelector(`#${STYLESHEET_RULES_ID}`);
stylesheetRulesEl?.remove();
}, { dependencies: { STYLESHEET_RULES_ID } });
export const appendElementTo = ClientFunction((
targetContainerSelector: string,
tagName: string,
elementId,
additionalStyles?: Partial<CSSStyleDeclaration>,
) => {
const containerElement = document.querySelector(targetContainerSelector);
const element = createElement(tagName, elementId, additionalStyles ?? {});
containerElement?.appendChild(element);
}, { dependencies: { createElement } });
export const getComputedPropertyValue = ClientFunction((selector, property) => {
const element = document.querySelector(selector);
return window.getComputedStyle(element).getPropertyValue(property);
});
export const getDocumentScrollTop = ClientFunction(() => document.documentElement.scrollTop);
export const setStylePropertyValue = ClientFunction((selector, property, value) => {
const element = document.querySelector(selector);
element.style[property] = value;
});
export const getPropertyValue = ClientFunction((selector, property) => {
const element = selector();
return element.style[property];
});
export const addCaptionTo = ClientFunction((
elementSelector: string,
caption: string,
where: InsertPosition = 'beforebegin',
) => {
const element = document.querySelector(elementSelector);
element?.insertAdjacentText(where, caption);
});
export const addFocusableElementBefore = ClientFunction((
targetSelector: string,
elementId = 'focusable-start',
) => {
const existing = document.getElementById(elementId);
existing?.remove();
const target = document.querySelector(targetSelector);
const button = document.createElement('button');
button.id = elementId;
button.textContent = 'Start';
button.style.position = 'fixed';
button.style.top = '0';
button.style.left = '0';
button.style.zIndex = '-1';
button.style.opacity = '0';
target?.parentElement?.insertBefore(button, target);
return button.id;
});
export const addFocusableElementAfter = ClientFunction((
targetSelector: string,
elementId = 'focusable-end',
) => {
const existing = document.getElementById(elementId);
existing?.remove();
const target = document.querySelector(targetSelector);
const button = document.createElement('button');
button.id = elementId;
button.textContent = 'End';
target?.parentElement?.insertBefore(button, target.nextElementSibling);
return button.id;
});
export const hasHorizontalScroll = ClientFunction((selector) => {
const element = selector();
return element.scrollWidth > element.clientWidth;
});