@wordpress/style-engine 1.34.0 → 1.35.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.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 1.35.0 (2024-02-21)
6
+
5
7
  ## 1.34.0 (2024-02-09)
6
8
 
7
9
  ## 1.33.0 (2024-01-24)
package/README.md CHANGED
@@ -126,10 +126,31 @@ $styles = array(
126
126
  'selector' => '.wp-tomato',
127
127
  'declarations' => array( 'padding' => '100px' )
128
128
  ),
129
+ );
130
+
131
+ $stylesheet = wp_style_engine_get_stylesheet_from_css_rules(
132
+ $styles,
129
133
  array(
130
- 'selector' => '.wp-kumquat',
134
+ 'context' => 'block-supports', // Indicates that these styles should be stored with block supports CSS.
135
+ )
136
+ );
137
+ print_r( $stylesheet ); // .wp-pumpkin{color:orange}.wp-tomato{color:red;padding:100px}
138
+ ```
139
+
140
+ It's also possible to build simple, nested CSS rules using the `rules_group` key.
141
+
142
+ ```php
143
+ $styles = array(
144
+ array(
145
+ 'rules_group' => '@media (min-width: 80rem)',
146
+ 'selector' => '.wp-carrot',
131
147
  'declarations' => array( 'color' => 'orange' )
132
148
  ),
149
+ array(
150
+ 'rules_group' => '@media (min-width: 80rem)',
151
+ 'selector' => '.wp-tomato',
152
+ 'declarations' => array( 'color' => 'red' )
153
+ ),
133
154
  );
134
155
 
135
156
  $stylesheet = wp_style_engine_get_stylesheet_from_css_rules(
@@ -138,7 +159,7 @@ $stylesheet = wp_style_engine_get_stylesheet_from_css_rules(
138
159
  'context' => 'block-supports', // Indicates that these styles should be stored with block supports CSS.
139
160
  )
140
161
  );
141
- print_r( $stylesheet ); // .wp-pumpkin,.wp-kumquat{color:orange}.wp-tomato{color:red;padding:100px}
162
+ print_r( $stylesheet ); // @media (min-width: 80rem){.wp-carrot{color:orange}}@media (min-width: 80rem){.wp-tomato{color:red;}}
142
163
  ```
143
164
 
144
165
  ### wp_style_engine_get_stylesheet_from_context()
@@ -33,12 +33,11 @@ if ( ! class_exists( 'WP_Style_Engine_CSS_Rule' ) ) {
33
33
  protected $declarations;
34
34
 
35
35
  /**
36
- * The CSS nested @rule, such as `@media (min-width: 80rem)` or `@layer module`.
36
+ * A parent CSS selector in the case of nested CSS, or a CSS nested @rule, such as `@media (min-width: 80rem)` or `@layer module`..
37
37
  *
38
38
  * @var string
39
39
  */
40
- protected $at_rule;
41
-
40
+ protected $rules_group;
42
41
 
43
42
  /**
44
43
  * Constructor
@@ -46,13 +45,13 @@ if ( ! class_exists( 'WP_Style_Engine_CSS_Rule' ) ) {
46
45
  * @param string $selector The CSS selector.
47
46
  * @param string[]|WP_Style_Engine_CSS_Declarations $declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ),
48
47
  * or a WP_Style_Engine_CSS_Declarations object.
49
- * @param string $at_rule A CSS nested @rule, such as `@media (min-width: 80rem)` or `@layer module`.
48
+ * @param string $rules_group A parent CSS selector in the case of nested CSS, or a CSS nested @rule, such as `@media (min-width: 80rem)` or `@layer module`.
50
49
  *
51
50
  */
52
- public function __construct( $selector = '', $declarations = array(), $at_rule = '' ) {
51
+ public function __construct( $selector = '', $declarations = array(), $rules_group = '' ) {
53
52
  $this->set_selector( $selector );
54
53
  $this->add_declarations( $declarations );
55
- $this->set_at_rule( $at_rule );
54
+ $this->set_rules_group( $rules_group );
56
55
  }
57
56
 
58
57
  /**
@@ -92,17 +91,26 @@ if ( ! class_exists( 'WP_Style_Engine_CSS_Rule' ) ) {
92
91
  }
93
92
 
94
93
  /**
95
- * Sets the at_rule.
94
+ * Sets the rules group.
96
95
  *
97
- * @param string $at_rule A CSS nested @rule, such as `@media (min-width: 80rem)` or `@layer module`.
96
+ * @param string $rules_group A parent CSS selector in the case of nested CSS, or a CSS nested @rule, such as `@media (min-width: 80rem)` or `@layer module`.
98
97
  *
99
98
  * @return WP_Style_Engine_CSS_Rule Returns the object to allow chaining of methods.
100
99
  */
101
- public function set_at_rule( $at_rule ) {
102
- $this->at_rule = $at_rule;
100
+ public function set_rules_group( $rules_group ) {
101
+ $this->rules_group = $rules_group;
103
102
  return $this;
104
103
  }
105
104
 
105
+ /**
106
+ * Gets the rules group.
107
+ *
108
+ * @return string
109
+ */
110
+ public function get_rules_group() {
111
+ return $this->rules_group;
112
+ }
113
+
106
114
  /**
107
115
  * Gets the declarations object.
108
116
  *
@@ -121,15 +129,6 @@ if ( ! class_exists( 'WP_Style_Engine_CSS_Rule' ) ) {
121
129
  return $this->selector;
122
130
  }
123
131
 
124
- /**
125
- * Gets the at_rule.
126
- *
127
- * @return string
128
- */
129
- public function get_at_rule() {
130
- return $this->at_rule;
131
- }
132
-
133
132
  /**
134
133
  * Gets the CSS.
135
134
  *
@@ -148,16 +147,16 @@ if ( ! class_exists( 'WP_Style_Engine_CSS_Rule' ) ) {
148
147
  // Trims any multiple selectors strings.
149
148
  $selector = $should_prettify ? implode( ',', array_map( 'trim', explode( ',', $this->get_selector() ) ) ) : $this->get_selector();
150
149
  $selector = $should_prettify ? str_replace( array( ',' ), ",\n", $selector ) : $selector;
151
- $at_rule = $this->get_at_rule();
152
- $has_at_rule = ! empty( $at_rule );
153
- $css_declarations = $this->declarations->get_declarations_string( $should_prettify, $has_at_rule ? $nested_declarations_indent : $declarations_indent );
150
+ $rules_group = $this->get_rules_group();
151
+ $has_rules_group = ! empty( $rules_group );
152
+ $css_declarations = $this->declarations->get_declarations_string( $should_prettify, $has_rules_group ? $nested_declarations_indent : $declarations_indent );
154
153
 
155
154
  if ( empty( $css_declarations ) ) {
156
155
  return '';
157
156
  }
158
157
 
159
- if ( $has_at_rule ) {
160
- $selector = "{$rule_indent}{$at_rule}{$spacer}{{$suffix}{$nested_rule_indent}{$selector}{$spacer}{{$suffix}{$css_declarations}{$suffix}{$nested_rule_indent}}{$suffix}{$rule_indent}}";
158
+ if ( $has_rules_group ) {
159
+ $selector = "{$rule_indent}{$rules_group}{$spacer}{{$suffix}{$nested_rule_indent}{$selector}{$spacer}{{$suffix}{$css_declarations}{$suffix}{$nested_rule_indent}}{$suffix}{$rule_indent}}";
161
160
  return $selector;
162
161
  }
163
162
 
@@ -109,25 +109,25 @@ if ( ! class_exists( 'WP_Style_Engine_CSS_Rules_Store' ) ) {
109
109
  * Gets a WP_Style_Engine_CSS_Rule object by its selector.
110
110
  * If the rule does not exist, it will be created.
111
111
  *
112
- * @param string $selector The CSS selector.
113
- * @param string $at_rule The CSS nested @rule, such as `@media (min-width: 80rem)` or `@layer module`.
112
+ * @param string $selector The CSS selector.
113
+ * @param string $rules_group A parent CSS selector in the case of nested CSS, or a CSS nested @rule, such as `@media (min-width: 80rem)` or `@layer module`..
114
114
  *
115
115
  * @return WP_Style_Engine_CSS_Rule|void Returns a WP_Style_Engine_CSS_Rule object, or null if the selector is empty.
116
116
  */
117
- public function add_rule( $selector, $at_rule = '' ) {
118
- $selector = trim( $selector );
119
- $at_rule = trim( $at_rule );
117
+ public function add_rule( $selector, $rules_group = '' ) {
118
+ $selector = $selector ? trim( $selector ) : '';
119
+ $rules_group = $rules_group ? trim( $rules_group ) : '';
120
120
 
121
121
  // Bail early if there is no selector.
122
122
  if ( empty( $selector ) ) {
123
123
  return;
124
124
  }
125
125
 
126
- if ( ! empty( $at_rule ) ) {
127
- if ( empty( $this->rules[ "$at_rule $selector" ] ) ) {
128
- $this->rules[ "$at_rule $selector" ] = new WP_Style_Engine_CSS_Rule( $selector, array(), $at_rule );
126
+ if ( ! empty( $rules_group ) ) {
127
+ if ( empty( $this->rules[ "$rules_group $selector" ] ) ) {
128
+ $this->rules[ "$rules_group $selector" ] = new WP_Style_Engine_CSS_Rule( $selector, array(), $rules_group );
129
129
  }
130
- return $this->rules[ "$at_rule $selector" ];
130
+ return $this->rules[ "$rules_group $selector" ];
131
131
  }
132
132
 
133
133
  // Create the rule if it doesn't exist.
@@ -65,20 +65,20 @@ if ( ! class_exists( 'WP_Style_Engine_Processor' ) ) {
65
65
  }
66
66
 
67
67
  foreach ( $css_rules as $rule ) {
68
- $selector = $rule->get_selector();
69
- $at_rule = $rule->get_at_rule();
68
+ $selector = $rule->get_selector();
69
+ $rules_group = $rule->get_rules_group();
70
70
 
71
71
  /**
72
72
  * If there is an at_rule and it already exists in the css_rules array,
73
73
  * add the rule to it.
74
74
  * Otherwise, create a new entry for the at_rule
75
75
  */
76
- if ( ! empty( $at_rule ) ) {
77
- if ( isset( $this->css_rules[ "$at_rule $selector" ] ) ) {
78
- $this->css_rules[ "$at_rule $selector" ]->add_declarations( $rule->get_declarations() );
76
+ if ( ! empty( $rules_group ) ) {
77
+ if ( isset( $this->css_rules[ "$rules_group $selector" ] ) ) {
78
+ $this->css_rules[ "$rules_group $selector" ]->add_declarations( $rule->get_declarations() );
79
79
  continue;
80
80
  }
81
- $this->css_rules[ "$at_rule $selector" ] = $rule;
81
+ $this->css_rules[ "$rules_group $selector" ] = $rule;
82
82
  continue;
83
83
  }
84
84
 
@@ -355,14 +355,15 @@ if ( ! class_exists( 'WP_Style_Engine' ) ) {
355
355
  * @param string $store_name A valid store key.
356
356
  * @param string $css_selector When a selector is passed, the function will return a full CSS rule `$selector { ...rules }`, otherwise a concatenated string of properties and values.
357
357
  * @param string[] $css_declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
358
+ * @param string $rules_group Optional. A parent CSS selector in the case of nested CSS, or a CSS nested @rule, such as `@media (min-width: 80rem)` or `@layer module`.
358
359
  *
359
360
  * @return void.
360
361
  */
361
- public static function store_css_rule( $store_name, $css_selector, $css_declarations, $css_at_rule = '' ) {
362
+ public static function store_css_rule( $store_name, $css_selector, $css_declarations, $rules_group = '' ) {
362
363
  if ( empty( $store_name ) || empty( $css_selector ) || empty( $css_declarations ) ) {
363
364
  return;
364
365
  }
365
- static::get_store( $store_name )->add_rule( $css_selector, $css_at_rule )->add_declarations( $css_declarations );
366
+ static::get_store( $store_name )->add_rule( $css_selector, $rules_group )->add_declarations( $css_declarations );
366
367
  }
367
368
 
368
369
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/style-engine",
3
- "version": "1.34.0",
3
+ "version": "1.35.0",
4
4
  "description": "A suite of parsers and compilers for WordPress styles.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -35,5 +35,5 @@
35
35
  "publishConfig": {
36
36
  "access": "public"
37
37
  },
38
- "gitHead": "eb796371e9630636a4a8837033807b0c4a06ed67"
38
+ "gitHead": "c139588f4c668b38bafbc5431f2f4e3903dbe683"
39
39
  }
package/style-engine.php CHANGED
@@ -83,7 +83,7 @@ function wp_style_engine_get_styles( $block_styles, $options = array() ) {
83
83
  * Required. A collection of CSS rules.
84
84
  *
85
85
  * @type array ...$0 {
86
- * @type string $at_rule A CSS nested @rule, such as `@media (min-width: 80rem)` or `@layer module`.
86
+ * @type string $rules_group A parent CSS selector in the case of nested CSS, or a CSS nested @rule, such as `@media (min-width: 80rem)` or `@layer module`.
87
87
  * @type string $selector A CSS selector.
88
88
  * @type string[] $declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
89
89
  * }
@@ -117,13 +117,12 @@ function wp_style_engine_get_stylesheet_from_css_rules( $css_rules, $options = a
117
117
  continue;
118
118
  }
119
119
 
120
- $at_rule = ! empty( $css_rule['at_rule'] ) ? $css_rule['at_rule'] : '';
121
-
120
+ $rules_group = $css_rule['rules_group'] ?? null;
122
121
  if ( ! empty( $options['context'] ) ) {
123
- WP_Style_Engine::store_css_rule( $options['context'], $css_rule['selector'], $css_rule['declarations'], $at_rule );
122
+ WP_Style_Engine::store_css_rule( $options['context'], $css_rule['selector'], $css_rule['declarations'], $rules_group );
124
123
  }
125
124
 
126
- $css_rule_objects[] = new WP_Style_Engine_CSS_Rule( $css_rule['selector'], $css_rule['declarations'], $at_rule );
125
+ $css_rule_objects[] = new WP_Style_Engine_CSS_Rule( $css_rule['selector'], $css_rule['declarations'], $rules_group );
127
126
  }
128
127
 
129
128
  if ( empty( $css_rule_objects ) ) {