@@ -607,6 +607,33 @@ function pickStyleKeys( treeToPickFrom ) {
607
607
return Object . fromEntries ( clonedEntries ) ;
608
608
}
609
609
610
+ function scopeFeatureSelectors ( scope , selectors ) {
611
+ if ( ! scope || ! selectors ) {
612
+ return ;
613
+ }
614
+
615
+ const featureSelectors = JSON . parse ( JSON . stringify ( selectors ) ) ;
616
+
617
+ Object . entries ( selectors ) . forEach ( ( [ feature , selector ] ) => {
618
+ if ( typeof selector === 'string' ) {
619
+ featureSelectors [ feature ] = scopeSelector ( scope , selector ) ;
620
+ }
621
+
622
+ if ( typeof selector === 'object' ) {
623
+ Object . entries ( selector ) . forEach (
624
+ ( [ subfeature , subfeatureSelector ] ) => {
625
+ featureSelectors [ feature ] [ subfeature ] = scopeSelector (
626
+ scope ,
627
+ subfeatureSelector
628
+ ) ;
629
+ }
630
+ ) ;
631
+ }
632
+ } ) ;
633
+
634
+ return featureSelectors ;
635
+ }
636
+
610
637
export const getNodesWithStyles = ( tree , blockSelectors ) => {
611
638
const nodes = [ ] ;
612
639
@@ -639,14 +666,78 @@ export const getNodesWithStyles = ( tree, blockSelectors ) => {
639
666
640
667
if ( node ?. variations ) {
641
668
const variations = { } ;
642
- Object . keys ( node . variations ) . forEach ( ( variation ) => {
643
- variations [ variation ] = pickStyleKeys (
644
- node . variations [ variation ]
645
- ) ;
646
- } ) ;
669
+
670
+ Object . entries ( node . variations ) . forEach (
671
+ ( [ variationName , variation ] ) => {
672
+ variations [ variationName ] =
673
+ pickStyleKeys ( variation ) ;
674
+ const variationSelector =
675
+ blockSelectors [ blockName ] . styleVariationSelectors [
676
+ variationName
677
+ ] ;
678
+
679
+ // Process the variations inner block type styles.
680
+ Object . entries ( variation . blocks ?? { } ) . forEach (
681
+ ( [
682
+ variationBlockName ,
683
+ variationBlockStyles ,
684
+ ] ) => {
685
+ const variationBlockSelector = scopeSelector (
686
+ variationSelector ,
687
+ blockSelectors [ variationBlockName ]
688
+ . selector
689
+ ) ;
690
+ const variationDuotoneSelector = scopeSelector (
691
+ variationSelector ,
692
+ blockSelectors [ variationBlockName ]
693
+ . duotoneSelector
694
+ ) ;
695
+ const variationFeatureSelectors =
696
+ scopeFeatureSelectors (
697
+ variationSelector ,
698
+ blockSelectors [ variationBlockName ]
699
+ . featureSelectors
700
+ ) ;
701
+
702
+ // TODO: Do we need to delay pushing these nodes so they come after the original block's node?
703
+ nodes . push ( {
704
+ selector : variationBlockSelector ,
705
+ duotoneSelector : variationDuotoneSelector ,
706
+ featureSelectors : variationFeatureSelectors ,
707
+ fallbackGapValue :
708
+ blockSelectors [ variationBlockName ]
709
+ . fallbackGapValue ,
710
+ hasLayoutSupport :
711
+ blockSelectors [ variationBlockName ]
712
+ . hasLayoutSupport ,
713
+ styles : pickStyleKeys (
714
+ variationBlockStyles
715
+ ) ,
716
+ } ) ;
717
+ }
718
+ ) ;
719
+
720
+ // Process the variations inner element styles.
721
+ Object . entries ( variation . elements ?? { } ) . forEach (
722
+ ( [ element , elementStyles ] ) => {
723
+ if ( elementStyles && ELEMENTS [ element ] ) {
724
+ nodes . push ( {
725
+ styles : elementStyles ,
726
+ selector : scopeSelector (
727
+ variationSelector ,
728
+ ELEMENTS [ element ]
729
+ ) ,
730
+ } ) ;
731
+ }
732
+ }
733
+ ) ;
734
+ }
735
+ ) ;
736
+
647
737
blockStyles . variations = variations ;
648
738
}
649
- if ( blockStyles && blockSelectors ?. [ blockName ] ?. selector ) {
739
+
740
+ if ( blockSelectors ?. [ blockName ] ?. selector ) {
650
741
nodes . push ( {
651
742
duotoneSelector :
652
743
blockSelectors [ blockName ] . duotoneSelector ,
@@ -839,6 +930,7 @@ export const toStyles = (
839
930
( [ styleVariationName , styleVariationSelector ] ) => {
840
931
const styleVariations =
841
932
styles ?. variations ?. [ styleVariationName ] ;
933
+
842
934
if ( styleVariations ) {
843
935
// If the block uses any custom selectors for block support, add those first.
844
936
if ( featureSelectors ) {
@@ -856,6 +948,7 @@ export const toStyles = (
856
948
baseSelector ,
857
949
styleVariationSelector
858
950
) ;
951
+
859
952
const rules =
860
953
declarations . join ( ';' ) ;
861
954
ruleset += `${ cssSelector } {${ rules } ;}` ;
@@ -872,6 +965,7 @@ export const toStyles = (
872
965
useRootPaddingAlign ,
873
966
tree
874
967
) ;
968
+
875
969
if ( styleVariationDeclarations . length ) {
876
970
ruleset += `${ styleVariationSelector } {${ styleVariationDeclarations . join (
877
971
';'
@@ -1065,13 +1159,12 @@ export const getBlockSelectors = ( blockTypes, getBlockStyles ) => {
1065
1159
1066
1160
const blockStyleVariations = getBlockStyles ( name ) ;
1067
1161
const styleVariationSelectors = { } ;
1068
- if ( blockStyleVariations ?. length ) {
1069
- blockStyleVariations . forEach ( ( variation ) => {
1070
- const styleVariationSelector = `.is-style-${ variation . name } ${ selector } ` ;
1071
- styleVariationSelectors [ variation . name ] =
1072
- styleVariationSelector ;
1073
- } ) ;
1074
- }
1162
+
1163
+ blockStyleVariations ?. forEach ( ( variation ) => {
1164
+ const styleVariationSelector = `.is-style-${ variation . name } ${ selector } ` ;
1165
+ styleVariationSelectors [ variation . name ] = styleVariationSelector ;
1166
+ } ) ;
1167
+
1075
1168
// For each block support feature add any custom selectors.
1076
1169
const featureSelectors = getSelectorsConfig ( blockType , selector ) ;
1077
1170
@@ -1084,8 +1177,7 @@ export const getBlockSelectors = ( blockTypes, getBlockStyles ) => {
1084
1177
hasLayoutSupport,
1085
1178
name,
1086
1179
selector,
1087
- styleVariationSelectors : Object . keys ( styleVariationSelectors )
1088
- . length
1180
+ styleVariationSelectors : blockStyleVariations ?. length
1089
1181
? styleVariationSelectors
1090
1182
: undefined ,
1091
1183
} ;
0 commit comments