@wordpress/style-engine 0.11.0 → 0.14.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 +162 -4
- package/build/styles/utils.js +1 -1
- package/build/styles/utils.js.map +1 -1
- package/build-module/styles/utils.js +1 -1
- package/build-module/styles/utils.js.map +1 -1
- package/class-wp-style-engine-css-declarations.php +167 -0
- package/class-wp-style-engine-css-rule.php +129 -0
- package/class-wp-style-engine-css-rules-store.php +143 -0
- package/class-wp-style-engine-processor.php +135 -0
- package/class-wp-style-engine.php +290 -152
- package/package.json +2 -2
- package/phpunit/class-wp-style-engine-css-declarations-test.php +232 -0
- package/phpunit/class-wp-style-engine-css-rule-test.php +127 -0
- package/phpunit/class-wp-style-engine-css-rules-store-test.php +157 -0
- package/phpunit/class-wp-style-engine-processor-test.php +266 -0
- package/phpunit/class-wp-style-engine-test.php +360 -27
- package/src/styles/utils.ts +3 -1
- package/src/test/index.js +41 -2
- package/build-types/index.d.ts +0 -23
- package/build-types/index.d.ts.map +0 -1
- package/build-types/styles/border/index.d.ts +0 -10
- package/build-types/styles/border/index.d.ts.map +0 -1
- package/build-types/styles/color/background.d.ts +0 -14
- package/build-types/styles/color/background.d.ts.map +0 -1
- package/build-types/styles/color/gradient.d.ts +0 -14
- package/build-types/styles/color/gradient.d.ts.map +0 -1
- package/build-types/styles/color/index.d.ts +0 -10
- package/build-types/styles/color/index.d.ts.map +0 -1
- package/build-types/styles/color/text.d.ts +0 -14
- package/build-types/styles/color/text.d.ts.map +0 -1
- package/build-types/styles/constants.d.ts +0 -4
- package/build-types/styles/constants.d.ts.map +0 -1
- package/build-types/styles/index.d.ts +0 -5
- package/build-types/styles/index.d.ts.map +0 -1
- package/build-types/styles/spacing/index.d.ts +0 -6
- package/build-types/styles/spacing/index.d.ts.map +0 -1
- package/build-types/styles/spacing/margin.d.ts +0 -10
- package/build-types/styles/spacing/margin.d.ts.map +0 -1
- package/build-types/styles/spacing/padding.d.ts +0 -10
- package/build-types/styles/spacing/padding.d.ts.map +0 -1
- package/build-types/styles/typography/index.d.ts +0 -14
- package/build-types/styles/typography/index.d.ts.map +0 -1
- package/build-types/styles/utils.d.ts +0 -48
- package/build-types/styles/utils.d.ts.map +0 -1
- package/build-types/types.d.ts +0 -86
- package/build-types/types.d.ts.map +0 -1
- package/tsconfig.tsbuildinfo +0 -1
|
@@ -0,0 +1,143 @@
|
|
|
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
|
+
/**
|
|
32
|
+
* The store name.
|
|
33
|
+
*
|
|
34
|
+
* @var string
|
|
35
|
+
*/
|
|
36
|
+
protected $name = '';
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* An array of CSS Rules objects assigned to the store.
|
|
40
|
+
*
|
|
41
|
+
* @var WP_Style_Engine_CSS_Rule[]
|
|
42
|
+
*/
|
|
43
|
+
protected $rules = array();
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Get an instance of the store.
|
|
47
|
+
*
|
|
48
|
+
* @param string $store_name The name of the store.
|
|
49
|
+
*
|
|
50
|
+
* @return WP_Style_Engine_CSS_Rules_Store
|
|
51
|
+
*/
|
|
52
|
+
public static function get_store( $store_name = 'default' ) {
|
|
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 ];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Get 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
|
+
}
|
|
69
|
+
|
|
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
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Set 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
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Get the store name.
|
|
92
|
+
*
|
|
93
|
+
* @return string
|
|
94
|
+
*/
|
|
95
|
+
public function get_name() {
|
|
96
|
+
return $this->name;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Get an array of all rules.
|
|
101
|
+
*
|
|
102
|
+
* @return WP_Style_Engine_CSS_Rule[]
|
|
103
|
+
*/
|
|
104
|
+
public function get_all_rules() {
|
|
105
|
+
return $this->rules;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Get 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
|
+
*
|
|
114
|
+
* @return WP_Style_Engine_CSS_Rule|null Returns a WP_Style_Engine_CSS_Rule object, or null if the selector is empty.
|
|
115
|
+
*/
|
|
116
|
+
public function add_rule( $selector ) {
|
|
117
|
+
|
|
118
|
+
$selector = trim( $selector );
|
|
119
|
+
|
|
120
|
+
// Bail early if there is no selector.
|
|
121
|
+
if ( empty( $selector ) ) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Create the rule if it doesn't exist.
|
|
126
|
+
if ( empty( $this->rules[ $selector ] ) ) {
|
|
127
|
+
$this->rules[ $selector ] = new WP_Style_Engine_CSS_Rule( $selector );
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return $this->rules[ $selector ];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Remove a selector from the store.
|
|
135
|
+
*
|
|
136
|
+
* @param string $selector The CSS selector.
|
|
137
|
+
*
|
|
138
|
+
* @return void
|
|
139
|
+
*/
|
|
140
|
+
public function remove_rule( $selector ) {
|
|
141
|
+
unset( $this->rules[ $selector ] );
|
|
142
|
+
}
|
|
143
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
/**
|
|
3
|
+
* WP_Style_Engine_Processor
|
|
4
|
+
*
|
|
5
|
+
* Compiles styles from stores or collection 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 stores or collection of CSS rules.
|
|
16
|
+
*
|
|
17
|
+
* @access private
|
|
18
|
+
*/
|
|
19
|
+
class WP_Style_Engine_Processor {
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The Style-Engine Store objects
|
|
23
|
+
*
|
|
24
|
+
* @var WP_Style_Engine_CSS_Rules_Store[]
|
|
25
|
+
*/
|
|
26
|
+
protected $stores = array();
|
|
27
|
+
|
|
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();
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Add a store to the processor.
|
|
37
|
+
*
|
|
38
|
+
* @param WP_Style_Engine_CSS_Rules_Store $store The store to add.
|
|
39
|
+
*/
|
|
40
|
+
public function add_store( WP_Style_Engine_CSS_Rules_Store $store ) {
|
|
41
|
+
$this->stores[ $store->get_name() ] = $store;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Adds rules to be processed.
|
|
46
|
+
*
|
|
47
|
+
* @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.
|
|
48
|
+
*/
|
|
49
|
+
public function add_rules( $css_rules ) {
|
|
50
|
+
if ( ! is_array( $css_rules ) ) {
|
|
51
|
+
$css_rules = array( $css_rules );
|
|
52
|
+
}
|
|
53
|
+
foreach ( $css_rules as $rule ) {
|
|
54
|
+
$selector = $rule->get_selector();
|
|
55
|
+
if ( isset( $this->css_rules[ $selector ] ) ) {
|
|
56
|
+
$this->css_rules[ $selector ]->add_declarations( $rule->get_declarations() );
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
$this->css_rules[ $rule->get_selector() ] = $rule;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Get the CSS rules as a string.
|
|
65
|
+
*
|
|
66
|
+
* @param array $options array(
|
|
67
|
+
* 'optimize' => (boolean) Whether to optimize the CSS output, e.g., combine rules.
|
|
68
|
+
* 'prettify' => (boolean) Whether to add new lines to output.
|
|
69
|
+
* );.
|
|
70
|
+
*
|
|
71
|
+
* @return string The computed CSS.
|
|
72
|
+
*/
|
|
73
|
+
public function get_css( $options = array() ) {
|
|
74
|
+
$defaults = array(
|
|
75
|
+
'optimize' => true,
|
|
76
|
+
'prettify' => false,
|
|
77
|
+
);
|
|
78
|
+
$options = wp_parse_args( $options, $defaults );
|
|
79
|
+
|
|
80
|
+
// If we have stores, get the rules from them.
|
|
81
|
+
foreach ( $this->stores as $store ) {
|
|
82
|
+
$this->add_rules( $store->get_all_rules() );
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Combine CSS selectors that have identical declarations.
|
|
86
|
+
if ( true === $options['optimize'] ) {
|
|
87
|
+
$this->combine_rules_selectors();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Build the CSS.
|
|
91
|
+
$css = '';
|
|
92
|
+
foreach ( $this->css_rules as $rule ) {
|
|
93
|
+
$css .= $rule->get_css( $options['prettify'] );
|
|
94
|
+
$css .= $options['prettify'] ? "\n" : '';
|
|
95
|
+
}
|
|
96
|
+
return $css;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Combines selectors from the rules store when they have the same styles.
|
|
101
|
+
*
|
|
102
|
+
* @return void
|
|
103
|
+
*/
|
|
104
|
+
private function combine_rules_selectors() {
|
|
105
|
+
// Build an array of selectors along with the JSON-ified styles to make comparisons easier.
|
|
106
|
+
$selectors_json = array();
|
|
107
|
+
foreach ( $this->css_rules as $rule ) {
|
|
108
|
+
$declarations = $rule->get_declarations()->get_declarations();
|
|
109
|
+
ksort( $declarations );
|
|
110
|
+
$selectors_json[ $rule->get_selector() ] = wp_json_encode( $declarations );
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Combine selectors that have the same styles.
|
|
114
|
+
foreach ( $selectors_json as $selector => $json ) {
|
|
115
|
+
// Get selectors that use the same styles.
|
|
116
|
+
$duplicates = array_keys( $selectors_json, $json, true );
|
|
117
|
+
// Skip if there are no duplicates.
|
|
118
|
+
if ( 1 >= count( $duplicates ) ) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
$declarations = $this->css_rules[ $selector ]->get_declarations();
|
|
123
|
+
|
|
124
|
+
foreach ( $duplicates as $key ) {
|
|
125
|
+
// Unset the duplicates from the $selectors_json array to avoid looping through them as well.
|
|
126
|
+
unset( $selectors_json[ $key ] );
|
|
127
|
+
// Remove the rules from the rules collection.
|
|
128
|
+
unset( $this->css_rules[ $key ] );
|
|
129
|
+
}
|
|
130
|
+
// Create a new rule with the combined selectors.
|
|
131
|
+
$duplicate_selectors = implode( ',', $duplicates );
|
|
132
|
+
$this->css_rules[ $duplicate_selectors ] = new WP_Style_Engine_CSS_Rule( $duplicate_selectors, $declarations );
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|