@@ -9,24 +9,24 @@ type ElementsCallback<T extends Element> = (el: T) => Promisable<any>;
9
9
type ElementsCallbackWithArgs = ( el : Element , ...args : any [ ] ) => Promisable < any > ;
10
10
export type DOMEvent < E extends Event , T extends Element = HTMLElement > = E & { target : Partial < T > ; } ;
11
11
12
- function elementsCall ( el : ElementArg , func : ElementsCallbackWithArgs , ...args : any [ ] ) {
12
+ function elementsCall ( el : ElementArg , func : ElementsCallbackWithArgs , ...args : any [ ] ) : ArrayLikeIterable < Element > {
13
13
if ( typeof el === 'string' || el instanceof String ) {
14
14
el = document . querySelectorAll ( el as string ) ;
15
15
}
16
16
if ( el instanceof Node ) {
17
17
func ( el , ...args ) ;
18
+ return [ el ] ;
18
19
} else if ( el . length !== undefined ) {
19
20
// this works for: NodeList, HTMLCollection, Array, jQuery
20
- for ( const e of ( el as ArrayLikeIterable < Element > ) ) {
21
- func ( e , ...args ) ;
22
- }
23
- } else {
24
- throw new Error ( 'invalid argument to be shown/hidden' ) ;
21
+ const elems = el as ArrayLikeIterable < Element > ;
22
+ for ( const elem of elems ) func ( elem , ...args ) ;
23
+ return elems ;
25
24
}
25
+ throw new Error ( 'invalid argument to be shown/hidden' ) ;
26
26
}
27
27
28
- export function toggleClass ( el : ElementArg , className : string , force ?: boolean ) {
29
- elementsCall ( el , ( e : Element ) => {
28
+ export function toggleClass ( el : ElementArg , className : string , force ?: boolean ) : ArrayLikeIterable < Element > {
29
+ return elementsCall ( el , ( e : Element ) => {
30
30
if ( force === true ) {
31
31
e . classList . add ( className ) ;
32
32
} else if ( force === false ) {
@@ -43,23 +43,16 @@ export function toggleClass(el: ElementArg, className: string, force?: boolean)
43
43
* @param el ElementArg
44
44
* @param force force=true to show or force=false to hide, undefined to toggle
45
45
*/
46
- export function toggleElem ( el : ElementArg , force ?: boolean ) {
47
- toggleClass ( el , 'tw-hidden' , force === undefined ? force : ! force ) ;
48
- }
49
-
50
- export function showElem ( el : ElementArg ) {
51
- toggleElem ( el , true ) ;
46
+ export function toggleElem ( el : ElementArg , force ?: boolean ) : ArrayLikeIterable < Element > {
47
+ return toggleClass ( el , 'tw-hidden' , force === undefined ? force : ! force ) ;
52
48
}
53
49
54
- export function hideElem ( el : ElementArg ) {
55
- toggleElem ( el , false ) ;
50
+ export function showElem ( el : ElementArg ) : ArrayLikeIterable < Element > {
51
+ return toggleElem ( el , true ) ;
56
52
}
57
53
58
- export function isElemHidden ( el : ElementArg ) {
59
- const res : boolean [ ] = [ ] ;
60
- elementsCall ( el , ( e ) => res . push ( e . classList . contains ( 'tw-hidden' ) ) ) ;
61
- if ( res . length > 1 ) throw new Error ( `isElemHidden doesn't work for multiple elements` ) ;
62
- return res [ 0 ] ;
54
+ export function hideElem ( el : ElementArg ) : ArrayLikeIterable < Element > {
55
+ return toggleElem ( el , false ) ;
63
56
}
64
57
65
58
function applyElemsCallback < T extends Element > ( elems : ArrayLikeIterable < T > , fn ?: ElementsCallback < T > ) : ArrayLikeIterable < T > {
@@ -275,14 +268,12 @@ export function initSubmitEventPolyfill() {
275
268
document . body . addEventListener ( 'focus' , submitEventPolyfillListener ) ;
276
269
}
277
270
278
- /**
279
- * Check if an element is visible, equivalent to jQuery's `:visible` pseudo.
280
- * Note: This function doesn't account for all possible visibility scenarios.
281
- */
282
- export function isElemVisible ( element : HTMLElement ) : boolean {
283
- if ( ! element ) return false ;
284
- // checking element.style.display is not necessary for browsers, but it is required by some tests with happy-dom because happy-dom doesn't really do layout
285
- return Boolean ( ( element . offsetWidth || element . offsetHeight || element . getClientRects ( ) . length ) && element . style . display !== 'none' ) ;
271
+ export function isElemVisible ( el : HTMLElement ) : boolean {
272
+ // Check if an element is visible, equivalent to jQuery's `:visible` pseudo.
273
+ // This function DOESN'T account for all possible visibility scenarios, its behavior is covered by the tests of "querySingleVisibleElem"
274
+ if ( ! el ) return false ;
275
+ // checking el.style.display is not necessary for browsers, but it is required by some tests with happy-dom because happy-dom doesn't really do layout
276
+ return ! el . classList . contains ( 'tw-hidden' ) && Boolean ( ( el . offsetWidth || el . offsetHeight || el . getClientRects ( ) . length ) && el . style . display !== 'none' ) ;
286
277
}
287
278
288
279
// replace selected text in a textarea while preserving editor history, e.g. CTRL-Z works after this
0 commit comments