Skip to content

Commit 9942434

Browse files
committed
chore: migrate label-helpers to typescript
1 parent 2da06c8 commit 9942434

File tree

6 files changed

+30
-20
lines changed

6 files changed

+30
-20
lines changed

src/__tests__/label-helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {getRealLabels} from '../label-helpers'
1+
import {getRealLabels} from '../label-helpers.ts'
22

33
test('hidden inputs are not labelable', () => {
44
const element = document.createElement('input')

src/label-helpers.js renamed to src/label-helpers.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {isNotNull} from './utils'
12
import {TEXT_NODE} from './helpers'
23

34
const labelledNodeNames = [
@@ -10,7 +11,9 @@ const labelledNodeNames = [
1011
'input',
1112
]
1213

13-
function getTextContent(node) {
14+
function getTextContent(
15+
node: Node | Element | HTMLInputElement,
16+
): string | null {
1417
if (labelledNodeNames.includes(node.nodeName.toLowerCase())) {
1518
return ''
1619
}
@@ -22,37 +25,46 @@ function getTextContent(node) {
2225
.join('')
2326
}
2427

25-
function getLabelContent(node) {
28+
function getLabelContent(node: Node | Element | HTMLInputElement) {
2629
let textContent
27-
if (node.tagName.toLowerCase() === 'label') {
30+
if ('tagName' in node && node.tagName.toLowerCase() === 'label') {
2831
textContent = getTextContent(node)
32+
} else if ('value' in node) {
33+
textContent = node.value
2934
} else {
30-
textContent = node.value || node.textContent
35+
textContent = node.textContent
3136
}
3237
return textContent
3338
}
3439

3540
// Based on https://github.com/eps1lon/dom-accessibility-api/pull/352
36-
function getRealLabels(element) {
37-
if (element.labels !== undefined) return element.labels ?? []
41+
function getRealLabels(element: Element | HTMLInputElement) {
42+
// for old browsers
43+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
44+
if ('labels' in element && element.labels !== undefined)
45+
return element.labels ?? []
3846

3947
if (!isLabelable(element)) return []
4048

4149
const labels = element.ownerDocument.querySelectorAll('label')
4250
return Array.from(labels).filter(label => label.control === element)
4351
}
4452

45-
function isLabelable(element) {
53+
function isLabelable(element: Element) {
54+
const labelableRegex = /BUTTON|METER|OUTPUT|PROGRESS|SELECT|TEXTAREA/
4655
return (
47-
element.tagName.match(/BUTTON|METER|OUTPUT|PROGRESS|SELECT|TEXTAREA/) ||
56+
labelableRegex.test(element.tagName) ||
4857
(element.tagName === 'INPUT' && element.getAttribute('type') !== 'hidden')
4958
)
5059
}
5160

52-
function getLabels(container, element, {selector = '*'} = {}) {
53-
const labelsId = element.getAttribute('aria-labelledby')
54-
? element.getAttribute('aria-labelledby').split(' ')
55-
: []
61+
function getLabels(
62+
container: Element,
63+
element: Element,
64+
{selector = '*'} = {},
65+
) {
66+
const ariaLabelledBy = element.getAttribute('aria-labelledby')
67+
const labelsId = isNotNull(ariaLabelledBy) ? ariaLabelledBy.split(' ') : []
5668
return labelsId.length
5769
? labelsId.map(labelId => {
5870
const labellingElement = container.querySelector(`[id="${labelId}"]`)
@@ -67,7 +79,6 @@ function getLabels(container, element, {selector = '*'} = {}) {
6779
const labelledFormControl = Array.from(
6880
label.querySelectorAll(formControlSelector),
6981
).filter(formControlElement => formControlElement.matches(selector))[0]
70-
7182
return {content: textToMatch, formControl: labelledFormControl}
7283
})
7384
}

src/queries/label-text.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {getConfig} from '../config.ts'
22
import {checkContainerType} from '../helpers'
3-
import {getLabels, getRealLabels, getLabelContent} from '../label-helpers'
3+
import {getLabels, getRealLabels, getLabelContent} from '../label-helpers.ts'
44
import {
55
fuzzyMatches,
66
matches,

src/suggestions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {getDefaultNormalizer} from './matches.ts'
33
import {getNodeText} from './get-node-text'
44
import {DEFAULT_IGNORE_TAGS, getConfig} from './config.ts'
55
import {getImplicitAriaRoles, isInaccessible} from './role-helpers'
6-
import {getLabels} from './label-helpers'
6+
import {getLabels} from './label-helpers.ts'
77

88
const normalize = getDefaultNormalizer()
99

src/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function isNotNull<T>(arg: T): arg is NonNullable<T> {
2+
return arg !== null
3+
}

types/utils.d.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
export type Nullish<T> = T | null | undefined
22

33
export type Callback<T> = () => T
4-
5-
export function isNotNull<T>(arg: T): arg is NonNullable<T> {
6-
return arg !== null
7-
}

0 commit comments

Comments
 (0)