Skip to content

Commit 01e0242

Browse files
authored
fix: Ignore pseudo elements for accessible name by default (#736)
1 parent 4e62a29 commit 01e0242

File tree

7 files changed

+36
-4
lines changed

7 files changed

+36
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"@babel/runtime": "^7.10.3",
4242
"@types/aria-query": "^4.2.0",
4343
"aria-query": "^4.2.2",
44-
"dom-accessibility-api": "^0.4.6",
44+
"dom-accessibility-api": "^0.5.0",
4545
"pretty-format": "^25.5.0"
4646
},
4747
"devDependencies": {

src/__tests__/role.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,4 +522,24 @@ describe('configuration', () => {
522522
const {getByRole} = render('<div hidden><ul /></div>')
523523
expect(getByRole('list')).not.toBeNull()
524524
})
525+
526+
test('can be configured to consider ::before and ::after for accessible names which logs errors in jsdom', () => {
527+
try {
528+
jest.spyOn(console, 'error').mockImplementation(() => {})
529+
configure({computedStyleSupportsPseudoElements: true})
530+
const {queryByRole} = render('<button>Hello, Dave!</button>')
531+
532+
queryByRole('button', {name: 'Hello, Dave!'})
533+
534+
expect(console.error).toHaveBeenCalledTimes(2)
535+
expect(console.error.mock.calls[0][0]).toMatch(
536+
'Error: Not implemented: window.computedStyle(elt, pseudoElt)',
537+
)
538+
expect(console.error.mock.calls[1][0]).toMatch(
539+
'Error: Not implemented: window.computedStyle(elt, pseudoElt)',
540+
)
541+
} finally {
542+
jest.restoreAllMocks()
543+
}
544+
})
525545
})

src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ let config = {
3232
return error
3333
},
3434
_disableExpensiveErrorDiagnostics: false,
35+
computedStyleSupportsPseudoElements: false,
3536
}
3637

3738
export const DEFAULT_IGNORE_TAGS = 'script, style'

src/queries/role.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,10 @@ function queryAllByRole(
123123
}
124124

125125
return matches(
126-
computeAccessibleName(element),
126+
computeAccessibleName(element, {
127+
computedStyleSupportsPseudoElements: getConfig()
128+
.computedStyleSupportsPseudoElements,
129+
}),
127130
element,
128131
name,
129132
text => text,

src/role-helpers.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {elementRoles} from 'aria-query'
22
import {computeAccessibleName} from 'dom-accessibility-api'
33
import {prettyDOM} from './pretty-dom'
4+
import {getConfig} from './config'
45

56
const elementRoleList = buildElementRoleList(elementRoles)
67

@@ -161,7 +162,10 @@ function prettyRoles(dom, {hidden}) {
161162
const delimiterBar = '-'.repeat(50)
162163
const elementsString = elements
163164
.map(el => {
164-
const nameString = `Name "${computeAccessibleName(el)}":\n`
165+
const nameString = `Name "${computeAccessibleName(el, {
166+
computedStyleSupportsPseudoElements: getConfig()
167+
.computedStyleSupportsPseudoElements,
168+
})}":\n`
165169
const domString = prettyDOM(el.cloneNode(false))
166170
return `${nameString}${domString}`
167171
})

src/suggestions.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ export function getSuggestedQuery(element, variant = 'get', method) {
9292
if (role !== 'generic' && canSuggest('Role', method, role)) {
9393
return makeSuggestion('Role', role, {
9494
variant,
95-
name: computeAccessibleName(element),
95+
name: computeAccessibleName(element, {
96+
computedStyleSupportsPseudoElements: getConfig()
97+
.computedStyleSupportsPseudoElements,
98+
}),
9699
})
97100
}
98101

types/config.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export interface Config {
33
asyncWrapper(cb: (...args: any[]) => any): Promise<any>
44
eventWrapper(cb: (...args: any[]) => any): void
55
asyncUtilTimeout: number
6+
computedStyleSupportsPseudoElements: boolean
67
defaultHidden: boolean
78
showOriginalStrackTrace: boolean
89
throwSuggestions: boolean

0 commit comments

Comments
 (0)