@wordpress/style-engine 2.50.0 → 2.50.1-next.v.202607070741.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/style-engine",
3
- "version": "2.50.0",
3
+ "version": "2.50.1-next.v.202607070741.0+a51d59513",
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",
@@ -77,5 +77,5 @@
77
77
  "publishConfig": {
78
78
  "access": "public"
79
79
  },
80
- "gitHead": "fee6e24e8e63d36f6bbcf487c193461e9bd79753"
80
+ "gitHead": "f637726e370c8b23695ed9af82adbe171ad235d8"
81
81
  }
@@ -22,6 +22,13 @@ if ( ! class_exists( 'WP_Style_Engine_CSS_Declarations' ) ) {
22
22
  */
23
23
  protected $declarations = array();
24
24
 
25
+ /**
26
+ * CSS declaration options keyed by property name.
27
+ *
28
+ * @var array
29
+ */
30
+ protected $declaration_options = array();
31
+
25
32
  /**
26
33
  * Constructor for this object.
27
34
  *
@@ -39,10 +46,15 @@ if ( ! class_exists( 'WP_Style_Engine_CSS_Declarations' ) ) {
39
46
  *
40
47
  * @param string $property The CSS property.
41
48
  * @param string $value The CSS value.
49
+ * @param array $options {
50
+ * Optional. An array of options. Default empty array.
51
+ *
52
+ * @type bool $important Whether to output the declaration with !important. Default false.
53
+ * }
42
54
  *
43
55
  * @return WP_Style_Engine_CSS_Declarations Returns the object to allow chaining methods.
44
56
  */
45
- public function add_declaration( $property, $value ) {
57
+ public function add_declaration( $property, $value, $options = array() ) {
46
58
  // Sanitizes the property.
47
59
  $property = $this->sanitize_property( $property );
48
60
  // Bails early if the property is empty.
@@ -59,8 +71,22 @@ if ( ! class_exists( 'WP_Style_Engine_CSS_Declarations' ) ) {
59
71
  return $this;
60
72
  }
61
73
 
74
+ $options = wp_parse_args(
75
+ $options,
76
+ array(
77
+ 'important' => false,
78
+ )
79
+ );
80
+
81
+ $options = array_filter( $options );
82
+
62
83
  // Adds the declaration property/value pair.
63
84
  $this->declarations[ $property ] = $value;
85
+ if ( $options ) {
86
+ $this->declaration_options[ $property ] = $options;
87
+ } else {
88
+ unset( $this->declaration_options[ $property ] );
89
+ }
64
90
 
65
91
  return $this;
66
92
  }
@@ -74,6 +100,7 @@ if ( ! class_exists( 'WP_Style_Engine_CSS_Declarations' ) ) {
74
100
  */
75
101
  public function remove_declaration( $property ) {
76
102
  unset( $this->declarations[ $property ] );
103
+ unset( $this->declaration_options[ $property ] );
77
104
  return $this;
78
105
  }
79
106
 
@@ -114,20 +141,48 @@ if ( ! class_exists( 'WP_Style_Engine_CSS_Declarations' ) ) {
114
141
  return $this->declarations;
115
142
  }
116
143
 
144
+ /**
145
+ * Gets declaration options keyed by property name.
146
+ *
147
+ * @return array
148
+ */
149
+ public function get_declaration_options() {
150
+ return $this->declaration_options;
151
+ }
152
+
117
153
  /**
118
154
  * Filters a CSS property + value pair.
119
155
  *
120
156
  * @param string $property The CSS property.
121
157
  * @param string $value The value to be filtered.
122
158
  * @param string $spacer The spacer between the colon and the value. Defaults to an empty string.
159
+ * @param array $options {
160
+ * Optional. An array of options. Default empty array.
161
+ *
162
+ * @type bool $important Whether to output the declaration with !important. Default false.
163
+ * }
123
164
  *
124
165
  * @return string The filtered declaration or an empty string.
125
166
  */
126
- protected static function filter_declaration( $property, $value, $spacer = '' ) {
167
+ protected static function filter_declaration( $property, $value, $spacer = '', $options = array() ) {
127
168
  $filtered_value = wp_strip_all_tags( $value, true );
128
169
 
129
170
  if ( '' !== $filtered_value ) {
130
- return safecss_filter_attr( "{$property}:{$spacer}{$filtered_value}" );
171
+ $options = wp_parse_args(
172
+ $options,
173
+ array(
174
+ 'important' => false,
175
+ )
176
+ );
177
+
178
+ $filtered_declaration = safecss_filter_attr( "{$property}:{$spacer}{$filtered_value}" );
179
+
180
+ // Only append !important in the presence of an option value and when sanitization returns a single declaration.
181
+ if ( true === $options['important'] && '' !== $filtered_declaration && ! str_contains( $filtered_declaration, ';' ) ) {
182
+ return "$filtered_declaration !important";
183
+ }
184
+
185
+ return $filtered_declaration;
131
186
  }
132
187
  return '';
133
188
  }
@@ -149,7 +204,12 @@ if ( ! class_exists( 'WP_Style_Engine_CSS_Declarations' ) ) {
149
204
  $spacer = $should_prettify ? ' ' : '';
150
205
 
151
206
  foreach ( $declarations_array as $property => $value ) {
152
- $filtered_declaration = static::filter_declaration( $property, $value, $spacer );
207
+ $filtered_declaration = static::filter_declaration(
208
+ $property,
209
+ $value,
210
+ $spacer,
211
+ $this->declaration_options[ $property ] ?? array()
212
+ );
153
213
  if ( $filtered_declaration ) {
154
214
  $declarations_output .= "{$indent}{$filtered_declaration};$suffix";
155
215
  }
@@ -77,15 +77,23 @@ if ( ! class_exists( 'WP_Style_Engine_CSS_Rule' ) ) {
77
77
  public function add_declarations( $declarations ) {
78
78
  $is_declarations_object = ! is_array( $declarations );
79
79
  $declarations_array = $is_declarations_object ? $declarations->get_declarations() : $declarations;
80
+ $declaration_options = $is_declarations_object ? $declarations->get_declaration_options() : array();
80
81
 
81
82
  if ( null === $this->declarations ) {
82
83
  if ( $is_declarations_object ) {
83
84
  $this->declarations = $declarations;
84
85
  return $this;
85
86
  }
86
- $this->declarations = new WP_Style_Engine_CSS_Declarations( $declarations_array );
87
+ $this->declarations = new WP_Style_Engine_CSS_Declarations();
88
+ }
89
+
90
+ foreach ( $declarations_array as $property => $value ) {
91
+ $this->declarations->add_declaration(
92
+ $property,
93
+ $value,
94
+ $declaration_options[ $property ] ?? array()
95
+ );
87
96
  }
88
- $this->declarations->add_declarations( $declarations_array );
89
97
 
90
98
  return $this;
91
99
  }
@@ -142,9 +142,23 @@ if ( ! class_exists( 'WP_Style_Engine_Processor' ) ) {
142
142
  // Build an array of selectors along with the JSON-ified styles to make comparisons easier.
143
143
  $selectors_json = array();
144
144
  foreach ( $this->css_rules as $rule ) {
145
- $declarations = $rule->get_declarations()->get_declarations();
145
+ $declarations = $rule->get_declarations()->get_declarations();
146
+ $declaration_options = $rule->get_declarations()->get_declaration_options();
146
147
  ksort( $declarations );
147
- $selectors_json[ $rule->get_selector() ] = wp_json_encode( $declarations );
148
+ // Declaration options are keyed by property and are part of the rule identity.
149
+ ksort( $declaration_options );
150
+ foreach ( $declaration_options as $property => $options ) {
151
+ if ( is_array( $options ) ) {
152
+ ksort( $options );
153
+ $declaration_options[ $property ] = $options;
154
+ }
155
+ }
156
+ $selectors_json[ $rule->get_selector() ] = wp_json_encode(
157
+ array(
158
+ 'declarations' => $declarations,
159
+ 'declaration_options' => $declaration_options,
160
+ )
161
+ );
148
162
  }
149
163
 
150
164
  // Combine selectors that have the same styles.
@@ -417,8 +417,9 @@ if ( ! class_exists( 'WP_Style_Engine' ) ) {
417
417
  *
418
418
  * @param string $store_name A valid store key.
419
419
  * @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.
420
- * @param string[] $css_declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
421
- * @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`.
420
+ * @param string[]|WP_Style_Engine_CSS_Declarations $css_declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ),
421
+ * or a WP_Style_Engine_CSS_Declarations object.
422
+ * @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`.
422
423
  *
423
424
  * @return void.
424
425
  */
@@ -85,7 +85,8 @@ function wp_style_engine_get_styles( $block_styles, $options = array() ) {
85
85
  * @type array ...$0 {
86
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
- * @type string[] $declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
88
+ * @type string[]|WP_Style_Engine_CSS_Declarations $declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ),
89
+ * or a WP_Style_Engine_CSS_Declarations object.
89
90
  * }
90
91
  * }
91
92
  * @param array $options {
@@ -113,16 +114,21 @@ function wp_style_engine_get_stylesheet_from_css_rules( $css_rules, $options = a
113
114
 
114
115
  $css_rule_objects = array();
115
116
  foreach ( $css_rules as $css_rule ) {
116
- if ( empty( $css_rule['selector'] ) || empty( $css_rule['declarations'] ) || ! is_array( $css_rule['declarations'] ) ) {
117
+ $declarations = $css_rule['declarations'] ?? null;
118
+ if (
119
+ empty( $css_rule['selector'] ) ||
120
+ empty( $declarations ) ||
121
+ ( ! is_array( $declarations ) && ! $declarations instanceof WP_Style_Engine_CSS_Declarations )
122
+ ) {
117
123
  continue;
118
124
  }
119
125
 
120
126
  $rules_group = $css_rule['rules_group'] ?? null;
121
127
  if ( ! empty( $options['context'] ) ) {
122
- WP_Style_Engine::store_css_rule( $options['context'], $css_rule['selector'], $css_rule['declarations'], $rules_group );
128
+ WP_Style_Engine::store_css_rule( $options['context'], $css_rule['selector'], $declarations, $rules_group );
123
129
  }
124
130
 
125
- $css_rule_objects[] = new WP_Style_Engine_CSS_Rule( $css_rule['selector'], $css_rule['declarations'], $rules_group );
131
+ $css_rule_objects[] = new WP_Style_Engine_CSS_Rule( $css_rule['selector'], $declarations, $rules_group );
126
132
  }
127
133
 
128
134
  if ( empty( $css_rule_objects ) ) {