@@ -14,6 +14,98 @@ interface BoundedLayoutOptions {
1414 focusedControl : Locator
1515}
1616
17+ export function collectBoundedLayoutFailures ( element : HTMLElement ) {
18+ const tolerance = 1
19+ const viewportWidth = document . documentElement . clientWidth
20+ const isScreenReaderOnly = ( candidate : HTMLElement ) => {
21+ let current : HTMLElement | null = candidate
22+ while ( current && element . contains ( current ) ) {
23+ const styles = getComputedStyle ( current )
24+ if (
25+ current . classList . contains ( "sr-only" ) ||
26+ ( styles . position === "absolute" &&
27+ current . offsetWidth <= 1 &&
28+ current . offsetHeight <= 1 &&
29+ ( styles . clip !== "auto" || styles . clipPath !== "none" ) )
30+ ) {
31+ return true
32+ }
33+ current = current . parentElement
34+ }
35+ return false
36+ }
37+ const semanticCandidates = Array . from (
38+ element . querySelectorAll < HTMLElement > (
39+ "h1, h2, h3, h4, h5, h6, p, label, button, input, textarea, select, [role='button'], [role='checkbox'], [role='combobox'], [role='slider'], [role='textbox']" ,
40+ ) ,
41+ )
42+ const clippedTextCandidates = Array . from ( element . querySelectorAll < HTMLElement > ( "span, div" ) ) . filter ( ( candidate ) => {
43+ const styles = getComputedStyle ( candidate )
44+ const hasDirectText = Array . from ( candidate . childNodes ) . some (
45+ ( node ) => node . nodeType === Node . TEXT_NODE && Boolean ( node . textContent ?. trim ( ) ) ,
46+ )
47+ return (
48+ hasDirectText &&
49+ ( styles . overflowX === "hidden" ||
50+ styles . overflowX === "clip" ||
51+ styles . overflowY === "hidden" ||
52+ styles . overflowY === "clip" ||
53+ styles . textOverflow === "ellipsis" )
54+ )
55+ } )
56+ const candidates = [ ...new Set ( [ ...semanticCandidates , ...clippedTextCandidates ] ) ] . filter (
57+ ( candidate ) => candidate . getClientRects ( ) . length > 0 && ! isScreenReaderOnly ( candidate ) ,
58+ )
59+
60+ const failures : string [ ] = [ ]
61+ if ( document . documentElement . scrollWidth > viewportWidth + tolerance )
62+ failures . push ( "document overflows horizontally" )
63+ if ( document . body . scrollWidth > viewportWidth + tolerance ) failures . push ( "body overflows horizontally" )
64+ if ( element . scrollWidth > element . clientWidth + tolerance ) failures . push ( "root overflows horizontally" )
65+
66+ for ( const candidate of candidates ) {
67+ const rect = candidate . getBoundingClientRect ( )
68+ const styles = getComputedStyle ( candidate )
69+ const name =
70+ candidate . getAttribute ( "aria-label" ) || candidate . textContent ?. trim ( ) . slice ( 0 , 40 ) || candidate . tagName
71+ if ( rect . left < - tolerance || rect . right > viewportWidth + tolerance ) failures . push ( `${ name } leaves viewport` )
72+ if (
73+ ( styles . overflowX === "hidden" || styles . overflowX === "clip" ) &&
74+ candidate . scrollWidth > candidate . clientWidth + tolerance
75+ ) {
76+ failures . push ( `${ name } clips horizontally` )
77+ }
78+ if (
79+ ( styles . overflowY === "hidden" || styles . overflowY === "clip" ) &&
80+ candidate . scrollHeight > candidate . clientHeight + tolerance
81+ ) {
82+ failures . push ( `${ name } clips vertically` )
83+ }
84+
85+ let ancestor = candidate . parentElement
86+ while ( ancestor && element . contains ( ancestor ) ) {
87+ const ancestorStyles = getComputedStyle ( ancestor )
88+ const clipsX = ancestorStyles . overflowX === "hidden" || ancestorStyles . overflowX === "clip"
89+ const clipsY = ancestorStyles . overflowY === "hidden" || ancestorStyles . overflowY === "clip"
90+ if ( clipsX || clipsY ) {
91+ const ancestorRect = ancestor . getBoundingClientRect ( )
92+ if (
93+ clipsX &&
94+ ( rect . left < ancestorRect . left - tolerance || rect . right > ancestorRect . right + tolerance )
95+ )
96+ failures . push ( `${ name } is clipped horizontally by an ancestor` )
97+ if (
98+ clipsY &&
99+ ( rect . top < ancestorRect . top - tolerance || rect . bottom > ancestorRect . bottom + tolerance )
100+ )
101+ failures . push ( `${ name } is clipped vertically by an ancestor` )
102+ }
103+ ancestor = ancestor . parentElement
104+ }
105+ }
106+ return failures
107+ }
108+
17109export async function expectBoundedLayout (
18110 page : Page ,
19111 root : Locator ,
@@ -31,72 +123,7 @@ export async function expectBoundedLayout(
31123 document . head . append ( style )
32124 } , WCAG_TEXT_SPACING )
33125
34- await expect
35- . poll ( ( ) =>
36- root . evaluate ( ( element ) => {
37- const tolerance = 1
38- const viewportWidth = document . documentElement . clientWidth
39- const candidates = Array . from (
40- element . querySelectorAll < HTMLElement > (
41- "h1, h2, h3, h4, h5, h6, p, label, button, input, textarea, select, [role='button'], [role='checkbox'], [role='combobox'], [role='slider'], [role='textbox']" ,
42- ) ,
43- ) . filter ( ( candidate ) => candidate . getClientRects ( ) . length > 0 )
44-
45- const failures : string [ ] = [ ]
46- if ( document . documentElement . scrollWidth > viewportWidth + tolerance )
47- failures . push ( "document overflows horizontally" )
48- if ( document . body . scrollWidth > viewportWidth + tolerance ) failures . push ( "body overflows horizontally" )
49- if ( element . scrollWidth > element . clientWidth + tolerance ) failures . push ( "root overflows horizontally" )
50-
51- for ( const candidate of candidates ) {
52- const rect = candidate . getBoundingClientRect ( )
53- const styles = getComputedStyle ( candidate )
54- const name =
55- candidate . getAttribute ( "aria-label" ) ||
56- candidate . textContent ?. trim ( ) . slice ( 0 , 40 ) ||
57- candidate . tagName
58- if ( rect . left < - tolerance || rect . right > viewportWidth + tolerance )
59- failures . push ( `${ name } leaves viewport` )
60- if (
61- ( styles . overflowX === "hidden" || styles . overflowX === "clip" ) &&
62- candidate . scrollWidth > candidate . clientWidth + tolerance
63- ) {
64- failures . push ( `${ name } clips horizontally` )
65- }
66- if (
67- ( styles . overflowY === "hidden" || styles . overflowY === "clip" ) &&
68- candidate . scrollHeight > candidate . clientHeight + tolerance
69- ) {
70- failures . push ( `${ name } clips vertically` )
71- }
72-
73- let ancestor = candidate . parentElement
74- while ( ancestor && element . contains ( ancestor ) ) {
75- const ancestorStyles = getComputedStyle ( ancestor )
76- const clipsX = ancestorStyles . overflowX === "hidden" || ancestorStyles . overflowX === "clip"
77- const clipsY = ancestorStyles . overflowY === "hidden" || ancestorStyles . overflowY === "clip"
78- if ( clipsX || clipsY ) {
79- const ancestorRect = ancestor . getBoundingClientRect ( )
80- if (
81- clipsX &&
82- ( rect . left < ancestorRect . left - tolerance ||
83- rect . right > ancestorRect . right + tolerance )
84- )
85- failures . push ( `${ name } is clipped horizontally by an ancestor` )
86- if (
87- clipsY &&
88- ( rect . top < ancestorRect . top - tolerance ||
89- rect . bottom > ancestorRect . bottom + tolerance )
90- )
91- failures . push ( `${ name } is clipped vertically by an ancestor` )
92- }
93- ancestor = ancestor . parentElement
94- }
95- }
96- return failures
97- } ) ,
98- )
99- . toEqual ( [ ] )
126+ await expect . poll ( ( ) => root . evaluate ( collectBoundedLayoutFailures ) ) . toEqual ( [ ] )
100127
101128 for ( const actionRow of actionRows ) {
102129 const containment = await actionRow . evaluate ( ( row ) => {
0 commit comments