@@ -12,6 +12,21 @@ import {
12
12
} from './all-utils'
13
13
import { queryAllByText } from './text'
14
14
15
+ function getCombinations ( labels , matcher ) {
16
+ const combs = [ [ ] ]
17
+ const matching = [ ]
18
+ for ( const label of labels ) {
19
+ const copy = [ ...combs ] // See note below.
20
+ for ( const prefix of copy ) {
21
+ const combination = prefix . concat ( label . textToMatch )
22
+ combs . push ( combination )
23
+ if ( matcher ( combination . join ( ' ' ) , label . node ) ) {
24
+ matching . push ( label . node )
25
+ }
26
+ }
27
+ }
28
+ return matching
29
+ }
15
30
function queryAllLabels ( container ) {
16
31
return Array . from ( container . querySelectorAll ( 'label,input' ) )
17
32
. map ( node => {
@@ -45,11 +60,25 @@ function queryAllLabelsByText(
45
60
46
61
const textToMatchByLabels = queryAllLabels ( container )
47
62
48
- return textToMatchByLabels
63
+ const nodesByLabelMatchingText = textToMatchByLabels
49
64
. filter ( ( { node, textToMatch} ) =>
50
65
matcher ( textToMatch , node , text , matchNormalizer ) ,
51
66
)
52
67
. map ( ( { node} ) => node )
68
+ const labelsNotMatchingTextAndNotEmpty = textToMatchByLabels . filter (
69
+ ( { node, textToMatch} ) =>
70
+ Boolean ( textToMatch ) &&
71
+ nodesByLabelMatchingText . findIndex ( nodeByLabelByText =>
72
+ nodeByLabelByText . isEqualNode ( node ) ,
73
+ ) === - 1 ,
74
+ )
75
+
76
+ const concatLabelsMatching = getCombinations (
77
+ labelsNotMatchingTextAndNotEmpty ,
78
+ ( textToMatch , node ) => matcher ( textToMatch , node , text , matchNormalizer ) ,
79
+ )
80
+
81
+ return nodesByLabelMatchingText . concat ( concatLabelsMatching )
53
82
}
54
83
55
84
function queryAllByLabelText (
0 commit comments