-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathclass-wp-style-engine-processor-test.php
396 lines (367 loc) · 12.1 KB
/
class-wp-style-engine-processor-test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<?php
/**
* Tests the Style Engine Processor class.
*
* @package Gutenberg
* @subpackage style-engine
*/
/**
* Tests for compiling and rendering styles from a store of CSS rules.
*
* @group style-engine
* @coversDefaultClass WP_Style_Engine_Processor_Gutenberg
*/
class WP_Style_Engine_Processor_Test extends WP_UnitTestCase {
/**
* Tests adding rules and returning compiled CSS rules.
*
* @covers ::add_rules
* @covers ::get_css
*/
public function test_should_return_rules_as_compiled_css() {
$a_nice_css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.a-nice-rule' );
$a_nice_css_rule->add_declarations(
array(
'color' => 'var(--nice-color)',
'background-color' => 'purple',
)
);
$a_nicer_css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.a-nicer-rule' );
$a_nicer_css_rule->add_declarations(
array(
'font-family' => 'Nice sans',
'font-size' => '1em',
'background-color' => 'purple',
)
);
$a_nice_processor = new WP_Style_Engine_Processor_Gutenberg();
$a_nice_processor->add_rules( array( $a_nice_css_rule, $a_nicer_css_rule ) );
$this->assertSame(
'.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_processor->get_css( array( 'prettify' => false ) )
);
}
/**
* Tests adding nested rules with at-rules and returning compiled CSS rules.
*
* @covers ::add_rules
* @covers ::get_css
*/
public function test_should_return_nested_rules_as_compiled_css() {
$a_nice_css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.a-nice-rule' );
$a_nice_css_rule->add_declarations(
array(
'color' => 'var(--nice-color)',
'background-color' => 'purple',
)
);
$a_nice_css_rule->set_rules_group( '@media (min-width: 80rem)' );
$a_nicer_css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.a-nicer-rule' );
$a_nicer_css_rule->add_declarations(
array(
'font-family' => 'Nice sans',
'font-size' => '1em',
'background-color' => 'purple',
)
);
$a_nicer_css_rule->set_rules_group( '@layer nicety' );
$a_nice_processor = new WP_Style_Engine_Processor_Gutenberg();
$a_nice_processor->add_rules( array( $a_nice_css_rule, $a_nicer_css_rule ) );
$this->assertSame(
'@media (min-width: 80rem){.a-nice-rule{color:var(--nice-color);background-color:purple;}}@layer nicety{.a-nicer-rule{font-family:Nice sans;font-size:1em;background-color:purple;}}',
$a_nice_processor->get_css( array( 'prettify' => false ) )
);
}
/**
* Tests compiling CSS rules and formatting them with new lines and indents.
*
* @covers ::get_css
*/
public function test_should_return_prettified_css_rules() {
$a_wonderful_css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.a-wonderful-rule' );
$a_wonderful_css_rule->add_declarations(
array(
'color' => 'var(--wonderful-color)',
'background-color' => 'orange',
)
);
$a_very_wonderful_css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.a-very_wonderful-rule' );
$a_very_wonderful_css_rule->add_declarations(
array(
'color' => 'var(--wonderful-color)',
'background-color' => 'orange',
)
);
$a_more_wonderful_css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.a-more-wonderful-rule' );
$a_more_wonderful_css_rule->add_declarations(
array(
'font-family' => 'Wonderful sans',
'font-size' => '1em',
'background-color' => 'orange',
)
);
$a_wonderful_processor = new WP_Style_Engine_Processor_Gutenberg();
$a_wonderful_processor->add_rules( array( $a_wonderful_css_rule, $a_very_wonderful_css_rule, $a_more_wonderful_css_rule ) );
$expected = '.a-wonderful-rule {
color: var(--wonderful-color);
background-color: orange;
}
.a-very_wonderful-rule {
color: var(--wonderful-color);
background-color: orange;
}
.a-more-wonderful-rule {
font-family: Wonderful sans;
font-size: 1em;
background-color: orange;
}
';
$this->assertSame(
$expected,
$a_wonderful_processor->get_css( array( 'prettify' => true ) )
);
}
/**
* Tests compiling nested CSS rules and formatting them with new lines and indents.
*
* @covers ::get_css
*/
public function test_should_return_prettified_nested_css_rules() {
$a_wonderful_css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.a-wonderful-rule' );
$a_wonderful_css_rule->add_declarations(
array(
'color' => 'var(--wonderful-color)',
'background-color' => 'orange',
)
);
$a_wonderful_css_rule->set_rules_group( '@media (min-width: 80rem)' );
$a_very_wonderful_css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.a-very_wonderful-rule' );
$a_very_wonderful_css_rule->add_declarations(
array(
'color' => 'var(--wonderful-color)',
'background-color' => 'orange',
)
);
$a_very_wonderful_css_rule->set_rules_group( '@layer wonderfulness' );
$a_wonderful_processor = new WP_Style_Engine_Processor_Gutenberg();
$a_wonderful_processor->add_rules( array( $a_wonderful_css_rule, $a_very_wonderful_css_rule ) );
$expected = '@media (min-width: 80rem) {
.a-wonderful-rule {
color: var(--wonderful-color);
background-color: orange;
}
}
@layer wonderfulness {
.a-very_wonderful-rule {
color: var(--wonderful-color);
background-color: orange;
}
}
';
$this->assertSame(
$expected,
$a_wonderful_processor->get_css( array( 'prettify' => true ) )
);
}
/**
* Tests adding a store and compiling CSS rules from that store.
*
* @covers ::add_store
*/
public function test_should_return_store_rules_as_css() {
$a_nice_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 'nice' );
$a_nice_store->add_rule( '.a-nice-rule' )->add_declarations(
array(
'color' => 'var(--nice-color)',
'background-color' => 'purple',
)
);
$a_nice_store->add_rule( '.a-nicer-rule' )->add_declarations(
array(
'font-family' => 'Nice sans',
'font-size' => '1em',
'background-color' => 'purple',
)
);
$a_nice_renderer = new WP_Style_Engine_Processor_Gutenberg();
$a_nice_renderer->add_store( $a_nice_store );
$this->assertSame(
'.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( array( 'prettify' => false ) )
);
}
/**
* Tests that CSS declarations are merged and deduped in the final CSS rules output.
*
* @covers ::add_rules
* @covers ::get_css
*/
public function test_should_dedupe_and_merge_css_declarations() {
$an_excellent_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.an-excellent-rule' );
$an_excellent_processor = new WP_Style_Engine_Processor_Gutenberg();
$an_excellent_rule->add_declarations(
array(
'color' => 'var(--excellent-color)',
'border-style' => 'dotted',
)
);
$an_excellent_processor->add_rules( $an_excellent_rule );
$another_excellent_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.an-excellent-rule' );
$another_excellent_rule->add_declarations(
array(
'color' => 'var(--excellent-color)',
'border-style' => 'dotted',
'border-color' => 'brown',
)
);
$an_excellent_processor->add_rules( $another_excellent_rule );
$this->assertSame(
'.an-excellent-rule{color:var(--excellent-color);border-style:dotted;border-color:brown;}',
$an_excellent_processor->get_css( array( 'prettify' => false ) ),
'Return value of get_css() does not match expectations with new, deduped and merged declarations.'
);
$yet_another_excellent_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.an-excellent-rule' );
$yet_another_excellent_rule->add_declarations(
array(
'color' => 'var(--excellent-color)',
'border-style' => 'dashed',
'border-width' => '2px',
)
);
$an_excellent_processor->add_rules( $yet_another_excellent_rule );
$this->assertSame(
'.an-excellent-rule{color:var(--excellent-color);border-style:dashed;border-color:brown;border-width:2px;}',
$an_excellent_processor->get_css( array( 'prettify' => false ) ),
'Return value of get_css() does not match expectations with deduped and merged declarations.'
);
}
/**
* Tests printing out 'unoptimized' CSS, that is, uncombined selectors and duplicate CSS rules.
* This is the default.
*
* @ticket 58811
*
* @covers ::get_css
*/
public function test_should_not_optimize_css_output() {
$a_sweet_rule = new WP_Style_Engine_CSS_Rule_Gutenberg(
'.a-sweet-rule',
array(
'color' => 'var(--sweet-color)',
'background-color' => 'purple',
)
);
$a_sweeter_rule = new WP_Style_Engine_CSS_Rule_Gutenberg(
'#an-even-sweeter-rule > marquee',
array(
'color' => 'var(--sweet-color)',
'background-color' => 'purple',
)
);
$the_sweetest_rule = new WP_Style_Engine_CSS_Rule_Gutenberg(
'.the-sweetest-rule-of-all a',
array(
'color' => 'var(--sweet-color)',
'background-color' => 'purple',
)
);
$a_sweet_processor = new WP_Style_Engine_Processor_Gutenberg();
$a_sweet_processor->add_rules( array( $a_sweet_rule, $a_sweeter_rule, $the_sweetest_rule ) );
$this->assertSame(
'.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;}',
$a_sweet_processor->get_css(
array(
'optimize' => false,
'prettify' => false,
)
)
);
}
/**
* Tests that 'optimized' CSS is output, that is, that duplicate CSS rules are combined under their corresponding selectors.
*
* @ticket 58811
*
* @covers ::get_css
*/
public function test_should_not_optimize_css_output_by_default() {
$a_sweet_rule = new WP_Style_Engine_CSS_Rule_Gutenberg(
'.a-sweet-rule',
array(
'color' => 'var(--sweet-color)',
'background-color' => 'purple',
)
);
$a_sweeter_rule = new WP_Style_Engine_CSS_Rule_Gutenberg(
'#an-even-sweeter-rule > marquee',
array(
'color' => 'var(--sweet-color)',
'background-color' => 'purple',
)
);
$a_sweet_processor = new WP_Style_Engine_Processor_Gutenberg();
$a_sweet_processor->add_rules( array( $a_sweet_rule, $a_sweeter_rule ) );
$this->assertSame(
'.a-sweet-rule{color:var(--sweet-color);background-color:purple;}#an-even-sweeter-rule > marquee{color:var(--sweet-color);background-color:purple;}',
$a_sweet_processor->get_css( array( 'prettify' => false ) )
);
}
/**
* Tests that incoming CSS rules are optimized and merged with existing CSS rules.
*
* @ticket 58811
*
* @covers ::add_rules
*/
public function test_should_combine_previously_added_css_rules() {
$a_lovely_processor = new WP_Style_Engine_Processor_Gutenberg();
$a_lovely_rule = new WP_Style_Engine_CSS_Rule_Gutenberg(
'.a-lovely-rule',
array(
'border-color' => 'purple',
)
);
$a_lovely_processor->add_rules( $a_lovely_rule );
$a_lovelier_rule = new WP_Style_Engine_CSS_Rule_Gutenberg(
'.a-lovelier-rule',
array(
'border-color' => 'purple',
)
);
$a_lovely_processor->add_rules( $a_lovelier_rule );
$this->assertSame(
'.a-lovely-rule,.a-lovelier-rule{border-color:purple;}',
$a_lovely_processor->get_css(
array(
'prettify' => false,
'optimize' => true,
)
),
'Return value of get_css() does not match expectations when combining 2 CSS rules'
);
$a_most_lovely_rule = new WP_Style_Engine_CSS_Rule_Gutenberg(
'.a-most-lovely-rule',
array(
'border-color' => 'purple',
)
);
$a_lovely_processor->add_rules( $a_most_lovely_rule );
$a_perfectly_lovely_rule = new WP_Style_Engine_CSS_Rule_Gutenberg(
'.a-perfectly-lovely-rule',
array(
'border-color' => 'purple',
)
);
$a_lovely_processor->add_rules( $a_perfectly_lovely_rule );
$this->assertSame(
'.a-lovely-rule,.a-lovelier-rule,.a-most-lovely-rule,.a-perfectly-lovely-rule{border-color:purple;}',
$a_lovely_processor->get_css(
array(
'prettify' => false,
'optimize' => true,
)
),
'Return value of get_css() does not match expectations when combining 4 CSS rules'
);
}
}