@@ -291,14 +291,17 @@ function apply_selector(blocks, node, to_encapsulate) {
291
291
}
292
292
return false ;
293
293
} else if ( block . combinator . name === '+' || block . combinator . name === '~' ) {
294
- const siblings = get_possible_element_siblings ( node , block . combinator . name === '+' ) ;
294
+ const [ siblings , has_slot_sibling ] = get_possible_element_siblings (
295
+ node ,
296
+ block . combinator . name === '+'
297
+ ) ;
295
298
let has_match = false ;
296
299
// NOTE: if we have :global(), we couldn't figure out what is selected within `:global` due to the
297
300
// css-tree limitation that does not parse the inner selector of :global
298
301
// so unless we are sure there will be no sibling to match, we will consider it as matched
299
302
const has_global = blocks . some ( ( block ) => block . global ) ;
300
303
if ( has_global ) {
301
- if ( siblings . size === 0 && get_element_parent ( node ) !== null ) {
304
+ if ( siblings . size === 0 && get_element_parent ( node ) !== null && ! has_slot_sibling ) {
302
305
return false ;
303
306
}
304
307
to_encapsulate . push ( { node, block } ) ;
@@ -542,13 +545,15 @@ function get_element_parent(node) {
542
545
* <h1>Heading 1</h1>
543
546
* <h2>Heading 2</h2>
544
547
* @param {import('../nodes/interfaces.js').INode } node
545
- * @returns {import('../nodes/interfaces.js').INode }
548
+ * @returns {[ import('../nodes/interfaces.js').INode, boolean] }
546
549
*/
547
550
function find_previous_sibling ( node ) {
548
551
/** @type {import('../nodes/interfaces.js').INode } */
549
552
let current_node = node ;
553
+ let has_slot_sibling = false ;
550
554
do {
551
555
if ( current_node . type === 'Slot' ) {
556
+ has_slot_sibling = true ;
552
557
const slot_children = current_node . children ;
553
558
if ( slot_children . length > 0 ) {
554
559
current_node = slot_children . slice ( - 1 ) [ 0 ] ; // go to its last child first
@@ -560,21 +565,24 @@ function find_previous_sibling(node) {
560
565
}
561
566
current_node = current_node . prev ;
562
567
} while ( current_node && current_node . type === 'Slot' ) ;
563
- return current_node ;
568
+ return [ current_node , has_slot_sibling ] ;
564
569
}
565
570
566
571
/**
567
572
* @param {import('../nodes/interfaces.js').INode } node
568
573
* @param {boolean } adjacent_only
569
- * @returns {Map<import('../nodes/Element.js').default, NodeExistsValue> }
574
+ * @returns {[ Map<import('../nodes/Element.js').default, NodeExistsValue>, boolean] }
570
575
*/
571
576
function get_possible_element_siblings ( node , adjacent_only ) {
572
577
/** @type {Map<import('../nodes/Element.js').default, NodeExistsValue> } */
573
578
const result = new Map ( ) ;
574
579
575
580
/** @type {import('../nodes/interfaces.js').INode } */
576
581
let prev = node ;
577
- while ( ( prev = find_previous_sibling ( prev ) ) ) {
582
+ let has_slot_sibling = false ;
583
+ let slot_sibling_found = false ;
584
+ while ( ( [ prev , slot_sibling_found ] = find_previous_sibling ( prev ) ) && prev ) {
585
+ has_slot_sibling = has_slot_sibling || slot_sibling_found ;
578
586
if ( prev . type === 'Element' ) {
579
587
if (
580
588
! prev . attributes . find (
@@ -590,7 +598,7 @@ function get_possible_element_siblings(node, adjacent_only) {
590
598
const possible_last_child = get_possible_last_child ( prev , adjacent_only ) ;
591
599
add_to_map ( possible_last_child , result ) ;
592
600
if ( adjacent_only && has_definite_elements ( possible_last_child ) ) {
593
- return result ;
601
+ return [ result , has_slot_sibling ] ;
594
602
}
595
603
}
596
604
}
@@ -605,7 +613,11 @@ function get_possible_element_siblings(node, adjacent_only) {
605
613
parent . type === 'ElseBlock' ||
606
614
parent . type === 'AwaitBlock' )
607
615
) {
608
- const possible_siblings = get_possible_element_siblings ( parent , adjacent_only ) ;
616
+ const [ possible_siblings , slot_sibling_found ] = get_possible_element_siblings (
617
+ parent ,
618
+ adjacent_only
619
+ ) ;
620
+ has_slot_sibling = has_slot_sibling || slot_sibling_found ;
609
621
add_to_map ( possible_siblings , result ) ;
610
622
if ( parent . type === 'EachBlock' ) {
611
623
// first child of each block can select the last child of each block as previous sibling
@@ -623,7 +635,7 @@ function get_possible_element_siblings(node, adjacent_only) {
623
635
}
624
636
}
625
637
}
626
- return result ;
638
+ return [ result , has_slot_sibling ] ;
627
639
}
628
640
629
641
/**
0 commit comments