@wordpress/style-engine 0.8.0 → 0.11.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 +6 -0
- package/README.md +38 -6
- package/build/styles/border/index.js +94 -0
- package/build/styles/border/index.js.map +1 -0
- package/build/styles/index.js +3 -1
- package/build/styles/index.js.map +1 -1
- package/build/styles/spacing/margin.js +4 -1
- package/build/styles/spacing/margin.js.map +1 -1
- package/build/styles/spacing/padding.js +4 -1
- package/build/styles/spacing/padding.js.map +1 -1
- package/build/styles/utils.js +25 -9
- package/build/styles/utils.js.map +1 -1
- package/build-module/styles/border/index.js +85 -0
- package/build-module/styles/border/index.js.map +1 -0
- package/build-module/styles/index.js +2 -1
- package/build-module/styles/index.js.map +1 -1
- package/build-module/styles/spacing/margin.js +4 -1
- package/build-module/styles/spacing/margin.js.map +1 -1
- package/build-module/styles/spacing/padding.js +4 -1
- package/build-module/styles/spacing/padding.js.map +1 -1
- package/build-module/styles/utils.js +24 -10
- package/build-module/styles/utils.js.map +1 -1
- package/build-types/styles/border/index.d.ts +10 -0
- package/build-types/styles/border/index.d.ts.map +1 -0
- package/build-types/styles/index.d.ts +3 -7
- package/build-types/styles/index.d.ts.map +1 -1
- package/build-types/styles/spacing/margin.d.ts.map +1 -1
- package/build-types/styles/spacing/padding.d.ts.map +1 -1
- package/build-types/styles/utils.d.ts +16 -7
- package/build-types/styles/utils.d.ts.map +1 -1
- package/build-types/types.d.ts +26 -1
- package/build-types/types.d.ts.map +1 -1
- package/class-wp-style-engine.php +303 -97
- package/package.json +2 -2
- package/phpunit/class-wp-style-engine-test.php +154 -6
- package/src/styles/border/index.ts +145 -0
- package/src/styles/index.ts +7 -1
- package/src/styles/spacing/margin.ts +4 -6
- package/src/styles/spacing/padding.ts +4 -6
- package/src/styles/utils.ts +33 -11
- package/src/test/index.js +51 -0
- package/src/test/utils.js +12 -0
- package/src/types.ts +33 -1
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -34,87 +34,183 @@ class WP_Style_Engine {
|
|
|
34
34
|
* Style definitions that contain the instructions to
|
|
35
35
|
* parse/output valid Gutenberg styles from a block's attributes.
|
|
36
36
|
* For every style definition, the follow properties are valid:
|
|
37
|
-
* - classnames
|
|
38
|
-
* A value of `true` means the classname should be applied always. Otherwise a valid CSS property
|
|
39
|
-
* to match the incoming value, e.g., "color" to match var:preset|color|
|
|
40
|
-
* -
|
|
41
|
-
*
|
|
37
|
+
* - classnames => (array) an array of classnames to be returned for block styles. The key is a classname or pattern.
|
|
38
|
+
* A value of `true` means the classname should be applied always. Otherwise, a valid CSS property (string)
|
|
39
|
+
* to match the incoming value, e.g., "color" to match var:preset|color|somePresetSlug.
|
|
40
|
+
* - css_vars => (array) an array of key value pairs used to generate CSS var values. The key is a CSS var pattern, whose `$slug` fragment will be replaced with a preset slug.
|
|
41
|
+
* The value should be a valid CSS property (string) to match the incoming value, e.g., "color" to match var:preset|color|somePresetSlug.
|
|
42
|
+
* - property_keys => (array) array of keys whose values represent a valid CSS property, e.g., "margin" or "border".
|
|
43
|
+
* - path => (array) a path that accesses the corresponding style value in the block style object.
|
|
44
|
+
* - value_func => (string) the name of a function to generate a CSS definition array for a particular style object. The output of this function should be `array( "$property" => "$value", ... )`.
|
|
42
45
|
*/
|
|
43
46
|
const BLOCK_STYLE_DEFINITIONS_METADATA = array(
|
|
44
47
|
'color' => array(
|
|
45
48
|
'text' => array(
|
|
46
|
-
'
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
'property_keys' => array(
|
|
50
|
+
'default' => 'color',
|
|
51
|
+
),
|
|
52
|
+
'path' => array( 'color', 'text' ),
|
|
53
|
+
'css_vars' => array(
|
|
54
|
+
'--wp--preset--color--$slug' => 'color',
|
|
55
|
+
),
|
|
56
|
+
'classnames' => array(
|
|
57
|
+
'has-text-color' => true,
|
|
58
|
+
'has-$slug-color' => 'color',
|
|
51
59
|
),
|
|
52
60
|
),
|
|
53
61
|
'background' => array(
|
|
54
|
-
'
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
62
|
+
'property_keys' => array(
|
|
63
|
+
'default' => 'background-color',
|
|
64
|
+
),
|
|
65
|
+
'path' => array( 'color', 'background' ),
|
|
66
|
+
'classnames' => array(
|
|
67
|
+
'has-background' => true,
|
|
68
|
+
'has-$slug-background-color' => 'color',
|
|
59
69
|
),
|
|
60
70
|
),
|
|
61
71
|
'gradient' => array(
|
|
62
|
-
'
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
72
|
+
'property_keys' => array(
|
|
73
|
+
'default' => 'background',
|
|
74
|
+
),
|
|
75
|
+
'path' => array( 'color', 'gradient' ),
|
|
76
|
+
'classnames' => array(
|
|
77
|
+
'has-background' => true,
|
|
78
|
+
'has-$slug-gradient-background' => 'gradient',
|
|
79
|
+
),
|
|
80
|
+
),
|
|
81
|
+
),
|
|
82
|
+
'border' => array(
|
|
83
|
+
'color' => array(
|
|
84
|
+
'property_keys' => array(
|
|
85
|
+
'default' => 'border-color',
|
|
86
|
+
'individual' => 'border-%s-color',
|
|
87
|
+
),
|
|
88
|
+
'path' => array( 'border', 'color' ),
|
|
89
|
+
'classnames' => array(
|
|
90
|
+
'has-border-color' => true,
|
|
91
|
+
'has-$slug-border-color' => 'color',
|
|
92
|
+
),
|
|
93
|
+
),
|
|
94
|
+
'radius' => array(
|
|
95
|
+
'property_keys' => array(
|
|
96
|
+
'default' => 'border-radius',
|
|
97
|
+
'individual' => 'border-%s-radius',
|
|
98
|
+
),
|
|
99
|
+
'path' => array( 'border', 'radius' ),
|
|
100
|
+
),
|
|
101
|
+
'style' => array(
|
|
102
|
+
'property_keys' => array(
|
|
103
|
+
'default' => 'border-style',
|
|
104
|
+
'individual' => 'border-%s-style',
|
|
105
|
+
),
|
|
106
|
+
'path' => array( 'border', 'style' ),
|
|
107
|
+
),
|
|
108
|
+
'width' => array(
|
|
109
|
+
'property_keys' => array(
|
|
110
|
+
'default' => 'border-width',
|
|
111
|
+
'individual' => 'border-%s-width',
|
|
112
|
+
),
|
|
113
|
+
'path' => array( 'border', 'width' ),
|
|
114
|
+
),
|
|
115
|
+
'top' => array(
|
|
116
|
+
'value_func' => 'static::get_individual_property_css_declarations',
|
|
117
|
+
'path' => array( 'border', 'top' ),
|
|
118
|
+
'css_vars' => array(
|
|
119
|
+
'color' => '--wp--preset--color--$slug',
|
|
120
|
+
),
|
|
121
|
+
),
|
|
122
|
+
'right' => array(
|
|
123
|
+
'value_func' => 'static::get_individual_property_css_declarations',
|
|
124
|
+
'path' => array( 'border', 'right' ),
|
|
125
|
+
'css_vars' => array(
|
|
126
|
+
'color' => '--wp--preset--color--$slug',
|
|
127
|
+
),
|
|
128
|
+
),
|
|
129
|
+
'bottom' => array(
|
|
130
|
+
'value_func' => 'static::get_individual_property_css_declarations',
|
|
131
|
+
'path' => array( 'border', 'bottom' ),
|
|
132
|
+
'css_vars' => array(
|
|
133
|
+
'color' => '--wp--preset--color--$slug',
|
|
134
|
+
),
|
|
135
|
+
),
|
|
136
|
+
'left' => array(
|
|
137
|
+
'value_func' => 'static::get_individual_property_css_declarations',
|
|
138
|
+
'path' => array( 'border', 'left' ),
|
|
139
|
+
'css_vars' => array(
|
|
140
|
+
'color' => '--wp--preset--color--$slug',
|
|
67
141
|
),
|
|
68
142
|
),
|
|
69
143
|
),
|
|
70
144
|
'spacing' => array(
|
|
71
145
|
'padding' => array(
|
|
72
|
-
'
|
|
73
|
-
|
|
146
|
+
'property_keys' => array(
|
|
147
|
+
'default' => 'padding',
|
|
148
|
+
'individual' => 'padding-%s',
|
|
149
|
+
),
|
|
150
|
+
'path' => array( 'spacing', 'padding' ),
|
|
74
151
|
),
|
|
75
152
|
'margin' => array(
|
|
76
|
-
'
|
|
77
|
-
|
|
153
|
+
'property_keys' => array(
|
|
154
|
+
'default' => 'margin',
|
|
155
|
+
'individual' => 'margin-%s',
|
|
156
|
+
),
|
|
157
|
+
'path' => array( 'spacing', 'margin' ),
|
|
78
158
|
),
|
|
79
159
|
),
|
|
80
160
|
'typography' => array(
|
|
81
161
|
'fontSize' => array(
|
|
82
|
-
'
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
162
|
+
'property_keys' => array(
|
|
163
|
+
'default' => 'font-size',
|
|
164
|
+
),
|
|
165
|
+
'path' => array( 'typography', 'fontSize' ),
|
|
166
|
+
'classnames' => array(
|
|
167
|
+
'has-$slug-font-size' => 'font-size',
|
|
86
168
|
),
|
|
87
169
|
),
|
|
88
170
|
'fontFamily' => array(
|
|
89
|
-
'
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
171
|
+
'property_keys' => array(
|
|
172
|
+
'default' => 'font-family',
|
|
173
|
+
),
|
|
174
|
+
'path' => array( 'typography', 'fontFamily' ),
|
|
175
|
+
'classnames' => array(
|
|
176
|
+
'has-$slug-font-family' => 'font-family',
|
|
93
177
|
),
|
|
94
178
|
),
|
|
95
179
|
'fontStyle' => array(
|
|
96
|
-
'
|
|
97
|
-
|
|
180
|
+
'property_keys' => array(
|
|
181
|
+
'default' => 'font-style',
|
|
182
|
+
),
|
|
183
|
+
'path' => array( 'typography', 'fontStyle' ),
|
|
98
184
|
),
|
|
99
185
|
'fontWeight' => array(
|
|
100
|
-
'
|
|
101
|
-
|
|
186
|
+
'property_keys' => array(
|
|
187
|
+
'default' => 'font-weight',
|
|
188
|
+
),
|
|
189
|
+
'path' => array( 'typography', 'fontWeight' ),
|
|
102
190
|
),
|
|
103
191
|
'lineHeight' => array(
|
|
104
|
-
'
|
|
105
|
-
|
|
192
|
+
'property_keys' => array(
|
|
193
|
+
'default' => 'line-height',
|
|
194
|
+
),
|
|
195
|
+
'path' => array( 'typography', 'lineHeight' ),
|
|
106
196
|
),
|
|
107
197
|
'textDecoration' => array(
|
|
108
|
-
'
|
|
109
|
-
|
|
198
|
+
'property_keys' => array(
|
|
199
|
+
'default' => 'text-decoration',
|
|
200
|
+
),
|
|
201
|
+
'path' => array( 'typography', 'textDecoration' ),
|
|
110
202
|
),
|
|
111
203
|
'textTransform' => array(
|
|
112
|
-
'
|
|
113
|
-
|
|
204
|
+
'property_keys' => array(
|
|
205
|
+
'default' => 'text-transform',
|
|
206
|
+
),
|
|
207
|
+
'path' => array( 'typography', 'textTransform' ),
|
|
114
208
|
),
|
|
115
209
|
'letterSpacing' => array(
|
|
116
|
-
'
|
|
117
|
-
|
|
210
|
+
'property_keys' => array(
|
|
211
|
+
'default' => 'letter-spacing',
|
|
212
|
+
),
|
|
213
|
+
'path' => array( 'typography', 'letterSpacing' ),
|
|
118
214
|
),
|
|
119
215
|
),
|
|
120
216
|
);
|
|
@@ -137,8 +233,8 @@ class WP_Style_Engine {
|
|
|
137
233
|
/**
|
|
138
234
|
* Extracts the slug in kebab case from a preset string, e.g., "heavenly-blue" from 'var:preset|color|heavenlyBlue'.
|
|
139
235
|
*
|
|
140
|
-
* @param string $style_value A single css preset value.
|
|
141
|
-
* @param string
|
|
236
|
+
* @param string? $style_value A single css preset value.
|
|
237
|
+
* @param string $property_key The CSS property that is the second element of the preset string. Used for matching.
|
|
142
238
|
*
|
|
143
239
|
* @return string|null The slug, or null if not found.
|
|
144
240
|
*/
|
|
@@ -150,6 +246,25 @@ class WP_Style_Engine {
|
|
|
150
246
|
return null;
|
|
151
247
|
}
|
|
152
248
|
|
|
249
|
+
/**
|
|
250
|
+
* Checks whether an incoming block style value is valid.
|
|
251
|
+
*
|
|
252
|
+
* @param string? $style_value A single css preset value.
|
|
253
|
+
*
|
|
254
|
+
* @return boolean
|
|
255
|
+
*/
|
|
256
|
+
protected static function is_valid_style_value( $style_value ) {
|
|
257
|
+
if ( '0' === $style_value ) {
|
|
258
|
+
return true;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if ( empty( $style_value ) ) {
|
|
262
|
+
return false;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return true;
|
|
266
|
+
}
|
|
267
|
+
|
|
153
268
|
/**
|
|
154
269
|
* Returns classnames, and generates classname(s) from a CSS preset property pattern, e.g., 'var:preset|color|heavenly-blue'.
|
|
155
270
|
*
|
|
@@ -160,6 +275,11 @@ class WP_Style_Engine {
|
|
|
160
275
|
*/
|
|
161
276
|
protected static function get_classnames( $style_value, $style_definition ) {
|
|
162
277
|
$classnames = array();
|
|
278
|
+
|
|
279
|
+
if ( empty( $style_value ) ) {
|
|
280
|
+
return $classnames;
|
|
281
|
+
}
|
|
282
|
+
|
|
163
283
|
if ( ! empty( $style_definition['classnames'] ) ) {
|
|
164
284
|
foreach ( $style_definition['classnames'] as $classname => $property_key ) {
|
|
165
285
|
if ( true === $property_key ) {
|
|
@@ -173,7 +293,7 @@ class WP_Style_Engine {
|
|
|
173
293
|
// One day, if there are no stored schemata, we could allow custom patterns or
|
|
174
294
|
// generate classnames based on other properties
|
|
175
295
|
// such as a path or a value or a prefix passed in options.
|
|
176
|
-
$classnames[] =
|
|
296
|
+
$classnames[] = strtr( $classname, array( '$slug' => $slug ) );
|
|
177
297
|
}
|
|
178
298
|
}
|
|
179
299
|
}
|
|
@@ -182,75 +302,130 @@ class WP_Style_Engine {
|
|
|
182
302
|
}
|
|
183
303
|
|
|
184
304
|
/**
|
|
185
|
-
* Returns CSS
|
|
305
|
+
* Returns an array of CSS declarations based on valid block style values.
|
|
186
306
|
*
|
|
187
307
|
* @param array $style_value A single raw style value from the generate() $block_styles array.
|
|
188
308
|
* @param array<string> $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
|
|
309
|
+
* @param boolean $should_return_css_vars Whether to try to build and return CSS var values.
|
|
189
310
|
*
|
|
190
|
-
* @return array An array of CSS
|
|
311
|
+
* @return array An array of CSS definitions, e.g., array( "$property" => "$value" ).
|
|
191
312
|
*/
|
|
192
|
-
protected static function
|
|
193
|
-
|
|
313
|
+
protected static function get_css_declarations( $style_value, $style_definition, $should_return_css_vars ) {
|
|
314
|
+
if (
|
|
315
|
+
isset( $style_definition['value_func'] ) &&
|
|
316
|
+
is_callable( $style_definition['value_func'] )
|
|
317
|
+
) {
|
|
318
|
+
return call_user_func( $style_definition['value_func'], $style_value, $style_definition );
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
$css_declarations = array();
|
|
322
|
+
$style_property_keys = $style_definition['property_keys'];
|
|
323
|
+
|
|
324
|
+
// Build CSS var values from var:? values, e.g, `var(--wp--css--rule-slug )`
|
|
325
|
+
// Check if the value is a CSS preset and there's a corresponding css_var pattern in the style definition.
|
|
194
326
|
if ( is_string( $style_value ) && strpos( $style_value, 'var:' ) !== false ) {
|
|
195
|
-
|
|
327
|
+
if ( $should_return_css_vars && ! empty( $style_definition['css_vars'] ) ) {
|
|
328
|
+
foreach ( $style_definition['css_vars'] as $css_var_pattern => $property_key ) {
|
|
329
|
+
$slug = static::get_slug_from_preset_value( $style_value, $property_key );
|
|
330
|
+
if ( $slug ) {
|
|
331
|
+
$css_var = strtr(
|
|
332
|
+
$css_var_pattern,
|
|
333
|
+
array( '$slug' => $slug )
|
|
334
|
+
);
|
|
335
|
+
$css_declarations[ $style_property_keys['default'] ] = "var($css_var)";
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return $css_declarations;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// Default rule builder.
|
|
343
|
+
// If the input contains an array, assume box model-like properties
|
|
344
|
+
// for styles such as margins and padding.
|
|
345
|
+
if ( is_array( $style_value ) ) {
|
|
346
|
+
foreach ( $style_value as $key => $value ) {
|
|
347
|
+
$individual_property = sprintf( $style_property_keys['individual'], _wp_to_kebab_case( $key ) );
|
|
348
|
+
$css_declarations[ $individual_property ] = $value;
|
|
349
|
+
}
|
|
350
|
+
} else {
|
|
351
|
+
$css_declarations[ $style_property_keys['default'] ] = $style_value;
|
|
196
352
|
}
|
|
197
353
|
|
|
198
|
-
|
|
199
|
-
return static::get_css_rules( $style_value, $style_definition['property_key'] );
|
|
354
|
+
return $css_declarations;
|
|
200
355
|
}
|
|
201
356
|
|
|
202
357
|
/**
|
|
203
|
-
* Returns
|
|
204
|
-
*
|
|
358
|
+
* Returns classname and CSS from a block styles object.
|
|
359
|
+
* Return values are parsed based on the instructions in BLOCK_STYLE_DEFINITIONS_METADATA.
|
|
205
360
|
*
|
|
206
361
|
* @param array $block_styles An array of styles from a block's attributes.
|
|
362
|
+
* @param array $options array(
|
|
363
|
+
* 'selector' => (string) When a selector is passed, `generate()` will return a full CSS rule `$selector { ...rules }`, otherwise a concatenated string of properties and values.
|
|
364
|
+
* 'css_vars' => (boolean) Whether to covert CSS values to var() values. If `true` the style engine will try to parse var:? values and output var( --wp--preset--* ) rules. Default is `false`.
|
|
365
|
+
* );.
|
|
207
366
|
*
|
|
208
367
|
* @return array|null array(
|
|
209
|
-
* '
|
|
368
|
+
* 'css' => (string) A CSS ruleset formatted to be placed in an HTML `style` attribute or tag. Default is a string of inline styles.
|
|
210
369
|
* 'classnames' => (string) Classnames separated by a space.
|
|
211
370
|
* );
|
|
212
371
|
*/
|
|
213
|
-
public function generate( $block_styles ) {
|
|
372
|
+
public function generate( $block_styles, $options ) {
|
|
214
373
|
if ( empty( $block_styles ) || ! is_array( $block_styles ) ) {
|
|
215
374
|
return null;
|
|
216
375
|
}
|
|
217
376
|
|
|
218
|
-
$
|
|
219
|
-
$classnames
|
|
220
|
-
$
|
|
377
|
+
$css_declarations = array();
|
|
378
|
+
$classnames = array();
|
|
379
|
+
$should_return_css_vars = isset( $options['css_vars'] ) && true === $options['css_vars'];
|
|
221
380
|
|
|
222
381
|
// Collect CSS and classnames.
|
|
223
|
-
foreach ( self::BLOCK_STYLE_DEFINITIONS_METADATA as $
|
|
224
|
-
|
|
382
|
+
foreach ( self::BLOCK_STYLE_DEFINITIONS_METADATA as $definition_group_key => $definition_group_style ) {
|
|
383
|
+
if ( empty( $block_styles[ $definition_group_key ] ) ) {
|
|
384
|
+
continue;
|
|
385
|
+
}
|
|
386
|
+
foreach ( $definition_group_style as $style_definition ) {
|
|
225
387
|
$style_value = _wp_array_get( $block_styles, $style_definition['path'], null );
|
|
226
388
|
|
|
227
|
-
if (
|
|
389
|
+
if ( ! static::is_valid_style_value( $style_value ) ) {
|
|
228
390
|
continue;
|
|
229
391
|
}
|
|
230
392
|
|
|
231
|
-
$classnames
|
|
232
|
-
$
|
|
393
|
+
$classnames = array_merge( $classnames, static::get_classnames( $style_value, $style_definition ) );
|
|
394
|
+
$css_declarations = array_merge( $css_declarations, static::get_css_declarations( $style_value, $style_definition, $should_return_css_vars ) );
|
|
233
395
|
}
|
|
234
396
|
}
|
|
235
397
|
|
|
236
398
|
// Build CSS rules output.
|
|
237
|
-
$
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
// inline
|
|
242
|
-
foreach ( $
|
|
243
|
-
$
|
|
244
|
-
if ( ! empty( $
|
|
245
|
-
$
|
|
399
|
+
$css_selector = isset( $options['selector'] ) ? $options['selector'] : null;
|
|
400
|
+
$filtered_css_declarations = array();
|
|
401
|
+
|
|
402
|
+
if ( ! empty( $css_declarations ) ) {
|
|
403
|
+
// Generate inline style declarations.
|
|
404
|
+
foreach ( $css_declarations as $css_property => $css_value ) {
|
|
405
|
+
$filtered_css_declaration = esc_html( safecss_filter_attr( "{$css_property}: {$css_value}" ) );
|
|
406
|
+
if ( ! empty( $filtered_css_declaration ) ) {
|
|
407
|
+
$filtered_css_declarations[] = $filtered_css_declaration . ';';
|
|
246
408
|
}
|
|
247
409
|
}
|
|
248
410
|
}
|
|
249
411
|
|
|
250
|
-
|
|
251
|
-
|
|
412
|
+
// The return object.
|
|
413
|
+
$styles_output = array();
|
|
414
|
+
|
|
415
|
+
// Return css, if any.
|
|
416
|
+
if ( ! empty( $filtered_css_declarations ) ) {
|
|
417
|
+
// Return an entire rule if there is a selector.
|
|
418
|
+
if ( $css_selector ) {
|
|
419
|
+
$css_rule = "$css_selector { ";
|
|
420
|
+
$css_rule .= implode( ' ', $filtered_css_declarations );
|
|
421
|
+
$css_rule .= ' }';
|
|
422
|
+
$styles_output['css'] = $css_rule;
|
|
423
|
+
} else {
|
|
424
|
+
$styles_output['css'] = implode( ' ', $filtered_css_declarations );
|
|
425
|
+
}
|
|
252
426
|
}
|
|
253
427
|
|
|
428
|
+
// Return classnames, if any.
|
|
254
429
|
if ( ! empty( $classnames ) ) {
|
|
255
430
|
$styles_output['classnames'] = implode( ' ', array_unique( $classnames ) );
|
|
256
431
|
}
|
|
@@ -258,33 +433,56 @@ class WP_Style_Engine {
|
|
|
258
433
|
return $styles_output;
|
|
259
434
|
}
|
|
260
435
|
|
|
436
|
+
|
|
261
437
|
/**
|
|
262
|
-
*
|
|
263
|
-
*
|
|
264
|
-
*
|
|
438
|
+
* Style value parser that returns a CSS definition array comprising style properties
|
|
439
|
+
* that have keys representing individual style properties, otherwise known as longhand CSS properties.
|
|
440
|
+
* e.g., "$style_property-$individual_feature: $value;", which could represent the following:
|
|
441
|
+
* "border-{top|right|bottom|left}-{color|width|style}: {value};" or,
|
|
442
|
+
* "border-image-{outset|source|width|repeat|slice}: {value};"
|
|
265
443
|
*
|
|
266
|
-
* @param
|
|
267
|
-
* @param
|
|
444
|
+
* @param array $style_value A single raw Gutenberg style attributes value for a CSS property.
|
|
445
|
+
* @param array $individual_property_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
|
|
268
446
|
*
|
|
269
|
-
* @return array
|
|
447
|
+
* @return array An array of CSS definitions, e.g., array( "$property" => "$value" ).
|
|
270
448
|
*/
|
|
271
|
-
protected static function
|
|
272
|
-
$
|
|
449
|
+
protected static function get_individual_property_css_declarations( $style_value, $individual_property_definition ) {
|
|
450
|
+
$css_declarations = array();
|
|
273
451
|
|
|
274
|
-
if ( ! $style_value ) {
|
|
275
|
-
return $
|
|
452
|
+
if ( ! is_array( $style_value ) || empty( $style_value ) || empty( $individual_property_definition['path'] ) ) {
|
|
453
|
+
return $css_declarations;
|
|
276
454
|
}
|
|
277
455
|
|
|
278
|
-
//
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
456
|
+
// The first item in $individual_property_definition['path'] array tells us the style property, e.g., "border".
|
|
457
|
+
// We use this to get a corresponding CSS style definition such as "color" or "width" from the same group.
|
|
458
|
+
// The second item in $individual_property_definition['path'] array refers to the individual property marker, e.g., "top".
|
|
459
|
+
$definition_group_key = $individual_property_definition['path'][0];
|
|
460
|
+
$individual_property_key = $individual_property_definition['path'][1];
|
|
461
|
+
|
|
462
|
+
foreach ( $style_value as $css_property => $value ) {
|
|
463
|
+
if ( empty( $value ) ) {
|
|
464
|
+
continue;
|
|
282
465
|
}
|
|
283
|
-
} else {
|
|
284
|
-
$rules[ $style_property ] = $style_value;
|
|
285
|
-
}
|
|
286
466
|
|
|
287
|
-
|
|
467
|
+
// Build a path to the individual rules in definitions.
|
|
468
|
+
$style_definition_path = array( $definition_group_key, $css_property );
|
|
469
|
+
$style_definition = _wp_array_get( self::BLOCK_STYLE_DEFINITIONS_METADATA, $style_definition_path, null );
|
|
470
|
+
|
|
471
|
+
if ( $style_definition && isset( $style_definition['property_keys']['individual'] ) ) {
|
|
472
|
+
// Set a CSS var if there is a valid preset value.
|
|
473
|
+
$slug = isset( $individual_property_definition['css_vars'][ $css_property ] ) ? static::get_slug_from_preset_value( $value, $css_property ) : null;
|
|
474
|
+
if ( $slug ) {
|
|
475
|
+
$css_var = strtr(
|
|
476
|
+
$individual_property_definition['css_vars'][ $css_property ],
|
|
477
|
+
array( '$slug' => $slug )
|
|
478
|
+
);
|
|
479
|
+
$value = "var($css_var)";
|
|
480
|
+
}
|
|
481
|
+
$individual_css_property = sprintf( $style_definition['property_keys']['individual'], $individual_property_key );
|
|
482
|
+
$css_declarations[ $individual_css_property ] = $value;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
return $css_declarations;
|
|
288
486
|
}
|
|
289
487
|
}
|
|
290
488
|
|
|
@@ -294,17 +492,25 @@ class WP_Style_Engine {
|
|
|
294
492
|
* Returns an CSS ruleset.
|
|
295
493
|
* Styles are bundled based on the instructions in BLOCK_STYLE_DEFINITIONS_METADATA.
|
|
296
494
|
*
|
|
495
|
+
* Example usage:
|
|
496
|
+
*
|
|
497
|
+
* $styles = wp_style_engine_generate( array( 'color' => array( 'text' => '#cccccc' ) ) );
|
|
498
|
+
* // Returns `'color: #cccccc'`.
|
|
499
|
+
*
|
|
500
|
+
* @access public
|
|
501
|
+
*
|
|
297
502
|
* @param array $block_styles An array of styles from a block's attributes.
|
|
503
|
+
* @param array $options An array of options to determine the output.
|
|
298
504
|
*
|
|
299
505
|
* @return array|null array(
|
|
300
506
|
* 'styles' => (string) A CSS ruleset formatted to be placed in an HTML `style` attribute or tag.
|
|
301
507
|
* 'classnames' => (string) Classnames separated by a space.
|
|
302
508
|
* );
|
|
303
509
|
*/
|
|
304
|
-
function wp_style_engine_generate( $block_styles ) {
|
|
510
|
+
function wp_style_engine_generate( $block_styles, $options = array() ) {
|
|
305
511
|
if ( class_exists( 'WP_Style_Engine' ) ) {
|
|
306
512
|
$style_engine = WP_Style_Engine::get_instance();
|
|
307
|
-
return $style_engine->generate( $block_styles );
|
|
513
|
+
return $style_engine->generate( $block_styles, $options );
|
|
308
514
|
}
|
|
309
515
|
return null;
|
|
310
516
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/style-engine",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "WordPress Style engine.",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -34,5 +34,5 @@
|
|
|
34
34
|
"publishConfig": {
|
|
35
35
|
"access": "public"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "a80eeb62ec7cb1418b9915c277e084a29d6665e3"
|
|
38
38
|
}
|