@@ -332,14 +332,23 @@ export class SelectorPrinting {
332
332
}
333
333
334
334
public selectorToMarkedString ( node : nodes . Selector ) : MarkedString [ ] {
335
- const root = selectorToElement ( node ) ;
336
- if ( root ) {
337
- const markedStrings = new MarkedStringPrinter ( '"' ) . print ( root ) ;
338
- markedStrings . push ( this . selectorToSpecificityMarkedString ( node ) ) ;
339
- return markedStrings ;
340
- } else {
341
- return [ ] ;
335
+ let hasChild : boolean = true ;
336
+ let markedStrings : Array < Array < MarkedString > > = Array ( ) ;
337
+ let childNumber : number = 0 ;
338
+ let childCount : number = 0 ;
339
+ while ( hasChild ) {
340
+ const elementObject = selectorToElement ( node , childNumber ) ;
341
+ let root = elementObject . root ;
342
+ hasChild = elementObject . hasChild ;
343
+ childNumber += 1 ;
344
+ if ( ( hasChild || ! childCount ) && root ) {
345
+ childCount += 1 ;
346
+ const markedString = new MarkedStringPrinter ( '"' ) . print ( root ) ;
347
+ markedString . push ( this . selectorToSpecificityMarkedString ( node ) ) ;
348
+ markedStrings . push ( markedString ) ;
349
+ }
342
350
}
351
+ return markedStrings . flat ( 1 ) ;
343
352
}
344
353
345
354
public simpleSelectorToMarkedString ( node : nodes . SimpleSelector ) : MarkedString [ ] {
@@ -452,7 +461,7 @@ export class SelectorPrinting {
452
461
return specificity ;
453
462
} ;
454
463
455
- const specificity = calculateScore ( node ) ; ;
464
+ const specificity = calculateScore ( node ) ;
456
465
return localize ( 'specificity' , "[Selector Specificity](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity): ({0}, {1}, {2})" , specificity . id , specificity . attr , specificity . tag ) ;
457
466
}
458
467
@@ -525,10 +534,17 @@ function isNewSelectorContext(node: nodes.Node): boolean {
525
534
}
526
535
return false ;
527
536
}
537
+ interface ElementObject {
538
+ root : Element | null ,
539
+ hasChild : boolean
540
+ }
528
541
529
- export function selectorToElement ( node : nodes . Selector ) : Element | null {
542
+ export function selectorToElement ( node : nodes . Selector , child : number = 0 ) : ElementObject {
530
543
if ( node . matches ( '@at-root' ) ) {
531
- return null ;
544
+ return {
545
+ root : null ,
546
+ hasChild : false
547
+ } ;
532
548
}
533
549
const root : Element = new RootElement ( ) ;
534
550
const parentRuleSets : nodes . RuleSet [ ] = [ ] ;
@@ -548,14 +564,20 @@ export function selectorToElement(node: nodes.Selector): Element | null {
548
564
}
549
565
550
566
const builder = new SelectorElementBuilder ( root ) ;
551
-
567
+ let hasChild : boolean = false ;
552
568
for ( let i = parentRuleSets . length - 1 ; i >= 0 ; i -- ) {
553
- const selector = < nodes . Selector > parentRuleSets [ i ] . getSelectors ( ) . getChild ( 0 ) ;
569
+ const selector = < nodes . Selector > parentRuleSets [ i ] . getSelectors ( ) . getChild ( i === 0 ? child : 0 ) ;
554
570
if ( selector ) {
571
+ hasChild = true ;
555
572
builder . processSelector ( selector ) ;
573
+ } else {
574
+ hasChild = false ;
556
575
}
557
576
}
558
577
559
578
builder . processSelector ( node ) ;
560
- return root ;
579
+ return {
580
+ root,
581
+ hasChild
582
+ } ;
561
583
}
0 commit comments