33 * License Apache-2.0
44 */
55
6+ import { normalizeSelector } from './normalize' ;
7+
68/**
79* Finds first matching elements on the page that may be in a shadow root using a complex selector of n-depth
810*
@@ -177,159 +179,3 @@ export function collectAllElementsDeep(selector = null, root, cachedElements = n
177179 return allElements . filter ( el => el . matches ( selector ) ) ;
178180}
179181
180- // normalize-selector-rev-02.js
181- /*
182- author: kyle simpson (@getify)
183- original source: https://gist.github.com/getify/9679380
184-
185- modified for tests by david kaye (@dfkaye)
186- 21 march 2014
187-
188- rev-02 incorporate kyle's changes 3/2/42014
189- */
190- /* istanbul ignore next */
191- function normalizeSelector ( sel ) {
192-
193- // save unmatched text, if any
194- function saveUnmatched ( ) {
195- if ( unmatched ) {
196- // whitespace needed after combinator?
197- if ( tokens . length > 0 &&
198- / ^ [ ~ + > ] $ / . test ( tokens [ tokens . length - 1 ] )
199- ) {
200- tokens . push ( " " ) ;
201- }
202-
203- // save unmatched text
204- tokens . push ( unmatched ) ;
205- }
206- }
207-
208- var tokens = [ ] , match , unmatched , regex , state = [ 0 ] ,
209- next_match_idx = 0 , prev_match_idx ,
210- not_escaped_pattern = / (?: [ ^ \\ ] | (?: ^ | [ ^ \\ ] ) (?: \\ \\ ) + ) $ / ,
211- whitespace_pattern = / ^ \s + $ / ,
212- state_patterns = [
213- / \s + | \/ \* | [ " ' > ~ + \[ \( ] / g, // general
214- / \s + | \/ \* | [ " ' \[ \] \( \) ] / g, // [..] set
215- / \s + | \/ \* | [ " ' \[ \] \( \) ] / g, // (..) set
216- null , // string literal (placeholder)
217- / \* \/ / g // comment
218- ]
219- ;
220-
221- sel = sel . trim ( ) ;
222-
223- while ( true ) {
224- unmatched = "" ;
225-
226- regex = state_patterns [ state [ state . length - 1 ] ] ;
227-
228- regex . lastIndex = next_match_idx ;
229- match = regex . exec ( sel ) ;
230-
231- // matched text to process?
232- if ( match ) {
233- prev_match_idx = next_match_idx ;
234- next_match_idx = regex . lastIndex ;
235-
236- // collect the previous string chunk not matched before this token
237- if ( prev_match_idx < next_match_idx - match [ 0 ] . length ) {
238- unmatched = sel . substring ( prev_match_idx , next_match_idx - match [ 0 ] . length ) ;
239- }
240-
241- // general, [ ] pair, ( ) pair?
242- if ( state [ state . length - 1 ] < 3 ) {
243- saveUnmatched ( ) ;
244-
245- // starting a [ ] pair?
246- if ( match [ 0 ] === "[" ) {
247- state . push ( 1 ) ;
248- }
249- // starting a ( ) pair?
250- else if ( match [ 0 ] === "(" ) {
251- state . push ( 2 ) ;
252- }
253- // starting a string literal?
254- else if ( / ^ [ " ' ] $ / . test ( match [ 0 ] ) ) {
255- state . push ( 3 ) ;
256- state_patterns [ 3 ] = new RegExp ( match [ 0 ] , "g" ) ;
257- }
258- // starting a comment?
259- else if ( match [ 0 ] === "/*" ) {
260- state . push ( 4 ) ;
261- }
262- // ending a [ ] or ( ) pair?
263- else if ( / ^ [ \] \) ] $ / . test ( match [ 0 ] ) && state . length > 0 ) {
264- state . pop ( ) ;
265- }
266- // handling whitespace or a combinator?
267- else if ( / ^ (?: \s + | [ ~ + > ] ) $ / . test ( match [ 0 ] ) ) {
268-
269- // need to insert whitespace before?
270- if ( tokens . length > 0 &&
271- ! whitespace_pattern . test ( tokens [ tokens . length - 1 ] ) &&
272- state [ state . length - 1 ] === 0
273- ) {
274- // add normalized whitespace
275- tokens . push ( " " ) ;
276- }
277-
278- // case-insensitive attribute selector CSS L4
279- if ( state [ state . length - 1 ] === 1 &&
280- tokens . length === 5 &&
281- tokens [ 2 ] . charAt ( tokens [ 2 ] . length - 1 ) === '=' ) {
282- tokens [ 4 ] = " " + tokens [ 4 ] ;
283- }
284-
285- // whitespace token we can skip?
286- if ( whitespace_pattern . test ( match [ 0 ] ) ) {
287- continue ;
288- }
289- }
290-
291- // save matched text
292- tokens . push ( match [ 0 ] ) ;
293- }
294- // otherwise, string literal or comment
295- else {
296- // save unmatched text
297- tokens [ tokens . length - 1 ] += unmatched ;
298-
299- // unescaped terminator to string literal or comment?
300- if ( not_escaped_pattern . test ( tokens [ tokens . length - 1 ] ) ) {
301- // comment terminator?
302- if ( state [ state . length - 1 ] === 4 ) {
303- // ok to drop comment?
304- if ( tokens . length < 2 ||
305- whitespace_pattern . test ( tokens [ tokens . length - 2 ] )
306- ) {
307- tokens . pop ( ) ;
308- }
309- // otherwise, turn comment into whitespace
310- else {
311- tokens [ tokens . length - 1 ] = " " ;
312- }
313-
314- // handled already
315- match [ 0 ] = "" ;
316- }
317-
318- state . pop ( ) ;
319- }
320-
321- // append matched text to existing token
322- tokens [ tokens . length - 1 ] += match [ 0 ] ;
323- }
324- }
325- // otherwise, end of processing (no more matches)
326- else {
327- unmatched = sel . substr ( next_match_idx ) ;
328- saveUnmatched ( ) ;
329-
330- break ;
331- }
332- }
333-
334- return tokens . join ( "" ) . trim ( ) ;
335- }
0 commit comments