Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/third_party/closure/goog/asserts/dom.js
3992 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
goog.module('goog.asserts.dom');
8
goog.module.declareLegacyNamespace();
9
10
const TagName = goog.require('goog.dom.TagName');
11
const asserts = goog.require('goog.asserts');
12
const element = goog.require('goog.dom.element');
13
const utils = goog.require('goog.utils');
14
15
/**
16
* Checks if the value is a DOM Element if goog.asserts.ENABLE_ASSERTS is true.
17
* @param {*} value The value to check.
18
* @return {!Element} The value, likely to be a DOM Element when asserts are
19
* enabled.
20
* @throws {!asserts.AssertionError} When the value is not an Element.
21
* @closurePrimitive {asserts.matchesReturn}
22
*/
23
const assertIsElement = (value) => {
24
if (asserts.ENABLE_ASSERTS && !element.isElement(value)) {
25
asserts.fail(
26
`Argument is not an Element; got: ${debugStringForType(value)}`);
27
}
28
return /** @type {!Element} */ (value);
29
};
30
31
/**
32
* Checks if the value is a DOM HTMLElement if goog.asserts.ENABLE_ASSERTS is
33
* true.
34
* @param {*} value The value to check.
35
* @return {!HTMLElement} The value, likely to be a DOM HTMLElement when asserts
36
* are enabled.
37
* @throws {!asserts.AssertionError} When the value is not an HTMLElement.
38
* @closurePrimitive {asserts.matchesReturn}
39
*/
40
const assertIsHtmlElement = (value) => {
41
if (asserts.ENABLE_ASSERTS && !element.isHtmlElement(value)) {
42
asserts.fail(
43
`Argument is not an HTML Element; got: ${debugStringForType(value)}`);
44
}
45
return /** @type {!HTMLElement} */ (value);
46
};
47
48
/**
49
* Checks if the value is a DOM HTMLElement of the specified tag name / subclass
50
* if goog.asserts.ENABLE_ASSERTS is true.
51
* @param {*} value The value to check.
52
* @param {!TagName<T>} tagName The element tagName to verify the value against.
53
* @return {T} The value, likely to be a DOM HTMLElement when asserts are
54
* enabled. The exact return type will match the parameterized type
55
* of the tagName as specified in goog.dom.TagName.
56
* @throws {!asserts.AssertionError} When the value is not an HTMLElement with
57
* the appropriate tagName.
58
* @template T
59
* @closurePrimitive {asserts.matchesReturn}
60
*/
61
const assertIsHtmlElementOfType = (value, tagName) => {
62
if (asserts.ENABLE_ASSERTS && !element.isHtmlElementOfType(value, tagName)) {
63
asserts.fail(
64
`Argument is not an HTML Element with tag name ` +
65
`${tagName.toString()}; got: ${debugStringForType(value)}`);
66
}
67
return /** @type {T} */ (value);
68
};
69
70
/**
71
* Checks if the value is an HTMLAnchorElement if goog.asserts.ENABLE_ASSERTS is
72
* true.
73
* @param {*} value
74
* @return {!HTMLAnchorElement}
75
* @throws {!asserts.AssertionError}
76
* @closurePrimitive {asserts.matchesReturn}
77
*/
78
const assertIsHtmlAnchorElement = (value) => {
79
return assertIsHtmlElementOfType(value, TagName.A);
80
};
81
82
/**
83
* Checks if the value is an HTMLButtonElement if goog.asserts.ENABLE_ASSERTS is
84
* true.
85
* @param {*} value
86
* @return {!HTMLButtonElement}
87
* @throws {!asserts.AssertionError}
88
* @closurePrimitive {asserts.matchesReturn}
89
*/
90
const assertIsHtmlButtonElement = (value) => {
91
return assertIsHtmlElementOfType(value, TagName.BUTTON);
92
};
93
94
/**
95
* Checks if the value is an HTMLLinkElement if goog.asserts.ENABLE_ASSERTS is
96
* true.
97
* @param {*} value
98
* @return {!HTMLLinkElement}
99
* @throws {!asserts.AssertionError}
100
* @closurePrimitive {asserts.matchesReturn}
101
*/
102
const assertIsHtmlLinkElement = (value) => {
103
return assertIsHtmlElementOfType(value, TagName.LINK);
104
};
105
106
/**
107
* Checks if the value is an HTMLImageElement if goog.asserts.ENABLE_ASSERTS is
108
* true.
109
* @param {*} value
110
* @return {!HTMLImageElement}
111
* @throws {!asserts.AssertionError}
112
* @closurePrimitive {asserts.matchesReturn}
113
*/
114
const assertIsHtmlImageElement = (value) => {
115
return assertIsHtmlElementOfType(value, TagName.IMG);
116
};
117
118
/**
119
* Checks if the value is an HTMLAudioElement if goog.asserts.ENABLE_ASSERTS is
120
* true.
121
* @param {*} value
122
* @return {!HTMLAudioElement}
123
* @throws {!asserts.AssertionError}
124
* @closurePrimitive {asserts.matchesReturn}
125
*/
126
const assertIsHtmlAudioElement = (value) => {
127
return assertIsHtmlElementOfType(value, TagName.AUDIO);
128
};
129
130
/**
131
* Checks if the value is an HTMLVideoElement if goog.asserts.ENABLE_ASSERTS is
132
* true.
133
* @param {*} value
134
* @return {!HTMLVideoElement}
135
* @throws {!asserts.AssertionError}
136
* @closurePrimitive {asserts.matchesReturn}
137
*/
138
const assertIsHtmlVideoElement = (value) => {
139
return assertIsHtmlElementOfType(value, TagName.VIDEO);
140
};
141
142
/**
143
* Checks if the value is an HTMLInputElement if goog.asserts.ENABLE_ASSERTS is
144
* true.
145
* @param {*} value
146
* @return {!HTMLInputElement}
147
* @throws {!asserts.AssertionError}
148
* @closurePrimitive {asserts.matchesReturn}
149
*/
150
const assertIsHtmlInputElement = (value) => {
151
return assertIsHtmlElementOfType(value, TagName.INPUT);
152
};
153
154
/**
155
* Checks if the value is an HTMLTextAreaElement if goog.asserts.ENABLE_ASSERTS
156
* is true.
157
* @param {*} value
158
* @return {!HTMLTextAreaElement}
159
* @throws {!asserts.AssertionError}
160
* @closurePrimitive {asserts.matchesReturn}
161
*/
162
const assertIsHtmlTextAreaElement = (value) => {
163
return assertIsHtmlElementOfType(value, TagName.TEXTAREA);
164
};
165
166
/**
167
* Checks if the value is an HTMLCanvasElement if goog.asserts.ENABLE_ASSERTS is
168
* true.
169
* @param {*} value
170
* @return {!HTMLCanvasElement}
171
* @throws {!asserts.AssertionError}
172
* @closurePrimitive {asserts.matchesReturn}
173
*/
174
const assertIsHtmlCanvasElement = (value) => {
175
return assertIsHtmlElementOfType(value, TagName.CANVAS);
176
};
177
178
/**
179
* Checks if the value is an HTMLEmbedElement if goog.asserts.ENABLE_ASSERTS is
180
* true.
181
* @param {*} value
182
* @return {!HTMLEmbedElement}
183
* @throws {!asserts.AssertionError}
184
* @closurePrimitive {asserts.matchesReturn}
185
*/
186
const assertIsHtmlEmbedElement = (value) => {
187
return assertIsHtmlElementOfType(value, TagName.EMBED);
188
};
189
190
/**
191
* Checks if the value is an HTMLFormElement if goog.asserts.ENABLE_ASSERTS is
192
* true.
193
* @param {*} value
194
* @return {!HTMLFormElement}
195
* @throws {!asserts.AssertionError}
196
* @closurePrimitive {asserts.matchesReturn}
197
*/
198
const assertIsHtmlFormElement = (value) => {
199
return assertIsHtmlElementOfType(value, TagName.FORM);
200
};
201
202
/**
203
* Checks if the value is an HTMLFrameElement if goog.asserts.ENABLE_ASSERTS is
204
* true.
205
* @param {*} value
206
* @return {!HTMLFrameElement}
207
* @throws {!asserts.AssertionError}
208
* @closurePrimitive {asserts.matchesReturn}
209
*/
210
const assertIsHtmlFrameElement = (value) => {
211
return assertIsHtmlElementOfType(value, TagName.FRAME);
212
};
213
214
/**
215
* Checks if the value is an HTMLIFrameElement if goog.asserts.ENABLE_ASSERTS is
216
* true.
217
* @param {*} value
218
* @return {!HTMLIFrameElement}
219
* @throws {!asserts.AssertionError}
220
* @closurePrimitive {asserts.matchesReturn}
221
*/
222
const assertIsHtmlIFrameElement = (value) => {
223
return assertIsHtmlElementOfType(value, TagName.IFRAME);
224
};
225
226
/**
227
* Checks if the value is an HTMLObjectElement if goog.asserts.ENABLE_ASSERTS is
228
* true.
229
* @param {*} value
230
* @return {!HTMLObjectElement}
231
* @throws {!asserts.AssertionError}
232
* @closurePrimitive {asserts.matchesReturn}
233
*/
234
const assertIsHtmlObjectElement = (value) => {
235
return assertIsHtmlElementOfType(value, TagName.OBJECT);
236
};
237
238
/**
239
* Checks if the value is an HTMLScriptElement if goog.asserts.ENABLE_ASSERTS is
240
* true.
241
* @param {*} value
242
* @return {!HTMLScriptElement}
243
* @throws {!asserts.AssertionError}
244
* @closurePrimitive {asserts.matchesReturn}
245
*/
246
const assertIsHtmlScriptElement = (value) => {
247
return assertIsHtmlElementOfType(value, TagName.SCRIPT);
248
};
249
250
/**
251
* Returns a string representation of a value's type.
252
* @param {*} value An object, or primitive.
253
* @return {string} The best display name for the value.
254
*/
255
const debugStringForType = (value) => {
256
if (utils.isObject(value)) {
257
try {
258
return /** @type {string|undefined} */ (value.constructor.displayName) ||
259
value.constructor.name ||
260
Object.prototype.toString.call(value);
261
} catch (e) {
262
return '<object could not be stringified>';
263
}
264
} else {
265
return value === undefined ? 'undefined' :
266
value === null ? 'null' : typeof value;
267
}
268
};
269
270
exports = {
271
assertIsElement,
272
assertIsHtmlElement,
273
assertIsHtmlElementOfType,
274
assertIsHtmlAnchorElement,
275
assertIsHtmlButtonElement,
276
assertIsHtmlLinkElement,
277
assertIsHtmlImageElement,
278
assertIsHtmlAudioElement,
279
assertIsHtmlVideoElement,
280
assertIsHtmlInputElement,
281
assertIsHtmlTextAreaElement,
282
assertIsHtmlCanvasElement,
283
assertIsHtmlEmbedElement,
284
assertIsHtmlFormElement,
285
assertIsHtmlFrameElement,
286
assertIsHtmlIFrameElement,
287
assertIsHtmlObjectElement,
288
assertIsHtmlScriptElement,
289
};
290
291