Skip to content

Commit 7e5da11

Browse files
authored
fix: use node property instead of selector for type=text input (#823)
1 parent d774028 commit 7e5da11

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/__tests__/role.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,3 +543,8 @@ describe('configuration', () => {
543543
}
544544
})
545545
})
546+
547+
test('should find the input using type property instead of attribute', () => {
548+
const {getByRole} = render('<input type="124">')
549+
expect(getByRole('textbox')).not.toBeNull()
550+
})

src/role-helpers.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ function isInaccessible(element, options = {}) {
6565
function getImplicitAriaRoles(currentNode) {
6666
// eslint bug here:
6767
// eslint-disable-next-line no-unused-vars
68-
for (const {selector, roles} of elementRoleList) {
69-
if (currentNode.matches(selector)) {
68+
for (const {match, roles} of elementRoleList) {
69+
if (match(currentNode)) {
7070
return [...roles]
7171
}
7272
}
@@ -75,7 +75,7 @@ function getImplicitAriaRoles(currentNode) {
7575
}
7676

7777
function buildElementRoleList(elementRolesMap) {
78-
function makeElementSelector({name, attributes = []}) {
78+
function makeElementSelector({name, attributes}) {
7979
return `${name}${attributes
8080
.map(({name: attributeName, value, constraints = []}) => {
8181
const shouldNotExist = constraints.indexOf('undefined') !== -1
@@ -101,6 +101,31 @@ function buildElementRoleList(elementRolesMap) {
101101
return rightSpecificity - leftSpecificity
102102
}
103103

104+
function match(element) {
105+
return node => {
106+
let {attributes = []} = element
107+
// https://github.com/testing-library/dom-testing-library/issues/814
108+
const typeTextIndex = attributes.findIndex(
109+
attribute =>
110+
attribute.value &&
111+
attribute.name === 'type' &&
112+
attribute.value === 'text',
113+
)
114+
if (typeTextIndex >= 0) {
115+
// not using splice to not mutate the attributes array
116+
attributes = [
117+
...attributes.slice(0, typeTextIndex),
118+
...attributes.slice(typeTextIndex + 1),
119+
]
120+
if (node.type !== 'text') {
121+
return false
122+
}
123+
}
124+
125+
return node.matches(makeElementSelector({...element, attributes}))
126+
}
127+
}
128+
104129
let result = []
105130

106131
// eslint bug here:
@@ -109,7 +134,7 @@ function buildElementRoleList(elementRolesMap) {
109134
result = [
110135
...result,
111136
{
112-
selector: makeElementSelector(element),
137+
match: match(element),
113138
roles: Array.from(roles),
114139
specificity: getSelectorSpecificity(element),
115140
},

0 commit comments

Comments
 (0)