mirrored from git://develop.git.wordpress.org/
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
styles.php
457 lines (368 loc) · 14.7 KB
/
styles.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<?php
/**
* @group dependencies
* @group scripts
* @covers ::wp_enqueue_style
* @covers ::wp_register_style
* @covers ::wp_print_styles
* @covers ::wp_style_add_data
* @covers ::wp_add_inline_style
*/
class Tests_Dependencies_Styles extends WP_UnitTestCase {
private $old_wp_styles;
private $old_wp_scripts;
function setUp() {
parent::setUp();
if ( empty( $GLOBALS['wp_styles'] ) ) {
$GLOBALS['wp_styles'] = null;
}
$this->old_wp_styles = $GLOBALS['wp_styles'];
if ( empty( $GLOBALS['wp_scripts'] ) ) {
$GLOBALS['wp_scripts'] = null;
}
$this->old_wp_styles = $GLOBALS['wp_scripts'];
remove_action( 'wp_default_styles', 'wp_default_styles' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
$GLOBALS['wp_styles'] = new WP_Styles();
$GLOBALS['wp_styles']->default_version = get_bloginfo( 'version' );
$GLOBALS['wp_scripts'] = new WP_Scripts();
$GLOBALS['wp_scripts']->default_version = get_bloginfo( 'version' );
}
function tearDown() {
$GLOBALS['wp_styles'] = $this->old_wp_styles;
$GLOBALS['wp_scripts'] = $this->old_wp_scripts;
add_action( 'wp_default_styles', 'wp_default_styles' );
add_action( 'wp_print_styles', 'print_emoji_styles' );
if ( current_theme_supports( 'wp-block-styles' ) ) {
remove_theme_support( 'wp-block-styles' );
}
parent::tearDown();
}
/**
* Test versioning
*
* @ticket 11315
*/
function test_wp_enqueue_style() {
wp_enqueue_style( 'no-deps-no-version', 'example.com' );
wp_enqueue_style( 'no-deps-version', 'example.com', array(), 1.2 );
wp_enqueue_style( 'no-deps-null-version', 'example.com', array(), null );
wp_enqueue_style( 'no-deps-null-version-print-media', 'example.com', array(), null, 'print' );
$ver = get_bloginfo( 'version' );
$expected = "<link rel='stylesheet' id='no-deps-no-version-css' href='http://example.com?ver=$ver' type='text/css' media='all' />\n";
$expected .= "<link rel='stylesheet' id='no-deps-version-css' href='http://example.com?ver=1.2' type='text/css' media='all' />\n";
$expected .= "<link rel='stylesheet' id='no-deps-null-version-css' href='http://example.com' type='text/css' media='all' />\n";
$expected .= "<link rel='stylesheet' id='no-deps-null-version-print-media-css' href='http://example.com' type='text/css' media='print' />\n";
$this->assertSame( $expected, get_echo( 'wp_print_styles' ) );
// No styles left to print.
$this->assertSame( '', get_echo( 'wp_print_styles' ) );
}
/**
* @ticket 42804
*/
function test_wp_enqueue_style_with_html5_support_does_not_contain_type_attribute() {
add_theme_support( 'html5', array( 'style' ) );
$GLOBALS['wp_styles'] = new WP_Styles();
$GLOBALS['wp_styles']->default_version = get_bloginfo( 'version' );
wp_enqueue_style( 'no-deps-no-version', 'example.com' );
$ver = get_bloginfo( 'version' );
$expected = "<link rel='stylesheet' id='no-deps-no-version-css' href='http://example.com?ver=$ver' media='all' />\n";
$this->assertSame( $expected, get_echo( 'wp_print_styles' ) );
}
/**
* Test the different protocol references in wp_enqueue_style
*
* @global WP_Styles $wp_styles
* @ticket 16560
*/
public function test_protocols() {
// Init.
global $wp_styles;
$base_url_backup = $wp_styles->base_url;
$wp_styles->base_url = 'http://example.com/wordpress';
$expected = '';
$ver = get_bloginfo( 'version' );
// Try with an HTTP reference.
wp_enqueue_style( 'reset-css-http', 'http://yui.yahooapis.com/2.8.1/build/reset/reset-min.css' );
$expected .= "<link rel='stylesheet' id='reset-css-http-css' href='http://yui.yahooapis.com/2.8.1/build/reset/reset-min.css?ver=$ver' type='text/css' media='all' />\n";
// Try with an HTTPS reference.
wp_enqueue_style( 'reset-css-https', 'http://yui.yahooapis.com/2.8.1/build/reset/reset-min.css' );
$expected .= "<link rel='stylesheet' id='reset-css-https-css' href='http://yui.yahooapis.com/2.8.1/build/reset/reset-min.css?ver=$ver' type='text/css' media='all' />\n";
// Try with an automatic protocol reference (//).
wp_enqueue_style( 'reset-css-doubleslash', '//yui.yahooapis.com/2.8.1/build/reset/reset-min.css' );
$expected .= "<link rel='stylesheet' id='reset-css-doubleslash-css' href='//yui.yahooapis.com/2.8.1/build/reset/reset-min.css?ver=$ver' type='text/css' media='all' />\n";
// Try with a local resource and an automatic protocol reference (//).
$url = '//my_plugin/style.css';
wp_enqueue_style( 'plugin-style', $url );
$expected .= "<link rel='stylesheet' id='plugin-style-css' href='$url?ver=$ver' type='text/css' media='all' />\n";
// Try with a bad protocol.
wp_enqueue_style( 'reset-css-ftp', 'ftp://yui.yahooapis.com/2.8.1/build/reset/reset-min.css' );
$expected .= "<link rel='stylesheet' id='reset-css-ftp-css' href='{$wp_styles->base_url}ftp://yui.yahooapis.com/2.8.1/build/reset/reset-min.css?ver=$ver' type='text/css' media='all' />\n";
// Go!
$this->assertSame( $expected, get_echo( 'wp_print_styles' ) );
// No styles left to print.
$this->assertSame( '', get_echo( 'wp_print_styles' ) );
// Cleanup.
$wp_styles->base_url = $base_url_backup;
}
/**
* Test if inline styles work
*
* @ticket 24813
*/
public function test_inline_styles() {
$style = ".thing {\n";
$style .= "\tbackground: red;\n";
$style .= '}';
$expected = "<link rel='stylesheet' id='handle-css' href='http://example.com?ver=1' type='text/css' media='all' />\n";
$expected .= "<style id='handle-inline-css' type='text/css'>\n";
$expected .= "$style\n";
$expected .= "</style>\n";
wp_enqueue_style( 'handle', 'http://example.com', array(), 1 );
wp_add_inline_style( 'handle', $style );
// No styles left to print.
$this->assertSame( $expected, get_echo( 'wp_print_styles' ) );
}
/**
* Test if inline styles work with concatination
*
* @global WP_Styles $wp_styles
* @ticket 24813
*/
public function test_inline_styles_concat() {
global $wp_styles;
$wp_styles->do_concat = true;
$wp_styles->default_dirs = array( '/wp-admin/', '/wp-includes/css/' ); // Default dirs as in wp-includes/script-loader.php.
$style = ".thing {\n";
$style .= "\tbackground: red;\n";
$style .= '}';
$expected = "<link rel='stylesheet' id='handle-css' href='http://example.com?ver=1' type='text/css' media='all' />\n";
$expected .= "<style id='handle-inline-css' type='text/css'>\n";
$expected .= "$style\n";
$expected .= "</style>\n";
wp_enqueue_style( 'handle', 'http://example.com', array(), 1 );
wp_add_inline_style( 'handle', $style );
wp_print_styles();
$this->assertSame( $expected, $wp_styles->print_html );
}
/**
* Test if multiple inline styles work
*
* @ticket 24813
*/
public function test_multiple_inline_styles() {
$style1 = ".thing1 {\n";
$style1 .= "\tbackground: red;\n";
$style1 .= '}';
$style2 = ".thing2 {\n";
$style2 .= "\tbackground: blue;\n";
$style2 .= '}';
$expected = "<link rel='stylesheet' id='handle-css' href='http://example.com?ver=1' type='text/css' media='all' />\n";
$expected .= "<style id='handle-inline-css' type='text/css'>\n";
$expected .= "$style1\n";
$expected .= "$style2\n";
$expected .= "</style>\n";
wp_enqueue_style( 'handle', 'http://example.com', array(), 1 );
wp_add_inline_style( 'handle', $style1 );
wp_add_inline_style( 'handle', $style2 );
// No styles left to print.
$this->assertSame( $expected, get_echo( 'wp_print_styles' ) );
}
/**
* Test if a plugin doing it the wrong way still works
*
* @expectedIncorrectUsage wp_add_inline_style
* @ticket 24813
*/
public function test_plugin_doing_inline_styles_wrong() {
$style = "<style id='handle-inline-css' type='text/css'>\n";
$style .= ".thing {\n";
$style .= "\tbackground: red;\n";
$style .= "}\n";
$style .= '</style>';
$expected = "<link rel='stylesheet' id='handle-css' href='http://example.com?ver=1' type='text/css' media='all' />\n";
$expected .= "$style\n";
wp_enqueue_style( 'handle', 'http://example.com', array(), 1 );
wp_add_inline_style( 'handle', $style );
$this->assertSame( $expected, get_echo( 'wp_print_styles' ) );
}
/**
* Test to make sure <style> tags aren't output if there are no inline styles.
*
* @ticket 24813
*/
public function test_unnecessary_style_tags() {
$expected = "<link rel='stylesheet' id='handle-css' href='http://example.com?ver=1' type='text/css' media='all' />\n";
wp_enqueue_style( 'handle', 'http://example.com', array(), 1 );
$this->assertSame( $expected, get_echo( 'wp_print_styles' ) );
}
/**
* Test to make sure that inline styles attached to conditional
* stylesheets are also conditional.
*/
public function test_conditional_inline_styles_are_also_conditional() {
$expected = <<<CSS
<!--[if IE]>
<link rel='stylesheet' id='handle-css' href='http://example.com?ver=1' type='text/css' media='all' />
<style id='handle-inline-css' type='text/css'>
a { color: blue; }
</style>
<![endif]-->
CSS;
wp_enqueue_style( 'handle', 'http://example.com', array(), 1 );
wp_style_add_data( 'handle', 'conditional', 'IE' );
wp_add_inline_style( 'handle', 'a { color: blue; }' );
$this->assertSameIgnoreEOL( $expected, get_echo( 'wp_print_styles' ) );
}
/**
* Testing 'wp_register_style' return boolean success/failure value.
*
* @ticket 31126
*/
function test_wp_register_style() {
$this->assertTrue( wp_register_style( 'duplicate-handler', 'http://example.com' ) );
$this->assertFalse( wp_register_style( 'duplicate-handler', 'http://example.com' ) );
}
/**
* @ticket 35229
*/
function test_wp_add_inline_style_for_handle_without_source() {
$style = 'a { color: blue; }';
$expected = "<link rel='stylesheet' id='handle-one-css' href='http://example.com?ver=1' type='text/css' media='all' />\n";
$expected .= "<link rel='stylesheet' id='handle-two-css' href='http://example.com?ver=1' type='text/css' media='all' />\n";
$expected .= "<style id='handle-three-inline-css' type='text/css'>\n";
$expected .= "$style\n";
$expected .= "</style>\n";
wp_register_style( 'handle-one', 'http://example.com', array(), 1 );
wp_register_style( 'handle-two', 'http://example.com', array(), 1 );
wp_register_style( 'handle-three', false, array( 'handle-one', 'handle-two' ) );
wp_enqueue_style( 'handle-three' );
wp_add_inline_style( 'handle-three', $style );
$this->assertSame( $expected, get_echo( 'wp_print_styles' ) );
}
/**
* @ticket 35921
* @dataProvider data_styles_with_media
*/
function test_wp_enqueue_style_with_media( $expected, $media ) {
wp_enqueue_style( 'handle', 'http://example.com', array(), 1, $media );
$this->assertContains( $expected, get_echo( 'wp_print_styles' ) );
}
function data_styles_with_media() {
return array(
array(
"media='all'",
'all',
),
array(
"media='(orientation: portrait)'",
'(orientation: portrait)',
),
array(
"media='(max-width: 640px)'",
'(max-width: 640px)',
),
array(
"media='print and (min-width: 25cm)'",
'print and (min-width: 25cm)',
),
array(
"media='screen and (color), projection and (color)'",
'screen and (color), projection and (color)',
),
array(
"media='not screen and (color)'",
'not screen and (color)',
),
);
}
/**
* Tests that visual block styles are enqueued in the editor even when there is not theme support for 'wp-block-styles'.
*
* Visual block styles should always be enqueued when editing to avoid the appearance of a broken editor.
*
* @covers ::wp_enqueue_style
*/
function test_block_styles_for_editing_without_theme_support() {
// Confirm we are without theme support by default.
$this->assertFalse( current_theme_supports( 'wp-block-styles' ) );
wp_default_styles( $GLOBALS['wp_styles'] );
$this->assertFalse( wp_style_is( 'wp-block-library-theme' ) );
wp_enqueue_style( 'wp-edit-blocks' );
$this->assertTrue( wp_style_is( 'wp-block-library-theme' ) );
}
/**
* Tests that visual block styles are enqueued when there is theme support for 'wp-block-styles'.
*
* Visual block styles should always be enqueued when editing to avoid the appearance of a broken editor.
*
* @covers ::wp_common_block_scripts_and_styles
*/
function test_block_styles_for_editing_with_theme_support() {
add_theme_support( 'wp-block-styles' );
wp_default_styles( $GLOBALS['wp_styles'] );
$this->assertFalse( wp_style_is( 'wp-block-library-theme' ) );
wp_common_block_scripts_and_styles();
$this->assertTrue( wp_style_is( 'wp-block-library-theme' ) );
}
/**
* Tests that visual block styles are not enqueued for viewing when there is no theme support for 'wp-block-styles'.
*
* Visual block styles should not be enqueued unless a theme opts in.
* This way we avoid style conflicts with existing themes.
*
* @covers ::wp_enqueue_style
*/
function test_no_block_styles_for_viewing_without_theme_support() {
// Confirm we are without theme support by default.
$this->assertFalse( current_theme_supports( 'wp-block-styles' ) );
wp_default_styles( $GLOBALS['wp_styles'] );
$this->assertFalse( wp_style_is( 'wp-block-library-theme' ) );
wp_enqueue_style( 'wp-block-library' );
$this->assertFalse( wp_style_is( 'wp-block-library-theme' ) );
}
/**
* Tests that visual block styles are enqueued for viewing when there is theme support for 'wp-block-styles'.
*
* Visual block styles should be enqueued when a theme opts in.
*
* @covers ::wp_common_block_scripts_and_styles
*/
function test_block_styles_for_viewing_with_theme_support() {
add_theme_support( 'wp-block-styles' );
wp_default_styles( $GLOBALS['wp_styles'] );
$this->assertFalse( wp_style_is( 'wp-block-library-theme' ) );
wp_common_block_scripts_and_styles();
$this->assertTrue( wp_style_is( 'wp-block-library-theme' ) );
}
/**
* Tests that the main "style.css" file gets enqueued when the site doesn't opt in to separate core block assets.
*
* @ticket 50263
*
* @covers ::wp_default_styles
*/
function test_block_styles_for_viewing_without_split_styles() {
add_filter( 'separate_core_block_assets', '__return_false' );
wp_default_styles( $GLOBALS['wp_styles'] );
$this->assertSame(
'/' . WPINC . '/css/dist/block-library/style.css',
$GLOBALS['wp_styles']->registered['wp-block-library']->src
);
}
/**
* Tests that the "common.css" file gets enqueued when the site opts in to separate core block assets.
*
* @ticket 50263
*
* @covers ::wp_default_styles
*/
function test_block_styles_for_viewing_with_split_styles() {
add_filter( 'separate_core_block_assets', '__return_true' );
wp_default_styles( $GLOBALS['wp_styles'] );
$this->assertSame(
'/' . WPINC . '/css/dist/block-library/common.css',
$GLOBALS['wp_styles']->registered['wp-block-library']->src
);
}
}