@@ -2,7 +2,7 @@ import {computeAccessibleName} from 'dom-accessibility-api'
2
2
import { getDefaultNormalizer } from './matches'
3
3
import { getNodeText } from './get-node-text'
4
4
import { DEFAULT_IGNORE_TAGS , getConfig } from './config'
5
- import { getImplicitAriaRoles } from './role-helpers'
5
+ import { getImplicitAriaRoles , isInaccessible } from './role-helpers'
6
6
7
7
const normalize = getDefaultNormalizer ( )
8
8
@@ -38,15 +38,28 @@ function getRegExpMatcher(string) {
38
38
return new RegExp ( escapeRegExp ( string . toLowerCase ( ) ) , 'i' )
39
39
}
40
40
41
- function makeSuggestion ( queryName , content , { variant, name} ) {
41
+ function makeSuggestion ( queryName , element , content , { variant, name} ) {
42
+ let warning = ''
43
+ const queryOptions = { }
42
44
const queryArgs = [
43
45
queryName === 'Role' || queryName === 'TestId'
44
46
? content
45
47
: getRegExpMatcher ( content ) ,
46
48
]
47
49
48
50
if ( name ) {
49
- queryArgs . push ( { name : getRegExpMatcher ( name ) } )
51
+ queryOptions . name = getRegExpMatcher ( name )
52
+ }
53
+
54
+ if ( queryName === 'Role' && isInaccessible ( element ) ) {
55
+ queryOptions . hidden = true
56
+ warning = `Element is inaccessible. This means that the element and all its children are invisible to screen readers.
57
+ If you are using the aria-hidden prop, make sure this is the right choice for your case.
58
+ `
59
+ console . warn ( warning )
60
+ }
61
+ if ( Object . keys ( queryOptions ) . length > 0 ) {
62
+ queryArgs . push ( queryOptions )
50
63
}
51
64
52
65
const queryMethod = `${ variant } By${ queryName } `
@@ -56,6 +69,7 @@ function makeSuggestion(queryName, content, {variant, name}) {
56
69
queryMethod,
57
70
queryArgs,
58
71
variant,
72
+ warning,
59
73
toString ( ) {
60
74
let [ text , options ] = queryArgs
61
75
@@ -90,7 +104,7 @@ export function getSuggestedQuery(element, variant = 'get', method) {
90
104
const role =
91
105
element . getAttribute ( 'role' ) ?? getImplicitAriaRoles ( element ) ?. [ 0 ]
92
106
if ( role !== 'generic' && canSuggest ( 'Role' , method , role ) ) {
93
- return makeSuggestion ( 'Role' , role , {
107
+ return makeSuggestion ( 'Role' , element , role , {
94
108
variant,
95
109
name : computeAccessibleName ( element , {
96
110
computedStyleSupportsPseudoElements : getConfig ( )
@@ -101,36 +115,40 @@ export function getSuggestedQuery(element, variant = 'get', method) {
101
115
102
116
const labelText = getLabelTextFor ( element )
103
117
if ( canSuggest ( 'LabelText' , method , labelText ) ) {
104
- return makeSuggestion ( 'LabelText' , labelText , { variant} )
118
+ return makeSuggestion ( 'LabelText' , element , labelText , { variant} )
105
119
}
106
120
107
121
const placeholderText = element . getAttribute ( 'placeholder' )
108
122
if ( canSuggest ( 'PlaceholderText' , method , placeholderText ) ) {
109
- return makeSuggestion ( 'PlaceholderText' , placeholderText , { variant} )
123
+ return makeSuggestion ( 'PlaceholderText' , element , placeholderText , {
124
+ variant,
125
+ } )
110
126
}
111
127
112
128
const textContent = normalize ( getNodeText ( element ) )
113
129
if ( canSuggest ( 'Text' , method , textContent ) ) {
114
- return makeSuggestion ( 'Text' , textContent , { variant} )
130
+ return makeSuggestion ( 'Text' , element , textContent , { variant} )
115
131
}
116
132
117
133
if ( canSuggest ( 'DisplayValue' , method , element . value ) ) {
118
- return makeSuggestion ( 'DisplayValue' , normalize ( element . value ) , { variant} )
134
+ return makeSuggestion ( 'DisplayValue' , element , normalize ( element . value ) , {
135
+ variant,
136
+ } )
119
137
}
120
138
121
139
const alt = element . getAttribute ( 'alt' )
122
140
if ( canSuggest ( 'AltText' , method , alt ) ) {
123
- return makeSuggestion ( 'AltText' , alt , { variant} )
141
+ return makeSuggestion ( 'AltText' , element , alt , { variant} )
124
142
}
125
143
126
144
const title = element . getAttribute ( 'title' )
127
145
if ( canSuggest ( 'Title' , method , title ) ) {
128
- return makeSuggestion ( 'Title' , title , { variant} )
146
+ return makeSuggestion ( 'Title' , element , title , { variant} )
129
147
}
130
148
131
149
const testId = element . getAttribute ( getConfig ( ) . testIdAttribute )
132
150
if ( canSuggest ( 'TestId' , method , testId ) ) {
133
- return makeSuggestion ( 'TestId' , testId , { variant} )
151
+ return makeSuggestion ( 'TestId' , element , testId , { variant} )
134
152
}
135
153
136
154
return undefined
0 commit comments