Skip to content

Commit 04a9ac7

Browse files
Add line breaks before/after Custom CSS for readability/debugging
Co-authored-by: Peter Wilson <[email protected]>
1 parent 5136870 commit 04a9ac7

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

src/js/_enqueues/wp/customize/preview.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@
645645
style = $( 'style#global-styles-inline-css' );
646646
value = value.replace( /\/\*(BEGIN|END)_CUSTOMIZER_CUSTOM_CSS\*\//g, '' ); // Forbid milestone comments from appearing in Custom CSS which would break live preview.
647647
var textContent = style.text().replace( /(\/\*BEGIN_CUSTOMIZER_CUSTOM_CSS\*\/)((?:.|\s)*?)(\/\*END_CUSTOMIZER_CUSTOM_CSS\*\/)/, function ( match, beforeComment, oldValue, afterComment ) {
648-
return beforeComment + value + afterComment;
648+
return beforeComment + '\n' + value + '\n' + afterComment;
649649
} );
650650
style.text( textContent );
651651
} else {

src/wp-includes/script-loader.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2542,19 +2542,25 @@ function wp_enqueue_global_styles() {
25422542
*/
25432543
remove_action( 'wp_head', 'wp_custom_css_cb', 101 );
25442544

2545-
// Get the custom CSS from the Customizer and add it to the global stylesheet.
2546-
$custom_css = wp_get_custom_css();
2547-
if ( is_customize_preview() ) {
2548-
/*
2549-
* When in the Customizer preview, wrap the Custom CSS in milestone comments to allow customize-preview.js
2550-
* to locate the CSS to replace for live previewing. Make sure that the milestone comments are omitted from
2551-
* the stored Custom CSS if by chance someone tried to add them, which would be highly unlikely, but it
2552-
* would break live previewing.
2553-
*/
2554-
$before_milestone = '/*BEGIN_CUSTOMIZER_CUSTOM_CSS*/';
2555-
$after_milestone = '/*END_CUSTOMIZER_CUSTOM_CSS*/';
2556-
$custom_css = str_replace( array( $before_milestone, $after_milestone ), '', $custom_css );
2557-
$custom_css = $before_milestone . $custom_css . $after_milestone;
2545+
/*
2546+
* Get the custom CSS from the Customizer and add it to the global stylesheet.
2547+
* Always do this in Customizer preview for the sake of live preview since it be empty.
2548+
*/
2549+
$custom_css = trim( wp_get_custom_css() );
2550+
if ( $custom_css || is_customize_preview() ) {
2551+
if ( is_customize_preview() ) {
2552+
/*
2553+
* When in the Customizer preview, wrap the Custom CSS in milestone comments to allow customize-preview.js
2554+
* to locate the CSS to replace for live previewing. Make sure that the milestone comments are omitted from
2555+
* the stored Custom CSS if by chance someone tried to add them, which would be highly unlikely, but it
2556+
* would break live previewing.
2557+
*/
2558+
$before_milestone = '/*BEGIN_CUSTOMIZER_CUSTOM_CSS*/';
2559+
$after_milestone = '/*END_CUSTOMIZER_CUSTOM_CSS*/';
2560+
$custom_css = str_replace( array( $before_milestone, $after_milestone ), '', $custom_css );
2561+
$custom_css = $before_milestone . "\n" . $custom_css . "\n" . $after_milestone;
2562+
}
2563+
$custom_css = "\n" . $custom_css;
25582564
}
25592565
$stylesheet .= $custom_css;
25602566

0 commit comments

Comments
 (0)