@wordpress/style-engine 1.33.1 → 1.34.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.
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,138 +7,146 @@
7
7
  * @package Gutenberg
8
8
  */
9
9
 
10
- if ( class_exists( 'WP_Style_Engine_CSS_Rules_Store' ) ) {
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_Rules_Store {
20
-
21
- /**
22
- * An array of named WP_Style_Engine_CSS_Rules_Store objects.
23
- *
24
- * @static
25
- *
26
- * @var WP_Style_Engine_CSS_Rules_Store[]
27
- */
28
- protected static $stores = array();
29
-
30
- /**
31
- * The store name.
32
- *
33
- * @var string
34
- */
35
- protected $name = '';
36
-
37
- /**
38
- * An array of CSS Rules objects assigned to the store.
39
- *
40
- * @var WP_Style_Engine_CSS_Rule[]
41
- */
42
- protected $rules = array();
10
+ if ( ! class_exists( 'WP_Style_Engine_CSS_Rules_Store' ) ) {
43
11
 
44
12
  /**
45
- * Gets an instance of the store.
46
- *
47
- * @param string $store_name The name of the store.
13
+ * Holds, sanitizes, processes and prints CSS declarations for the Style Engine.
48
14
  *
49
- * @return WP_Style_Engine_CSS_Rules_Store|void
15
+ * @access private
50
16
  */
51
- public static function get_store( $store_name = 'default' ) {
52
- if ( ! is_string( $store_name ) || empty( $store_name ) ) {
53
- return;
17
+ class WP_Style_Engine_CSS_Rules_Store {
18
+
19
+ /**
20
+ * An array of named WP_Style_Engine_CSS_Rules_Store objects.
21
+ *
22
+ * @static
23
+ *
24
+ * @var WP_Style_Engine_CSS_Rules_Store[]
25
+ */
26
+ protected static $stores = array();
27
+
28
+ /**
29
+ * The store name.
30
+ *
31
+ * @var string
32
+ */
33
+ protected $name = '';
34
+
35
+ /**
36
+ * An array of CSS Rules objects assigned to the store.
37
+ *
38
+ * @var WP_Style_Engine_CSS_Rule[]
39
+ */
40
+ protected $rules = array();
41
+
42
+ /**
43
+ * Gets an instance of the store.
44
+ *
45
+ * @param string $store_name The name of the store.
46
+ *
47
+ * @return WP_Style_Engine_CSS_Rules_Store|void
48
+ */
49
+ public static function get_store( $store_name = 'default' ) {
50
+ if ( ! is_string( $store_name ) || empty( $store_name ) ) {
51
+ return;
52
+ }
53
+ if ( ! isset( static::$stores[ $store_name ] ) ) {
54
+ static::$stores[ $store_name ] = new static();
55
+ // Set the store name.
56
+ static::$stores[ $store_name ]->set_name( $store_name );
57
+ }
58
+ return static::$stores[ $store_name ];
54
59
  }
55
- if ( ! isset( static::$stores[ $store_name ] ) ) {
56
- static::$stores[ $store_name ] = new static();
57
- // Set the store name.
58
- static::$stores[ $store_name ]->set_name( $store_name );
59
- }
60
- return static::$stores[ $store_name ];
61
- }
62
60
 
63
- /**
64
- * Gets an array of all available stores.
65
- *
66
- * @return WP_Style_Engine_CSS_Rules_Store[]
67
- */
68
- public static function get_stores() {
69
- return static::$stores;
70
- }
71
-
72
- /**
73
- * Clears all stores from static::$stores.
74
- *
75
- * @return void
76
- */
77
- public static function remove_all_stores() {
78
- static::$stores = array();
79
- }
80
-
81
- /**
82
- * Sets the store name.
83
- *
84
- * @param string $name The store name.
85
- *
86
- * @return void
87
- */
88
- public function set_name( $name ) {
89
- $this->name = $name;
90
- }
91
-
92
- /**
93
- * Gets the store name.
94
- *
95
- * @return string
96
- */
97
- public function get_name() {
98
- return $this->name;
99
- }
61
+ /**
62
+ * Gets an array of all available stores.
63
+ *
64
+ * @return WP_Style_Engine_CSS_Rules_Store[]
65
+ */
66
+ public static function get_stores() {
67
+ return static::$stores;
68
+ }
100
69
 
101
- /**
102
- * Gets an array of all rules.
103
- *
104
- * @return WP_Style_Engine_CSS_Rule[]
105
- */
106
- public function get_all_rules() {
107
- return $this->rules;
108
- }
70
+ /**
71
+ * Clears all stores from static::$stores.
72
+ *
73
+ * @return void
74
+ */
75
+ public static function remove_all_stores() {
76
+ static::$stores = array();
77
+ }
109
78
 
110
- /**
111
- * Gets a WP_Style_Engine_CSS_Rule object by its selector.
112
- * If the rule does not exist, it will be created.
113
- *
114
- * @param string $selector The CSS selector.
115
- *
116
- * @return WP_Style_Engine_CSS_Rule|void Returns a WP_Style_Engine_CSS_Rule object, or null if the selector is empty.
117
- */
118
- public function add_rule( $selector ) {
119
- $selector = trim( $selector );
79
+ /**
80
+ * Sets the store name.
81
+ *
82
+ * @param string $name The store name.
83
+ *
84
+ * @return void
85
+ */
86
+ public function set_name( $name ) {
87
+ $this->name = $name;
88
+ }
120
89
 
121
- // Bail early if there is no selector.
122
- if ( empty( $selector ) ) {
123
- return;
90
+ /**
91
+ * Gets the store name.
92
+ *
93
+ * @return string
94
+ */
95
+ public function get_name() {
96
+ return $this->name;
124
97
  }
125
98
 
126
- // Create the rule if it doesn't exist.
127
- if ( empty( $this->rules[ $selector ] ) ) {
128
- $this->rules[ $selector ] = new WP_Style_Engine_CSS_Rule( $selector );
99
+ /**
100
+ * Gets an array of all rules.
101
+ *
102
+ * @return WP_Style_Engine_CSS_Rule[]
103
+ */
104
+ public function get_all_rules() {
105
+ return $this->rules;
129
106
  }
130
107
 
131
- return $this->rules[ $selector ];
132
- }
108
+ /**
109
+ * Gets a WP_Style_Engine_CSS_Rule object by its selector.
110
+ * If the rule does not exist, it will be created.
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`.
114
+ *
115
+ * @return WP_Style_Engine_CSS_Rule|void Returns a WP_Style_Engine_CSS_Rule object, or null if the selector is empty.
116
+ */
117
+ public function add_rule( $selector, $at_rule = '' ) {
118
+ $selector = trim( $selector );
119
+ $at_rule = trim( $at_rule );
120
+
121
+ // Bail early if there is no selector.
122
+ if ( empty( $selector ) ) {
123
+ return;
124
+ }
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 );
129
+ }
130
+ return $this->rules[ "$at_rule $selector" ];
131
+ }
132
+
133
+ // Create the rule if it doesn't exist.
134
+ if ( empty( $this->rules[ $selector ] ) ) {
135
+ $this->rules[ $selector ] = new WP_Style_Engine_CSS_Rule( $selector );
136
+ }
137
+
138
+ return $this->rules[ $selector ];
139
+ }
133
140
 
134
- /**
135
- * Removes a selector from the store.
136
- *
137
- * @param string $selector The CSS selector.
138
- *
139
- * @return void
140
- */
141
- public function remove_rule( $selector ) {
142
- unset( $this->rules[ $selector ] );
141
+ /**
142
+ * Removes a selector from the store.
143
+ *
144
+ * @param string $selector The CSS selector.
145
+ *
146
+ * @return void
147
+ */
148
+ public function remove_rule( $selector ) {
149
+ unset( $this->rules[ $selector ] );
150
+ }
143
151
  }
144
152
  }
@@ -7,151 +7,167 @@
7
7
  * @package Gutenberg
8
8
  */
9
9
 
10
- if ( class_exists( 'WP_Style_Engine_Processor' ) ) {
11
- return;
12
- }
13
-
14
- /**
15
- * Compiles styles from stores or collection of CSS rules.
16
- *
17
- * @access private
18
- */
19
- class WP_Style_Engine_Processor {
10
+ if ( ! class_exists( 'WP_Style_Engine_Processor' ) ) {
20
11
 
21
12
  /**
22
- * A collection of Style Engine Store objects.
13
+ * Compiles styles from stores or collection of CSS rules.
23
14
  *
24
- * @var WP_Style_Engine_CSS_Rules_Store[]
15
+ * @access private
25
16
  */
26
- protected $stores = array();
17
+ class WP_Style_Engine_Processor {
18
+
19
+ /**
20
+ * A collection of Style Engine Store objects.
21
+ *
22
+ * @var WP_Style_Engine_CSS_Rules_Store[]
23
+ */
24
+ protected $stores = array();
25
+
26
+ /**
27
+ * The set of CSS rules that this processor will work on.
28
+ *
29
+ * @var WP_Style_Engine_CSS_Rule[]
30
+ */
31
+ protected $css_rules = array();
32
+
33
+ /**
34
+ * Add a store to the processor.
35
+ *
36
+ * @param WP_Style_Engine_CSS_Rules_Store $store The store to add.
37
+ *
38
+ * @return WP_Style_Engine_Processor Returns the object to allow chaining methods.
39
+ */
40
+ public function add_store( $store ) {
41
+ if ( ! $store instanceof WP_Style_Engine_CSS_Rules_Store ) {
42
+ _doing_it_wrong(
43
+ __METHOD__,
44
+ __( '$store must be an instance of WP_Style_Engine_CSS_Rules_Store', 'default' ),
45
+ '6.1.0'
46
+ );
47
+ return $this;
48
+ }
27
49
 
28
- /**
29
- * The set of CSS rules that this processor will work on.
30
- *
31
- * @var WP_Style_Engine_CSS_Rule[]
32
- */
33
- protected $css_rules = array();
50
+ $this->stores[ $store->get_name() ] = $store;
34
51
 
35
- /**
36
- * Add a store to the processor.
37
- *
38
- * @param WP_Style_Engine_CSS_Rules_Store $store The store to add.
39
- *
40
- * @return WP_Style_Engine_Processor Returns the object to allow chaining methods.
41
- */
42
- public function add_store( $store ) {
43
- if ( ! $store instanceof WP_Style_Engine_CSS_Rules_Store ) {
44
- _doing_it_wrong(
45
- __METHOD__,
46
- __( '$store must be an instance of WP_Style_Engine_CSS_Rules_Store', 'default' ),
47
- '6.1.0'
48
- );
49
52
  return $this;
50
53
  }
51
54
 
52
- $this->stores[ $store->get_name() ] = $store;
53
-
54
- return $this;
55
- }
56
-
57
- /**
58
- * Adds rules to be processed.
59
- *
60
- * @param WP_Style_Engine_CSS_Rule|WP_Style_Engine_CSS_Rule[] $css_rules A single, or an array of, WP_Style_Engine_CSS_Rule objects from a store or otherwise.
61
- *
62
- * @return WP_Style_Engine_Processor Returns the object to allow chaining methods.
63
- */
64
- public function add_rules( $css_rules ) {
65
- if ( ! is_array( $css_rules ) ) {
66
- $css_rules = array( $css_rules );
67
- }
55
+ /**
56
+ * Adds rules to be processed.
57
+ *
58
+ * @param WP_Style_Engine_CSS_Rule|WP_Style_Engine_CSS_Rule[] $css_rules A single, or an array of, WP_Style_Engine_CSS_Rule objects from a store or otherwise.
59
+ *
60
+ * @return WP_Style_Engine_Processor Returns the object to allow chaining methods.
61
+ */
62
+ public function add_rules( $css_rules ) {
63
+ if ( ! is_array( $css_rules ) ) {
64
+ $css_rules = array( $css_rules );
65
+ }
68
66
 
69
- foreach ( $css_rules as $rule ) {
70
- $selector = $rule->get_selector();
71
- if ( isset( $this->css_rules[ $selector ] ) ) {
72
- $this->css_rules[ $selector ]->add_declarations( $rule->get_declarations() );
73
- continue;
67
+ foreach ( $css_rules as $rule ) {
68
+ $selector = $rule->get_selector();
69
+ $at_rule = $rule->get_at_rule();
70
+
71
+ /**
72
+ * If there is an at_rule and it already exists in the css_rules array,
73
+ * add the rule to it.
74
+ * Otherwise, create a new entry for the at_rule
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() );
79
+ continue;
80
+ }
81
+ $this->css_rules[ "$at_rule $selector" ] = $rule;
82
+ continue;
83
+ }
84
+
85
+ // If the selector already exists, add the declarations to it.
86
+ if ( isset( $this->css_rules[ $selector ] ) ) {
87
+ $this->css_rules[ $selector ]->add_declarations( $rule->get_declarations() );
88
+ continue;
89
+ }
90
+ $this->css_rules[ $rule->get_selector() ] = $rule;
74
91
  }
75
- $this->css_rules[ $rule->get_selector() ] = $rule;
92
+ return $this;
76
93
  }
77
94
 
78
- return $this;
79
- }
80
-
81
- /**
82
- * Get the CSS rules as a string.
83
- *
84
- * Since 6.4.0 Optimization is no longer the default.
85
- *
86
- * @param array $options {
87
- * Optional. An array of options. Default empty array.
88
- *
89
- * @type bool $optimize Whether to optimize the CSS output, e.g., combine rules. Default is `false`.
90
- * @type bool $prettify Whether to add new lines and indents to output. Default is to inherit the value of the global constant `SCRIPT_DEBUG`, if it is defined.
91
- * }
92
- *
93
- * @return string The computed CSS.
94
- */
95
- public function get_css( $options = array() ) {
96
- $defaults = array(
97
- 'optimize' => false,
98
- 'prettify' => SCRIPT_DEBUG,
99
- );
100
- $options = wp_parse_args( $options, $defaults );
101
-
102
- // If we have stores, get the rules from them.
103
- foreach ( $this->stores as $store ) {
104
- $this->add_rules( $store->get_all_rules() );
105
- }
95
+ /**
96
+ * Get the CSS rules as a string.
97
+ *
98
+ * Since 6.4.0 Optimization is no longer the default.
99
+ *
100
+ * @param array $options {
101
+ * Optional. An array of options. Default empty array.
102
+ *
103
+ * @type bool $optimize Whether to optimize the CSS output, e.g., combine rules. Default is `false`.
104
+ * @type bool $prettify Whether to add new lines and indents to output. Default is to inherit the value of the global constant `SCRIPT_DEBUG`, if it is defined.
105
+ * }
106
+ *
107
+ * @return string The computed CSS.
108
+ */
109
+ public function get_css( $options = array() ) {
110
+ $defaults = array(
111
+ 'optimize' => false,
112
+ 'prettify' => SCRIPT_DEBUG,
113
+ );
114
+ $options = wp_parse_args( $options, $defaults );
106
115
 
107
- // Combine CSS selectors that have identical declarations.
108
- if ( true === $options['optimize'] ) {
109
- $this->combine_rules_selectors();
110
- }
116
+ // If we have stores, get the rules from them.
117
+ foreach ( $this->stores as $store ) {
118
+ $this->add_rules( $store->get_all_rules() );
119
+ }
111
120
 
112
- // Build the CSS.
113
- $css = '';
114
- foreach ( $this->css_rules as $rule ) {
115
- $css .= $rule->get_css( $options['prettify'] );
116
- $css .= $options['prettify'] ? "\n" : '';
117
- }
118
- return $css;
119
- }
121
+ // Combine CSS selectors that have identical declarations.
122
+ if ( true === $options['optimize'] ) {
123
+ $this->combine_rules_selectors();
124
+ }
120
125
 
121
- /**
122
- * Combines selectors from the rules store when they have the same styles.
123
- *
124
- * @return void
125
- */
126
- private function combine_rules_selectors() {
127
- // Build an array of selectors along with the JSON-ified styles to make comparisons easier.
128
- $selectors_json = array();
129
- foreach ( $this->css_rules as $rule ) {
130
- $declarations = $rule->get_declarations()->get_declarations();
131
- ksort( $declarations );
132
- $selectors_json[ $rule->get_selector() ] = wp_json_encode( $declarations );
126
+ // Build the CSS.
127
+ $css = '';
128
+ foreach ( $this->css_rules as $rule ) {
129
+ // See class WP_Style_Engine_CSS_Rule for the get_css method.
130
+ $css .= $rule->get_css( $options['prettify'] );
131
+ $css .= $options['prettify'] ? "\n" : '';
132
+ }
133
+ return $css;
133
134
  }
134
135
 
135
- // Combine selectors that have the same styles.
136
- foreach ( $selectors_json as $selector => $json ) {
137
- // Get selectors that use the same styles.
138
- $duplicates = array_keys( $selectors_json, $json, true );
139
- // Skip if there are no duplicates.
140
- if ( 1 >= count( $duplicates ) ) {
141
- continue;
136
+ /**
137
+ * Combines selectors from the rules store when they have the same styles.
138
+ *
139
+ * @return void
140
+ */
141
+ private function combine_rules_selectors() {
142
+ // Build an array of selectors along with the JSON-ified styles to make comparisons easier.
143
+ $selectors_json = array();
144
+ foreach ( $this->css_rules as $rule ) {
145
+ $declarations = $rule->get_declarations()->get_declarations();
146
+ ksort( $declarations );
147
+ $selectors_json[ $rule->get_selector() ] = wp_json_encode( $declarations );
142
148
  }
143
149
 
144
- $declarations = $this->css_rules[ $selector ]->get_declarations();
145
-
146
- foreach ( $duplicates as $key ) {
147
- // Unset the duplicates from the $selectors_json array to avoid looping through them as well.
148
- unset( $selectors_json[ $key ] );
149
- // Remove the rules from the rules collection.
150
- unset( $this->css_rules[ $key ] );
150
+ // Combine selectors that have the same styles.
151
+ foreach ( $selectors_json as $selector => $json ) {
152
+ // Get selectors that use the same styles.
153
+ $duplicates = array_keys( $selectors_json, $json, true );
154
+ // Skip if there are no duplicates.
155
+ if ( 1 >= count( $duplicates ) ) {
156
+ continue;
157
+ }
158
+
159
+ $declarations = $this->css_rules[ $selector ]->get_declarations();
160
+
161
+ foreach ( $duplicates as $key ) {
162
+ // Unset the duplicates from the $selectors_json array to avoid looping through them as well.
163
+ unset( $selectors_json[ $key ] );
164
+ // Remove the rules from the rules collection.
165
+ unset( $this->css_rules[ $key ] );
166
+ }
167
+ // Create a new rule with the combined selectors.
168
+ $duplicate_selectors = implode( ',', $duplicates );
169
+ $this->css_rules[ $duplicate_selectors ] = new WP_Style_Engine_CSS_Rule( $duplicate_selectors, $declarations );
151
170
  }
152
- // Create a new rule with the combined selectors.
153
- $duplicate_selectors = implode( ',', $duplicates );
154
- $this->css_rules[ $duplicate_selectors ] = new WP_Style_Engine_CSS_Rule( $duplicate_selectors, $declarations );
155
171
  }
156
172
  }
157
173
  }