@@ -86,21 +86,33 @@ function queryInternal(root: SelectorRoot, matcher: Matcher, shadow: boolean): E
86
86
const shadowRoots : ShadowRoot [ ] = [ ] ;
87
87
if ( shadow && ( root as Element ) . shadowRoot )
88
88
shadowRoots . push ( ( root as Element ) . shadowRoot ! ) ;
89
- while ( walker . nextNode ( ) ) {
90
- const node = walker . currentNode ;
91
- if ( node . nodeType === Node . ELEMENT_NODE ) {
89
+
90
+ let lastTextParent : Element | null = null ;
91
+ let lastText = '' ;
92
+ while ( true ) {
93
+ const node = walker . nextNode ( ) ;
94
+
95
+ const textParent = ( node && node . nodeType === Node . TEXT_NODE ) ? node . parentElement : null ;
96
+ if ( lastTextParent && textParent !== lastTextParent ) {
97
+ if ( lastTextParent . nodeName !== 'SCRIPT' && lastTextParent . nodeName !== 'STYLE' && matcher ( lastText ) )
98
+ return lastTextParent ;
99
+ lastText = '' ;
100
+ }
101
+ lastTextParent = textParent ;
102
+
103
+ if ( ! node )
104
+ break ;
105
+ if ( node . nodeType === Node . TEXT_NODE ) {
106
+ lastText += node . nodeValue ;
107
+ } else {
92
108
const element = node as Element ;
93
109
if ( ( element instanceof HTMLInputElement ) && ( element . type === 'submit' || element . type === 'button' ) && matcher ( element . value ) )
94
110
return element ;
95
111
if ( shadow && element . shadowRoot )
96
112
shadowRoots . push ( element . shadowRoot ) ;
97
- } else {
98
- const element = node . parentElement ;
99
- const text = node . nodeValue ;
100
- if ( element && element . nodeName !== 'SCRIPT' && element . nodeName !== 'STYLE' && text && matcher ( text ) )
101
- return element ;
102
113
}
103
114
}
115
+
104
116
for ( const shadowRoot of shadowRoots ) {
105
117
const element = queryInternal ( shadowRoot , matcher , shadow ) ;
106
118
if ( element )
@@ -114,21 +126,33 @@ function queryAllInternal(root: SelectorRoot, matcher: Matcher, shadow: boolean,
114
126
const shadowRoots : ShadowRoot [ ] = [ ] ;
115
127
if ( shadow && ( root as Element ) . shadowRoot )
116
128
shadowRoots . push ( ( root as Element ) . shadowRoot ! ) ;
117
- while ( walker . nextNode ( ) ) {
118
- const node = walker . currentNode ;
119
- if ( node . nodeType === Node . ELEMENT_NODE ) {
129
+
130
+ let lastTextParent : Element | null = null ;
131
+ let lastText = '' ;
132
+ while ( true ) {
133
+ const node = walker . nextNode ( ) ;
134
+
135
+ const textParent = ( node && node . nodeType === Node . TEXT_NODE ) ? node . parentElement : null ;
136
+ if ( lastTextParent && textParent !== lastTextParent ) {
137
+ if ( lastTextParent . nodeName !== 'SCRIPT' && lastTextParent . nodeName !== 'STYLE' && matcher ( lastText ) )
138
+ result . push ( lastTextParent ) ;
139
+ lastText = '' ;
140
+ }
141
+ lastTextParent = textParent ;
142
+
143
+ if ( ! node )
144
+ break ;
145
+ if ( node . nodeType === Node . TEXT_NODE ) {
146
+ lastText += node . nodeValue ;
147
+ } else {
120
148
const element = node as Element ;
121
149
if ( ( element instanceof HTMLInputElement ) && ( element . type === 'submit' || element . type === 'button' ) && matcher ( element . value ) )
122
150
result . push ( element ) ;
123
151
if ( shadow && element . shadowRoot )
124
152
shadowRoots . push ( element . shadowRoot ) ;
125
- } else {
126
- const element = node . parentElement ;
127
- const text = node . nodeValue ;
128
- if ( element && element . nodeName !== 'SCRIPT' && element . nodeName !== 'STYLE' && text && matcher ( text ) )
129
- result . push ( element ) ;
130
153
}
131
154
}
155
+
132
156
for ( const shadowRoot of shadowRoots )
133
157
queryAllInternal ( shadowRoot , matcher , shadow , result ) ;
134
158
}
0 commit comments