Skip to content

Commit 6c08f92

Browse files
committed
Polish
- revert most runtime changes - test over exec because semantics
1 parent 03f20e1 commit 6c08f92

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

src/label-helpers.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,22 @@ function getTextContent(
2424
.join('')
2525
}
2626

27-
function getLabelContent(element: Element | HTMLInputElement) {
28-
let textContent
27+
function getLabelContent(element: Element): string | null {
28+
let textContent: string | null
2929
if (element.tagName.toLowerCase() === 'label') {
3030
textContent = getTextContent(element)
31-
} else if ('value' in element) {
32-
return element.value
3331
} else {
34-
textContent = element.textContent
32+
textContent = (element as HTMLInputElement).value || element.textContent
3533
}
3634
return textContent
3735
}
3836

3937
// Based on https://github.com/eps1lon/dom-accessibility-api/pull/352
40-
function getRealLabels(element: Element | HTMLInputElement) {
41-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
42-
if ('labels' in element && element.labels !== undefined)
43-
return element.labels ?? []
38+
function getRealLabels(element: Element) {
39+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- types are not aware of older browsers that don't implement `labels`
40+
if ((element as HTMLInputElement).labels !== undefined) {
41+
return (element as HTMLInputElement).labels ?? []
42+
}
4443

4544
if (!isLabelable(element)) return []
4645

@@ -49,9 +48,8 @@ function getRealLabels(element: Element | HTMLInputElement) {
4948
}
5049

5150
function isLabelable(element: Element) {
52-
const labelableRegex = /BUTTON|METER|OUTPUT|PROGRESS|SELECT|TEXTAREA/
5351
return (
54-
labelableRegex.test(element.tagName) ||
52+
/BUTTON|METER|OUTPUT|PROGRESS|SELECT|TEXTAREA/.test(element.tagName) ||
5553
(element.tagName === 'INPUT' && element.getAttribute('type') !== 'hidden')
5654
)
5755
}

src/matches.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function assertNotNullOrUndefined<T>(
1212
): asserts matcher is NonNullable<T> {
1313
if (matcher === null || matcher === undefined) {
1414
throw new Error(
15-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
15+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- implicitly converting `T` to `string`
1616
`It looks like ${matcher} was passed instead of a matcher. Did you do something like getByText(${matcher})?`,
1717
)
1818
}

0 commit comments

Comments
 (0)