Skip to content

Commit 0226aea

Browse files
kalmieps1lon
andauthored
feat(ByRole): improved byRole query performance (#1086)
Co-authored-by: Sebastian Silbermann <[email protected]>
1 parent 4f965e9 commit 0226aea

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

src/role-helpers.js

+23-18
Original file line numberDiff line numberDiff line change
@@ -105,27 +105,32 @@ function buildElementRoleList(elementRolesMap) {
105105
}
106106

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

128-
return node.matches(makeElementSelector({...element, attributes}))
133+
return node.matches(selector)
129134
}
130135
}
131136

0 commit comments

Comments
 (0)