@wordpress/style-engine 1.33.1 → 1.34.1

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.
Files changed (44) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/build/styles/background/index.js +7 -2
  3. package/build/styles/background/index.js.map +1 -1
  4. package/build/styles/border/index.js +1 -2
  5. package/build/styles/border/index.js.map +1 -1
  6. package/build/styles/color/background.js +1 -2
  7. package/build/styles/color/background.js.map +1 -1
  8. package/build/styles/color/gradient.js +1 -2
  9. package/build/styles/color/gradient.js.map +1 -1
  10. package/build/styles/color/index.js +1 -2
  11. package/build/styles/color/index.js.map +1 -1
  12. package/build/styles/color/text.js +1 -2
  13. package/build/styles/color/text.js.map +1 -1
  14. package/build/styles/constants.js +3 -6
  15. package/build/styles/constants.js.map +1 -1
  16. package/build/styles/dimensions/index.js +1 -2
  17. package/build/styles/dimensions/index.js.map +1 -1
  18. package/build/styles/index.js +1 -2
  19. package/build/styles/index.js.map +1 -1
  20. package/build/styles/outline/index.js +1 -2
  21. package/build/styles/outline/index.js.map +1 -1
  22. package/build/styles/shadow/index.js +1 -2
  23. package/build/styles/shadow/index.js.map +1 -1
  24. package/build/styles/spacing/index.js +1 -2
  25. package/build/styles/spacing/index.js.map +1 -1
  26. package/build/styles/spacing/margin.js +1 -2
  27. package/build/styles/spacing/margin.js.map +1 -1
  28. package/build/styles/spacing/padding.js +1 -2
  29. package/build/styles/spacing/padding.js.map +1 -1
  30. package/build/styles/typography/index.js +1 -2
  31. package/build/styles/typography/index.js.map +1 -1
  32. package/build-module/styles/background/index.js +7 -1
  33. package/build-module/styles/background/index.js.map +1 -1
  34. package/build-types/styles/background/index.d.ts.map +1 -1
  35. package/class-wp-style-engine-css-declarations.php +135 -136
  36. package/class-wp-style-engine-css-rule.php +140 -100
  37. package/class-wp-style-engine-css-rules-store.php +127 -119
  38. package/class-wp-style-engine-processor.php +140 -124
  39. package/class-wp-style-engine.php +571 -572
  40. package/package.json +2 -2
  41. package/src/styles/background/index.ts +18 -1
  42. package/src/test/index.js +6 -0
  43. package/style-engine.php +5 -2
  44. package/tsconfig.tsbuildinfo +1 -1
@@ -7,163 +7,162 @@
7
7
  * @package Gutenberg
8
8
  */
9
9
 
10
- if ( class_exists( 'WP_Style_Engine_CSS_Declarations' ) ) {
11
- return;
12
- }
10
+ if ( ! class_exists( 'WP_Style_Engine_CSS_Declarations' ) ) {
13
11
 
14
- /**
15
- * Holds, sanitizes, processes and prints CSS declarations for the Style Engine.
16
- *
17
- * @access private
18
- */
19
- class WP_Style_Engine_CSS_Declarations {
20
12
  /**
21
- * An array of CSS declarations (property => value pairs).
13
+ * Holds, sanitizes, processes and prints CSS declarations for the Style Engine.
22
14
  *
23
- * @var array
15
+ * @access private
24
16
  */
25
- protected $declarations = array();
17
+ class WP_Style_Engine_CSS_Declarations {
18
+ /**
19
+ * An array of CSS declarations (property => value pairs).
20
+ *
21
+ * @var array
22
+ */
23
+ protected $declarations = array();
24
+
25
+ /**
26
+ * Constructor for this object.
27
+ *
28
+ * If a `$declarations` array is passed, it will be used to populate
29
+ * the initial $declarations prop of the object by calling add_declarations().
30
+ *
31
+ * @param string[] $declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
32
+ */
33
+ public function __construct( $declarations = array() ) {
34
+ $this->add_declarations( $declarations );
35
+ }
26
36
 
27
- /**
28
- * Constructor for this object.
29
- *
30
- * If a `$declarations` array is passed, it will be used to populate
31
- * the initial $declarations prop of the object by calling add_declarations().
32
- *
33
- * @param string[] $declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
34
- */
35
- public function __construct( $declarations = array() ) {
36
- $this->add_declarations( $declarations );
37
- }
37
+ /**
38
+ * Adds a single declaration.
39
+ *
40
+ * @param string $property The CSS property.
41
+ * @param string $value The CSS value.
42
+ *
43
+ * @return WP_Style_Engine_CSS_Declarations Returns the object to allow chaining methods.
44
+ */
45
+ public function add_declaration( $property, $value ) {
46
+ // Sanitizes the property.
47
+ $property = $this->sanitize_property( $property );
48
+ // Bails early if the property is empty.
49
+ if ( empty( $property ) ) {
50
+ return $this;
51
+ }
52
+
53
+ // Trims the value. If empty, bail early.
54
+ $value = trim( $value );
55
+ if ( '' === $value ) {
56
+ return $this;
57
+ }
58
+
59
+ // Adds the declaration property/value pair.
60
+ $this->declarations[ $property ] = $value;
38
61
 
39
- /**
40
- * Adds a single declaration.
41
- *
42
- * @param string $property The CSS property.
43
- * @param string $value The CSS value.
44
- *
45
- * @return WP_Style_Engine_CSS_Declarations Returns the object to allow chaining methods.
46
- */
47
- public function add_declaration( $property, $value ) {
48
- // Sanitizes the property.
49
- $property = $this->sanitize_property( $property );
50
- // Bails early if the property is empty.
51
- if ( empty( $property ) ) {
52
62
  return $this;
53
63
  }
54
64
 
55
- // Trims the value. If empty, bail early.
56
- $value = trim( $value );
57
- if ( '' === $value ) {
65
+ /**
66
+ * Removes a single declaration.
67
+ *
68
+ * @param string $property The CSS property.
69
+ *
70
+ * @return WP_Style_Engine_CSS_Declarations Returns the object to allow chaining methods.
71
+ */
72
+ public function remove_declaration( $property ) {
73
+ unset( $this->declarations[ $property ] );
58
74
  return $this;
59
75
  }
60
76
 
61
- // Adds the declaration property/value pair.
62
- $this->declarations[ $property ] = $value;
63
-
64
- return $this;
65
- }
66
-
67
- /**
68
- * Removes a single declaration.
69
- *
70
- * @param string $property The CSS property.
71
- *
72
- * @return WP_Style_Engine_CSS_Declarations Returns the object to allow chaining methods.
73
- */
74
- public function remove_declaration( $property ) {
75
- unset( $this->declarations[ $property ] );
76
- return $this;
77
- }
78
-
79
- /**
80
- * Adds multiple declarations.
81
- *
82
- * @param array $declarations An array of declarations.
83
- *
84
- * @return WP_Style_Engine_CSS_Declarations Returns the object to allow chaining methods.
85
- */
86
- public function add_declarations( $declarations ) {
87
- foreach ( $declarations as $property => $value ) {
88
- $this->add_declaration( $property, $value );
77
+ /**
78
+ * Adds multiple declarations.
79
+ *
80
+ * @param array $declarations An array of declarations.
81
+ *
82
+ * @return WP_Style_Engine_CSS_Declarations Returns the object to allow chaining methods.
83
+ */
84
+ public function add_declarations( $declarations ) {
85
+ foreach ( $declarations as $property => $value ) {
86
+ $this->add_declaration( $property, $value );
87
+ }
88
+ return $this;
89
89
  }
90
- return $this;
91
- }
92
90
 
93
- /**
94
- * Removes multiple declarations.
95
- *
96
- * @param array $properties An array of properties.
97
- *
98
- * @return WP_Style_Engine_CSS_Declarations Returns the object to allow chaining methods.
99
- */
100
- public function remove_declarations( $properties = array() ) {
101
- foreach ( $properties as $property ) {
102
- $this->remove_declaration( $property );
91
+ /**
92
+ * Removes multiple declarations.
93
+ *
94
+ * @param array $properties An array of properties.
95
+ *
96
+ * @return WP_Style_Engine_CSS_Declarations Returns the object to allow chaining methods.
97
+ */
98
+ public function remove_declarations( $properties = array() ) {
99
+ foreach ( $properties as $property ) {
100
+ $this->remove_declaration( $property );
101
+ }
102
+ return $this;
103
103
  }
104
- return $this;
105
- }
106
-
107
- /**
108
- * Gets the declarations array.
109
- *
110
- * @return array
111
- */
112
- public function get_declarations() {
113
- return $this->declarations;
114
- }
115
104
 
116
- /**
117
- * Filters a CSS property + value pair.
118
- *
119
- * @param string $property The CSS property.
120
- * @param string $value The value to be filtered.
121
- * @param string $spacer The spacer between the colon and the value. Defaults to an empty string.
122
- *
123
- * @return string The filtered declaration or an empty string.
124
- */
125
- protected static function filter_declaration( $property, $value, $spacer = '' ) {
126
- $filtered_value = wp_strip_all_tags( $value, true );
105
+ /**
106
+ * Gets the declarations array.
107
+ *
108
+ * @return array
109
+ */
110
+ public function get_declarations() {
111
+ return $this->declarations;
112
+ }
127
113
 
128
- if ( '' !== $filtered_value ) {
129
- return safecss_filter_attr( "{$property}:{$spacer}{$filtered_value}" );
114
+ /**
115
+ * Filters a CSS property + value pair.
116
+ *
117
+ * @param string $property The CSS property.
118
+ * @param string $value The value to be filtered.
119
+ * @param string $spacer The spacer between the colon and the value. Defaults to an empty string.
120
+ *
121
+ * @return string The filtered declaration or an empty string.
122
+ */
123
+ protected static function filter_declaration( $property, $value, $spacer = '' ) {
124
+ $filtered_value = wp_strip_all_tags( $value, true );
125
+
126
+ if ( '' !== $filtered_value ) {
127
+ return safecss_filter_attr( "{$property}:{$spacer}{$filtered_value}" );
128
+ }
129
+ return '';
130
130
  }
131
- return '';
132
- }
133
131
 
134
- /**
135
- * Filters and compiles the CSS declarations.
136
- *
137
- * @param bool $should_prettify Whether to add spacing, new lines and indents.
138
- * @param number $indent_count The number of tab indents to apply to the rule. Applies if `prettify` is `true`.
139
- *
140
- * @return string The CSS declarations.
141
- */
142
- public function get_declarations_string( $should_prettify = false, $indent_count = 0 ) {
143
- $declarations_array = $this->get_declarations();
144
- $declarations_output = '';
145
- $indent = $should_prettify ? str_repeat( "\t", $indent_count ) : '';
146
- $suffix = $should_prettify ? ' ' : '';
147
- $suffix = $should_prettify && $indent_count > 0 ? "\n" : $suffix;
148
- $spacer = $should_prettify ? ' ' : '';
149
-
150
- foreach ( $declarations_array as $property => $value ) {
151
- $filtered_declaration = static::filter_declaration( $property, $value, $spacer );
152
- if ( $filtered_declaration ) {
153
- $declarations_output .= "{$indent}{$filtered_declaration};$suffix";
132
+ /**
133
+ * Filters and compiles the CSS declarations.
134
+ *
135
+ * @param bool $should_prettify Whether to add spacing, new lines and indents.
136
+ * @param number $indent_count The number of tab indents to apply to the rule. Applies if `prettify` is `true`.
137
+ *
138
+ * @return string The CSS declarations.
139
+ */
140
+ public function get_declarations_string( $should_prettify = false, $indent_count = 0 ) {
141
+ $declarations_array = $this->get_declarations();
142
+ $declarations_output = '';
143
+ $indent = $should_prettify ? str_repeat( "\t", $indent_count ) : '';
144
+ $suffix = $should_prettify ? ' ' : '';
145
+ $suffix = $should_prettify && $indent_count > 0 ? "\n" : $suffix;
146
+ $spacer = $should_prettify ? ' ' : '';
147
+
148
+ foreach ( $declarations_array as $property => $value ) {
149
+ $filtered_declaration = static::filter_declaration( $property, $value, $spacer );
150
+ if ( $filtered_declaration ) {
151
+ $declarations_output .= "{$indent}{$filtered_declaration};$suffix";
152
+ }
154
153
  }
154
+ return rtrim( $declarations_output );
155
155
  }
156
- return rtrim( $declarations_output );
157
- }
158
156
 
159
- /**
160
- * Sanitizes property names.
161
- *
162
- * @param string $property The CSS property.
163
- *
164
- * @return string The sanitized property name.
165
- */
166
- protected function sanitize_property( $property ) {
167
- return sanitize_key( $property );
157
+ /**
158
+ * Sanitizes property names.
159
+ *
160
+ * @param string $property The CSS property.
161
+ *
162
+ * @return string The sanitized property name.
163
+ */
164
+ protected function sanitize_property( $property ) {
165
+ return sanitize_key( $property );
166
+ }
168
167
  }
169
168
  }
@@ -7,121 +7,161 @@
7
7
  * @package Gutenberg
8
8
  */
9
9
 
10
- if ( class_exists( 'WP_Style_Engine_CSS_Rule' ) ) {
11
- return;
12
- }
13
-
14
- /**
15
- * Holds, sanitizes, processes and prints CSS declarations for the Style Engine.
16
- *
17
- * @access private
18
- */
19
- class WP_Style_Engine_CSS_Rule {
10
+ if ( ! class_exists( 'WP_Style_Engine_CSS_Rule' ) ) {
20
11
 
21
12
  /**
22
- * The selector.
13
+ * Holds, sanitizes, processes and prints CSS declarations for the Style Engine.
23
14
  *
24
- * @var string
15
+ * @access private
25
16
  */
26
- protected $selector;
17
+ class WP_Style_Engine_CSS_Rule {
27
18
 
28
- /**
29
- * The selector declarations.
30
- *
31
- * Contains a WP_Style_Engine_CSS_Declarations object.
32
- *
33
- * @var WP_Style_Engine_CSS_Declarations
34
- */
35
- protected $declarations;
19
+ /**
20
+ * The selector.
21
+ *
22
+ * @var string
23
+ */
24
+ protected $selector;
36
25
 
37
- /**
38
- * Constructor
39
- *
40
- * @param string $selector The CSS selector.
41
- * @param string[]|WP_Style_Engine_CSS_Declarations $declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ),
42
- * or a WP_Style_Engine_CSS_Declarations object.
43
- */
44
- public function __construct( $selector = '', $declarations = array() ) {
45
- $this->set_selector( $selector );
46
- $this->add_declarations( $declarations );
47
- }
26
+ /**
27
+ * The selector declarations.
28
+ *
29
+ * Contains a WP_Style_Engine_CSS_Declarations object.
30
+ *
31
+ * @var WP_Style_Engine_CSS_Declarations
32
+ */
33
+ protected $declarations;
48
34
 
49
- /**
50
- * Sets the selector.
51
- *
52
- * @param string $selector The CSS selector.
53
- *
54
- * @return WP_Style_Engine_CSS_Rule Returns the object to allow chaining of methods.
55
- */
56
- public function set_selector( $selector ) {
57
- $this->selector = $selector;
58
- return $this;
59
- }
35
+ /**
36
+ * The CSS nested @rule, such as `@media (min-width: 80rem)` or `@layer module`.
37
+ *
38
+ * @var string
39
+ */
40
+ protected $at_rule;
60
41
 
61
- /**
62
- * Sets the declarations.
63
- *
64
- * @param array|WP_Style_Engine_CSS_Declarations $declarations An array of declarations (property => value pairs),
65
- * or a WP_Style_Engine_CSS_Declarations object.
66
- *
67
- * @return WP_Style_Engine_CSS_Rule Returns the object to allow chaining of methods.
68
- */
69
- public function add_declarations( $declarations ) {
70
- $is_declarations_object = ! is_array( $declarations );
71
- $declarations_array = $is_declarations_object ? $declarations->get_declarations() : $declarations;
72
-
73
- if ( null === $this->declarations ) {
74
- if ( $is_declarations_object ) {
75
- $this->declarations = $declarations;
76
- return $this;
42
+
43
+ /**
44
+ * Constructor
45
+ *
46
+ * @param string $selector The CSS selector.
47
+ * @param string[]|WP_Style_Engine_CSS_Declarations $declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ),
48
+ * 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`.
50
+ *
51
+ */
52
+ public function __construct( $selector = '', $declarations = array(), $at_rule = '' ) {
53
+ $this->set_selector( $selector );
54
+ $this->add_declarations( $declarations );
55
+ $this->set_at_rule( $at_rule );
56
+ }
57
+
58
+ /**
59
+ * Sets the selector.
60
+ *
61
+ * @param string $selector The CSS selector.
62
+ *
63
+ * @return WP_Style_Engine_CSS_Rule Returns the object to allow chaining of methods.
64
+ */
65
+ public function set_selector( $selector ) {
66
+ $this->selector = $selector;
67
+ return $this;
68
+ }
69
+
70
+ /**
71
+ * Sets the declarations.
72
+ *
73
+ * @param array|WP_Style_Engine_CSS_Declarations $declarations An array of declarations (property => value pairs),
74
+ * or a WP_Style_Engine_CSS_Declarations object.
75
+ *
76
+ * @return WP_Style_Engine_CSS_Rule Returns the object to allow chaining of methods.
77
+ */
78
+ public function add_declarations( $declarations ) {
79
+ $is_declarations_object = ! is_array( $declarations );
80
+ $declarations_array = $is_declarations_object ? $declarations->get_declarations() : $declarations;
81
+
82
+ if ( null === $this->declarations ) {
83
+ if ( $is_declarations_object ) {
84
+ $this->declarations = $declarations;
85
+ return $this;
86
+ }
87
+ $this->declarations = new WP_Style_Engine_CSS_Declarations( $declarations_array );
77
88
  }
78
- $this->declarations = new WP_Style_Engine_CSS_Declarations( $declarations_array );
89
+ $this->declarations->add_declarations( $declarations_array );
90
+
91
+ return $this;
79
92
  }
80
- $this->declarations->add_declarations( $declarations_array );
81
93
 
82
- return $this;
83
- }
94
+ /**
95
+ * Sets the at_rule.
96
+ *
97
+ * @param string $at_rule A CSS nested @rule, such as `@media (min-width: 80rem)` or `@layer module`.
98
+ *
99
+ * @return WP_Style_Engine_CSS_Rule Returns the object to allow chaining of methods.
100
+ */
101
+ public function set_at_rule( $at_rule ) {
102
+ $this->at_rule = $at_rule;
103
+ return $this;
104
+ }
84
105
 
85
- /**
86
- * Gets the declarations object.
87
- *
88
- * @return WP_Style_Engine_CSS_Declarations The declarations object.
89
- */
90
- public function get_declarations() {
91
- return $this->declarations;
92
- }
106
+ /**
107
+ * Gets the declarations object.
108
+ *
109
+ * @return WP_Style_Engine_CSS_Declarations The declarations object.
110
+ */
111
+ public function get_declarations() {
112
+ return $this->declarations;
113
+ }
93
114
 
94
- /**
95
- * Gets the full selector.
96
- *
97
- * @return string
98
- */
99
- public function get_selector() {
100
- return $this->selector;
101
- }
115
+ /**
116
+ * Gets the full selector.
117
+ *
118
+ * @return string
119
+ */
120
+ public function get_selector() {
121
+ return $this->selector;
122
+ }
102
123
 
103
- /**
104
- * Gets the CSS.
105
- *
106
- * @param bool $should_prettify Whether to add spacing, new lines and indents.
107
- * @param number $indent_count The number of tab indents to apply to the rule. Applies if `prettify` is `true`.
108
- *
109
- * @return string
110
- */
111
- public function get_css( $should_prettify = false, $indent_count = 0 ) {
112
- $rule_indent = $should_prettify ? str_repeat( "\t", $indent_count ) : '';
113
- $declarations_indent = $should_prettify ? $indent_count + 1 : 0;
114
- $suffix = $should_prettify ? "\n" : '';
115
- $spacer = $should_prettify ? ' ' : '';
116
- // Trims any multiple selectors strings.
117
- $selector = $should_prettify ? implode( ',', array_map( 'trim', explode( ',', $this->get_selector() ) ) ) : $this->get_selector();
118
- $selector = $should_prettify ? str_replace( array( ',' ), ",\n", $selector ) : $selector;
119
- $css_declarations = $this->declarations->get_declarations_string( $should_prettify, $declarations_indent );
120
-
121
- if ( empty( $css_declarations ) ) {
122
- return '';
124
+ /**
125
+ * Gets the at_rule.
126
+ *
127
+ * @return string
128
+ */
129
+ public function get_at_rule() {
130
+ return $this->at_rule;
123
131
  }
124
132
 
125
- return "{$rule_indent}{$selector}{$spacer}{{$suffix}{$css_declarations}{$suffix}{$rule_indent}}";
133
+ /**
134
+ * Gets the CSS.
135
+ *
136
+ * @param bool $should_prettify Whether to add spacing, new lines and indents.
137
+ * @param number $indent_count The number of tab indents to apply to the rule. Applies if `prettify` is `true`.
138
+ *
139
+ * @return string
140
+ */
141
+ public function get_css( $should_prettify = false, $indent_count = 0 ) {
142
+ $rule_indent = $should_prettify ? str_repeat( "\t", $indent_count ) : '';
143
+ $nested_rule_indent = $should_prettify ? str_repeat( "\t", $indent_count + 1 ) : '';
144
+ $declarations_indent = $should_prettify ? $indent_count + 1 : 0;
145
+ $nested_declarations_indent = $should_prettify ? $indent_count + 2 : 0;
146
+ $suffix = $should_prettify ? "\n" : '';
147
+ $spacer = $should_prettify ? ' ' : '';
148
+ // Trims any multiple selectors strings.
149
+ $selector = $should_prettify ? implode( ',', array_map( 'trim', explode( ',', $this->get_selector() ) ) ) : $this->get_selector();
150
+ $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 );
154
+
155
+ if ( empty( $css_declarations ) ) {
156
+ return '';
157
+ }
158
+
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}}";
161
+ return $selector;
162
+ }
163
+
164
+ return "{$rule_indent}{$selector}{$spacer}{{$suffix}{$css_declarations}{$suffix}{$rule_indent}}";
165
+ }
126
166
  }
127
167
  }