1
1
import { getConfig } from '../config'
2
- import { checkContainerType } from '../helpers'
2
+ import { checkContainerType , TEXT_NODE } from '../helpers'
3
3
import {
4
4
fuzzyMatches ,
5
5
matches ,
@@ -11,25 +11,20 @@ import {
11
11
wrapSingleQueryWithSuggestion ,
12
12
} from './all-utils'
13
13
14
+ const labelledNodeNames = [
15
+ 'button' ,
16
+ 'meter' ,
17
+ 'output' ,
18
+ 'progress' ,
19
+ 'select' ,
20
+ 'textarea' ,
21
+ 'input' ,
22
+ ]
23
+
14
24
function queryAllLabels ( container ) {
15
25
return Array . from ( container . querySelectorAll ( 'label,input' ) )
16
26
. 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 ) }
33
28
} )
34
29
. filter ( ( { textToMatch} ) => textToMatch !== null )
35
30
}
@@ -51,15 +46,26 @@ function queryAllLabelsByText(
51
46
. map ( ( { node} ) => node )
52
47
}
53
48
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
63
69
}
64
70
65
71
function queryAllByLabelText (
@@ -88,8 +94,7 @@ function queryAllByLabelText(
88
94
} )
89
95
: Array . from ( getLabels ( labelledElement ) ) . map ( label => {
90
96
const textToMatch = getLabelContent ( label )
91
- const formControlSelector =
92
- 'button, input, meter, output, progress, select, textarea'
97
+ const formControlSelector = labelledNodeNames . join ( ',' )
93
98
const labelledFormControl = Array . from (
94
99
label . querySelectorAll ( formControlSelector ) ,
95
100
) . filter ( element => element . matches ( selector ) ) [ 0 ]
0 commit comments