11var escapeStringRegexp = require ( 'escape-string-regexp' ) ;
22
3- var isPieceIsAlwaysAncestorSelector = require ( './is-piece-always-ancestor-selector' ) ;
3+ var isPieceAlwaysAncestorSelector = require ( './is-piece-always-ancestor-selector' ) ;
4+ var generateDirectDescendantPiecesFromSelector = require ( './generate-direct-descendant-pieces-from-selector' ) ;
45
5- // Given the nodes scope, and the target scope,
6- // Is the node in the same or under the target scope (cascade wise)
7- //
8- // Another way to think about it: Can the target cascade properties to the node?
9- //
10- // For scope-lists see: `generateScopeList`
11- var isUnderScope = function ( nodeScopeList , scopeNodeScopeList ) {
12- var matchesScope = scopeNodeScopeList . some ( function ( scopeNodeScopePieces ) {
6+
7+
8+ function asdfqwer ( nodeScopeList , scopeNodeScopeList ) {
9+ var currentPieceOffset ;
10+ var scopePieceIndex ;
11+
12+ // Check each comma separated piece of the complex selector
13+ var doesMatchScope = scopeNodeScopeList . some ( function ( scopeNodeScopePieces ) {
1314 return nodeScopeList . some ( function ( nodeScopePieces ) {
14- var currentPieceOffset ;
15- var wasEveryPieceFound = scopeNodeScopePieces . every ( function ( scopePiece ) {
15+
16+
17+ currentPieceOffset = null ;
18+ var wasEveryPieceFound = true ;
19+ // scopeNodeScopePieces.every(function(scopePiece) {
20+ for ( scopePieceIndex = 0 ; scopePieceIndex < scopeNodeScopePieces . length ; scopePieceIndex ++ ) {
21+ var scopePiece = scopeNodeScopePieces [ scopePieceIndex ] ;
1622 var pieceOffset = currentPieceOffset || 0 ;
1723
1824 var foundIndex = - 1 ;
19- var firstAlwaysAncestorPieceIndex = - 1 ;
2025 // Look through the remaining pieces(start from the offset)
2126 var piecesWeCanMatch = nodeScopePieces . slice ( pieceOffset ) ;
22- piecesWeCanMatch . some ( function ( nodeScopePiece , index ) {
23- var overallIndex = pieceOffset + index ;
24-
25- if ( firstAlwaysAncestorPieceIndex < 0 && isPieceIsAlwaysAncestorSelector ( nodeScopePiece ) ) {
26- firstAlwaysAncestorPieceIndex = overallIndex ;
27- }
27+ //piecesWeCanMatch.some(function(nodeScopePiece, index) {
28+ for ( var nodeScopePieceIndex = 0 ; nodeScopePieceIndex < piecesWeCanMatch . length ; nodeScopePieceIndex ++ ) {
29+ var nodeScopePiece = piecesWeCanMatch [ nodeScopePieceIndex ] ;
30+ var overallIndex = pieceOffset + nodeScopePieceIndex ;
2831
2932 // Find the scope piece at the end of the node selector
3033 // Last-occurence
31- if ( new RegExp ( escapeStringRegexp ( scopePiece ) + '$' ) . test ( nodeScopePiece ) ) {
34+ if (
35+ // If the part on the end of the piece itself matches:
36+ // scopePiece `.bar` matches node `.bar`
37+ // scopePiece `.bar` matches node `.foo + .bar`
38+ new RegExp ( escapeStringRegexp ( scopePiece ) + '$' ) . test ( nodeScopePiece )
39+ ) {
3240 foundIndex = overallIndex ;
33- // Escape
34- return true ;
41+ break ;
42+ }
43+
44+
45+ // If the scope piece is a always-ancestor, then it is valid no matter what
46+ //
47+ // Or the node scope piece could be an always-ancestor selector itself
48+ // And we only want the first occurence so we can keep matching future scope pieces
49+ if ( isPieceAlwaysAncestorSelector ( scopePiece ) || isPieceAlwaysAncestorSelector ( nodeScopePiece ) ) {
50+ foundIndex = overallIndex ;
51+
52+ break ;
53+ }
54+
55+
56+ // Handle any direct descendant operators in each piece
57+ var directDescendantPieces = generateDirectDescendantPiecesFromSelector ( nodeScopePiece ) ;
58+ if ( directDescendantPieces . length > 1 ) {
59+
60+ var ddNodeScopeList = [ ] . concat ( [ directDescendantPieces ] ) ;
61+ var ddScopeList = [ ] . concat ( [
62+ scopeNodeScopePieces
63+ . slice ( scopePieceIndex )
64+ . reduce ( function ( prevScopePieces , scopePiece ) {
65+ return prevScopePieces . concat ( generateDirectDescendantPiecesFromSelector ( scopePiece ) ) ;
66+ } , [ ] )
67+ ] ) ;
68+ var result = asdfqwer ( ddNodeScopeList , ddScopeList ) ;
69+
70+ // If it matches completely
71+ // or there are still more pieces to match in the future
72+ if ( result . doesMatchScope || scopePieceIndex + 1 < scopeNodeScopePieces . length ) {
73+ foundIndex = overallIndex ;
74+ // -1 because the fo loop increments at the top
75+ scopePieceIndex += result . scopePieceIndex - 1 ;
76+ }
77+
78+ break ;
79+ }
80+
81+ if ( directDescendantPieces . length > 1 ) {
82+ var asdf = scopeNodeScopePieces . slice ( scopePieceIndex ) ;
83+
3584 }
36- return false ;
37- } ) ;
38- // If the scope piece is a always-ancestor, then it is valid no matter what
39- if ( foundIndex < 0 && isPieceIsAlwaysAncestorSelector ( scopePiece ) ) {
40- foundIndex = pieceOffset + 1 ;
41- }
42- // The piece could be a always-ancestor selector itself
43- // And we only want the first occurence so we can keep matching future scope pieces
44- else if ( foundIndex < 0 && firstAlwaysAncestorPieceIndex > 0 ) {
45- foundIndex = firstAlwaysAncestorPieceIndex ;
4685 }
86+
4787
48- var isFurther = foundIndex > pieceOffset || ( foundIndex >= 0 && currentPieceOffset === undefined ) ;
88+ var isFurther = foundIndex >= pieceOffset ;
4989
50- currentPieceOffset = foundIndex ;
51- return isFurther ;
52- } ) ;
90+ currentPieceOffset = foundIndex + 1 ;
91+
92+ wasEveryPieceFound = wasEveryPieceFound && isFurther ;
93+ if ( ! wasEveryPieceFound ) {
94+ break ;
95+ }
96+ }
5397
5498 return wasEveryPieceFound ;
5599 } ) ;
56100 } ) ;
57101
58- return matchesScope ;
102+ return {
103+ doesMatchScope : doesMatchScope ,
104+ nodeScopePieceIndex : currentPieceOffset - 1 ,
105+ scopePieceIndex : scopePieceIndex
106+ } ;
107+ }
108+
109+
110+
111+
112+
113+ // Given the nodes scope, and the target scope,
114+ // Is the node in the same or under the target scope (cascade wise)
115+ //
116+ // Another way to think about it: Can the target scope cascade properties to the node?
117+ //
118+ // For scope-lists see: `generateScopeList`
119+ var isUnderScope = function isUnderScope ( nodeScopeList , scopeNodeScopeList ) {
120+ return asdfqwer ( nodeScopeList , scopeNodeScopeList ) . doesMatchScope ;
59121} ;
60122
61123module . exports = isUnderScope ;
0 commit comments