@wordpress/style-engine 0.14.0 → 0.14.1-next.d6164808d3.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/README.md +64 -58
- package/build/index.js +7 -5
- package/build/index.js.map +1 -1
- package/build/styles/typography/index.js +7 -1
- package/build/styles/typography/index.js.map +1 -1
- package/build/styles/utils.js +5 -5
- package/build/styles/utils.js.map +1 -1
- package/build-module/index.js +6 -4
- package/build-module/index.js.map +1 -1
- package/build-module/styles/typography/index.js +7 -1
- package/build-module/styles/typography/index.js.map +1 -1
- package/build-module/styles/utils.js +5 -5
- package/build-module/styles/utils.js.map +1 -1
- package/build-types/index.d.ts +23 -0
- package/build-types/index.d.ts.map +1 -0
- 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/color/background.d.ts +14 -0
- package/build-types/styles/color/background.d.ts.map +1 -0
- package/build-types/styles/color/gradient.d.ts +14 -0
- package/build-types/styles/color/gradient.d.ts.map +1 -0
- package/build-types/styles/color/index.d.ts +10 -0
- package/build-types/styles/color/index.d.ts.map +1 -0
- package/build-types/styles/color/text.d.ts +14 -0
- package/build-types/styles/color/text.d.ts.map +1 -0
- package/build-types/styles/constants.d.ts +4 -0
- package/build-types/styles/constants.d.ts.map +1 -0
- package/build-types/styles/index.d.ts +5 -0
- package/build-types/styles/index.d.ts.map +1 -0
- package/build-types/styles/spacing/index.d.ts +6 -0
- package/build-types/styles/spacing/index.d.ts.map +1 -0
- package/build-types/styles/spacing/margin.d.ts +10 -0
- package/build-types/styles/spacing/margin.d.ts.map +1 -0
- package/build-types/styles/spacing/padding.d.ts +10 -0
- package/build-types/styles/spacing/padding.d.ts.map +1 -0
- package/build-types/styles/typography/index.d.ts +14 -0
- package/build-types/styles/typography/index.d.ts.map +1 -0
- package/build-types/styles/utils.d.ts +48 -0
- package/build-types/styles/utils.d.ts.map +1 -0
- package/build-types/types.d.ts +85 -0
- package/build-types/types.d.ts.map +1 -0
- package/class-wp-style-engine-css-declarations.php +18 -15
- package/class-wp-style-engine-css-rule.php +5 -9
- package/class-wp-style-engine-css-rules-store.php +5 -4
- package/class-wp-style-engine-processor.php +9 -1
- package/class-wp-style-engine.php +83 -92
- package/package.json +2 -2
- package/phpunit/class-wp-style-engine-css-declarations-test.php +2 -0
- package/phpunit/class-wp-style-engine-css-rules-store-test.php +14 -0
- package/phpunit/class-wp-style-engine-test.php +23 -19
- package/src/index.ts +4 -4
- package/src/styles/typography/index.ts +13 -0
- package/src/styles/utils.ts +5 -5
- package/src/test/index.js +24 -10
- package/src/types.ts +3 -4
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -18,7 +18,7 @@ if ( class_exists( 'WP_Style_Engine' ) ) {
|
|
|
18
18
|
* CSS styles generation.
|
|
19
19
|
*
|
|
20
20
|
* This class is for internal core usage and is not supposed to be used by extenders (plugins and/or themes).
|
|
21
|
-
* This is a low-level API that may need to do breaking changes. Please, use
|
|
21
|
+
* This is a low-level API that may need to do breaking changes. Please, use wp_style_engine_get_styles instead.
|
|
22
22
|
*
|
|
23
23
|
* @access private
|
|
24
24
|
*/
|
|
@@ -220,14 +220,14 @@ class WP_Style_Engine {
|
|
|
220
220
|
* @param string? $style_value A single css preset value.
|
|
221
221
|
* @param string $property_key The CSS property that is the second element of the preset string. Used for matching.
|
|
222
222
|
*
|
|
223
|
-
* @return string
|
|
223
|
+
* @return string The slug, or empty string if not found.
|
|
224
224
|
*/
|
|
225
225
|
protected static function get_slug_from_preset_value( $style_value, $property_key ) {
|
|
226
|
-
if ( is_string( $style_value ) &&
|
|
226
|
+
if ( is_string( $style_value ) && str_contains( $style_value, "var:preset|{$property_key}|" ) ) {
|
|
227
227
|
$index_to_splice = strrpos( $style_value, '|' ) + 1;
|
|
228
228
|
return _wp_to_kebab_case( substr( $style_value, $index_to_splice ) );
|
|
229
229
|
}
|
|
230
|
-
return
|
|
230
|
+
return '';
|
|
231
231
|
}
|
|
232
232
|
|
|
233
233
|
/**
|
|
@@ -236,12 +236,12 @@ class WP_Style_Engine {
|
|
|
236
236
|
* @param string $style_value A single css preset value.
|
|
237
237
|
* @param array $css_vars The css var patterns used to generate the var string.
|
|
238
238
|
*
|
|
239
|
-
* @return string
|
|
239
|
+
* @return string The css var, or an empty string if no match for slug found.
|
|
240
240
|
*/
|
|
241
241
|
protected static function get_css_var_value( $style_value, $css_vars ) {
|
|
242
242
|
foreach ( $css_vars as $property_key => $css_var_pattern ) {
|
|
243
243
|
$slug = static::get_slug_from_preset_value( $style_value, $property_key );
|
|
244
|
-
if ( $slug ) {
|
|
244
|
+
if ( static::is_valid_style_value( $slug ) ) {
|
|
245
245
|
$var = strtr(
|
|
246
246
|
$css_var_pattern,
|
|
247
247
|
array( '$slug' => $slug )
|
|
@@ -249,7 +249,7 @@ class WP_Style_Engine {
|
|
|
249
249
|
return "var($var)";
|
|
250
250
|
}
|
|
251
251
|
}
|
|
252
|
-
return
|
|
252
|
+
return '';
|
|
253
253
|
}
|
|
254
254
|
|
|
255
255
|
/**
|
|
@@ -260,42 +260,34 @@ class WP_Style_Engine {
|
|
|
260
260
|
* @return boolean
|
|
261
261
|
*/
|
|
262
262
|
protected static function is_valid_style_value( $style_value ) {
|
|
263
|
-
|
|
264
|
-
return true;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
if ( empty( $style_value ) ) {
|
|
268
|
-
return false;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
return true;
|
|
263
|
+
return '0' === $style_value || ! empty( $style_value );
|
|
272
264
|
}
|
|
273
265
|
|
|
274
266
|
/**
|
|
275
|
-
* Stores a CSS rule using the
|
|
267
|
+
* Stores a CSS rule using the provided CSS selector and CSS declarations.
|
|
276
268
|
*
|
|
277
|
-
* @param string $
|
|
269
|
+
* @param string $store_name A valid store key.
|
|
278
270
|
* @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.
|
|
279
271
|
* @param array $css_declarations An array of parsed CSS property => CSS value pairs.
|
|
280
272
|
*
|
|
281
273
|
* @return void.
|
|
282
274
|
*/
|
|
283
|
-
public static function store_css_rule( $
|
|
284
|
-
if ( empty( $
|
|
275
|
+
public static function store_css_rule( $store_name, $css_selector, $css_declarations ) {
|
|
276
|
+
if ( empty( $store_name ) || empty( $css_selector ) || empty( $css_declarations ) ) {
|
|
285
277
|
return;
|
|
286
278
|
}
|
|
287
|
-
static::get_store( $
|
|
279
|
+
static::get_store( $store_name )->add_rule( $css_selector )->add_declarations( $css_declarations );
|
|
288
280
|
}
|
|
289
281
|
|
|
290
282
|
/**
|
|
291
283
|
* Returns a store by store key.
|
|
292
284
|
*
|
|
293
|
-
* @param string $
|
|
285
|
+
* @param string $store_name A store key.
|
|
294
286
|
*
|
|
295
287
|
* @return WP_Style_Engine_CSS_Rules_Store
|
|
296
288
|
*/
|
|
297
|
-
public static function get_store( $
|
|
298
|
-
return WP_Style_Engine_CSS_Rules_Store::get_store( $
|
|
289
|
+
public static function get_store( $store_name ) {
|
|
290
|
+
return WP_Style_Engine_CSS_Rules_Store::get_store( $store_name );
|
|
299
291
|
}
|
|
300
292
|
|
|
301
293
|
/**
|
|
@@ -314,14 +306,14 @@ class WP_Style_Engine {
|
|
|
314
306
|
* );
|
|
315
307
|
*/
|
|
316
308
|
public static function parse_block_styles( $block_styles, $options ) {
|
|
309
|
+
$parsed_styles = array(
|
|
310
|
+
'classnames' => array(),
|
|
311
|
+
'declarations' => array(),
|
|
312
|
+
);
|
|
317
313
|
if ( empty( $block_styles ) || ! is_array( $block_styles ) ) {
|
|
318
|
-
return
|
|
314
|
+
return $parsed_styles;
|
|
319
315
|
}
|
|
320
316
|
|
|
321
|
-
$css_declarations = array();
|
|
322
|
-
$classnames = array();
|
|
323
|
-
$should_skip_css_vars = isset( $options['convert_vars_to_classnames'] ) && true === $options['convert_vars_to_classnames'];
|
|
324
|
-
|
|
325
317
|
// Collect CSS and classnames.
|
|
326
318
|
foreach ( static::BLOCK_STYLE_DEFINITIONS_METADATA as $definition_group_key => $definition_group_style ) {
|
|
327
319
|
if ( empty( $block_styles[ $definition_group_key ] ) ) {
|
|
@@ -334,15 +326,12 @@ class WP_Style_Engine {
|
|
|
334
326
|
continue;
|
|
335
327
|
}
|
|
336
328
|
|
|
337
|
-
$classnames
|
|
338
|
-
$
|
|
329
|
+
$parsed_styles['classnames'] = array_merge( $parsed_styles['classnames'], static::get_classnames( $style_value, $style_definition ) );
|
|
330
|
+
$parsed_styles['declarations'] = array_merge( $parsed_styles['declarations'], static::get_css_declarations( $style_value, $style_definition, $options ) );
|
|
339
331
|
}
|
|
340
332
|
}
|
|
341
333
|
|
|
342
|
-
return
|
|
343
|
-
'classnames' => $classnames,
|
|
344
|
-
'declarations' => $css_declarations,
|
|
345
|
-
);
|
|
334
|
+
return $parsed_styles;
|
|
346
335
|
}
|
|
347
336
|
|
|
348
337
|
/**
|
|
@@ -354,12 +343,11 @@ class WP_Style_Engine {
|
|
|
354
343
|
* @return array An array of CSS classnames.
|
|
355
344
|
*/
|
|
356
345
|
protected static function get_classnames( $style_value, $style_definition ) {
|
|
357
|
-
$classnames = array();
|
|
358
|
-
|
|
359
346
|
if ( empty( $style_value ) ) {
|
|
360
|
-
return
|
|
347
|
+
return array();
|
|
361
348
|
}
|
|
362
349
|
|
|
350
|
+
$classnames = array();
|
|
363
351
|
if ( ! empty( $style_definition['classnames'] ) ) {
|
|
364
352
|
foreach ( $style_definition['classnames'] as $classname => $property_key ) {
|
|
365
353
|
if ( true === $property_key ) {
|
|
@@ -386,24 +374,27 @@ class WP_Style_Engine {
|
|
|
386
374
|
*
|
|
387
375
|
* @param array $style_value A single raw style value from the generate() $block_styles array.
|
|
388
376
|
* @param array<string> $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
|
|
389
|
-
* @param
|
|
377
|
+
* @param array $options array(
|
|
378
|
+
* 'convert_vars_to_classnames' => (boolean) Whether to skip converting CSS var:? values to var( --wp--preset--* ) values. Default is `false`.
|
|
379
|
+
* );.
|
|
390
380
|
*
|
|
391
381
|
* @return array An array of CSS definitions, e.g., array( "$property" => "$value" ).
|
|
392
382
|
*/
|
|
393
|
-
protected static function get_css_declarations( $style_value, $style_definition, $
|
|
383
|
+
protected static function get_css_declarations( $style_value, $style_definition, $options ) {
|
|
394
384
|
if ( isset( $style_definition['value_func'] ) && is_callable( $style_definition['value_func'] ) ) {
|
|
395
|
-
return call_user_func( $style_definition['value_func'], $style_value, $style_definition, $
|
|
385
|
+
return call_user_func( $style_definition['value_func'], $style_value, $style_definition, $options );
|
|
396
386
|
}
|
|
397
387
|
|
|
398
|
-
$css_declarations
|
|
399
|
-
$style_property_keys
|
|
388
|
+
$css_declarations = array();
|
|
389
|
+
$style_property_keys = $style_definition['property_keys'];
|
|
390
|
+
$should_skip_css_vars = isset( $options['convert_vars_to_classnames'] ) && true === $options['convert_vars_to_classnames'];
|
|
400
391
|
|
|
401
392
|
// Build CSS var values from var:? values, e.g, `var(--wp--css--rule-slug )`
|
|
402
393
|
// Check if the value is a CSS preset and there's a corresponding css_var pattern in the style definition.
|
|
403
|
-
if ( is_string( $style_value ) &&
|
|
394
|
+
if ( is_string( $style_value ) && str_contains( $style_value, 'var:' ) ) {
|
|
404
395
|
if ( ! $should_skip_css_vars && ! empty( $style_definition['css_vars'] ) ) {
|
|
405
396
|
$css_var = static::get_css_var_value( $style_value, $style_definition['css_vars'] );
|
|
406
|
-
if ( $css_var ) {
|
|
397
|
+
if ( static::is_valid_style_value( $css_var ) ) {
|
|
407
398
|
$css_declarations[ $style_property_keys['default'] ] = $css_var;
|
|
408
399
|
}
|
|
409
400
|
}
|
|
@@ -414,8 +405,13 @@ class WP_Style_Engine {
|
|
|
414
405
|
// If the input contains an array, assume box model-like properties
|
|
415
406
|
// for styles such as margins and padding.
|
|
416
407
|
if ( is_array( $style_value ) ) {
|
|
408
|
+
// Bail out early if the `'individual'` property is not defined.
|
|
409
|
+
if ( ! isset( $style_property_keys['individual'] ) ) {
|
|
410
|
+
return $css_declarations;
|
|
411
|
+
}
|
|
412
|
+
|
|
417
413
|
foreach ( $style_value as $key => $value ) {
|
|
418
|
-
if ( is_string( $value ) &&
|
|
414
|
+
if ( is_string( $value ) && str_contains( $value, 'var:' ) && ! $should_skip_css_vars && ! empty( $style_definition['css_vars'] ) ) {
|
|
419
415
|
$value = static::get_css_var_value( $value, $style_definition['css_vars'] );
|
|
420
416
|
}
|
|
421
417
|
|
|
@@ -425,10 +421,11 @@ class WP_Style_Engine {
|
|
|
425
421
|
$css_declarations[ $individual_property ] = $value;
|
|
426
422
|
}
|
|
427
423
|
}
|
|
428
|
-
|
|
429
|
-
$css_declarations
|
|
424
|
+
|
|
425
|
+
return $css_declarations;
|
|
430
426
|
}
|
|
431
427
|
|
|
428
|
+
$css_declarations[ $style_property_keys['default'] ] = $style_value;
|
|
432
429
|
return $css_declarations;
|
|
433
430
|
}
|
|
434
431
|
|
|
@@ -439,17 +436,17 @@ class WP_Style_Engine {
|
|
|
439
436
|
* "border-{top|right|bottom|left}-{color|width|style}: {value};" or,
|
|
440
437
|
* "border-image-{outset|source|width|repeat|slice}: {value};"
|
|
441
438
|
*
|
|
442
|
-
* @param array
|
|
443
|
-
* @param array
|
|
444
|
-
* @param
|
|
439
|
+
* @param array $style_value A single raw Gutenberg style attributes value for a CSS property.
|
|
440
|
+
* @param array $individual_property_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
|
|
441
|
+
* @param array $options array(
|
|
442
|
+
* 'convert_vars_to_classnames' => (boolean) Whether to skip converting CSS var:? values to var( --wp--preset--* ) values. Default is `false`.
|
|
443
|
+
* );.
|
|
445
444
|
*
|
|
446
445
|
* @return array An array of CSS definitions, e.g., array( "$property" => "$value" ).
|
|
447
446
|
*/
|
|
448
|
-
protected static function get_individual_property_css_declarations( $style_value, $individual_property_definition, $
|
|
449
|
-
$css_declarations = array();
|
|
450
|
-
|
|
447
|
+
protected static function get_individual_property_css_declarations( $style_value, $individual_property_definition, $options ) {
|
|
451
448
|
if ( ! is_array( $style_value ) || empty( $style_value ) || empty( $individual_property_definition['path'] ) ) {
|
|
452
|
-
return
|
|
449
|
+
return array();
|
|
453
450
|
}
|
|
454
451
|
|
|
455
452
|
// The first item in $individual_property_definition['path'] array tells us the style property, e.g., "border".
|
|
@@ -457,6 +454,8 @@ class WP_Style_Engine {
|
|
|
457
454
|
// The second item in $individual_property_definition['path'] array refers to the individual property marker, e.g., "top".
|
|
458
455
|
$definition_group_key = $individual_property_definition['path'][0];
|
|
459
456
|
$individual_property_key = $individual_property_definition['path'][1];
|
|
457
|
+
$should_skip_css_vars = isset( $options['convert_vars_to_classnames'] ) && true === $options['convert_vars_to_classnames'];
|
|
458
|
+
$css_declarations = array();
|
|
460
459
|
|
|
461
460
|
foreach ( $style_value as $css_property => $value ) {
|
|
462
461
|
if ( empty( $value ) ) {
|
|
@@ -469,7 +468,7 @@ class WP_Style_Engine {
|
|
|
469
468
|
|
|
470
469
|
if ( $style_definition && isset( $style_definition['property_keys']['individual'] ) ) {
|
|
471
470
|
// Set a CSS var if there is a valid preset value.
|
|
472
|
-
if ( is_string( $value ) &&
|
|
471
|
+
if ( is_string( $value ) && str_contains( $value, 'var:' ) && ! $should_skip_css_vars && ! empty( $individual_property_definition['css_vars'] ) ) {
|
|
473
472
|
$value = static::get_css_var_value( $value, $individual_property_definition['css_vars'] );
|
|
474
473
|
}
|
|
475
474
|
$individual_css_property = sprintf( $style_definition['property_keys']['individual'], $individual_property_key );
|
|
@@ -497,8 +496,8 @@ class WP_Style_Engine {
|
|
|
497
496
|
$css_rule = new WP_Style_Engine_CSS_Rule( $css_selector, $css_declarations );
|
|
498
497
|
return $css_rule->get_css();
|
|
499
498
|
}
|
|
500
|
-
$css_declarations = new WP_Style_Engine_CSS_Declarations( $css_declarations );
|
|
501
499
|
|
|
500
|
+
$css_declarations = new WP_Style_Engine_CSS_Declarations( $css_declarations );
|
|
502
501
|
return $css_declarations->get_declarations_string();
|
|
503
502
|
}
|
|
504
503
|
|
|
@@ -512,7 +511,7 @@ class WP_Style_Engine {
|
|
|
512
511
|
public static function compile_stylesheet_from_css_rules( $css_rules ) {
|
|
513
512
|
$processor = new WP_Style_Engine_Processor();
|
|
514
513
|
$processor->add_rules( $css_rules );
|
|
515
|
-
return $processor->get_css(
|
|
514
|
+
return $processor->get_css();
|
|
516
515
|
}
|
|
517
516
|
}
|
|
518
517
|
|
|
@@ -531,13 +530,13 @@ class WP_Style_Engine {
|
|
|
531
530
|
*
|
|
532
531
|
* @param array $block_styles The style object.
|
|
533
532
|
* @param array<string|boolean> $options array(
|
|
534
|
-
* 'context'
|
|
535
|
-
*
|
|
533
|
+
* 'context' => (string|null) An identifier describing the origin of the style object, e.g., 'block-supports' or 'global-styles'. Default is 'block-supports'.
|
|
534
|
+
* When set, the style engine will attempt to store the CSS rules, where a selector is also passed.
|
|
536
535
|
* 'convert_vars_to_classnames' => (boolean) Whether to skip converting CSS var:? values to var( --wp--preset--* ) values. Default is `false`.
|
|
537
536
|
* 'selector' => (string) When a selector is passed, `generate()` will return a full CSS rule `$selector { ...rules }`, otherwise a concatenated string of properties and values.
|
|
538
537
|
* );.
|
|
539
538
|
*
|
|
540
|
-
* @return array<string|array
|
|
539
|
+
* @return array<string|array> array(
|
|
541
540
|
* 'css' => (string) A CSS ruleset or declarations block formatted to be placed in an HTML `style` attribute or tag.
|
|
542
541
|
* 'declarations' => (array) An array of property/value pairs representing parsed CSS declarations.
|
|
543
542
|
* 'classnames' => (string) Classnames separated by a space.
|
|
@@ -547,32 +546,25 @@ function wp_style_engine_get_styles( $block_styles, $options = array() ) {
|
|
|
547
546
|
if ( ! class_exists( 'WP_Style_Engine' ) ) {
|
|
548
547
|
return array();
|
|
549
548
|
}
|
|
550
|
-
$defaults = array(
|
|
551
|
-
'selector' => null,
|
|
552
|
-
'context' => 'block-supports',
|
|
553
|
-
'convert_vars_to_classnames' => false,
|
|
554
|
-
'enqueue' => false,
|
|
555
|
-
);
|
|
556
549
|
|
|
557
|
-
$options
|
|
558
|
-
|
|
550
|
+
$options = wp_parse_args(
|
|
551
|
+
$options,
|
|
552
|
+
array(
|
|
553
|
+
'selector' => null,
|
|
554
|
+
'context' => null,
|
|
555
|
+
'convert_vars_to_classnames' => false,
|
|
556
|
+
)
|
|
557
|
+
);
|
|
559
558
|
|
|
560
|
-
|
|
561
|
-
if ( 'block-supports' === $options['context'] ) {
|
|
562
|
-
$parsed_styles = WP_Style_Engine::parse_block_styles( $block_styles, $options );
|
|
563
|
-
}
|
|
559
|
+
$parsed_styles = WP_Style_Engine::parse_block_styles( $block_styles, $options );
|
|
564
560
|
|
|
565
561
|
// Output.
|
|
566
562
|
$styles_output = array();
|
|
567
563
|
|
|
568
|
-
if ( ! $parsed_styles ) {
|
|
569
|
-
return $styles_output;
|
|
570
|
-
}
|
|
571
|
-
|
|
572
564
|
if ( ! empty( $parsed_styles['declarations'] ) ) {
|
|
573
565
|
$styles_output['css'] = WP_Style_Engine::compile_css( $parsed_styles['declarations'], $options['selector'] );
|
|
574
566
|
$styles_output['declarations'] = $parsed_styles['declarations'];
|
|
575
|
-
if (
|
|
567
|
+
if ( ! empty( $options['context'] ) ) {
|
|
576
568
|
WP_Style_Engine::store_css_rule( $options['context'], $options['selector'], $parsed_styles['declarations'] );
|
|
577
569
|
}
|
|
578
570
|
}
|
|
@@ -597,8 +589,8 @@ function wp_style_engine_get_styles( $block_styles, $options = array() ) {
|
|
|
597
589
|
* )
|
|
598
590
|
* );.
|
|
599
591
|
* @param array<string> $options array(
|
|
600
|
-
* 'context' => (string) An identifier describing the origin of the style object, e.g., 'block-supports' or 'global-styles'. Default is 'block-supports'.
|
|
601
|
-
*
|
|
592
|
+
* 'context' => (string|null) An identifier describing the origin of the style object, e.g., 'block-supports' or 'global-styles'. Default is 'block-supports'.
|
|
593
|
+
* When set, the style engine will attempt to store the CSS rules.
|
|
602
594
|
* );.
|
|
603
595
|
*
|
|
604
596
|
* @return string A compiled CSS string.
|
|
@@ -608,19 +600,20 @@ function wp_style_engine_get_stylesheet_from_css_rules( $css_rules, $options = a
|
|
|
608
600
|
return '';
|
|
609
601
|
}
|
|
610
602
|
|
|
611
|
-
$
|
|
612
|
-
|
|
613
|
-
|
|
603
|
+
$options = wp_parse_args(
|
|
604
|
+
$options,
|
|
605
|
+
array(
|
|
606
|
+
'context' => null,
|
|
607
|
+
)
|
|
614
608
|
);
|
|
615
|
-
$options = wp_parse_args( $options, $defaults );
|
|
616
|
-
$css_rule_objects = array();
|
|
617
609
|
|
|
610
|
+
$css_rule_objects = array();
|
|
618
611
|
foreach ( $css_rules as $css_rule ) {
|
|
619
612
|
if ( empty( $css_rule['selector'] ) || empty( $css_rule['declarations'] ) || ! is_array( $css_rule['declarations'] ) ) {
|
|
620
613
|
continue;
|
|
621
614
|
}
|
|
622
615
|
|
|
623
|
-
if (
|
|
616
|
+
if ( ! empty( $options['context'] ) ) {
|
|
624
617
|
WP_Style_Engine::store_css_rule( $options['context'], $css_rule['selector'], $css_rule['declarations'] );
|
|
625
618
|
}
|
|
626
619
|
|
|
@@ -639,16 +632,14 @@ function wp_style_engine_get_stylesheet_from_css_rules( $css_rules, $options = a
|
|
|
639
632
|
*
|
|
640
633
|
* @access public
|
|
641
634
|
*
|
|
642
|
-
* @param string $
|
|
635
|
+
* @param string $store_name A valid store name.
|
|
643
636
|
*
|
|
644
637
|
* @return string A compiled CSS string.
|
|
645
638
|
*/
|
|
646
|
-
function wp_style_engine_get_stylesheet_from_store( $
|
|
647
|
-
if ( ! class_exists( 'WP_Style_Engine' ) || empty( $
|
|
639
|
+
function wp_style_engine_get_stylesheet_from_store( $store_name ) {
|
|
640
|
+
if ( ! class_exists( 'WP_Style_Engine' ) || empty( $store_name ) ) {
|
|
648
641
|
return '';
|
|
649
642
|
}
|
|
650
643
|
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
return WP_Style_Engine::compile_stylesheet_from_css_rules( $store->get_all_rules() );
|
|
644
|
+
return WP_Style_Engine::compile_stylesheet_from_css_rules( WP_Style_Engine::get_store( $store_name )->get_all_rules() );
|
|
654
645
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/style-engine",
|
|
3
|
-
"version": "0.14.0",
|
|
3
|
+
"version": "0.14.1-next.d6164808d3.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": "ba8a396d2f418e53a6c4c50575582f3f3eb11ff7"
|
|
38
38
|
}
|
|
@@ -150,7 +150,9 @@ class WP_Style_Engine_CSS_Declarations_Test extends WP_UnitTestCase {
|
|
|
150
150
|
public function test_remove_unsafe_properties_and_values() {
|
|
151
151
|
$input_declarations = array(
|
|
152
152
|
'color' => 'url("https://wordpress.org")',
|
|
153
|
+
'font-size' => '<red/>',
|
|
153
154
|
'margin-right' => '10em',
|
|
155
|
+
'padding' => '</style>',
|
|
154
156
|
'potato' => 'uppercase',
|
|
155
157
|
);
|
|
156
158
|
$css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
|
|
@@ -29,6 +29,20 @@ class WP_Style_Engine_CSS_Rules_Store_Test extends WP_UnitTestCase {
|
|
|
29
29
|
$this->assertInstanceOf( 'WP_Style_Engine_CSS_Rules_Store', $new_pancakes_store );
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Should not create a new store with invalid $store_name.
|
|
34
|
+
*/
|
|
35
|
+
public function test_store_name_required() {
|
|
36
|
+
$not_a_store = WP_Style_Engine_CSS_Rules_Store::get_store( '' );
|
|
37
|
+
$this->assertEmpty( $not_a_store );
|
|
38
|
+
|
|
39
|
+
$also_not_a_store = WP_Style_Engine_CSS_Rules_Store::get_store( 123 );
|
|
40
|
+
$this->assertEmpty( $also_not_a_store );
|
|
41
|
+
|
|
42
|
+
$definitely_not_a_store = WP_Style_Engine_CSS_Rules_Store::get_store( null );
|
|
43
|
+
$this->assertEmpty( $definitely_not_a_store );
|
|
44
|
+
}
|
|
45
|
+
|
|
32
46
|
/**
|
|
33
47
|
* Should return previously created store when the same selector key is passed.
|
|
34
48
|
*/
|
|
@@ -133,22 +133,6 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
|
|
|
133
133
|
),
|
|
134
134
|
),
|
|
135
135
|
|
|
136
|
-
'invalid_context' => array(
|
|
137
|
-
'block_styles' => array(
|
|
138
|
-
'color' => array(
|
|
139
|
-
'text' => 'var:preset|color|sugar',
|
|
140
|
-
),
|
|
141
|
-
'spacing' => array(
|
|
142
|
-
'padding' => '20000px',
|
|
143
|
-
),
|
|
144
|
-
),
|
|
145
|
-
'options' => array(
|
|
146
|
-
'convert_vars_to_classnames' => true,
|
|
147
|
-
'context' => 'i-love-doughnuts',
|
|
148
|
-
),
|
|
149
|
-
'expected_output' => array(),
|
|
150
|
-
),
|
|
151
|
-
|
|
152
136
|
'inline_valid_box_model_style' => array(
|
|
153
137
|
'block_styles' => array(
|
|
154
138
|
'spacing' => array(
|
|
@@ -511,7 +495,7 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
|
|
|
511
495
|
/**
|
|
512
496
|
* Tests adding rules to a store and retrieving a generated stylesheet.
|
|
513
497
|
*/
|
|
514
|
-
public function
|
|
498
|
+
public function test_store_block_styles_using_context() {
|
|
515
499
|
$block_styles = array(
|
|
516
500
|
'spacing' => array(
|
|
517
501
|
'padding' => array(
|
|
@@ -526,7 +510,6 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
|
|
|
526
510
|
$generated_styles = wp_style_engine_get_styles(
|
|
527
511
|
$block_styles,
|
|
528
512
|
array(
|
|
529
|
-
'enqueue' => true,
|
|
530
513
|
'context' => 'block-supports',
|
|
531
514
|
'selector' => 'article',
|
|
532
515
|
)
|
|
@@ -536,6 +519,28 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
|
|
|
536
519
|
$this->assertSame( $generated_styles['css'], $rule->get_css() );
|
|
537
520
|
}
|
|
538
521
|
|
|
522
|
+
/**
|
|
523
|
+
* Tests adding rules to a store and retrieving a generated stylesheet.
|
|
524
|
+
*/
|
|
525
|
+
public function test_does_not_store_block_styles_without_context() {
|
|
526
|
+
$block_styles = array(
|
|
527
|
+
'typography' => array(
|
|
528
|
+
'fontSize' => '999px',
|
|
529
|
+
),
|
|
530
|
+
);
|
|
531
|
+
|
|
532
|
+
wp_style_engine_get_styles(
|
|
533
|
+
$block_styles,
|
|
534
|
+
array(
|
|
535
|
+
'selector' => '#font-size-rulez',
|
|
536
|
+
)
|
|
537
|
+
);
|
|
538
|
+
|
|
539
|
+
$all_stores = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_stores();
|
|
540
|
+
|
|
541
|
+
$this->assertEmpty( $all_stores );
|
|
542
|
+
}
|
|
543
|
+
|
|
539
544
|
/**
|
|
540
545
|
* Tests adding rules to a store and retrieving a generated stylesheet.
|
|
541
546
|
*/
|
|
@@ -564,7 +569,6 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
|
|
|
564
569
|
$css_rules,
|
|
565
570
|
array(
|
|
566
571
|
'context' => 'test-store',
|
|
567
|
-
'enqueue' => true,
|
|
568
572
|
)
|
|
569
573
|
);
|
|
570
574
|
|
package/src/index.ts
CHANGED
|
@@ -17,12 +17,12 @@ import { styleDefinitions } from './styles';
|
|
|
17
17
|
/**
|
|
18
18
|
* Generates a stylesheet for a given style object and selector.
|
|
19
19
|
*
|
|
20
|
-
* @param style Style object.
|
|
20
|
+
* @param style Style object, for example, the value of a block's attributes.style object or the top level styles in theme.json
|
|
21
21
|
* @param options Options object with settings to adjust how the styles are generated.
|
|
22
22
|
*
|
|
23
23
|
* @return generated stylesheet.
|
|
24
24
|
*/
|
|
25
|
-
export function
|
|
25
|
+
export function compileCSS( style: Style, options: StyleOptions = {} ): string {
|
|
26
26
|
const rules = getCSSRules( style, options );
|
|
27
27
|
|
|
28
28
|
// If no selector is provided, treat generated rules as inline styles to be returned as a single string.
|
|
@@ -56,14 +56,14 @@ export function generate( style: Style, options: StyleOptions ): string {
|
|
|
56
56
|
/**
|
|
57
57
|
* Returns a JSON representation of the generated CSS rules.
|
|
58
58
|
*
|
|
59
|
-
* @param style Style object.
|
|
59
|
+
* @param style Style object, for example, the value of a block's attributes.style object or the top level styles in theme.json
|
|
60
60
|
* @param options Options object with settings to adjust how the styles are generated.
|
|
61
61
|
*
|
|
62
62
|
* @return generated styles.
|
|
63
63
|
*/
|
|
64
64
|
export function getCSSRules(
|
|
65
65
|
style: Style,
|
|
66
|
-
options: StyleOptions
|
|
66
|
+
options: StyleOptions = {}
|
|
67
67
|
): GeneratedCSSRule[] {
|
|
68
68
|
const rules: GeneratedCSSRule[] = [];
|
|
69
69
|
styleDefinitions.forEach( ( definition: StyleDefinition ) => {
|
|
@@ -40,6 +40,18 @@ const fontWeight = {
|
|
|
40
40
|
},
|
|
41
41
|
};
|
|
42
42
|
|
|
43
|
+
const fontFamily = {
|
|
44
|
+
name: 'fontFamily',
|
|
45
|
+
generate: ( style: Style, options: StyleOptions ) => {
|
|
46
|
+
return generateRule(
|
|
47
|
+
style,
|
|
48
|
+
options,
|
|
49
|
+
[ 'typography', 'fontFamily' ],
|
|
50
|
+
'fontFamily'
|
|
51
|
+
);
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
|
|
43
55
|
const letterSpacing = {
|
|
44
56
|
name: 'letterSpacing',
|
|
45
57
|
generate: ( style: Style, options: StyleOptions ) => {
|
|
@@ -89,6 +101,7 @@ const textTransform = {
|
|
|
89
101
|
};
|
|
90
102
|
|
|
91
103
|
export default [
|
|
104
|
+
fontFamily,
|
|
92
105
|
fontSize,
|
|
93
106
|
fontStyle,
|
|
94
107
|
fontWeight,
|
package/src/styles/utils.ts
CHANGED
|
@@ -51,11 +51,11 @@ export function generateRule(
|
|
|
51
51
|
/**
|
|
52
52
|
* Returns a JSON representation of the generated CSS rules taking into account box model properties, top, right, bottom, left.
|
|
53
53
|
*
|
|
54
|
-
* @param
|
|
55
|
-
* @param
|
|
56
|
-
* @param
|
|
57
|
-
* @param
|
|
58
|
-
* @param
|
|
54
|
+
* @param style Style object.
|
|
55
|
+
* @param options Options object with settings to adjust how the styles are generated.
|
|
56
|
+
* @param path An array of strings representing the path to the style value in the style object.
|
|
57
|
+
* @param ruleKeys An array of CSS property keys and patterns.
|
|
58
|
+
* @param individualProperties The "sides" or individual properties for which to generate rules.
|
|
59
59
|
*
|
|
60
60
|
* @return GeneratedCSSRule[] CSS rules.
|
|
61
61
|
*/
|