Skip to content

Commit

Permalink
feat(ByRole): improved byRole query performance (#1086)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastian Silbermann <silbermann.sebastian@gmail.com>
  • Loading branch information
kalmi and eps1lon committed Aug 9, 2022
1 parent 4f965e9 commit 0226aea
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions src/role-helpers.js
Expand Up @@ -105,27 +105,32 @@ function buildElementRoleList(elementRolesMap) {
}

function match(element) {
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),
]
}

const selector = makeElementSelector({...element, attributes})

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
}
if (typeTextIndex >= 0 && node.type !== 'text') {
return false
}

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

Expand Down

0 comments on commit 0226aea

Please sign in to comment.