2
2
3
3
/*
4
4
* These are routines used by our testing code.
5
- *
6
- * jQuery is not necessarily present. Don't rely on it
7
- * for routine operations.
8
5
*/
9
6
10
- function ph_select ( sel ) {
7
+ window . ph_select = function ( sel ) {
11
8
if ( ! window . Sizzle ) {
12
9
return Array . from ( document . querySelectorAll ( sel ) ) ;
13
10
}
@@ -20,95 +17,95 @@ function ph_select(sel) {
20
17
} else {
21
18
return Array . from ( document . querySelectorAll ( sel ) ) ;
22
19
}
23
- }
20
+ } ;
24
21
25
- function ph_only ( els , sel ) {
22
+ window . ph_only = function ( els , sel ) {
26
23
if ( els . length === 0 )
27
24
throw new Error ( sel + " not found" ) ;
28
25
if ( els . length > 1 )
29
26
throw new Error ( sel + " is ambiguous" ) ;
30
27
return els [ 0 ] ;
31
- }
28
+ } ;
32
29
33
- function ph_find ( sel ) {
34
- const els = ph_select ( sel ) ;
35
- return ph_only ( els , sel ) ;
36
- }
30
+ window . ph_find = function ( sel ) {
31
+ const els = window . ph_select ( sel ) ;
32
+ return window . ph_only ( els , sel ) ;
33
+ } ;
37
34
38
- function ph_count ( sel ) {
39
- const els = ph_select ( sel ) ;
35
+ window . ph_count = function ( sel ) {
36
+ const els = window . ph_select ( sel ) ;
40
37
return els . length ;
41
- }
38
+ } ;
42
39
43
- function ph_count_check ( sel , expected_num ) {
44
- return ( ph_count ( sel ) == expected_num ) ;
45
- }
40
+ window . ph_count_check = function ( sel , expected_num ) {
41
+ return ( window . ph_count ( sel ) == expected_num ) ;
42
+ } ;
46
43
47
- function ph_val ( sel ) {
48
- const el = ph_find ( sel ) ;
44
+ window . ph_val = function ( sel ) {
45
+ const el = window . ph_find ( sel ) ;
49
46
if ( el . value === undefined )
50
47
throw new Error ( sel + " does not have a value" ) ;
51
48
return el . value ;
52
- }
49
+ } ;
53
50
54
- function ph_set_val ( sel , val ) {
55
- const el = ph_find ( sel ) ;
51
+ window . ph_set_val = function ( sel , val ) {
52
+ const el = window . ph_find ( sel ) ;
56
53
if ( el . value === undefined )
57
54
throw new Error ( sel + " does not have a value" ) ;
58
55
el . value = val ;
59
56
const ev = new Event ( "change" , { bubbles : true , cancelable : false } ) ;
60
57
el . dispatchEvent ( ev ) ;
61
- }
58
+ } ;
62
59
63
- function ph_has_val ( sel , val ) {
64
- return ph_val ( sel ) == val ;
65
- }
60
+ window . ph_has_val = function ( sel , val ) {
61
+ return window . ph_val ( sel ) == val ;
62
+ } ;
66
63
67
- function ph_collected_text_is ( sel , val ) {
68
- const els = ph_select ( sel ) ;
64
+ window . ph_collected_text_is = function ( sel , val ) {
65
+ const els = window . ph_select ( sel ) ;
69
66
const rest = els . map ( el => {
70
67
if ( el . textContent === undefined )
71
68
throw new Error ( sel + " can not have text" ) ;
72
69
return el . textContent . replaceAll ( "\xa0" , " " ) ;
73
70
} ) . join ( "" ) ;
74
71
return rest === val ;
75
- }
72
+ } ;
76
73
77
- function ph_text ( sel ) {
78
- const el = ph_find ( sel ) ;
74
+ window . ph_text = function ( sel ) {
75
+ const el = window . ph_find ( sel ) ;
79
76
if ( el . textContent === undefined )
80
77
throw new Error ( sel + " can not have text" ) ;
81
78
// 0xa0 is a non-breakable space, which is a rendering detail of Chromium
82
79
// and awkward to handle in tests; turn it into normal spaces
83
80
return el . textContent . replaceAll ( "\xa0" , " " ) ;
84
- }
81
+ } ;
85
82
86
- function ph_attr ( sel , attr ) {
87
- return ph_find ( sel ) . getAttribute ( attr ) ;
88
- }
83
+ window . ph_attr = function ( sel , attr ) {
84
+ return window . ph_find ( sel ) . getAttribute ( attr ) ;
85
+ } ;
89
86
90
- function ph_set_attr ( sel , attr , val ) {
91
- const el = ph_find ( sel ) ;
87
+ window . ph_set_attr = function ( sel , attr , val ) {
88
+ const el = window . ph_find ( sel ) ;
92
89
if ( val === null || val === undefined )
93
90
el . removeAttribute ( attr ) ;
94
91
else
95
92
el . setAttribute ( attr , val ) ;
96
93
97
94
const ev = new Event ( "change" , { bubbles : true , cancelable : false } ) ;
98
95
el . dispatchEvent ( ev ) ;
99
- }
96
+ } ;
100
97
101
- function ph_has_attr ( sel , attr , val ) {
102
- return ph_attr ( sel , attr ) == val ;
103
- }
98
+ window . ph_has_attr = function ( sel , attr , val ) {
99
+ return window . ph_attr ( sel , attr ) == val ;
100
+ } ;
104
101
105
- function ph_attr_contains ( sel , attr , val ) {
106
- const a = ph_attr ( sel , attr ) ;
102
+ window . ph_attr_contains = function ( sel , attr , val ) {
103
+ const a = window . ph_attr ( sel , attr ) ;
107
104
return a && a . indexOf ( val ) > - 1 ;
108
- }
105
+ } ;
109
106
110
- function ph_mouse ( sel , type , x , y , btn , ctrlKey , shiftKey , altKey , metaKey ) {
111
- const el = ph_find ( sel ) ;
107
+ window . ph_mouse = function ( sel , type , x , y , btn , ctrlKey , shiftKey , altKey , metaKey ) {
108
+ const el = window . ph_find ( sel ) ;
112
109
113
110
/* The element has to be visible, and not collapsed */
114
111
if ( el . offsetWidth <= 0 && el . offsetHeight <= 0 && el . tagName != 'svg' )
@@ -160,48 +157,48 @@ function ph_mouse(sel, type, x, y, btn, ctrlKey, shiftKey, altKey, metaKey) {
160
157
/* It really had to work */
161
158
if ( ! processed )
162
159
throw new Error ( sel + " is disabled or somehow doesn't process events" ) ;
163
- }
160
+ } ;
164
161
165
- function ph_get_checked ( sel ) {
166
- const el = ph_find ( sel ) ;
162
+ window . ph_get_checked = function ( sel ) {
163
+ const el = window . ph_find ( sel ) ;
167
164
if ( el . checked === undefined )
168
165
throw new Error ( sel + " is not checkable" ) ;
169
166
170
167
return el . checked ;
171
- }
168
+ } ;
172
169
173
- function ph_set_checked ( sel , val ) {
174
- const el = ph_find ( sel ) ;
170
+ window . ph_set_checked = function ( sel , val ) {
171
+ const el = window . ph_find ( sel ) ;
175
172
if ( el . checked === undefined )
176
173
throw new Error ( sel + " is not checkable" ) ;
177
174
178
175
if ( el . checked != val )
179
- ph_mouse ( sel , "click" , 0 , 0 , 0 ) ;
180
- }
176
+ window . ph_mouse ( sel , "click" , 0 , 0 , 0 ) ;
177
+ } ;
181
178
182
- function ph_is_visible ( sel ) {
183
- const el = ph_find ( sel ) ;
179
+ window . ph_is_visible = function ( sel ) {
180
+ const el = window . ph_find ( sel ) ;
184
181
return el . tagName == "svg" || ( ( el . offsetWidth > 0 || el . offsetHeight > 0 ) && ! ( el . style . visibility == "hidden" || el . style . display == "none" ) ) ;
185
- }
182
+ } ;
186
183
187
- function ph_is_present ( sel ) {
188
- const els = ph_select ( sel ) ;
184
+ window . ph_is_present = function ( sel ) {
185
+ const els = window . ph_select ( sel ) ;
189
186
return els . length > 0 ;
190
- }
187
+ } ;
191
188
192
- function ph_in_text ( sel , text ) {
193
- return ph_text ( sel ) . indexOf ( text ) != - 1 ;
194
- }
189
+ window . ph_in_text = function ( sel , text ) {
190
+ return window . ph_text ( sel ) . indexOf ( text ) != - 1 ;
191
+ } ;
195
192
196
- function ph_text_is ( sel , text ) {
197
- return ph_text ( sel ) == text ;
198
- }
193
+ window . ph_text_is = function ( sel , text ) {
194
+ return window . ph_text ( sel ) == text ;
195
+ } ;
199
196
200
- function ph_text_matches ( sel , pattern ) {
201
- return ph_text ( sel ) . match ( pattern ) ;
202
- }
197
+ window . ph_text_matches = function ( sel , pattern ) {
198
+ return window . ph_text ( sel ) . match ( pattern ) ;
199
+ } ;
203
200
204
- function ph_go ( href ) {
201
+ window . ph_go = function ( href ) {
205
202
if ( href . indexOf ( "#" ) === 0 ) {
206
203
window . location . hash = href ;
207
204
} else {
@@ -213,25 +210,25 @@ function ph_go(href) {
213
210
} ;
214
211
window . parent . postMessage ( "\n" + JSON . stringify ( control ) , "*" ) ;
215
212
}
216
- }
213
+ } ;
217
214
218
- function ph_focus ( sel ) {
219
- ph_find ( sel ) . focus ( ) ;
220
- }
215
+ window . ph_focus = function ( sel ) {
216
+ window . ph_find ( sel ) . focus ( ) ;
217
+ } ;
221
218
222
- function ph_scrollIntoViewIfNeeded ( sel ) {
223
- ph_find ( sel ) . scrollIntoViewIfNeeded ( ) ;
224
- }
219
+ window . ph_scrollIntoViewIfNeeded = function ( sel ) {
220
+ window . ph_find ( sel ) . scrollIntoViewIfNeeded ( ) ;
221
+ } ;
225
222
226
- function ph_blur ( sel ) {
227
- ph_find ( sel ) . blur ( ) ;
228
- }
223
+ window . ph_blur = function ( sel ) {
224
+ window . ph_find ( sel ) . blur ( ) ;
225
+ } ;
229
226
230
- function ph_blur_active ( ) {
227
+ window . ph_blur_active = function ( ) {
231
228
const elt = window . document . activeElement ;
232
229
if ( elt )
233
230
elt . blur ( ) ;
234
- }
231
+ } ;
235
232
236
233
class PhWaitCondTimeout extends Error {
237
234
constructor ( description ) {
@@ -244,7 +241,7 @@ class PhWaitCondTimeout extends Error {
244
241
}
245
242
}
246
243
247
- function ph_wait_cond ( cond , timeout , error_description ) {
244
+ window . ph_wait_cond = function ( cond , timeout , error_description ) {
248
245
return new Promise ( ( resolve , reject ) => {
249
246
// poll every 100 ms for now; FIXME: poll less often and re-check on mutations using
250
247
// https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
@@ -269,7 +266,7 @@ function ph_wait_cond(cond, timeout, error_description) {
269
266
}
270
267
step ( ) ;
271
268
} ) ;
272
- }
269
+ } ;
273
270
274
271
function currentFrameAbsolutePosition ( ) {
275
272
let currentWindow = window ;
@@ -307,27 +304,27 @@ function flatten(array_of_arrays) {
307
304
return [ ] ;
308
305
}
309
306
310
- function ph_selector_clips ( sels ) {
307
+ window . ph_selector_clips = function ( sels ) {
311
308
const f = currentFrameAbsolutePosition ( ) ;
312
- const elts = flatten ( sels . map ( ph_select ) ) ;
309
+ const elts = flatten ( sels . map ( window . ph_select ) ) ;
313
310
return elts . map ( e => {
314
311
const r = e . getBoundingClientRect ( ) ;
315
312
return { x : r . x + f . x , y : r . y + f . y , width : r . width , height : r . height , scale : 1 } ;
316
313
} ) ;
317
- }
314
+ } ;
318
315
319
- function ph_element_clip ( sel ) {
320
- ph_find ( sel ) ; // just to make sure it is not ambiguous
321
- return ph_selector_clips ( [ sel ] ) [ 0 ] ;
322
- }
316
+ window . ph_element_clip = function ( sel ) {
317
+ window . ph_find ( sel ) ; // just to make sure it is not ambiguous
318
+ return window . ph_selector_clips ( [ sel ] ) [ 0 ] ;
319
+ } ;
323
320
324
- function ph_count_animations ( sel ) {
325
- return ph_find ( sel ) . getAnimations ( { subtree : true } ) . length ;
326
- }
321
+ window . ph_count_animations = function ( sel ) {
322
+ return window . ph_find ( sel ) . getAnimations ( { subtree : true } ) . length ;
323
+ } ;
327
324
328
- function ph_set_texts ( new_texts ) {
325
+ window . ph_set_texts = function ( new_texts ) {
329
326
for ( const sel in new_texts ) {
330
- const elts = ph_select ( sel ) ;
327
+ const elts = window . ph_select ( sel ) ;
331
328
if ( elts . length == 0 )
332
329
throw new Error ( sel + " not found" ) ;
333
330
for ( let elt of elts ) {
@@ -357,4 +354,4 @@ function ph_set_texts(new_texts) {
357
354
}
358
355
}
359
356
}
360
- }
357
+ } ;
0 commit comments