-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update custom CSS handling to be consistent with block global styles. #62357
Changes from all commits
453c9e6
6db7830
000d1c6
01a0e42
2dbc8aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
https://github.com/WordPress/wordpress-develop/pull/6750 | ||
|
||
* https://github.com/WordPress/gutenberg/pull/62357 |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -1355,6 +1355,12 @@ public function get_stylesheet( $types = array( 'variables', 'styles', 'presets' | |||
$stylesheet .= $this->get_preset_classes( $setting_nodes, $origins ); | ||||
} | ||||
|
||||
// Load the custom CSS last so it has the highest specificity. | ||||
if ( in_array( 'custom-css', $types, true ) ) { | ||||
// Add the global styles root CSS. | ||||
$stylesheet .= _wp_array_get( $this->theme_json, array( 'styles', 'css' ) ); | ||||
} | ||||
|
||||
return $stylesheet; | ||||
} | ||||
|
||||
|
@@ -1399,10 +1405,12 @@ protected function process_blocks_custom_css( $css, $selector ) { | |||
* Returns the global styles custom css. | ||||
* | ||||
* @since 6.2.0 | ||||
* @deprecated 6.7.0 Use {@see 'get_stylesheet'} instead. | ||||
* | ||||
* @return string The global styles custom CSS. | ||||
*/ | ||||
public function get_custom_css() { | ||||
_deprecated_function( __METHOD__, '6.7.0', 'get_stylesheet' ); | ||||
$block_custom_css = ''; | ||||
$block_nodes = $this->get_block_custom_css_nodes(); | ||||
foreach ( $block_nodes as $node ) { | ||||
|
@@ -1415,23 +1423,23 @@ public function get_custom_css() { | |||
|
||||
/** | ||||
* Returns the global styles base custom CSS. | ||||
* | ||||
* @since 6.6.0 | ||||
* This function is deprecated; please do not sync to core. | ||||
* | ||||
* @return string The global styles base custom CSS. | ||||
*/ | ||||
public function get_base_custom_css() { | ||||
_deprecated_function( __METHOD__, 'Gutenberg 18.6.0', 'get_stylesheet' ); | ||||
return isset( $this->theme_json['styles']['css'] ) ? $this->theme_json['styles']['css'] : ''; | ||||
} | ||||
|
||||
/** | ||||
* Returns the block nodes with custom CSS. | ||||
* | ||||
* @since 6.6.0 | ||||
* This function is deprecated; please do not sync to core. | ||||
* | ||||
* @return array The block nodes. | ||||
*/ | ||||
public function get_block_custom_css_nodes() { | ||||
_deprecated_function( __METHOD__, 'Gutenberg 18.6.0', 'get_block_nodes' ); | ||||
$block_nodes = array(); | ||||
|
||||
// Add the global styles block CSS. | ||||
|
@@ -1455,15 +1463,15 @@ public function get_block_custom_css_nodes() { | |||
|
||||
/** | ||||
* Returns the global styles custom CSS for a single block. | ||||
* | ||||
* @since 6.6.0 | ||||
* This function is deprecated; please do not sync to core. | ||||
* | ||||
* @param array $css The block css node. | ||||
* @param string $selector The block selector. | ||||
* | ||||
* @return string The global styles custom CSS for the block. | ||||
*/ | ||||
public function get_block_custom_css( $css, $selector ) { | ||||
_deprecated_function( __METHOD__, 'Gutenberg 18.6.0', 'get_styles_for_block' ); | ||||
return $this->process_blocks_custom_css( $css, $selector ); | ||||
} | ||||
|
||||
|
@@ -2646,6 +2654,7 @@ private static function get_block_nodes( $theme_json, $selectors = array() ) { | |||
'selectors' => $feature_selectors, | ||||
'duotone' => $duotone_selector, | ||||
'variations' => $variation_selectors, | ||||
'css' => $selector, | ||||
); | ||||
|
||||
if ( isset( $theme_json['styles']['blocks'][ $name ]['elements'] ) ) { | ||||
|
@@ -2855,6 +2864,11 @@ static function ( $pseudo_selector ) use ( $selector ) { | |||
$block_rules .= static::to_ruleset( ":root :where($style_variation_selector)", $individual_style_variation_declarations ); | ||||
} | ||||
|
||||
// 7. Generate and append any custom CSS rules. | ||||
if ( isset( $node['css'] ) && ! $is_root_selector ) { | ||||
$block_rules .= $this->process_blocks_custom_css( $node['css'], $selector ); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this line allows adding custom CSS declarations to the root selector in theme.json "styles": {
"css": "background-color:purple;"
}, I can see the background color on the frontend but not in the site editor Whereas in trunk only this works: "styles": {
"css": "body{background-color:purple;}"
}, Should Gutenberg allow both? Or is it a regression? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm that's a good question. This is how it was before #47396, but given that PR is over a year old it might be best to keep things as they were more recently. I'd expect to have to write complete rules in the top level custom CSS control and not necessarily for loose properties to be attached to the body element. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oooooh this is interesting; I think we've detected a bug in the existing behaviour. Testing in trunk, the loose properties are output outside any rule, so they don't apply because they're invalid, both in editor and front end: Whereas in this branch, those properties are nested under the root selector in the front end, though they're still invalid output in the editor. I think ideally we should fix things so they aren't output at all. I'll look into it! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok I fixed things so that this branch behaves like trunk (no valid CSS output if only properties are added to the top level custom CSS control) but the loose properties are still being output (just like they are on trunk). The problem with this is that any rule output straight after a loose property won't work. I can repro this in core trunk, and it's also a problem with the customizer additional CSS (which may not manifest in classic themes because additional CSS is enqueued after everything else, but on hybrid themes we're adding it before global styles custom CSS, so it can also break stuff) We could maybe just check that there aren't any loose properties at the end of the custom CSS string? Checking the whole string is valid CSS might be overkill as site admins should be responsible for the validity of their CSS. We could also Do Nothing 😄 and leave things as they are. If we do want to do anything though, it's probably better as a separate PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For sure. Thanks for looking into it.
Or maybe some light-weight parsing where the styles are added? gutenberg/lib/class-wp-theme-json-gutenberg.php Line 1361 in 01a0e42
|
||||
} | ||||
|
||||
return $block_rules; | ||||
} | ||||
|
||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So these never made it to Core, right? Should we remove the
@since
annotations and maybe add a "don't backport note"?Doesn't have to be here, just wondering if it'll help later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh good point, we should remove the
@since
on these!