11import { getConfig } from '../config'
22import { checkContainerType } from '../helpers'
33import { getLabels , getRealLabels , getLabelContent } from '../label-helpers'
4+ import { AllByText , GetErrorFunction , Nullish } from '../../types'
45import {
56 fuzzyMatches ,
67 matches ,
@@ -12,19 +13,21 @@ import {
1213 wrapSingleQueryWithSuggestion ,
1314} from './all-utils'
1415
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' ) )
1720 . map ( node => {
1821 return { node, textToMatch : getLabelContent ( node ) }
1922 } )
2023 . filter ( ( { textToMatch} ) => textToMatch !== null )
2124}
2225
23- function queryAllLabelsByText (
26+ const queryAllLabelsByText : AllByText = (
2427 container ,
2528 text ,
2629 { exact = true , trim, collapseWhitespace, normalizer} = { } ,
27- ) {
30+ ) => {
2831 const matcher = exact ? matches : fuzzyMatches
2932 const matchNormalizer = makeNormalizer ( { collapseWhitespace, trim, normalizer} )
3033
@@ -37,27 +40,32 @@ function queryAllLabelsByText(
3740 . map ( ( { node} ) => node )
3841}
3942
40- function queryAllByLabelText (
43+ const queryAllByLabelText : AllByText = (
4144 container ,
4245 text ,
4346 { selector = '*' , exact = true , collapseWhitespace, trim, normalizer} = { } ,
44- ) {
47+ ) => {
4548 checkContainerType ( container )
4649
4750 const matcher = exact ? matches : fuzzyMatches
4851 const matchNormalizer = makeNormalizer ( { collapseWhitespace, trim, normalizer} )
49- const matchingLabelledElements = Array . from ( container . querySelectorAll ( '*' ) )
52+ const matchingLabelledElements = Array . from (
53+ container . querySelectorAll < HTMLElement > ( '*' ) ,
54+ )
5055 . filter ( element => {
5156 return (
5257 getRealLabels ( element ) . length || element . hasAttribute ( 'aria-labelledby' )
5358 )
5459 } )
55- . reduce ( ( labelledElements , labelledElement ) => {
60+ . reduce < HTMLElement [ ] > ( ( labelledElements , labelledElement ) => {
5661 const labelList = getLabels ( container , labelledElement , { selector} )
5762 labelList
5863 . filter ( label => Boolean ( label . formControl ) )
5964 . 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+ )
6169 labelledElements . push ( label . formControl )
6270 } )
6371 const labelsValue = labelList
@@ -92,6 +100,9 @@ function queryAllByLabelText(
92100 return labelledElements
93101 } , [ ] )
94102 . 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
95106 queryAllByAttribute ( 'aria-label' , container , text , {
96107 exact,
97108 normalizer : matchNormalizer ,
@@ -110,7 +121,7 @@ function queryAllByLabelText(
110121// )
111122// however, we can give a more helpful error message than the generic one,
112123// so we're writing this one out by hand.
113- const getAllByLabelText = ( container , text , ...rest ) => {
124+ const getAllByLabelText : AllByText = ( container , text , ...rest ) => {
114125 const els = queryAllByLabelText ( container , text , ...rest )
115126 if ( ! els . length ) {
116127 const labels = queryAllLabelsByText ( container , text , ...rest )
@@ -146,7 +157,10 @@ const getAllByLabelText = (container, text, ...rest) => {
146157 return els
147158}
148159
149- function getTagNameOfElementAssociatedWithLabelViaFor ( container , label ) {
160+ function getTagNameOfElementAssociatedWithLabelViaFor (
161+ container : Element ,
162+ label : Element ,
163+ ) : Nullish < string > {
150164 const htmlFor = label . getAttribute ( 'for' )
151165 if ( ! htmlFor ) {
152166 return null
@@ -157,7 +171,7 @@ function getTagNameOfElementAssociatedWithLabelViaFor(container, label) {
157171}
158172
159173// the reason mentioned above is the same reason we're not using buildQueries
160- const getMultipleError = ( c , text ) =>
174+ const getMultipleError : GetErrorFunction = ( c , text ) =>
161175 `Found multiple elements with the text of: ${ text } `
162176const queryByLabelText = wrapSingleQueryWithSuggestion (
163177 makeSingleQuery ( queryAllByLabelText , getMultipleError ) ,
0 commit comments