File tree 2 files changed +14
-8
lines changed
2 files changed +14
-8
lines changed Original file line number Diff line number Diff line change 1
- import { createElementFromAttrs , createElementFromHTML } from './dom.ts' ;
1
+ import { createElementFromAttrs , createElementFromHTML , querySingleVisibleElem } from './dom.ts' ;
2
2
3
3
test ( 'createElementFromHTML' , ( ) => {
4
4
expect ( createElementFromHTML ( '<a>foo<span>bar</span></a>' ) . outerHTML ) . toEqual ( '<a>foo<span>bar</span></a>' ) ;
@@ -16,3 +16,12 @@ test('createElementFromAttrs', () => {
16
16
} , 'txt' , createElementFromHTML ( '<span>inner</span>' ) ) ;
17
17
expect ( el . outerHTML ) . toEqual ( '<button id="the-id" class="cls-1 cls-2" disabled="" tabindex="0" data-foo="the-data">txt<span>inner</span></button>' ) ;
18
18
} ) ;
19
+
20
+ test ( 'querySingleVisibleElem' , ( ) => {
21
+ let el = createElementFromHTML ( '<div><span>foo</span></div>' ) ;
22
+ expect ( querySingleVisibleElem ( el , 'span' ) . textContent ) . toEqual ( 'foo' ) ;
23
+ el = createElementFromHTML ( '<div><span style="display: none;">foo</span><span>bar</span></div>' ) ;
24
+ expect ( querySingleVisibleElem ( el , 'span' ) . textContent ) . toEqual ( 'bar' ) ;
25
+ el = createElementFromHTML ( '<div><span>foo</span><span>bar</span></div>' ) ;
26
+ expect ( ( ) => querySingleVisibleElem ( el , 'span' ) ) . toThrowError ( 'Expected exactly one visible element' ) ;
27
+ } ) ;
Original file line number Diff line number Diff line change @@ -269,8 +269,8 @@ export function initSubmitEventPolyfill() {
269
269
*/
270
270
export function isElemVisible ( element : HTMLElement ) : boolean {
271
271
if ( ! element ) return false ;
272
-
273
- return Boolean ( element . offsetWidth || element . offsetHeight || element . getClientRects ( ) . length ) ;
272
+ // 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
273
+ return Boolean ( ( element . offsetWidth || element . offsetHeight || element . getClientRects ( ) . length ) && element . style . display !== 'none' ) ;
274
274
}
275
275
276
276
// replace selected text in a textarea while preserving editor history, e.g. CTRL-Z works after this
@@ -333,10 +333,7 @@ export function animateOnce(el: Element, animationClassName: string): Promise<vo
333
333
334
334
export function querySingleVisibleElem < T extends HTMLElement > ( parent : Element , selector : string ) : T | null {
335
335
const elems = parent . querySelectorAll < HTMLElement > ( selector ) ;
336
- const candidates = [ ] ;
337
- for ( const button of elems ) {
338
- if ( isElemVisible ( button ) ) candidates . push ( button ) ;
339
- }
340
- if ( candidates . length > 1 ) throw new Error ( 'multiple primary buttons found, only one could be defined' ) ;
336
+ const candidates = Array . from ( elems ) . filter ( isElemVisible ) ;
337
+ if ( candidates . length > 1 ) throw new Error ( `Expected exactly one visible element matching selector "${ selector } ", but found ${ candidates . length } ` ) ;
341
338
return candidates . length ? candidates [ 0 ] as T : null ;
342
339
}
You can’t perform that action at this time.
0 commit comments