Skip to content

Commit

Permalink
fix: use type property instead of selector when matching type=text input
Browse files Browse the repository at this point in the history
close #814
  • Loading branch information
marcosvega91 committed Nov 16, 2020
1 parent 710d57e commit 686492c
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/role-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ function isInaccessible(element, options = {}) {
function getImplicitAriaRoles(currentNode) {
// eslint bug here:
// eslint-disable-next-line no-unused-vars
for (const {selector, roles} of elementRoleList) {
if (currentNode.matches(selector)) {
for (const {match, roles} of elementRoleList) {
if (match(currentNode)) {
return [...roles]
}
}
Expand Down Expand Up @@ -101,6 +101,31 @@ function buildElementRoleList(elementRolesMap) {
return rightSpecificity - leftSpecificity
}

function match(element) {
return node => {
let {attributes = []} = element
// https://github.com/testing-library/dom-testing-library/issues/814
const typeTextIndex = attributes.findIndex(
attribute =>
attribute.value &&
attribute.name === 'type' &&
attribute.value === 'text',
)
if (typeTextIndex >= 0) {
// not using splice to not mutate the attributes array
attributes = [
...attributes.slice(0, typeTextIndex),
...attributes.slice(typeTextIndex + 1),
]
if (node.type !== 'text') {
return false
}
}

return node.matches(makeElementSelector({...element, attributes}))
}
}

let result = []

// eslint bug here:
Expand All @@ -109,7 +134,7 @@ function buildElementRoleList(elementRolesMap) {
result = [
...result,
{
selector: makeElementSelector(element),
match: match(element),
roles: Array.from(roles),
specificity: getSelectorSpecificity(element),
},
Expand Down

0 comments on commit 686492c

Please sign in to comment.