Skip to content
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

Fix invalid css for nested fullwidth layouts with zero padding applied #63436

Merged
merged 5 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions backport-changelog/6.6/7036.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/7036

* https://github.com/WordPress/gutenberg/pull/63436
12 changes: 10 additions & 2 deletions lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,22 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support
* They're added separately because padding might only be set on one side.
*/
if ( isset( $block_spacing_values['declarations']['padding-right'] ) ) {
$padding_right = $block_spacing_values['declarations']['padding-right'];
$padding_right = $block_spacing_values['declarations']['padding-right'];
// Add unit if 0.
if ( '0' === $padding_right ) {
Copy link
Member

@noisysocks noisysocks Jul 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we guaranteed to get a string back from gutenberg_style_engine_get_styles? I'm wondering if $padding_right could sometimes be 0 not '0'.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At a quick glance, the value is passed through the style engine's is_valid_style_value function. Which should exclude an integer 0.

		protected static function is_valid_style_value( $style_value ) {
			return '0' === $style_value || ! empty( $style_value );
		}

Copy link
Contributor

@andrewserong andrewserong Jul 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I don't think gutenberg_style_engine_get_styles will coerce the value for declarations from a quick look at the code, but padding values set by the UI will always be strings, and it's expected that values for spacing will be strings, I believe.

That said, this could be hardened in a follow-up to be if ( '0' === $padding_right || 0 === $padding_right ) { and similarly for the left padding.

Copy link
Contributor

@andrewserong andrewserong Jul 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I missed that one, Aaron! 👍

$padding_right = '0px';
}
$layout_styles[] = array(
'selector' => "$selector > .alignfull",
'declarations' => array( 'margin-right' => "calc($padding_right * -1)" ),
);
}
if ( isset( $block_spacing_values['declarations']['padding-left'] ) ) {
$padding_left = $block_spacing_values['declarations']['padding-left'];
$padding_left = $block_spacing_values['declarations']['padding-left'];
// Add unit if 0.
if ( '0' === $padding_left ) {
$padding_left = '0px';
}
$layout_styles[] = array(
'selector' => "$selector > .alignfull",
'declarations' => array( 'margin-left' => "calc($padding_left * -1)" ),
Expand Down
12 changes: 10 additions & 2 deletions packages/block-editor/src/layouts/constrained.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,23 @@ export default {
const paddingValues = getCSSRules( style );
paddingValues.forEach( ( rule ) => {
if ( rule.key === 'paddingRight' ) {
// Add unit if 0, to avoid calc(0 * -1) which is invalid.
const paddingRightValue =
rule.value === '0' ? '0px' : rule.value;

output += `
${ appendSelectors( selector, '> .alignfull' ) } {
margin-right: calc(${ rule.value } * -1);
margin-right: calc(${ paddingRightValue } * -1);
}
`;
} else if ( rule.key === 'paddingLeft' ) {
// Add unit if 0, to avoid calc(0 * -1) which is invalid.
const paddingLeftValue =
rule.value === '0' ? '0px' : rule.value;

output += `
${ appendSelectors( selector, '> .alignfull' ) } {
margin-left: calc(${ rule.value } * -1);
margin-left: calc(${ paddingLeftValue } * -1);
}
`;
}
Expand Down
Loading