@wordpress/global-styles-engine 1.12.0 → 1.12.1-next.v.202605131032.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -60,6 +60,30 @@ var VALID_BLOCK_PSEUDO_SELECTORS = {
60
60
  "core/button": [":hover", ":focus", ":focus-visible", ":active"],
61
61
  "core/navigation-link": [":hover", ":focus", ":focus-visible", ":active"]
62
62
  };
63
+ var VALID_ELEMENT_PSEUDO_SELECTORS = {
64
+ link: [
65
+ ":link",
66
+ ":any-link",
67
+ ":visited",
68
+ ":hover",
69
+ ":focus",
70
+ ":focus-visible",
71
+ ":active"
72
+ ],
73
+ button: [
74
+ ":link",
75
+ ":any-link",
76
+ ":visited",
77
+ ":hover",
78
+ ":focus",
79
+ ":focus-visible",
80
+ ":active"
81
+ ]
82
+ };
83
+ var RESPONSIVE_BREAKPOINTS = {
84
+ mobile: "@media (width <= 480px)",
85
+ tablet: "@media (480px < width <= 782px)"
86
+ };
63
87
  function getPresetsClasses(blockSelector = "*", blockPresets = {}) {
64
88
  return import_common.PRESET_METADATA.reduce(
65
89
  (declarations, { path, cssVarInfix, classes }) => {
@@ -441,7 +465,7 @@ function pickStyleAndPseudoKeys(treeToPickFrom, blockName) {
441
465
  const entries = Object.entries(treeToPickFrom);
442
466
  const allowedPseudoSelectors = blockName ? VALID_BLOCK_PSEUDO_SELECTORS[blockName] ?? [] : [];
443
467
  const pickedEntries = entries.filter(
444
- ([key]) => STYLE_KEYS.includes(key) || allowedPseudoSelectors.includes(key)
468
+ ([key]) => STYLE_KEYS.includes(key) || allowedPseudoSelectors.includes(key) || RESPONSIVE_BREAKPOINTS[key]
445
469
  );
446
470
  const clonedEntries = pickedEntries.map(([key, style]) => [
447
471
  key,
@@ -449,65 +473,100 @@ function pickStyleAndPseudoKeys(treeToPickFrom, blockName) {
449
473
  ]);
450
474
  return Object.fromEntries(clonedEntries);
451
475
  }
452
- function appendPseudoSelectorStyles(styles, selector, ruleset, featureSelectors, treeSettings, blockName, styleVariationSelector) {
453
- const pseudoSelectorStyles = Object.entries(styles).filter(
454
- ([key]) => key.startsWith(":")
455
- );
456
- if (!pseudoSelectorStyles.length) {
457
- return ruleset;
476
+ function getPseudoStyleNodes(node) {
477
+ const {
478
+ styles,
479
+ selector,
480
+ featureSelectors,
481
+ name,
482
+ elementName,
483
+ mediaQuery
484
+ } = node;
485
+ const pseudoSelectors = name ? VALID_BLOCK_PSEUDO_SELECTORS[name] ?? [] : VALID_ELEMENT_PSEUDO_SELECTORS[elementName ?? ""] ?? [];
486
+ if (!pseudoSelectors.length) {
487
+ return [];
458
488
  }
459
- pseudoSelectorStyles.forEach(([pseudoKey, pseudoStyle]) => {
460
- if (!pseudoStyle || typeof pseudoStyle !== "object") {
461
- return;
489
+ return pseudoSelectors.flatMap((pseudoSelector) => {
490
+ const pseudoStyles = styles?.[pseudoSelector];
491
+ if (!pseudoStyles || typeof pseudoStyles !== "object") {
492
+ return [];
462
493
  }
463
- const remainingPseudoStyles = JSON.parse(
464
- JSON.stringify(pseudoStyle)
465
- );
466
- if (featureSelectors && typeof featureSelectors !== "string") {
467
- let pseudoFeatureDeclarations = getFeatureDeclarations(
468
- featureSelectors,
469
- remainingPseudoStyles
470
- );
471
- pseudoFeatureDeclarations = updateParagraphTextIndentSelector(
472
- pseudoFeatureDeclarations,
473
- treeSettings,
474
- blockName
475
- );
476
- pseudoFeatureDeclarations = updateButtonWidthDeclarations(
477
- pseudoFeatureDeclarations,
478
- treeSettings
479
- );
480
- Object.entries(pseudoFeatureDeclarations).forEach(
481
- ([baseSelector, declarations]) => {
482
- if (!declarations.length) {
483
- return;
484
- }
485
- const pseudoFeatureSelector = (0, import_common.appendToSelector)(
486
- baseSelector,
487
- pseudoKey
488
- );
489
- const cssSelector = styleVariationSelector ? concatFeatureVariationSelectorString(
490
- pseudoFeatureSelector,
491
- styleVariationSelector
492
- ) : pseudoFeatureSelector;
493
- const rules = declarations.join(";");
494
- ruleset += `:root :where(${cssSelector}){${rules};}`;
494
+ return [
495
+ {
496
+ styles: JSON.parse(JSON.stringify(pseudoStyles)),
497
+ selector,
498
+ selectorSuffix: pseudoSelector,
499
+ mediaQuery,
500
+ featureSelectors: featureSelectors && typeof featureSelectors !== "string" ? featureSelectors : void 0,
501
+ name,
502
+ elementName
503
+ }
504
+ ];
505
+ });
506
+ }
507
+ function getResponsiveStyleNodes(node) {
508
+ const {
509
+ styles,
510
+ selector,
511
+ featureSelectors,
512
+ name,
513
+ elementName,
514
+ isStyleVariation
515
+ } = node;
516
+ if (!name && !elementName) {
517
+ return [];
518
+ }
519
+ return Object.entries(RESPONSIVE_BREAKPOINTS).flatMap(
520
+ ([breakpointKey, mediaQuery]) => {
521
+ const breakpointStyles = styles?.[breakpointKey];
522
+ if (!breakpointStyles || typeof breakpointStyles !== "object") {
523
+ return [];
524
+ }
525
+ return [
526
+ {
527
+ styles: JSON.parse(JSON.stringify(breakpointStyles)),
528
+ selector,
529
+ mediaQuery,
530
+ featureSelectors: featureSelectors && typeof featureSelectors !== "string" ? featureSelectors : void 0,
531
+ name,
532
+ elementName,
533
+ isStyleVariation
495
534
  }
496
- );
497
- }
498
- const pseudoDeclarations = getStylesDeclarations(
499
- remainingPseudoStyles
500
- );
501
- if (!pseudoDeclarations.length) {
502
- return;
535
+ ];
503
536
  }
504
- const pseudoSelector = (0, import_common.appendToSelector)(selector, pseudoKey);
505
- const pseudoRule = `:root :where(${pseudoSelector}){${pseudoDeclarations.join(
506
- ";"
507
- )};}`;
508
- ruleset += pseudoRule;
509
- });
510
- return ruleset;
537
+ );
538
+ }
539
+ function getVariationFeatureSelectors(featureSelectors, styleVariationSelector) {
540
+ if (!featureSelectors || typeof featureSelectors === "string") {
541
+ return void 0;
542
+ }
543
+ return Object.fromEntries(
544
+ Object.entries(featureSelectors).map(([feature, selector]) => {
545
+ if (typeof selector === "string") {
546
+ return [
547
+ feature,
548
+ concatFeatureVariationSelectorString(
549
+ selector,
550
+ styleVariationSelector
551
+ )
552
+ ];
553
+ }
554
+ return [
555
+ feature,
556
+ Object.fromEntries(
557
+ Object.entries(selector).map(
558
+ ([subfeature, subfeatureSelector]) => [
559
+ subfeature,
560
+ concatFeatureVariationSelectorString(
561
+ subfeatureSelector,
562
+ styleVariationSelector
563
+ )
564
+ ]
565
+ )
566
+ )
567
+ ];
568
+ })
569
+ );
511
570
  }
512
571
  var getNodesWithStyles = (tree, blockSelectors) => {
513
572
  const nodes = [];
@@ -529,6 +588,7 @@ var getNodesWithStyles = (tree, blockSelectors) => {
529
588
  nodes.push({
530
589
  styles: tree.styles?.elements?.[name] ?? {},
531
590
  selector,
591
+ elementName: name,
532
592
  // Top level elements that don't use a class name should not receive the
533
593
  // `:root :where()` wrapper to maintain backwards compatibility.
534
594
  skipSelectorWrapper: !ELEMENT_CLASS_NAMES[name]
@@ -540,19 +600,36 @@ var getNodesWithStyles = (tree, blockSelectors) => {
540
600
  const blockStyles = pickStyleAndPseudoKeys(node, blockName);
541
601
  const typedNode = node;
542
602
  const variationNodesToAdd = [];
603
+ const variationStyleNodesToAdd = [];
543
604
  if (typedNode?.variations) {
544
- const variations = {};
545
605
  Object.entries(typedNode.variations).forEach(
546
606
  ([variationName, variation]) => {
547
607
  const typedVariation = variation;
548
- variations[variationName] = pickStyleAndPseudoKeys(
608
+ const variationStyles = pickStyleAndPseudoKeys(
549
609
  typedVariation,
550
610
  blockName
551
611
  );
552
612
  if (typedVariation?.css) {
553
- variations[variationName].css = typedVariation.css;
613
+ variationStyles.css = typedVariation.css;
554
614
  }
555
615
  const variationSelector = typeof blockSelectors !== "string" ? blockSelectors[blockName]?.styleVariationSelectors?.[variationName] : void 0;
616
+ if (variationSelector && typeof blockSelectors !== "string") {
617
+ const blockSelector = blockSelectors[blockName];
618
+ variationStyleNodesToAdd.push({
619
+ styles: variationStyles,
620
+ selector: variationSelector,
621
+ featureSelectors: getVariationFeatureSelectors(
622
+ blockSelector?.featureSelectors,
623
+ variationSelector
624
+ ),
625
+ fallbackGapValue: blockSelector?.fallbackGapValue,
626
+ hasLayoutSupport: blockSelector?.hasLayoutSupport,
627
+ isStyleVariation: true,
628
+ layoutSelector: variationSelector + blockSelector.selector,
629
+ layoutHasBlockGapSupport: true,
630
+ name: blockName
631
+ });
632
+ }
556
633
  Object.entries(
557
634
  typedVariation?.elements ?? {}
558
635
  ).forEach(([element, elementStyles]) => {
@@ -562,7 +639,9 @@ var getNodesWithStyles = (tree, blockSelectors) => {
562
639
  selector: (0, import_common.scopeSelector)(
563
640
  variationSelector,
564
641
  import_blocks.__EXPERIMENTAL_ELEMENTS[element]
565
- )
642
+ ),
643
+ elementName: element,
644
+ isStyleVariation: true
566
645
  });
567
646
  }
568
647
  });
@@ -595,6 +674,8 @@ var getNodesWithStyles = (tree, blockSelectors) => {
595
674
  }
596
675
  variationNodesToAdd.push({
597
676
  selector: variationBlockSelector,
677
+ name: variationBlockName,
678
+ isStyleVariation: true,
598
679
  duotoneSelector: variationDuotoneSelector,
599
680
  featureSelectors: variationFeatureSelectors,
600
681
  fallbackGapValue: blockSelectors[variationBlockName]?.fallbackGapValue,
@@ -614,7 +695,9 @@ var getNodesWithStyles = (tree, blockSelectors) => {
614
695
  selector: (0, import_common.scopeSelector)(
615
696
  variationBlockSelector,
616
697
  import_blocks.__EXPERIMENTAL_ELEMENTS[variationBlockElement]
617
- )
698
+ ),
699
+ elementName: variationBlockElement,
700
+ isStyleVariation: true
618
701
  });
619
702
  }
620
703
  }
@@ -623,7 +706,6 @@ var getNodesWithStyles = (tree, blockSelectors) => {
623
706
  );
624
707
  }
625
708
  );
626
- blockStyles.variations = variations;
627
709
  }
628
710
  if (typeof blockSelectors !== "string" && blockSelectors?.[blockName]?.selector) {
629
711
  nodes.push({
@@ -633,10 +715,10 @@ var getNodesWithStyles = (tree, blockSelectors) => {
633
715
  selector: blockSelectors[blockName].selector,
634
716
  styles: blockStyles,
635
717
  featureSelectors: blockSelectors[blockName].featureSelectors,
636
- styleVariationSelectors: blockSelectors[blockName].styleVariationSelectors,
637
718
  name: blockName
638
719
  });
639
720
  }
721
+ nodes.push(...variationStyleNodesToAdd);
640
722
  Object.entries(typedNode?.elements ?? {}).forEach(
641
723
  ([elementName, value]) => {
642
724
  if (typeof blockSelectors !== "string" && value && blockSelectors?.[blockName] && import_blocks.__EXPERIMENTAL_ELEMENTS[elementName]) {
@@ -647,7 +729,8 @@ var getNodesWithStyles = (tree, blockSelectors) => {
647
729
  return elementSelectors.map(
648
730
  (elementSelector) => sel + " " + elementSelector
649
731
  );
650
- }).join(",")
732
+ }).join(","),
733
+ elementName
651
734
  });
652
735
  }
653
736
  }
@@ -785,6 +868,100 @@ var generateCustomProperties = (tree, blockSelectors) => {
785
868
  }
786
869
  return ruleset;
787
870
  };
871
+ function renderStylesNode(node, {
872
+ tree,
873
+ useRootPaddingAlign,
874
+ disableLayoutStyles,
875
+ hasBlockGapSupport,
876
+ hasFallbackGapSupport,
877
+ disableRootPadding
878
+ }) {
879
+ const {
880
+ selector,
881
+ selectorSuffix,
882
+ mediaQuery,
883
+ duotoneSelector,
884
+ styles,
885
+ fallbackGapValue,
886
+ hasLayoutSupport,
887
+ featureSelectors,
888
+ layoutSelector,
889
+ layoutHasBlockGapSupport,
890
+ skipSelectorWrapper,
891
+ name
892
+ } = node;
893
+ let ruleset = "";
894
+ const effectiveSelector = selectorSuffix ? (0, import_common.appendToSelector)(selector, selectorSuffix) : selector;
895
+ if (featureSelectors && typeof featureSelectors !== "string") {
896
+ let featureDeclarations = getFeatureDeclarations(
897
+ featureSelectors,
898
+ styles
899
+ );
900
+ featureDeclarations = updateParagraphTextIndentSelector(
901
+ featureDeclarations,
902
+ tree.settings,
903
+ name
904
+ );
905
+ featureDeclarations = updateButtonWidthDeclarations(
906
+ featureDeclarations,
907
+ tree.settings
908
+ );
909
+ Object.entries(featureDeclarations).forEach(
910
+ ([featureSelector, declarations]) => {
911
+ if (declarations.length) {
912
+ const selectorForRule = selectorSuffix ? (0, import_common.appendToSelector)(featureSelector, selectorSuffix) : featureSelector;
913
+ const rules = declarations.join(";");
914
+ ruleset += `:root :where(${selectorForRule}){${rules};}`;
915
+ }
916
+ }
917
+ );
918
+ }
919
+ if (duotoneSelector) {
920
+ const duotoneStyles = {};
921
+ if (styles?.filter) {
922
+ duotoneStyles.filter = styles.filter;
923
+ delete styles.filter;
924
+ }
925
+ const duotoneDeclarations = getStylesDeclarations(duotoneStyles);
926
+ if (duotoneDeclarations.length) {
927
+ ruleset += `${duotoneSelector}{${duotoneDeclarations.join(
928
+ ";"
929
+ )};}`;
930
+ }
931
+ }
932
+ const selectorForLayout = layoutSelector ?? effectiveSelector;
933
+ const hasBlockGapSupportForLayout = layoutHasBlockGapSupport ?? hasBlockGapSupport;
934
+ if (!disableLayoutStyles && (import_common.ROOT_BLOCK_SELECTOR === selectorForLayout || hasLayoutSupport)) {
935
+ ruleset += getLayoutStyles({
936
+ style: styles,
937
+ selector: selectorForLayout,
938
+ hasBlockGapSupport: hasBlockGapSupportForLayout,
939
+ hasFallbackGapSupport,
940
+ fallbackGapValue
941
+ });
942
+ }
943
+ const styleDeclarations = getStylesDeclarations(
944
+ styles,
945
+ effectiveSelector,
946
+ useRootPaddingAlign,
947
+ tree,
948
+ disableRootPadding
949
+ );
950
+ if (styleDeclarations?.length) {
951
+ const generalSelector = skipSelectorWrapper ? effectiveSelector : `:root :where(${effectiveSelector})`;
952
+ ruleset += `${generalSelector}{${styleDeclarations.join(";")};}`;
953
+ }
954
+ if (styles?.css) {
955
+ ruleset += processCSSNesting(
956
+ styles.css,
957
+ `:root :where(${effectiveSelector})`
958
+ );
959
+ }
960
+ if (mediaQuery && ruleset) {
961
+ return `${mediaQuery}{${ruleset}}`;
962
+ }
963
+ return ruleset;
964
+ }
788
965
  var transformToStyles = (tree, blockSelectors, hasBlockGapSupport, hasFallbackGapSupport, disableLayoutStyles = false, disableRootPadding = false, styleOptions = {}) => {
789
966
  const options = {
790
967
  blockGap: true,
@@ -821,166 +998,27 @@ var transformToStyles = (tree, blockSelectors, hasBlockGapSupport, hasFallbackGa
821
998
  ruleset += "}";
822
999
  }
823
1000
  if (options.blockStyles) {
824
- nodesWithStyles.forEach(
825
- ({
826
- selector,
827
- duotoneSelector,
828
- styles,
829
- fallbackGapValue,
830
- hasLayoutSupport,
831
- featureSelectors,
832
- styleVariationSelectors,
833
- skipSelectorWrapper,
834
- name
835
- }) => {
836
- if (featureSelectors) {
837
- let featureDeclarations = getFeatureDeclarations(
838
- featureSelectors,
839
- styles
840
- );
841
- featureDeclarations = updateParagraphTextIndentSelector(
842
- featureDeclarations,
843
- tree.settings,
844
- name
845
- );
846
- featureDeclarations = updateButtonWidthDeclarations(
847
- featureDeclarations,
848
- tree.settings
849
- );
850
- Object.entries(featureDeclarations).forEach(
851
- ([cssSelector, declarations]) => {
852
- if (declarations.length) {
853
- const rules = declarations.join(";");
854
- ruleset += `:root :where(${cssSelector}){${rules};}`;
855
- }
856
- }
857
- );
858
- }
859
- if (duotoneSelector) {
860
- const duotoneStyles = {};
861
- if (styles?.filter) {
862
- duotoneStyles.filter = styles.filter;
863
- delete styles.filter;
864
- }
865
- const duotoneDeclarations = getStylesDeclarations(duotoneStyles);
866
- if (duotoneDeclarations.length) {
867
- ruleset += `${duotoneSelector}{${duotoneDeclarations.join(
868
- ";"
869
- )};}`;
870
- }
871
- }
872
- if (!disableLayoutStyles && (import_common.ROOT_BLOCK_SELECTOR === selector || hasLayoutSupport)) {
873
- ruleset += getLayoutStyles({
874
- style: styles,
875
- selector,
876
- hasBlockGapSupport,
877
- hasFallbackGapSupport,
878
- fallbackGapValue
879
- });
880
- }
881
- const styleDeclarations = getStylesDeclarations(
882
- styles,
883
- selector,
884
- useRootPaddingAlign,
1001
+ nodesWithStyles.forEach((node) => {
1002
+ if (node.isStyleVariation && !options.variationStyles) {
1003
+ return;
1004
+ }
1005
+ const responsiveNodes = getResponsiveStyleNodes(node);
1006
+ [
1007
+ node,
1008
+ ...responsiveNodes,
1009
+ ...getPseudoStyleNodes(node),
1010
+ ...responsiveNodes.flatMap(getPseudoStyleNodes)
1011
+ ].forEach((expandedNode) => {
1012
+ ruleset += renderStylesNode(expandedNode, {
885
1013
  tree,
1014
+ useRootPaddingAlign,
1015
+ disableLayoutStyles,
1016
+ hasBlockGapSupport,
1017
+ hasFallbackGapSupport,
886
1018
  disableRootPadding
887
- );
888
- if (styleDeclarations?.length) {
889
- const generalSelector = skipSelectorWrapper ? selector : `:root :where(${selector})`;
890
- ruleset += `${generalSelector}{${styleDeclarations.join(
891
- ";"
892
- )};}`;
893
- }
894
- if (styles?.css) {
895
- ruleset += processCSSNesting(
896
- styles.css,
897
- `:root :where(${selector})`
898
- );
899
- }
900
- if (options.variationStyles && styleVariationSelectors) {
901
- Object.entries(styleVariationSelectors).forEach(
902
- ([styleVariationName, styleVariationSelector]) => {
903
- const styleVariations = styles?.variations?.[styleVariationName];
904
- if (styleVariations) {
905
- if (featureSelectors) {
906
- let featureDeclarations = getFeatureDeclarations(
907
- featureSelectors,
908
- styleVariations
909
- );
910
- featureDeclarations = updateParagraphTextIndentSelector(
911
- featureDeclarations,
912
- tree.settings,
913
- name
914
- );
915
- featureDeclarations = updateButtonWidthDeclarations(
916
- featureDeclarations,
917
- tree.settings
918
- );
919
- Object.entries(
920
- featureDeclarations
921
- ).forEach(
922
- ([baseSelector, declarations]) => {
923
- if (declarations.length) {
924
- const cssSelector = concatFeatureVariationSelectorString(
925
- baseSelector,
926
- styleVariationSelector
927
- );
928
- const rules = declarations.join(";");
929
- ruleset += `:root :where(${cssSelector}){${rules};}`;
930
- }
931
- }
932
- );
933
- }
934
- const styleVariationDeclarations = getStylesDeclarations(
935
- styleVariations,
936
- styleVariationSelector,
937
- useRootPaddingAlign,
938
- tree
939
- );
940
- if (styleVariationDeclarations.length) {
941
- ruleset += `:root :where(${styleVariationSelector}){${styleVariationDeclarations.join(
942
- ";"
943
- )};}`;
944
- }
945
- if (styleVariations?.css) {
946
- ruleset += processCSSNesting(
947
- styleVariations.css,
948
- `:root :where(${styleVariationSelector})`
949
- );
950
- }
951
- ruleset = appendPseudoSelectorStyles(
952
- styleVariations,
953
- styleVariationSelector,
954
- ruleset,
955
- featureSelectors,
956
- tree.settings,
957
- name,
958
- styleVariationSelector
959
- );
960
- if (hasLayoutSupport && styleVariations?.spacing?.blockGap) {
961
- const variationSelectorWithBlock = styleVariationSelector + selector;
962
- ruleset += getLayoutStyles({
963
- style: styleVariations,
964
- selector: variationSelectorWithBlock,
965
- hasBlockGapSupport: true,
966
- hasFallbackGapSupport,
967
- fallbackGapValue
968
- });
969
- }
970
- }
971
- }
972
- );
973
- }
974
- ruleset = appendPseudoSelectorStyles(
975
- styles,
976
- selector,
977
- ruleset,
978
- featureSelectors,
979
- tree.settings,
980
- name
981
- );
982
- }
983
- );
1019
+ });
1020
+ });
1021
+ });
984
1022
  }
985
1023
  if (options.layoutStyles) {
986
1024
  ruleset = ruleset + ".wp-site-blocks > .alignleft { float: left; margin-right: 2em; }";