Skip to content

Commit 172aa5b

Browse files
committed
fix: use type property instead of selector when matching type=text input
close #814
1 parent f638ec0 commit 172aa5b

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/role-helpers.js

Lines changed: 28 additions & 3 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
}
@@ -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)