Skip to content

Commit 9b86cde

Browse files
committed
Only use placeholder comment for ensuring wp-block-library inline style is printed and ensure it is added just in time
1 parent 9607f0e commit 9b86cde

File tree

2 files changed

+53
-12
lines changed

2 files changed

+53
-12
lines changed

src/wp-includes/script-loader.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3644,12 +3644,18 @@ function wp_hoist_late_printed_styles() {
36443644
}
36453645

36463646
/*
3647-
* Print a placeholder comment where the late styles can be hoisted from the footer to be printed in the header
3648-
* by means of a filter below on the template enhancement output buffer.
3647+
* Add a placeholder comment into the inline styles for wp-block-library, after which where the late block styles
3648+
* can be hoisted from the footer to be printed in the header by means of a filter below on the template enhancement
3649+
* output buffer. The `wp_print_styles` action is used to ensure that if the inline style gets replaced at
3650+
* `enqueue_block_assets` or `wp_enqueue_scripts` that the placeholder will be sure to be present.
36493651
*/
3650-
$placeholder = sprintf( '/*%s*/', uniqid( 'wp_late_styles_placeholder:' ) );
3651-
3652-
wp_add_inline_style( 'wp-block-library', $placeholder );
3652+
$placeholder = sprintf( '/*%s*/', uniqid( 'wp_block_styles_on_demand_placeholder:' ) );
3653+
add_action(
3654+
'wp_print_styles',
3655+
static function () use ( $placeholder ) {
3656+
wp_add_inline_style( 'wp-block-library', $placeholder );
3657+
}
3658+
);
36533659

36543660
/*
36553661
* Create a substitute for `print_late_styles()` which is aware of block styles. This substitute does not print
@@ -3785,15 +3791,13 @@ public function remove() {
37853791
'STYLE' === $processor->get_tag() &&
37863792
'wp-block-library-inline-css' === $processor->get_attribute( 'id' )
37873793
) {
3788-
// If the inline style lacks the placeholder comment, then all the styles will be inserted below at </head>.
37893794
$css_text = $processor->get_modifiable_text();
3790-
if ( ! str_contains( $css_text, $placeholder ) ) {
3791-
continue;
3792-
}
37933795

37943796
/*
3795-
* Remove the placeholder now that we've located the inline style (and remove the inline style if it
3796-
* is now empty, aside from a sourceURL comment).
3797+
* A placeholder CSS comment is added to the inline style in order to force an inline STYLE tag to
3798+
* be printed. Now that we've located the inline style, the placeholder comment can be removed. If
3799+
* there is no CSS left in the STYLE tag after removing the placeholder (aside from the sourceURL
3800+
* comment, then remove the STYLE entirely.)
37973801
*/
37983802
$css_text = str_replace( $placeholder, '', $css_text );
37993803
if ( preg_match( ':^/\*# sourceURL=\S+? \*/$:', trim( $css_text ) ) ) {

tests/phpunit/tests/template.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1632,6 +1632,38 @@ function (): void {
16321632
),
16331633
),
16341634
),
1635+
'override_block_library_inline_style_late' => array(
1636+
'set_up' => static function () {
1637+
add_action(
1638+
'enqueue_block_assets',
1639+
function (): void {
1640+
// This tests what happens when the placeholder comment gets replaced unexpectedly.
1641+
wp_styles()->registered['wp-block-library']->extra['after'] = array( '/* OVERRIDDEN! */' );
1642+
}
1643+
);
1644+
},
1645+
'inline_size_limit' => 0,
1646+
'expected_styles' => array(
1647+
'HEAD' => array(
1648+
'wp-img-auto-sizes-contain-inline-css',
1649+
'early-css',
1650+
'early-inline-css',
1651+
'wp-emoji-styles-inline-css',
1652+
'wp-block-library-css',
1653+
'wp-block-library-inline-css', // This contains the "OVERRIDDEN" text.
1654+
'wp-block-separator-css',
1655+
'global-styles-inline-css',
1656+
'core-block-supports-inline-css',
1657+
'classic-theme-styles-css',
1658+
'normal-css',
1659+
'normal-inline-css',
1660+
'wp-custom-css',
1661+
'late-css',
1662+
'late-inline-css',
1663+
),
1664+
'BODY' => array(),
1665+
),
1666+
),
16351667
);
16361668
}
16371669

@@ -1721,12 +1753,17 @@ static function () {
17211753
// Create a simulated output buffer.
17221754
$buffer = '<html lang="en"><head><meta charset="utf-8">' . $head_output . '</head><body><main>' . $content . '</main>' . $footer_output . '</body></html>';
17231755

1756+
$placeholder_regexp = '#/\*wp_block_styles_on_demand_placeholder:[a-f0-9]+\*/#';
1757+
if ( has_action( 'wp_template_enhancement_output_buffer_started', 'wp_hoist_late_printed_styles' ) ) {
1758+
$this->assertMatchesRegularExpression( $placeholder_regexp, $buffer, 'Expected the placeholder to be present in the buffer.' );
1759+
}
1760+
17241761
// Apply the output buffer filter.
17251762
$filtered_buffer = apply_filters( 'wp_template_enhancement_output_buffer', $buffer );
17261763

17271764
$this->assertStringContainsString( '</head>', $filtered_buffer, 'Expected the closing HEAD tag to be in the response.' );
17281765

1729-
$this->assertDoesNotMatchRegularExpression( '#/\*wp_late_styles_placeholder:[a-f0-9]+\*/#', $filtered_buffer, 'Expected the placeholder to be removed.' );
1766+
$this->assertDoesNotMatchRegularExpression( $placeholder_regexp, $filtered_buffer, 'Expected the placeholder to be removed.' );
17301767
$found_styles = array(
17311768
'HEAD' => array(),
17321769
'BODY' => array(),

0 commit comments

Comments
 (0)