Skip to content

fix: use node property instead of selector for type=text input #823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/__tests__/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,8 @@ describe('configuration', () => {
}
})
})

test('should find the input using type property instead of attribute', () => {
const {getByRole} = render('<input type="124">')
expect(getByRole('textbox')).not.toBeNull()
})
33 changes: 29 additions & 4 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 All @@ -75,7 +75,7 @@ function getImplicitAriaRoles(currentNode) {
}

function buildElementRoleList(elementRolesMap) {
function makeElementSelector({name, attributes = []}) {
function makeElementSelector({name, attributes}) {
return `${name}${attributes
.map(({name: attributeName, value, constraints = []}) => {
const shouldNotExist = constraints.indexOf('undefined') !== -1
Expand All @@ -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