@wordpress/style-engine 0.12.0 → 0.13.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 +2 -0
- package/class-wp-style-engine-css-declarations.php +24 -0
- package/class-wp-style-engine-css-rule.php +115 -0
- package/class-wp-style-engine-css-rules-store.php +94 -0
- package/class-wp-style-engine-processor.php +93 -0
- package/class-wp-style-engine.php +12 -11
- package/package.json +2 -2
- package/phpunit/class-wp-style-engine-css-declarations-test.php +46 -0
- package/phpunit/class-wp-style-engine-css-rule-test.php +96 -0
- package/phpunit/class-wp-style-engine-css-rules-store-test.php +115 -0
- package/phpunit/class-wp-style-engine-processor-test.php +127 -0
- package/src/test/index.js +41 -2
package/CHANGELOG.md
CHANGED
|
@@ -67,6 +67,17 @@ class WP_Style_Engine_CSS_Declarations {
|
|
|
67
67
|
$this->declarations[ $property ] = $value;
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
+
/**
|
|
71
|
+
* Remove a single declaration.
|
|
72
|
+
*
|
|
73
|
+
* @param string $property The CSS property.
|
|
74
|
+
*
|
|
75
|
+
* @return void
|
|
76
|
+
*/
|
|
77
|
+
public function remove_declaration( $property ) {
|
|
78
|
+
unset( $this->declarations[ $property ] );
|
|
79
|
+
}
|
|
80
|
+
|
|
70
81
|
/**
|
|
71
82
|
* Add multiple declarations.
|
|
72
83
|
*
|
|
@@ -80,6 +91,19 @@ class WP_Style_Engine_CSS_Declarations {
|
|
|
80
91
|
}
|
|
81
92
|
}
|
|
82
93
|
|
|
94
|
+
/**
|
|
95
|
+
* Remove multiple declarations.
|
|
96
|
+
*
|
|
97
|
+
* @param array $declarations An array of properties.
|
|
98
|
+
*
|
|
99
|
+
* @return void
|
|
100
|
+
*/
|
|
101
|
+
public function remove_declarations( $declarations = array() ) {
|
|
102
|
+
foreach ( $declarations as $property ) {
|
|
103
|
+
$this->remove_declaration( $property );
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
83
107
|
/**
|
|
84
108
|
* Get the declarations array.
|
|
85
109
|
*
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
/**
|
|
3
|
+
* WP_Style_Engine_CSS_Rule
|
|
4
|
+
*
|
|
5
|
+
* An object for CSS rules.
|
|
6
|
+
*
|
|
7
|
+
* @package Gutenberg
|
|
8
|
+
*/
|
|
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 {
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The selector.
|
|
23
|
+
*
|
|
24
|
+
* @var string
|
|
25
|
+
*/
|
|
26
|
+
protected $selector;
|
|
27
|
+
|
|
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;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Constructor
|
|
39
|
+
*
|
|
40
|
+
* @param string $selector The CSS selector.
|
|
41
|
+
* @param array|WP_Style_Engine_CSS_Declarations $declarations An array of declarations (property => value pairs),
|
|
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
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Set 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
|
+
if ( empty( $selector ) ) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
$this->selector = $selector;
|
|
61
|
+
|
|
62
|
+
return $this;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Set the declarations.
|
|
67
|
+
*
|
|
68
|
+
* @param array|WP_Style_Engine_CSS_Declarations $declarations An array of declarations (property => value pairs),
|
|
69
|
+
* or a WP_Style_Engine_CSS_Declarations object.
|
|
70
|
+
*
|
|
71
|
+
* @return WP_Style_Engine_CSS_Rule Returns the object to allow chaining of methods.
|
|
72
|
+
*/
|
|
73
|
+
public function add_declarations( $declarations ) {
|
|
74
|
+
$is_declarations_object = ! is_array( $declarations );
|
|
75
|
+
$declarations_array = $is_declarations_object ? $declarations->get_declarations() : $declarations;
|
|
76
|
+
|
|
77
|
+
if ( null === $this->declarations && $is_declarations_object ) {
|
|
78
|
+
$this->declarations = $declarations;
|
|
79
|
+
return $this;
|
|
80
|
+
}
|
|
81
|
+
if ( null === $this->declarations ) {
|
|
82
|
+
$this->declarations = new WP_Style_Engine_CSS_Declarations( $declarations_array );
|
|
83
|
+
}
|
|
84
|
+
$this->declarations->add_declarations( $declarations_array );
|
|
85
|
+
|
|
86
|
+
return $this;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Get the declarations object.
|
|
91
|
+
*
|
|
92
|
+
* @return WP_Style_Engine_CSS_Declarations
|
|
93
|
+
*/
|
|
94
|
+
public function get_declarations() {
|
|
95
|
+
return $this->declarations;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Get the full selector.
|
|
100
|
+
*
|
|
101
|
+
* @return string
|
|
102
|
+
*/
|
|
103
|
+
public function get_selector() {
|
|
104
|
+
return $this->selector;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Get the CSS.
|
|
109
|
+
*
|
|
110
|
+
* @return string
|
|
111
|
+
*/
|
|
112
|
+
public function get_css() {
|
|
113
|
+
return $this->get_selector() . ' {' . $this->declarations->get_declarations_string() . '}';
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
/**
|
|
3
|
+
* WP_Style_Engine_CSS_Rules_Store
|
|
4
|
+
*
|
|
5
|
+
* A store for WP_Style_Engine_CSS_Rule objects.
|
|
6
|
+
*
|
|
7
|
+
* @package Gutenberg
|
|
8
|
+
*/
|
|
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
|
+
* An array of CSS Rules objects assigned to the store.
|
|
32
|
+
*
|
|
33
|
+
* @var WP_Style_Engine_CSS_Rule[]
|
|
34
|
+
*/
|
|
35
|
+
protected $rules = array();
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Get an instance of the store.
|
|
39
|
+
*
|
|
40
|
+
* @param string $store_name The name of the store.
|
|
41
|
+
*
|
|
42
|
+
* @return WP_Style_Engine_CSS_Rules_Store
|
|
43
|
+
*/
|
|
44
|
+
public static function get_store( $store_name = 'default' ) {
|
|
45
|
+
if ( ! isset( static::$stores[ $store_name ] ) ) {
|
|
46
|
+
static::$stores[ $store_name ] = new static();
|
|
47
|
+
}
|
|
48
|
+
return static::$stores[ $store_name ];
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Get an array of all rules.
|
|
52
|
+
*
|
|
53
|
+
* @return WP_Style_Engine_CSS_Rule[]
|
|
54
|
+
*/
|
|
55
|
+
public function get_all_rules() {
|
|
56
|
+
return $this->rules;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Get a WP_Style_Engine_CSS_Rule object by its selector.
|
|
61
|
+
* If the rule does not exist, it will be created.
|
|
62
|
+
*
|
|
63
|
+
* @param string $selector The CSS selector.
|
|
64
|
+
*
|
|
65
|
+
* @return WP_Style_Engine_CSS_Rule|null Returns a WP_Style_Engine_CSS_Rule object, or null if the selector is empty.
|
|
66
|
+
*/
|
|
67
|
+
public function add_rule( $selector ) {
|
|
68
|
+
|
|
69
|
+
$selector = trim( $selector );
|
|
70
|
+
|
|
71
|
+
// Bail early if there is no selector.
|
|
72
|
+
if ( empty( $selector ) ) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Create the rule if it doesn't exist.
|
|
77
|
+
if ( empty( $this->rules[ $selector ] ) ) {
|
|
78
|
+
$this->rules[ $selector ] = new WP_Style_Engine_CSS_Rule( $selector );
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return $this->rules[ $selector ];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Remove a selector from the store.
|
|
86
|
+
*
|
|
87
|
+
* @param string $selector The CSS selector.
|
|
88
|
+
*
|
|
89
|
+
* @return void
|
|
90
|
+
*/
|
|
91
|
+
public function remove_rule( $selector ) {
|
|
92
|
+
unset( $this->rules[ $selector ] );
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
/**
|
|
3
|
+
* WP_Style_Engine_Processor
|
|
4
|
+
*
|
|
5
|
+
* Compiles styles from a store of CSS rules.
|
|
6
|
+
*
|
|
7
|
+
* @package Gutenberg
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
if ( class_exists( 'WP_Style_Engine_Processor' ) ) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Compiles styles from a store of CSS rules.
|
|
16
|
+
*
|
|
17
|
+
* @access private
|
|
18
|
+
*/
|
|
19
|
+
class WP_Style_Engine_Processor {
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The Style-Engine Store object.
|
|
23
|
+
*
|
|
24
|
+
* @var WP_Style_Engine_CSS_Rules_Store
|
|
25
|
+
*/
|
|
26
|
+
protected $store;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Constructor.
|
|
30
|
+
*
|
|
31
|
+
* @param WP_Style_Engine_CSS_Rules_Store $store The store to render.
|
|
32
|
+
*/
|
|
33
|
+
public function __construct( WP_Style_Engine_CSS_Rules_Store $store ) {
|
|
34
|
+
$this->store = $store;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Get the CSS rules as a string.
|
|
39
|
+
*
|
|
40
|
+
* @return string The computed CSS.
|
|
41
|
+
*/
|
|
42
|
+
public function get_css() {
|
|
43
|
+
// Combine CSS selectors that have identical declarations.
|
|
44
|
+
$this->combine_rules_selectors();
|
|
45
|
+
|
|
46
|
+
// Build the CSS.
|
|
47
|
+
$css = '';
|
|
48
|
+
$rules = $this->store->get_all_rules();
|
|
49
|
+
foreach ( $rules as $rule ) {
|
|
50
|
+
$css .= $rule->get_css();
|
|
51
|
+
}
|
|
52
|
+
return $css;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Combines selectors from the rules store when they have the same styles.
|
|
57
|
+
*
|
|
58
|
+
* @return void
|
|
59
|
+
*/
|
|
60
|
+
private function combine_rules_selectors() {
|
|
61
|
+
$rules = $this->store->get_all_rules();
|
|
62
|
+
|
|
63
|
+
// Build an array of selectors along with the JSON-ified styles to make comparisons easier.
|
|
64
|
+
$selectors_json = array();
|
|
65
|
+
foreach ( $rules as $selector => $rule ) {
|
|
66
|
+
$declarations = $rule->get_declarations()->get_declarations();
|
|
67
|
+
ksort( $declarations );
|
|
68
|
+
$selectors_json[ $selector ] = json_encode( $declarations );
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Combine selectors that have the same styles.
|
|
72
|
+
foreach ( $selectors_json as $selector => $json ) {
|
|
73
|
+
// Get selectors that use the same styles.
|
|
74
|
+
$duplicates = array_keys( $selectors_json, $json, true );
|
|
75
|
+
// Skip if there are no duplicates.
|
|
76
|
+
if ( 1 >= count( $duplicates ) ) {
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
foreach ( $duplicates as $key ) {
|
|
80
|
+
// Unset the duplicates from the $selectors_json array to avoid looping through them as well.
|
|
81
|
+
unset( $selectors_json[ $key ] );
|
|
82
|
+
// Remove the rules from the store.
|
|
83
|
+
$this->store->remove_rule( $key );
|
|
84
|
+
}
|
|
85
|
+
// Create a new rule with the combined selectors.
|
|
86
|
+
$new_rule = $this->store->add_rule( implode( ',', $duplicates ) );
|
|
87
|
+
// Set the declarations. The extra check is in place because `add_rule` in the store can return `null`.
|
|
88
|
+
if ( $new_rule ) {
|
|
89
|
+
$new_rule->add_declarations( $rules[ $selector ]->get_declarations() );
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -229,11 +229,11 @@ class WP_Style_Engine {
|
|
|
229
229
|
* @return WP_Style_Engine The main instance.
|
|
230
230
|
*/
|
|
231
231
|
public static function get_instance() {
|
|
232
|
-
if ( null ===
|
|
233
|
-
|
|
232
|
+
if ( null === static::$instance ) {
|
|
233
|
+
static::$instance = new static();
|
|
234
234
|
}
|
|
235
235
|
|
|
236
|
-
return
|
|
236
|
+
return static::$instance;
|
|
237
237
|
}
|
|
238
238
|
|
|
239
239
|
/**
|
|
@@ -370,7 +370,7 @@ class WP_Style_Engine {
|
|
|
370
370
|
$value = static::get_css_var_value( $value, $style_definition['css_vars'] );
|
|
371
371
|
}
|
|
372
372
|
$individual_property = sprintf( $style_property_keys['individual'], _wp_to_kebab_case( $key ) );
|
|
373
|
-
if ( static::is_valid_style_value( $
|
|
373
|
+
if ( $individual_property && static::is_valid_style_value( $value ) ) {
|
|
374
374
|
$css_declarations[ $individual_property ] = $value;
|
|
375
375
|
}
|
|
376
376
|
}
|
|
@@ -423,17 +423,17 @@ class WP_Style_Engine {
|
|
|
423
423
|
}
|
|
424
424
|
|
|
425
425
|
// Build CSS rules output.
|
|
426
|
-
$css_selector
|
|
427
|
-
$
|
|
426
|
+
$css_selector = isset( $options['selector'] ) ? $options['selector'] : null;
|
|
427
|
+
$css_declarations = new WP_Style_Engine_CSS_Declarations( $css_declarations );
|
|
428
428
|
|
|
429
429
|
// The return object.
|
|
430
430
|
$styles_output = array();
|
|
431
|
-
$css = $
|
|
431
|
+
$css = $css_declarations->get_declarations_string();
|
|
432
432
|
|
|
433
433
|
// Return css, if any.
|
|
434
434
|
if ( ! empty( $css ) ) {
|
|
435
435
|
$styles_output['css'] = $css;
|
|
436
|
-
$styles_output['declarations'] = $
|
|
436
|
+
$styles_output['declarations'] = $css_declarations->get_declarations();
|
|
437
437
|
// Return an entire rule if there is a selector.
|
|
438
438
|
if ( $css_selector ) {
|
|
439
439
|
$styles_output['css'] = $css_selector . ' { ' . $css . ' }';
|
|
@@ -503,7 +503,7 @@ class WP_Style_Engine {
|
|
|
503
503
|
* Example usage:
|
|
504
504
|
*
|
|
505
505
|
* $styles = wp_style_engine_get_block_supports_styles( array( 'color' => array( 'text' => '#cccccc' ) ) );
|
|
506
|
-
* // Returns `array( 'css' => 'color: #cccccc', 'classnames' => 'has-color' )`.
|
|
506
|
+
* // Returns `array( 'css' => 'color: #cccccc', 'declarations' => array( 'color' => '#cccccc' ), 'classnames' => 'has-color' )`.
|
|
507
507
|
*
|
|
508
508
|
* @access public
|
|
509
509
|
*
|
|
@@ -511,8 +511,9 @@ class WP_Style_Engine {
|
|
|
511
511
|
* @param array<string> $options An array of options to determine the output.
|
|
512
512
|
*
|
|
513
513
|
* @return array<string>|null array(
|
|
514
|
-
* '
|
|
515
|
-
* '
|
|
514
|
+
* 'css' => (string) A CSS ruleset or declarations block formatted to be placed in an HTML `style` attribute or tag.
|
|
515
|
+
* 'declarations' => (array) An array of property/value pairs representing parsed CSS declarations.
|
|
516
|
+
* 'classnames' => (string) Classnames separated by a space.
|
|
516
517
|
* );
|
|
517
518
|
*/
|
|
518
519
|
function wp_style_engine_get_block_supports_styles( $block_styles, $options = array() ) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/style-engine",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.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": "0315dbc240cb2aa146d7c1bafd251f004b88300e"
|
|
38
38
|
}
|
|
@@ -105,4 +105,50 @@ class WP_Style_Engine_CSS_Declarations_Test extends WP_UnitTestCase {
|
|
|
105
105
|
$css_declarations->get_declarations_string()
|
|
106
106
|
);
|
|
107
107
|
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Should remove a declaration
|
|
111
|
+
*/
|
|
112
|
+
public function test_remove_declaration() {
|
|
113
|
+
$input_declarations = array(
|
|
114
|
+
'color' => 'tomato',
|
|
115
|
+
'margin' => '10em 10em 20em 1px',
|
|
116
|
+
'font-family' => 'Happy Font serif',
|
|
117
|
+
);
|
|
118
|
+
$css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
|
|
119
|
+
|
|
120
|
+
$this->assertSame(
|
|
121
|
+
'color: tomato; margin: 10em 10em 20em 1px; font-family: Happy Font serif;',
|
|
122
|
+
$css_declarations->get_declarations_string()
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
$css_declarations->remove_declaration( 'color' );
|
|
126
|
+
$this->assertSame(
|
|
127
|
+
'margin: 10em 10em 20em 1px; font-family: Happy Font serif;',
|
|
128
|
+
$css_declarations->get_declarations_string()
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Should remove declarations
|
|
134
|
+
*/
|
|
135
|
+
public function test_remove_declarations() {
|
|
136
|
+
$input_declarations = array(
|
|
137
|
+
'color' => 'cucumber',
|
|
138
|
+
'margin' => '10em 10em 20em 1px',
|
|
139
|
+
'font-family' => 'Happy Font serif',
|
|
140
|
+
);
|
|
141
|
+
$css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
|
|
142
|
+
|
|
143
|
+
$this->assertSame(
|
|
144
|
+
'color: cucumber; margin: 10em 10em 20em 1px; font-family: Happy Font serif;',
|
|
145
|
+
$css_declarations->get_declarations_string()
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
$css_declarations->remove_declarations( array( 'color', 'margin' ) );
|
|
149
|
+
$this->assertSame(
|
|
150
|
+
'font-family: Happy Font serif;',
|
|
151
|
+
$css_declarations->get_declarations_string()
|
|
152
|
+
);
|
|
153
|
+
}
|
|
108
154
|
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
/**
|
|
3
|
+
* Tests the Style Engine CSS Rule class.
|
|
4
|
+
*
|
|
5
|
+
* @package Gutenberg
|
|
6
|
+
* @subpackage style-engine
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
require __DIR__ . '/../class-wp-style-engine-css-rule.php';
|
|
10
|
+
require __DIR__ . '/../class-wp-style-engine-css-declarations.php';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Tests for registering, storing and generating CSS declarations.
|
|
14
|
+
*/
|
|
15
|
+
class WP_Style_Engine_CSS_Rule_Test extends WP_UnitTestCase {
|
|
16
|
+
/**
|
|
17
|
+
* Should set declarations on instantiation.
|
|
18
|
+
*/
|
|
19
|
+
public function test_instantiate_with_selector_and_rules() {
|
|
20
|
+
$selector = '.law-and-order';
|
|
21
|
+
$input_declarations = array(
|
|
22
|
+
'margin-top' => '10px',
|
|
23
|
+
'font-size' => '2rem',
|
|
24
|
+
);
|
|
25
|
+
$css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
|
|
26
|
+
$css_rule = new WP_Style_Engine_CSS_Rule( $selector, $css_declarations );
|
|
27
|
+
|
|
28
|
+
$this->assertSame( $selector, $css_rule->get_selector() );
|
|
29
|
+
|
|
30
|
+
$expected = "$selector {{$css_declarations->get_declarations_string()}}";
|
|
31
|
+
$this->assertSame( $expected, $css_rule->get_css() );
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Test dedupe declaration properties.
|
|
36
|
+
*/
|
|
37
|
+
public function test_dedupe_properties_in_rules() {
|
|
38
|
+
$selector = '.taggart';
|
|
39
|
+
$first_declaration = array(
|
|
40
|
+
'font-size' => '2rem',
|
|
41
|
+
);
|
|
42
|
+
$overwrite_first_declaration = array(
|
|
43
|
+
'font-size' => '4px',
|
|
44
|
+
);
|
|
45
|
+
$css_rule = new WP_Style_Engine_CSS_Rule( $selector, $first_declaration );
|
|
46
|
+
$css_rule->add_declarations( new WP_Style_Engine_CSS_Declarations( $overwrite_first_declaration ) );
|
|
47
|
+
|
|
48
|
+
$expected = '.taggart {font-size: 4px;}';
|
|
49
|
+
$this->assertSame( $expected, $css_rule->get_css() );
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Should set selector and rules on instantiation.
|
|
54
|
+
*/
|
|
55
|
+
public function test_add_declarations() {
|
|
56
|
+
// Declarations using a WP_Style_Engine_CSS_Declarations object.
|
|
57
|
+
$some_css_declarations = new WP_Style_Engine_CSS_Declarations( array( 'margin-top' => '10px' ) );
|
|
58
|
+
// Declarations using a property => value array.
|
|
59
|
+
$some_more_css_declarations = array( 'font-size' => '1rem' );
|
|
60
|
+
$css_rule = new WP_Style_Engine_CSS_Rule( '.hill-street-blues', $some_css_declarations );
|
|
61
|
+
$css_rule->add_declarations( $some_more_css_declarations );
|
|
62
|
+
|
|
63
|
+
$expected = '.hill-street-blues {margin-top: 10px; font-size: 1rem;}';
|
|
64
|
+
$this->assertSame( $expected, $css_rule->get_css() );
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Should set selector and rules on instantiation.
|
|
69
|
+
*/
|
|
70
|
+
public function test_set_selector() {
|
|
71
|
+
$selector = '.taggart';
|
|
72
|
+
$css_rule = new WP_Style_Engine_CSS_Rule( $selector );
|
|
73
|
+
|
|
74
|
+
$this->assertSame( $selector, $css_rule->get_selector() );
|
|
75
|
+
|
|
76
|
+
$css_rule->set_selector( '.law-and-order' );
|
|
77
|
+
|
|
78
|
+
$this->assertSame( '.law-and-order', $css_rule->get_selector() );
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Should set selector and rules on instantiation.
|
|
83
|
+
*/
|
|
84
|
+
public function test_get_css() {
|
|
85
|
+
$selector = '.chips';
|
|
86
|
+
$input_declarations = array(
|
|
87
|
+
'margin-top' => '10px',
|
|
88
|
+
'font-size' => '2rem',
|
|
89
|
+
);
|
|
90
|
+
$css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
|
|
91
|
+
$css_rule = new WP_Style_Engine_CSS_Rule( $selector, $css_declarations );
|
|
92
|
+
$expected = "$selector {{$css_declarations->get_declarations_string()}}";
|
|
93
|
+
|
|
94
|
+
$this->assertSame( $expected, $css_rule->get_css() );
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
/**
|
|
3
|
+
* Tests the Style Engine CSS Rules Store class.
|
|
4
|
+
*
|
|
5
|
+
* @package Gutenberg
|
|
6
|
+
* @subpackage style-engine
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
require __DIR__ . '/../class-wp-style-engine-css-rules-store.php';
|
|
10
|
+
require __DIR__ . '/../class-wp-style-engine-css-rule.php';
|
|
11
|
+
require __DIR__ . '/../class-wp-style-engine-css-declarations.php';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Tests for registering, storing and retrieving CSS Rules.
|
|
15
|
+
*/
|
|
16
|
+
class WP_Style_Engine_CSS_Rules_Store_Test extends WP_UnitTestCase {
|
|
17
|
+
/**
|
|
18
|
+
* Should create a new store.
|
|
19
|
+
*/
|
|
20
|
+
public function test_create_new_store() {
|
|
21
|
+
$new_pancakes_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'pancakes-with-strawberries' );
|
|
22
|
+
$this->assertInstanceOf( 'WP_Style_Engine_CSS_Rules_Store', $new_pancakes_store );
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Should return previously created store when the same selector key is passed.
|
|
27
|
+
*/
|
|
28
|
+
public function test_get_store() {
|
|
29
|
+
$new_fish_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'fish-n-chips' );
|
|
30
|
+
$selector = '.haddock';
|
|
31
|
+
|
|
32
|
+
$new_fish_store->add_rule( $selector )->get_selector();
|
|
33
|
+
$this->assertEquals( $selector, $new_fish_store->add_rule( $selector )->get_selector() );
|
|
34
|
+
|
|
35
|
+
$the_same_fish_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'fish-n-chips' );
|
|
36
|
+
$this->assertEquals( $selector, $the_same_fish_store->add_rule( $selector )->get_selector() );
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Should return a stored rule.
|
|
41
|
+
*/
|
|
42
|
+
public function test_add_rule() {
|
|
43
|
+
$new_pie_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'meat-pie' );
|
|
44
|
+
$selector = '.wp-block-sauce a:hover';
|
|
45
|
+
$store_rule = $new_pie_store->add_rule( $selector );
|
|
46
|
+
$expected = "$selector {}";
|
|
47
|
+
$this->assertEquals( $expected, $store_rule->get_css() );
|
|
48
|
+
|
|
49
|
+
$pie_declarations = array(
|
|
50
|
+
'color' => 'brown',
|
|
51
|
+
'border-color' => 'yellow',
|
|
52
|
+
'border-radius' => '10rem',
|
|
53
|
+
);
|
|
54
|
+
$css_declarations = new WP_Style_Engine_CSS_Declarations( $pie_declarations );
|
|
55
|
+
$store_rule->add_declarations( $css_declarations );
|
|
56
|
+
|
|
57
|
+
$store_rule = $new_pie_store->add_rule( $selector );
|
|
58
|
+
$expected = "$selector {{$css_declarations->get_declarations_string()}}";
|
|
59
|
+
$this->assertEquals( $expected, $store_rule->get_css() );
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Should return all stored rules.
|
|
64
|
+
*/
|
|
65
|
+
public function test_get_all_rules() {
|
|
66
|
+
$new_pizza_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'pizza-with-mozzarella' );
|
|
67
|
+
$selector = '.wp-block-anchovies a:hover';
|
|
68
|
+
$store_rule = $new_pizza_store->add_rule( $selector );
|
|
69
|
+
$expected = array(
|
|
70
|
+
$selector => $store_rule,
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
$this->assertEquals( $expected, $new_pizza_store->get_all_rules() );
|
|
74
|
+
|
|
75
|
+
$pizza_declarations = array(
|
|
76
|
+
'color' => 'red',
|
|
77
|
+
'border-color' => 'yellow',
|
|
78
|
+
'border-radius' => '10rem',
|
|
79
|
+
);
|
|
80
|
+
$css_declarations = new WP_Style_Engine_CSS_Declarations( $pizza_declarations );
|
|
81
|
+
$store_rule->add_declarations( array( $css_declarations ) );
|
|
82
|
+
|
|
83
|
+
$expected = array(
|
|
84
|
+
$selector => $store_rule,
|
|
85
|
+
);
|
|
86
|
+
$this->assertEquals( $expected, $new_pizza_store->get_all_rules() );
|
|
87
|
+
|
|
88
|
+
$new_pizza_declarations = array(
|
|
89
|
+
'color' => 'red',
|
|
90
|
+
'border-color' => 'red',
|
|
91
|
+
'font-size' => '10rem',
|
|
92
|
+
);
|
|
93
|
+
$css_declarations = new WP_Style_Engine_CSS_Declarations( $new_pizza_declarations );
|
|
94
|
+
$store_rule->add_declarations( array( $css_declarations ) );
|
|
95
|
+
|
|
96
|
+
$expected = array(
|
|
97
|
+
$selector => $store_rule,
|
|
98
|
+
);
|
|
99
|
+
$this->assertEquals( $expected, $new_pizza_store->get_all_rules() );
|
|
100
|
+
|
|
101
|
+
$new_selector = '.wp-block-mushroom a:hover';
|
|
102
|
+
$newer_pizza_declarations = array(
|
|
103
|
+
'padding' => '100px',
|
|
104
|
+
);
|
|
105
|
+
$new_store_rule = $new_pizza_store->add_rule( $new_selector );
|
|
106
|
+
$css_declarations = new WP_Style_Engine_CSS_Declarations( $newer_pizza_declarations );
|
|
107
|
+
$new_store_rule->add_declarations( array( $css_declarations ) );
|
|
108
|
+
|
|
109
|
+
$expected = array(
|
|
110
|
+
$selector => $store_rule,
|
|
111
|
+
$new_selector => $new_store_rule,
|
|
112
|
+
);
|
|
113
|
+
$this->assertEquals( $expected, $new_pizza_store->get_all_rules() );
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
/**
|
|
3
|
+
* Tests the Style Engine Renderer class.
|
|
4
|
+
*
|
|
5
|
+
* @package Gutenberg
|
|
6
|
+
* @subpackage style-engine
|
|
7
|
+
*/
|
|
8
|
+
require __DIR__ . '/../class-wp-style-engine-css-rules-store.php';
|
|
9
|
+
require __DIR__ . '/../class-wp-style-engine-css-rule.php';
|
|
10
|
+
require __DIR__ . '/../class-wp-style-engine-css-declarations.php';
|
|
11
|
+
require __DIR__ . '/../class-wp-style-engine-processor.php';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Tests for compiling and rendering styles from a store of CSS rules.
|
|
15
|
+
*/
|
|
16
|
+
class WP_Style_Engine_Processor_Test extends WP_UnitTestCase {
|
|
17
|
+
/**
|
|
18
|
+
* Should compile CSS rules from the store.
|
|
19
|
+
*/
|
|
20
|
+
public function test_return_store_rules_as_css() {
|
|
21
|
+
$a_nice_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 'nice' );
|
|
22
|
+
$a_nice_renderer = new WP_Style_Engine_Processor_Gutenberg( $a_nice_store );
|
|
23
|
+
$a_nice_store->add_rule( '.a-nice-rule' )->add_declarations(
|
|
24
|
+
array(
|
|
25
|
+
'color' => 'var(--nice-color)',
|
|
26
|
+
'background-color' => 'purple',
|
|
27
|
+
)
|
|
28
|
+
);
|
|
29
|
+
$a_nice_store->add_rule( '.a-nicer-rule' )->add_declarations(
|
|
30
|
+
array(
|
|
31
|
+
'font-family' => 'Nice sans',
|
|
32
|
+
'font-size' => '1em',
|
|
33
|
+
'background-color' => 'purple',
|
|
34
|
+
)
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
$this->assertEquals( '.a-nice-rule {color: var(--nice-color); background-color: purple;}.a-nicer-rule {font-family: Nice sans; font-size: 1em; background-color: purple;}', $a_nice_renderer->get_css() );
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Should merge CSS declarations.
|
|
42
|
+
*/
|
|
43
|
+
public function test_dedupe_and_merge_css_declarations() {
|
|
44
|
+
$an_excellent_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 'excellent' );
|
|
45
|
+
$an_excellent_renderer = new WP_Style_Engine_Processor_Gutenberg( $an_excellent_store );
|
|
46
|
+
$an_excellent_store->add_rule( '.an-excellent-rule' )->add_declarations(
|
|
47
|
+
array(
|
|
48
|
+
'color' => 'var(--excellent-color)',
|
|
49
|
+
'border-style' => 'dotted',
|
|
50
|
+
)
|
|
51
|
+
);
|
|
52
|
+
$an_excellent_store->add_rule( '.an-excellent-rule' )->add_declarations(
|
|
53
|
+
array(
|
|
54
|
+
'color' => 'var(--excellent-color)',
|
|
55
|
+
'border-style' => 'dotted',
|
|
56
|
+
'border-color' => 'brown',
|
|
57
|
+
)
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
$this->assertEquals( '.an-excellent-rule {color: var(--excellent-color); border-style: dotted; border-color: brown;}', $an_excellent_renderer->get_css() );
|
|
61
|
+
|
|
62
|
+
$an_excellent_store->add_rule( '.an-excellent-rule' )->add_declarations(
|
|
63
|
+
array(
|
|
64
|
+
'color' => 'var(--excellent-color)',
|
|
65
|
+
'border-style' => 'dashed',
|
|
66
|
+
'border-width' => '2px',
|
|
67
|
+
)
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
$this->assertEquals( '.an-excellent-rule {color: var(--excellent-color); border-style: dashed; border-color: brown; border-width: 2px;}', $an_excellent_renderer->get_css() );
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Should combine duplicate CSS rules.
|
|
75
|
+
*/
|
|
76
|
+
public function test_combine_css_rules() {
|
|
77
|
+
$a_sweet_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 'sweet' );
|
|
78
|
+
$a_sweet_renderer = new WP_Style_Engine_Processor_Gutenberg( $a_sweet_store );
|
|
79
|
+
$a_sweet_store->add_rule( '.a-sweet-rule' )->add_declarations(
|
|
80
|
+
array(
|
|
81
|
+
'color' => 'var(--sweet-color)',
|
|
82
|
+
'background-color' => 'purple',
|
|
83
|
+
)
|
|
84
|
+
);
|
|
85
|
+
$a_sweet_store->add_rule( '#an-even-sweeter-rule > marquee' )->add_declarations(
|
|
86
|
+
array(
|
|
87
|
+
'color' => 'var(--sweet-color)',
|
|
88
|
+
'background-color' => 'purple',
|
|
89
|
+
)
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
$this->assertEquals( '.a-sweet-rule,#an-even-sweeter-rule > marquee {color: var(--sweet-color); background-color: purple;}', $a_sweet_renderer->get_css() );
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Should combine and store CSS rules.
|
|
97
|
+
*/
|
|
98
|
+
public function test_store_combined_css_rules() {
|
|
99
|
+
$a_lovely_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 'lovely' );
|
|
100
|
+
$a_lovely_renderer = new WP_Style_Engine_Processor_Gutenberg( $a_lovely_store );
|
|
101
|
+
$a_lovely_store->add_rule( '.a-lovely-rule' )->add_declarations(
|
|
102
|
+
array(
|
|
103
|
+
'border-color' => 'purple',
|
|
104
|
+
)
|
|
105
|
+
);
|
|
106
|
+
$a_lovely_store->add_rule( '.a-lovelier-rule' )->add_declarations(
|
|
107
|
+
array(
|
|
108
|
+
'border-color' => 'purple',
|
|
109
|
+
)
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
$this->assertEquals( '.a-lovely-rule,.a-lovelier-rule {border-color: purple;}', $a_lovely_renderer->get_css() );
|
|
113
|
+
|
|
114
|
+
$a_lovely_store->add_rule( '.a-most-lovely-rule' )->add_declarations(
|
|
115
|
+
array(
|
|
116
|
+
'border-color' => 'purple',
|
|
117
|
+
)
|
|
118
|
+
);
|
|
119
|
+
$a_lovely_store->add_rule( '.a-perfectly-lovely-rule' )->add_declarations(
|
|
120
|
+
array(
|
|
121
|
+
'border-color' => 'purple',
|
|
122
|
+
)
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
$this->assertEquals( '.a-lovely-rule,.a-lovelier-rule,.a-most-lovely-rule,.a-perfectly-lovely-rule {border-color: purple;}', $a_lovely_renderer->get_css() );
|
|
126
|
+
}
|
|
127
|
+
}
|
package/src/test/index.js
CHANGED
|
@@ -8,6 +8,15 @@ describe( 'generate', () => {
|
|
|
8
8
|
expect( generate( {}, '.some-selector' ) ).toEqual( '' );
|
|
9
9
|
} );
|
|
10
10
|
|
|
11
|
+
it( 'should generate empty style with empty keys', () => {
|
|
12
|
+
expect(
|
|
13
|
+
generate( {
|
|
14
|
+
spacing: undefined,
|
|
15
|
+
color: undefined,
|
|
16
|
+
} )
|
|
17
|
+
).toEqual( '' );
|
|
18
|
+
} );
|
|
19
|
+
|
|
11
20
|
it( 'should generate inline styles where there is no selector', () => {
|
|
12
21
|
expect(
|
|
13
22
|
generate( {
|
|
@@ -72,14 +81,17 @@ describe( 'generate', () => {
|
|
|
72
81
|
);
|
|
73
82
|
} );
|
|
74
83
|
|
|
75
|
-
it( 'should parse preset values
|
|
84
|
+
it( 'should parse preset values', () => {
|
|
76
85
|
expect(
|
|
77
86
|
generate( {
|
|
78
87
|
color: {
|
|
79
88
|
text: 'var:preset|color|ham-sandwich',
|
|
80
89
|
},
|
|
90
|
+
spacing: { margin: '3px' },
|
|
81
91
|
} )
|
|
82
|
-
).toEqual(
|
|
92
|
+
).toEqual(
|
|
93
|
+
'color: var(--wp--preset--color--ham-sandwich); margin: 3px;'
|
|
94
|
+
);
|
|
83
95
|
} );
|
|
84
96
|
|
|
85
97
|
it( 'should parse border rules', () => {
|
|
@@ -278,4 +290,31 @@ describe( 'getCSSRules', () => {
|
|
|
278
290
|
},
|
|
279
291
|
] );
|
|
280
292
|
} );
|
|
293
|
+
|
|
294
|
+
it( 'should handle styles with CSS vars', () => {
|
|
295
|
+
expect(
|
|
296
|
+
getCSSRules(
|
|
297
|
+
{
|
|
298
|
+
color: {
|
|
299
|
+
text: 'var:preset|color|bomba-picante',
|
|
300
|
+
},
|
|
301
|
+
spacing: { padding: '11px' },
|
|
302
|
+
},
|
|
303
|
+
{
|
|
304
|
+
selector: '.some-selector a',
|
|
305
|
+
}
|
|
306
|
+
)
|
|
307
|
+
).toEqual( [
|
|
308
|
+
{
|
|
309
|
+
selector: '.some-selector a',
|
|
310
|
+
key: 'color',
|
|
311
|
+
value: 'var(--wp--preset--color--bomba-picante)',
|
|
312
|
+
},
|
|
313
|
+
{
|
|
314
|
+
selector: '.some-selector a',
|
|
315
|
+
key: 'padding',
|
|
316
|
+
value: '11px',
|
|
317
|
+
},
|
|
318
|
+
] );
|
|
319
|
+
} );
|
|
281
320
|
} );
|