@wordpress/style-engine 0.12.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.
Files changed (42) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/README.md +206 -42
  3. package/build/index.js +7 -5
  4. package/build/index.js.map +1 -1
  5. package/build/styles/typography/index.js +7 -1
  6. package/build/styles/typography/index.js.map +1 -1
  7. package/build/styles/utils.js +5 -5
  8. package/build/styles/utils.js.map +1 -1
  9. package/build-module/index.js +6 -4
  10. package/build-module/index.js.map +1 -1
  11. package/build-module/styles/typography/index.js +7 -1
  12. package/build-module/styles/typography/index.js.map +1 -1
  13. package/build-module/styles/utils.js +5 -5
  14. package/build-module/styles/utils.js.map +1 -1
  15. package/build-types/index.d.ts +4 -4
  16. package/build-types/index.d.ts.map +1 -1
  17. package/build-types/styles/color/background.d.ts +1 -1
  18. package/build-types/styles/color/gradient.d.ts +1 -1
  19. package/build-types/styles/color/index.d.ts +1 -1
  20. package/build-types/styles/color/text.d.ts +1 -1
  21. package/build-types/styles/typography/index.d.ts +1 -1
  22. package/build-types/styles/typography/index.d.ts.map +1 -1
  23. package/build-types/styles/utils.d.ts +6 -6
  24. package/build-types/types.d.ts +3 -4
  25. package/build-types/types.d.ts.map +1 -1
  26. package/class-wp-style-engine-css-declarations.php +61 -10
  27. package/class-wp-style-engine-css-rule.php +125 -0
  28. package/class-wp-style-engine-css-rules-store.php +144 -0
  29. package/class-wp-style-engine-processor.php +143 -0
  30. package/class-wp-style-engine.php +265 -144
  31. package/package.json +2 -2
  32. package/phpunit/class-wp-style-engine-css-declarations-test.php +129 -3
  33. package/phpunit/class-wp-style-engine-css-rule-test.php +127 -0
  34. package/phpunit/class-wp-style-engine-css-rules-store-test.php +171 -0
  35. package/phpunit/class-wp-style-engine-processor-test.php +266 -0
  36. package/phpunit/class-wp-style-engine-test.php +223 -22
  37. package/src/index.ts +4 -4
  38. package/src/styles/typography/index.ts +13 -0
  39. package/src/styles/utils.ts +5 -5
  40. package/src/test/index.js +64 -11
  41. package/src/types.ts +3 -4
  42. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,266 @@
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.
19
+ */
20
+ public function test_return_rules_as_css() {
21
+ $a_nice_css_rule = new WP_Style_Engine_CSS_Rule( '.a-nice-rule' );
22
+ $a_nice_css_rule->add_declarations(
23
+ array(
24
+ 'color' => 'var(--nice-color)',
25
+ 'background-color' => 'purple',
26
+ )
27
+ );
28
+ $a_nicer_css_rule = new WP_Style_Engine_CSS_Rule( '.a-nicer-rule' );
29
+ $a_nicer_css_rule->add_declarations(
30
+ array(
31
+ 'font-family' => 'Nice sans',
32
+ 'font-size' => '1em',
33
+ 'background-color' => 'purple',
34
+ )
35
+ );
36
+ $a_nice_processor = new WP_Style_Engine_Processor();
37
+ $a_nice_processor->add_rules( array( $a_nice_css_rule, $a_nicer_css_rule ) );
38
+ $this->assertEquals(
39
+ '.a-nice-rule{color:var(--nice-color);background-color:purple;}.a-nicer-rule{font-family:Nice sans;font-size:1em;background-color:purple;}',
40
+ $a_nice_processor->get_css()
41
+ );
42
+ }
43
+
44
+ /**
45
+ * Should compile CSS rules.
46
+ */
47
+ public function test_return_prettified_rules_as_css() {
48
+ $a_wonderful_css_rule = new WP_Style_Engine_CSS_Rule( '.a-wonderful-rule' );
49
+ $a_wonderful_css_rule->add_declarations(
50
+ array(
51
+ 'color' => 'var(--wonderful-color)',
52
+ 'background-color' => 'orange',
53
+ )
54
+ );
55
+ $a_very_wonderful_css_rule = new WP_Style_Engine_CSS_Rule( '.a-very_wonderful-rule' );
56
+ $a_very_wonderful_css_rule->add_declarations(
57
+ array(
58
+ 'color' => 'var(--wonderful-color)',
59
+ 'background-color' => 'orange',
60
+ )
61
+ );
62
+ $a_more_wonderful_css_rule = new WP_Style_Engine_CSS_Rule( '.a-more-wonderful-rule' );
63
+ $a_more_wonderful_css_rule->add_declarations(
64
+ array(
65
+ 'font-family' => 'Wonderful sans',
66
+ 'font-size' => '1em',
67
+ 'background-color' => 'orange',
68
+ )
69
+ );
70
+ $a_wonderful_processor = new WP_Style_Engine_Processor();
71
+ $a_wonderful_processor->add_rules( array( $a_wonderful_css_rule, $a_very_wonderful_css_rule, $a_more_wonderful_css_rule ) );
72
+
73
+ $expected = '.a-more-wonderful-rule {
74
+ font-family: Wonderful sans;
75
+ font-size: 1em;
76
+ background-color: orange;
77
+ }
78
+ .a-wonderful-rule,
79
+ .a-very_wonderful-rule {
80
+ color: var(--wonderful-color);
81
+ background-color: orange;
82
+ }
83
+ ';
84
+ $this->assertEquals(
85
+ $expected,
86
+ $a_wonderful_processor->get_css( array( 'prettify' => true ) )
87
+ );
88
+ }
89
+
90
+ /**
91
+ * Should compile CSS rules from the store.
92
+ */
93
+ public function test_return_store_rules_as_css() {
94
+ $a_nice_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'nice' );
95
+ $a_nice_store->add_rule( '.a-nice-rule' )->add_declarations(
96
+ array(
97
+ 'color' => 'var(--nice-color)',
98
+ 'background-color' => 'purple',
99
+ )
100
+ );
101
+ $a_nice_store->add_rule( '.a-nicer-rule' )->add_declarations(
102
+ array(
103
+ 'font-family' => 'Nice sans',
104
+ 'font-size' => '1em',
105
+ 'background-color' => 'purple',
106
+ )
107
+ );
108
+ $a_nice_renderer = new WP_Style_Engine_Processor();
109
+ $a_nice_renderer->add_store( $a_nice_store );
110
+ $this->assertEquals(
111
+ '.a-nice-rule{color:var(--nice-color);background-color:purple;}.a-nicer-rule{font-family:Nice sans;font-size:1em;background-color:purple;}',
112
+ $a_nice_renderer->get_css()
113
+ );
114
+ }
115
+
116
+ /**
117
+ * Should merge CSS declarations.
118
+ */
119
+ public function test_dedupe_and_merge_css_declarations() {
120
+ $an_excellent_rule = new WP_Style_Engine_CSS_Rule( '.an-excellent-rule' );
121
+ $an_excellent_processor = new WP_Style_Engine_Processor();
122
+ $an_excellent_rule->add_declarations(
123
+ array(
124
+ 'color' => 'var(--excellent-color)',
125
+ 'border-style' => 'dotted',
126
+ )
127
+ );
128
+ $an_excellent_processor->add_rules( $an_excellent_rule );
129
+
130
+ $another_excellent_rule = new WP_Style_Engine_CSS_Rule( '.an-excellent-rule' );
131
+ $another_excellent_rule->add_declarations(
132
+ array(
133
+ 'color' => 'var(--excellent-color)',
134
+ 'border-style' => 'dotted',
135
+ 'border-color' => 'brown',
136
+ )
137
+ );
138
+ $an_excellent_processor->add_rules( $another_excellent_rule );
139
+ $this->assertEquals(
140
+ '.an-excellent-rule{color:var(--excellent-color);border-style:dotted;border-color:brown;}',
141
+ $an_excellent_processor->get_css()
142
+ );
143
+
144
+ $yet_another_excellent_rule = new WP_Style_Engine_CSS_Rule( '.an-excellent-rule' );
145
+ $yet_another_excellent_rule->add_declarations(
146
+ array(
147
+ 'color' => 'var(--excellent-color)',
148
+ 'border-style' => 'dashed',
149
+ 'border-width' => '2px',
150
+ )
151
+ );
152
+ $an_excellent_processor->add_rules( $yet_another_excellent_rule );
153
+ $this->assertEquals(
154
+ '.an-excellent-rule{color:var(--excellent-color);border-style:dashed;border-color:brown;border-width:2px;}',
155
+ $an_excellent_processor->get_css()
156
+ );
157
+ }
158
+
159
+ /**
160
+ * Should print out uncombined selectors duplicate CSS rules.
161
+ */
162
+ public function test_output_verbose_css_rules() {
163
+ $a_sweet_rule = new WP_Style_Engine_CSS_Rule(
164
+ '.a-sweet-rule',
165
+ array(
166
+ 'color' => 'var(--sweet-color)',
167
+ 'background-color' => 'purple',
168
+ )
169
+ );
170
+
171
+ $a_sweeter_rule = new WP_Style_Engine_CSS_Rule(
172
+ '#an-even-sweeter-rule > marquee',
173
+ array(
174
+ 'color' => 'var(--sweet-color)',
175
+ 'background-color' => 'purple',
176
+ )
177
+ );
178
+
179
+ $the_sweetest_rule = new WP_Style_Engine_CSS_Rule(
180
+ '.the-sweetest-rule-of-all a',
181
+ array(
182
+ 'color' => 'var(--sweet-color)',
183
+ 'background-color' => 'purple',
184
+ )
185
+ );
186
+
187
+ $a_sweet_processor = new WP_Style_Engine_Processor();
188
+ $a_sweet_processor->add_rules( array( $a_sweet_rule, $a_sweeter_rule, $the_sweetest_rule ) );
189
+
190
+ $this->assertEquals(
191
+ '.a-sweet-rule{color:var(--sweet-color);background-color:purple;}#an-even-sweeter-rule > marquee{color:var(--sweet-color);background-color:purple;}.the-sweetest-rule-of-all a{color:var(--sweet-color);background-color:purple;}',
192
+ $a_sweet_processor->get_css( array( 'optimize' => false ) )
193
+ );
194
+ }
195
+
196
+ /**
197
+ * Should combine duplicate CSS rules.
198
+ */
199
+ public function test_combine_css_rules() {
200
+ $a_sweet_rule = new WP_Style_Engine_CSS_Rule(
201
+ '.a-sweet-rule',
202
+ array(
203
+ 'color' => 'var(--sweet-color)',
204
+ 'background-color' => 'purple',
205
+ )
206
+ );
207
+
208
+ $a_sweeter_rule = new WP_Style_Engine_CSS_Rule(
209
+ '#an-even-sweeter-rule > marquee',
210
+ array(
211
+ 'color' => 'var(--sweet-color)',
212
+ 'background-color' => 'purple',
213
+ )
214
+ );
215
+
216
+ $a_sweet_processor = new WP_Style_Engine_Processor();
217
+ $a_sweet_processor->add_rules( array( $a_sweet_rule, $a_sweeter_rule ) );
218
+
219
+ $this->assertEquals(
220
+ '.a-sweet-rule,#an-even-sweeter-rule > marquee{color:var(--sweet-color);background-color:purple;}',
221
+ $a_sweet_processor->get_css()
222
+ );
223
+ }
224
+ /**
225
+ * Should combine and store CSS rules.
226
+ */
227
+ public function test_combine_previously_added_css_rules() {
228
+ $a_lovely_processor = new WP_Style_Engine_Processor();
229
+ $a_lovely_rule = new WP_Style_Engine_CSS_Rule(
230
+ '.a-lovely-rule',
231
+ array(
232
+ 'border-color' => 'purple',
233
+ )
234
+ );
235
+ $a_lovely_processor->add_rules( $a_lovely_rule );
236
+ $a_lovelier_rule = new WP_Style_Engine_CSS_Rule(
237
+ '.a-lovelier-rule',
238
+ array(
239
+ 'border-color' => 'purple',
240
+ )
241
+ );
242
+ $a_lovely_processor->add_rules( $a_lovelier_rule );
243
+ $this->assertEquals( '.a-lovely-rule,.a-lovelier-rule{border-color:purple;}', $a_lovely_processor->get_css() );
244
+
245
+ $a_most_lovely_rule = new WP_Style_Engine_CSS_Rule(
246
+ '.a-most-lovely-rule',
247
+ array(
248
+ 'border-color' => 'purple',
249
+ )
250
+ );
251
+ $a_lovely_processor->add_rules( $a_most_lovely_rule );
252
+
253
+ $a_perfectly_lovely_rule = new WP_Style_Engine_CSS_Rule(
254
+ '.a-perfectly-lovely-rule',
255
+ array(
256
+ 'border-color' => 'purple',
257
+ )
258
+ );
259
+ $a_lovely_processor->add_rules( $a_perfectly_lovely_rule );
260
+
261
+ $this->assertEquals(
262
+ '.a-lovely-rule,.a-lovelier-rule,.a-most-lovely-rule,.a-perfectly-lovely-rule{border-color:purple;}',
263
+ $a_lovely_processor->get_css()
264
+ );
265
+ }
266
+ }
@@ -6,44 +6,55 @@
6
6
  * @subpackage style-engine
7
7
  */
8
8
 
9
+ require __DIR__ . '/../class-wp-style-engine-processor.php';
9
10
  require __DIR__ . '/../class-wp-style-engine-css-declarations.php';
11
+ require __DIR__ . '/../class-wp-style-engine-css-rule.php';
12
+ require __DIR__ . '/../class-wp-style-engine-css-rules-store.php';
10
13
  require __DIR__ . '/../class-wp-style-engine.php';
11
14
 
12
15
  /**
13
16
  * Tests for registering, storing and generating styles.
14
17
  */
15
18
  class WP_Style_Engine_Test extends WP_UnitTestCase {
19
+ /**
20
+ * Tear down after each test.
21
+ */
22
+ public function tear_down() {
23
+ parent::tear_down();
24
+ WP_Style_Engine_CSS_Rules_Store::remove_all_stores();
25
+ }
26
+
16
27
  /**
17
28
  * Tests generating block styles and classnames based on various manifestations of the $block_styles argument.
18
29
  *
19
- * @dataProvider data_generate_block_supports_styles_fixtures
30
+ * @dataProvider data_get_styles_fixtures
20
31
  *
21
- * @param array $block_styles The incoming block styles object.
22
- * @param array $options Style engine options.
32
+ * @param array $block_styles The incoming block styles object.
33
+ * @param array $options Style engine options.
23
34
  * @param string $expected_output The expected output.
24
35
  */
25
- public function test_generate_block_supports_styles( $block_styles, $options, $expected_output ) {
26
- $generated_styles = wp_style_engine_get_block_supports_styles( $block_styles, $options );
36
+ public function test_generate_get_styles( $block_styles, $options, $expected_output ) {
37
+ $generated_styles = wp_style_engine_get_styles( $block_styles, $options );
27
38
  $this->assertSame( $expected_output, $generated_styles );
28
39
  }
29
40
 
30
41
  /**
31
- * Data provider.
42
+ * Data provider for test_generate_get_styles().
32
43
  *
33
44
  * @return array
34
45
  */
35
- public function data_generate_block_supports_styles_fixtures() {
46
+ public function data_get_styles_fixtures() {
36
47
  return array(
37
48
  'default_return_value' => array(
38
49
  'block_styles' => array(),
39
50
  'options' => null,
40
- 'expected_output' => null,
51
+ 'expected_output' => array(),
41
52
  ),
42
53
 
43
54
  'inline_invalid_block_styles_empty' => array(
44
55
  'block_styles' => 'hello world!',
45
56
  'options' => null,
46
- 'expected_output' => null,
57
+ 'expected_output' => array(),
47
58
  ),
48
59
 
49
60
  'inline_invalid_block_styles_unknown_style' => array(
@@ -72,7 +83,7 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
72
83
  'expected_output' => array(),
73
84
  ),
74
85
 
75
- 'valid_inline_css_and_classnames' => array(
86
+ 'valid_inline_css_and_classnames_as_default_context' => array(
76
87
  'block_styles' => array(
77
88
  'color' => array(
78
89
  'text' => 'var:preset|color|texas-flood',
@@ -89,7 +100,7 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
89
100
  ),
90
101
  'options' => array( 'convert_vars_to_classnames' => true ),
91
102
  'expected_output' => array(
92
- 'css' => 'border-style: dotted; border-width: 2rem; padding: 0; margin: 111px;',
103
+ 'css' => 'border-style:dotted;border-width:2rem;padding:0;margin:111px;',
93
104
  'declarations' => array(
94
105
  'border-style' => 'dotted',
95
106
  'border-width' => '2rem',
@@ -100,6 +111,28 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
100
111
  ),
101
112
  ),
102
113
 
114
+ 'valid_inline_css_and_classnames_with_context' => array(
115
+ 'block_styles' => array(
116
+ 'color' => array(
117
+ 'text' => 'var:preset|color|little-lamb',
118
+ ),
119
+ 'spacing' => array(
120
+ 'margin' => '20px',
121
+ ),
122
+ ),
123
+ 'options' => array(
124
+ 'convert_vars_to_classnames' => true,
125
+ 'context' => 'block-supports',
126
+ ),
127
+ 'expected_output' => array(
128
+ 'css' => 'margin:20px;',
129
+ 'declarations' => array(
130
+ 'margin' => '20px',
131
+ ),
132
+ 'classnames' => 'has-text-color has-little-lamb-color',
133
+ ),
134
+ ),
135
+
103
136
  'inline_valid_box_model_style' => array(
104
137
  'block_styles' => array(
105
138
  'spacing' => array(
@@ -127,7 +160,7 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
127
160
  ),
128
161
  'options' => null,
129
162
  'expected_output' => array(
130
- 'css' => 'border-top-left-radius: 99px; border-top-right-radius: 98px; border-bottom-left-radius: 97px; border-bottom-right-radius: 96px; padding-top: 42px; padding-left: 2%; padding-bottom: 44px; padding-right: 5rem; margin-top: 12rem; margin-left: 2vh; margin-bottom: 2px; margin-right: 10em;',
163
+ 'css' => 'border-top-left-radius:99px;border-top-right-radius:98px;border-bottom-left-radius:97px;border-bottom-right-radius:96px;padding-top:42px;padding-left:2%;padding-bottom:44px;padding-right:5rem;margin-top:12rem;margin-left:2vh;margin-bottom:2px;margin-right:10em;',
131
164
  'declarations' => array(
132
165
  'border-top-left-radius' => '99px',
133
166
  'border-top-right-radius' => '98px',
@@ -160,7 +193,7 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
160
193
  ),
161
194
  'options' => null,
162
195
  'expected_output' => array(
163
- 'css' => 'font-family: Roboto,Oxygen-Sans,Ubuntu,sans-serif; font-style: italic; font-weight: 800; line-height: 1.3; text-decoration: underline; text-transform: uppercase; letter-spacing: 2;',
196
+ 'css' => 'font-size:clamp(2em, 2vw, 4em);font-family:Roboto,Oxygen-Sans,Ubuntu,sans-serif;font-style:italic;font-weight:800;line-height:1.3;text-decoration:underline;text-transform:uppercase;letter-spacing:2;',
164
197
  'declarations' => array(
165
198
  'font-size' => 'clamp(2em, 2vw, 4em)',
166
199
  'font-family' => 'Roboto,Oxygen-Sans,Ubuntu,sans-serif',
@@ -187,7 +220,7 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
187
220
  ),
188
221
  'options' => array( 'selector' => '.wp-selector > p' ),
189
222
  'expected_output' => array(
190
- 'css' => '.wp-selector > p { padding-top: 42px; padding-left: 2%; padding-bottom: 44px; padding-right: 5rem; }',
223
+ 'css' => '.wp-selector > p{padding-top:42px;padding-left:2%;padding-bottom:44px;padding-right:5rem;}',
191
224
  'declarations' => array(
192
225
  'padding-top' => '42px',
193
226
  'padding-left' => '2%',
@@ -207,7 +240,7 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
207
240
  'selector' => '.wp-selector',
208
241
  ),
209
242
  'expected_output' => array(
210
- 'css' => '.wp-selector { color: var(--wp--preset--color--my-little-pony); }',
243
+ 'css' => '.wp-selector{color:var(--wp--preset--color--my-little-pony);}',
211
244
  'declarations' => array(
212
245
  'color' => 'var(--wp--preset--color--my-little-pony)',
213
246
  ),
@@ -253,7 +286,7 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
253
286
  ),
254
287
  'options' => array(),
255
288
  'expected_output' => array(
256
- 'css' => 'color: var(--wp--preset--color--teal-independents);',
289
+ 'css' => 'color:var(--wp--preset--color--teal-independents);',
257
290
  'declarations' => array(
258
291
  'color' => 'var(--wp--preset--color--teal-independents)',
259
292
  ),
@@ -270,7 +303,7 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
270
303
  ),
271
304
  'options' => array(),
272
305
  'expected_output' => array(
273
- 'css' => 'color: #fff;',
306
+ 'css' => 'color:#fff;',
274
307
  'declarations' => array(
275
308
  'color' => '#fff',
276
309
  ),
@@ -304,7 +337,7 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
304
337
  ),
305
338
  'options' => array(),
306
339
  'expected_output' => array(
307
- 'css' => 'padding: var(--wp--preset--spacing--20); margin: var(--wp--preset--spacing--10);',
340
+ 'css' => 'padding:var(--wp--preset--spacing--20);margin:var(--wp--preset--spacing--10);',
308
341
  'declarations' => array(
309
342
  'padding' => 'var(--wp--preset--spacing--20)',
310
343
  'margin' => 'var(--wp--preset--spacing--10)',
@@ -331,7 +364,7 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
331
364
  ),
332
365
  'options' => array(),
333
366
  'expected_output' => array(
334
- 'css' => 'padding-left: var(--wp--preset--spacing--30); padding-right: var(--wp--preset--spacing--40); padding-top: 14px; padding-bottom: 14px; margin-left: var(--wp--preset--spacing--10); margin-right: var(--wp--preset--spacing--20); margin-top: 1rem; margin-bottom: 1rem;',
367
+ 'css' => 'padding-left:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--40);padding-top:14px;padding-bottom:14px;margin-left:var(--wp--preset--spacing--10);margin-right:var(--wp--preset--spacing--20);margin-top:1rem;margin-bottom:1rem;',
335
368
  'declarations' => array(
336
369
  'padding-left' => 'var(--wp--preset--spacing--30)',
337
370
  'padding-right' => 'var(--wp--preset--spacing--40)',
@@ -358,7 +391,7 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
358
391
  ),
359
392
  'options' => array(),
360
393
  'expected_output' => array(
361
- 'css' => 'margin-top: 1rem; margin-bottom: 0;',
394
+ 'css' => 'margin-top:1rem;margin-bottom:0;',
362
395
  'declarations' => array(
363
396
  'margin-top' => '1rem',
364
397
  'margin-bottom' => '0',
@@ -407,7 +440,7 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
407
440
  ),
408
441
  'options' => array(),
409
442
  'expected_output' => array(
410
- 'css' => 'border-top-color: #fe1; border-top-width: 1.5rem; border-top-style: dashed; border-right-color: #fe2; border-right-width: 1.4rem; border-right-style: solid; border-bottom-color: #fe3; border-bottom-width: 1.3rem; border-left-color: var(--wp--preset--color--swampy-yellow); border-left-width: 0.5rem; border-left-style: dotted;',
443
+ 'css' => 'border-top-color:#fe1;border-top-width:1.5rem;border-top-style:dashed;border-right-color:#fe2;border-right-width:1.4rem;border-right-style:solid;border-bottom-color:#fe3;border-bottom-width:1.3rem;border-left-color:var(--wp--preset--color--swampy-yellow);border-left-width:0.5rem;border-left-style:dotted;',
411
444
  'declarations' => array(
412
445
  'border-top-color' => '#fe1',
413
446
  'border-top-width' => '1.5rem',
@@ -450,7 +483,7 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
450
483
  ),
451
484
  'options' => array(),
452
485
  'expected_output' => array(
453
- 'css' => 'border-bottom-color: var(--wp--preset--color--terrible-lizard);',
486
+ 'css' => 'border-bottom-color:var(--wp--preset--color--terrible-lizard);',
454
487
  'declarations' => array(
455
488
  'border-bottom-color' => 'var(--wp--preset--color--terrible-lizard)',
456
489
  ),
@@ -458,4 +491,172 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
458
491
  ),
459
492
  );
460
493
  }
494
+
495
+ /**
496
+ * Tests adding rules to a store and retrieving a generated stylesheet.
497
+ */
498
+ public function test_store_block_styles_using_context() {
499
+ $block_styles = array(
500
+ 'spacing' => array(
501
+ 'padding' => array(
502
+ 'top' => '42px',
503
+ 'left' => '2%',
504
+ 'bottom' => '44px',
505
+ 'right' => '5rem',
506
+ ),
507
+ ),
508
+ );
509
+
510
+ $generated_styles = wp_style_engine_get_styles(
511
+ $block_styles,
512
+ array(
513
+ 'context' => 'block-supports',
514
+ 'selector' => 'article',
515
+ )
516
+ );
517
+ $store = WP_Style_Engine::get_store( 'block-supports' );
518
+ $rule = $store->get_all_rules()['article'];
519
+ $this->assertSame( $generated_styles['css'], $rule->get_css() );
520
+ }
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
+
544
+ /**
545
+ * Tests adding rules to a store and retrieving a generated stylesheet.
546
+ */
547
+ public function test_add_to_store() {
548
+ $css_rules = array(
549
+ array(
550
+ 'selector' => '.frodo',
551
+ 'declarations' => array(
552
+ 'color' => 'brown',
553
+ 'height' => '10px',
554
+ 'width' => '30px',
555
+ 'border-style' => 'dotted',
556
+ ),
557
+ ),
558
+ array(
559
+ 'selector' => '.samwise',
560
+ 'declarations' => array(
561
+ 'color' => 'brown',
562
+ 'height' => '20px',
563
+ 'width' => '50px',
564
+ 'border-style' => 'solid',
565
+ ),
566
+ ),
567
+ );
568
+ $compiled_stylesheet = wp_style_engine_get_stylesheet_from_css_rules(
569
+ $css_rules,
570
+ array(
571
+ 'context' => 'test-store',
572
+ )
573
+ );
574
+
575
+ // Check that the style engine knows about the store.
576
+ $stored_store = WP_Style_Engine::get_store( 'test-store' );
577
+ $this->assertInstanceOf( 'WP_Style_Engine_CSS_Rules_Store', $stored_store );
578
+ $this->assertSame( $compiled_stylesheet, WP_Style_Engine::compile_stylesheet_from_css_rules( $stored_store->get_all_rules() ) );
579
+ }
580
+
581
+ /**
582
+ * Tests retrieving a generated stylesheet from any rules.
583
+ */
584
+ public function test_get_stylesheet_from_css_rules() {
585
+ $css_rules = array(
586
+ array(
587
+ 'selector' => '.saruman',
588
+ 'declarations' => array(
589
+ 'color' => 'white',
590
+ 'height' => '100px',
591
+ 'border-style' => 'solid',
592
+ 'align-self' => 'unset',
593
+ ),
594
+ ),
595
+ array(
596
+ 'selector' => '.gandalf',
597
+ 'declarations' => array(
598
+ 'color' => 'grey',
599
+ 'height' => '90px',
600
+ 'border-style' => 'dotted',
601
+ 'align-self' => 'safe center',
602
+ ),
603
+ ),
604
+ array(
605
+ 'selector' => '.radagast',
606
+ 'declarations' => array(
607
+ 'color' => 'brown',
608
+ 'height' => '60px',
609
+ 'border-style' => 'dashed',
610
+ 'align-self' => 'stretch',
611
+ ),
612
+ ),
613
+ );
614
+
615
+ $compiled_stylesheet = wp_style_engine_get_stylesheet_from_css_rules( $css_rules );
616
+ $this->assertSame( '.saruman{color:white;height:100px;border-style:solid;align-self:unset;}.gandalf{color:grey;height:90px;border-style:dotted;align-self:safe center;}.radagast{color:brown;height:60px;border-style:dashed;align-self:stretch;}', $compiled_stylesheet );
617
+ }
618
+
619
+ /**
620
+ * Tests that incoming styles are deduped and merged.
621
+ */
622
+ public function test_get_deduped_and_merged_stylesheet() {
623
+ $css_rules = array(
624
+ array(
625
+ 'selector' => '.gandalf',
626
+ 'declarations' => array(
627
+ 'color' => 'grey',
628
+ 'height' => '90px',
629
+ 'border-style' => 'dotted',
630
+ ),
631
+ ),
632
+ array(
633
+ 'selector' => '.gandalf',
634
+ 'declarations' => array(
635
+ 'color' => 'white',
636
+ 'height' => '190px',
637
+ 'padding' => '10px',
638
+ 'margin-bottom' => '100px',
639
+ ),
640
+ ),
641
+ array(
642
+ 'selector' => '.dumbledore',
643
+ 'declarations' => array(
644
+ 'color' => 'grey',
645
+ 'height' => '90px',
646
+ 'border-style' => 'dotted',
647
+ ),
648
+ ),
649
+ array(
650
+ 'selector' => '.rincewind',
651
+ 'declarations' => array(
652
+ 'color' => 'grey',
653
+ 'height' => '90px',
654
+ 'border-style' => 'dotted',
655
+ ),
656
+ ),
657
+ );
658
+
659
+ $compiled_stylesheet = wp_style_engine_get_stylesheet_from_css_rules( $css_rules );
660
+ $this->assertSame( '.gandalf{color:white;height:190px;border-style:dotted;padding:10px;margin-bottom:100px;}.dumbledore,.rincewind{color:grey;height:90px;border-style:dotted;}', $compiled_stylesheet );
661
+ }
461
662
  }
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 generate( style: Style, options: StyleOptions ): string {
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 ) => {