11import { getConfig } from '../config'
2- import { checkContainerType } from '../helpers'
2+ import { checkContainerType , TEXT_NODE } from '../helpers'
33import {
44 fuzzyMatches ,
55 matches ,
@@ -11,25 +11,20 @@ import {
1111 wrapSingleQueryWithSuggestion ,
1212} from './all-utils'
1313
14+ const labelledNodeNames = [
15+ 'button' ,
16+ 'meter' ,
17+ 'output' ,
18+ 'progress' ,
19+ 'select' ,
20+ 'textarea' ,
21+ 'input' ,
22+ ]
23+
1424function queryAllLabels ( container ) {
1525 return Array . from ( container . querySelectorAll ( 'label,input' ) )
1626 . map ( node => {
17- let textToMatch =
18- node . tagName . toLowerCase ( ) === 'label'
19- ? node . textContent
20- : node . value || null
21- // The children of a textarea are part of `textContent` as well. We
22- // need to remove them from the string so we can match it afterwards.
23- Array . from ( node . querySelectorAll ( 'textarea' ) ) . forEach ( textarea => {
24- textToMatch = textToMatch . replace ( textarea . value , '' )
25- } )
26-
27- // The children of a select are also part of `textContent`, so we
28- // need also to remove their text.
29- Array . from ( node . querySelectorAll ( 'select' ) ) . forEach ( select => {
30- textToMatch = textToMatch . replace ( select . textContent , '' )
31- } )
32- return { node, textToMatch}
27+ return { node, textToMatch : getLabelContent ( node ) }
3328 } )
3429 . filter ( ( { textToMatch} ) => textToMatch !== null )
3530}
@@ -51,15 +46,26 @@ function queryAllLabelsByText(
5146 . map ( ( { node} ) => node )
5247}
5348
54- function getLabelContent ( label ) {
55- let labelContent = label . getAttribute ( 'value' ) || label . textContent
56- Array . from ( label . querySelectorAll ( 'textarea' ) ) . forEach ( textarea => {
57- labelContent = labelContent . replace ( textarea . value , '' )
58- } )
59- Array . from ( label . querySelectorAll ( 'select' ) ) . forEach ( select => {
60- labelContent = labelContent . replace ( select . textContent , '' )
61- } )
62- return labelContent
49+ function getTextContent ( node ) {
50+ if ( labelledNodeNames . includes ( node . nodeName . toLowerCase ( ) ) ) {
51+ return ''
52+ }
53+
54+ if ( node . nodeType === TEXT_NODE ) return node . textContent
55+
56+ return Array . from ( node . childNodes )
57+ . map ( childNode => getTextContent ( childNode ) )
58+ . join ( '' )
59+ }
60+
61+ function getLabelContent ( node ) {
62+ let textContent
63+ if ( node . tagName . toLowerCase ( ) === 'label' ) {
64+ textContent = getTextContent ( node )
65+ } else {
66+ textContent = node . value || node . textContent
67+ }
68+ return textContent
6369}
6470
6571function queryAllByLabelText (
@@ -88,8 +94,7 @@ function queryAllByLabelText(
8894 } )
8995 : Array . from ( getLabels ( labelledElement ) ) . map ( label => {
9096 const textToMatch = getLabelContent ( label )
91- const formControlSelector =
92- 'button, input, meter, output, progress, select, textarea'
97+ const formControlSelector = labelledNodeNames . join ( ',' )
9398 const labelledFormControl = Array . from (
9499 label . querySelectorAll ( formControlSelector ) ,
95100 ) . filter ( element => element . matches ( selector ) ) [ 0 ]
0 commit comments