1
1
import { getConfig } from '../config'
2
2
import { checkContainerType } from '../helpers'
3
3
import { getLabels , getRealLabels , getLabelContent } from '../label-helpers'
4
+ import { AllByText , GetErrorFunction , Nullish } from '../../types'
4
5
import {
5
6
fuzzyMatches ,
6
7
matches ,
@@ -12,19 +13,21 @@ import {
12
13
wrapSingleQueryWithSuggestion ,
13
14
} from './all-utils'
14
15
15
- function queryAllLabels ( container ) {
16
- return Array . from ( container . querySelectorAll ( 'label,input' ) )
16
+ function queryAllLabels (
17
+ container : HTMLElement ,
18
+ ) : { textToMatch : Nullish < string > ; node : HTMLElement } [ ] {
19
+ return Array . from ( container . querySelectorAll < HTMLElement > ( 'label,input' ) )
17
20
. map ( node => {
18
21
return { node, textToMatch : getLabelContent ( node ) }
19
22
} )
20
23
. filter ( ( { textToMatch} ) => textToMatch !== null )
21
24
}
22
25
23
- function queryAllLabelsByText (
26
+ const queryAllLabelsByText : AllByText = (
24
27
container ,
25
28
text ,
26
29
{ exact = true , trim, collapseWhitespace, normalizer} = { } ,
27
- ) {
30
+ ) => {
28
31
const matcher = exact ? matches : fuzzyMatches
29
32
const matchNormalizer = makeNormalizer ( { collapseWhitespace, trim, normalizer} )
30
33
@@ -37,27 +40,32 @@ function queryAllLabelsByText(
37
40
. map ( ( { node} ) => node )
38
41
}
39
42
40
- function queryAllByLabelText (
43
+ const queryAllByLabelText : AllByText = (
41
44
container ,
42
45
text ,
43
46
{ selector = '*' , exact = true , collapseWhitespace, trim, normalizer} = { } ,
44
- ) {
47
+ ) => {
45
48
checkContainerType ( container )
46
49
47
50
const matcher = exact ? matches : fuzzyMatches
48
51
const matchNormalizer = makeNormalizer ( { collapseWhitespace, trim, normalizer} )
49
- const matchingLabelledElements = Array . from ( container . querySelectorAll ( '*' ) )
52
+ const matchingLabelledElements = Array . from (
53
+ container . querySelectorAll < HTMLElement > ( '*' ) ,
54
+ )
50
55
. filter ( element => {
51
56
return (
52
57
getRealLabels ( element ) . length || element . hasAttribute ( 'aria-labelledby' )
53
58
)
54
59
} )
55
- . reduce ( ( labelledElements , labelledElement ) => {
60
+ . reduce < HTMLElement [ ] > ( ( labelledElements , labelledElement ) => {
56
61
const labelList = getLabels ( container , labelledElement , { selector} )
57
62
labelList
58
63
. filter ( label => Boolean ( label . formControl ) )
59
64
. forEach ( label => {
60
- if ( matcher ( label . content , label . formControl , text , matchNormalizer ) )
65
+ if (
66
+ matcher ( label . content , label . formControl , text , matchNormalizer ) &&
67
+ label . formControl
68
+ )
61
69
labelledElements . push ( label . formControl )
62
70
} )
63
71
const labelsValue = labelList
@@ -92,6 +100,9 @@ function queryAllByLabelText(
92
100
return labelledElements
93
101
} , [ ] )
94
102
. concat (
103
+ // TODO: Remove ignore after `queryAllByAttribute` will be moved to TS
104
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
105
+ // @ts -expect-error
95
106
queryAllByAttribute ( 'aria-label' , container , text , {
96
107
exact,
97
108
normalizer : matchNormalizer ,
@@ -110,7 +121,7 @@ function queryAllByLabelText(
110
121
// )
111
122
// however, we can give a more helpful error message than the generic one,
112
123
// so we're writing this one out by hand.
113
- const getAllByLabelText = ( container , text , ...rest ) => {
124
+ const getAllByLabelText : AllByText = ( container , text , ...rest ) => {
114
125
const els = queryAllByLabelText ( container , text , ...rest )
115
126
if ( ! els . length ) {
116
127
const labels = queryAllLabelsByText ( container , text , ...rest )
@@ -146,7 +157,10 @@ const getAllByLabelText = (container, text, ...rest) => {
146
157
return els
147
158
}
148
159
149
- function getTagNameOfElementAssociatedWithLabelViaFor ( container , label ) {
160
+ function getTagNameOfElementAssociatedWithLabelViaFor (
161
+ container : Element ,
162
+ label : Element ,
163
+ ) : Nullish < string > {
150
164
const htmlFor = label . getAttribute ( 'for' )
151
165
if ( ! htmlFor ) {
152
166
return null
@@ -157,7 +171,7 @@ function getTagNameOfElementAssociatedWithLabelViaFor(container, label) {
157
171
}
158
172
159
173
// the reason mentioned above is the same reason we're not using buildQueries
160
- const getMultipleError = ( c , text ) =>
174
+ const getMultipleError : GetErrorFunction = ( c , text ) =>
161
175
`Found multiple elements with the text of: ${ text } `
162
176
const queryByLabelText = wrapSingleQueryWithSuggestion (
163
177
makeSingleQuery ( queryAllByLabelText , getMultipleError ) ,
0 commit comments