Skip to content
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
1 change: 1 addition & 0 deletions backport-changelog/7.2/12674.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
https://github.com/WordPress/wordpress-develop/pull/12674

* https://github.com/WordPress/gutenberg/pull/80501
* https://github.com/WordPress/gutenberg/pull/81560
44 changes: 32 additions & 12 deletions lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,17 @@ function gutenberg_get_child_layout_style_rules( $selector, $child_layout, $pare
}
}

$column_start = $child_layout['columnStart'] ?? null;
$column_span = $child_layout['columnSpan'] ?? null;
/*
* Grid line numbers and spans are whole numbers. The editor stores them as numbers, but
* content saved by WordPress 6.3 to 6.6 stored them as numeric strings, and that
* migration only runs when a block is parsed in JavaScript, so the front end still sees
* strings. Accept any numeric value and cast it, and treat anything else as absent
* because it can't render as valid CSS.
*/
$column_start_attr = $child_layout['columnStart'] ?? null;
$column_start = is_numeric( $column_start_attr ) ? (int) $column_start_attr : null;
$column_span_attr = $child_layout['columnSpan'] ?? null;
$column_span = is_numeric( $column_span_attr ) ? (int) $column_span_attr : null;
if ( null === $viewport_overrides || $has_viewport_property_override( 'columnStart' ) || $has_viewport_property_override( 'columnSpan' ) ) {
if ( $column_start && $column_span ) {
$child_layout_declarations['grid-column'] = "$column_start / span $column_span";
Expand All @@ -392,8 +401,10 @@ function gutenberg_get_child_layout_style_rules( $selector, $child_layout, $pare
}
}

$row_start = $child_layout['rowStart'] ?? null;
$row_span = $child_layout['rowSpan'] ?? null;
$row_start_attr = $child_layout['rowStart'] ?? null;
$row_start = is_numeric( $row_start_attr ) ? (int) $row_start_attr : null;
$row_span_attr = $child_layout['rowSpan'] ?? null;
$row_span = is_numeric( $row_span_attr ) ? (int) $row_span_attr : null;
if ( null === $viewport_overrides || $has_viewport_property_override( 'rowStart' ) || $has_viewport_property_override( 'rowSpan' ) ) {
if ( $row_start && $row_span ) {
$child_layout_declarations['grid-row'] = "$row_start / span $row_span";
Expand Down Expand Up @@ -822,6 +833,15 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support
}
}
} elseif ( 'grid' === $layout_type ) {
/*
* Column and row counts are whole numbers, for the same reason as the grid line
* numbers in gutenberg_get_child_layout_style_rules().
*/
$column_count_attr = $layout_for_styles['columnCount'] ?? null;
$column_count = is_numeric( $column_count_attr ) ? (int) $column_count_attr : null;
$row_count_attr = $layout_for_styles['rowCount'] ?? null;
$row_count = is_numeric( $row_count_attr ) ? (int) $row_count_attr : null;

/*
* If the gap value is an array, we use the "left" value because it represents the vertical gap, which
* is the relevant one for computation of responsive grid columns.
Expand Down Expand Up @@ -867,32 +887,32 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support
* value for any of the grid properties.
*/
$should_output_grid_columns = null === $viewport_overrides || $has_viewport_property_override( 'minimumColumnWidth' ) || $has_viewport_property_override( 'columnCount' ) || $has_viewport_property_override( 'autoFit' );
$uses_gap_in_grid_columns = ! empty( $layout_for_styles['columnCount'] ) && ! empty( $layout_for_styles['minimumColumnWidth'] );
$uses_gap_in_grid_columns = ! empty( $column_count ) && ! empty( $layout_for_styles['minimumColumnWidth'] );
if ( $has_block_gap_override && $uses_gap_in_grid_columns ) {
$should_output_grid_columns = true;
}

$should_output_grid_rows = ( null === $viewport_overrides || $has_viewport_property_override( 'rowCount' ) ) && ! empty( $layout_for_styles['columnCount'] ) && ! empty( $layout_for_styles['rowCount'] );
$should_output_grid_rows = ( null === $viewport_overrides || $has_viewport_property_override( 'rowCount' ) ) && ! empty( $column_count ) && ! empty( $row_count );
$grid_declarations = array();

/* When enabled, columns stretch to fill the available space using
* `auto-fit`; otherwise empty tracks are preserved with `auto-fill`.
*/
$auto_placement = ! empty( $layout_for_styles['autoFit'] ) ? 'auto-fit' : 'auto-fill';

if ( $should_output_grid_columns && ! empty( $layout_for_styles['columnCount'] ) && ! empty( $layout_for_styles['minimumColumnWidth'] ) ) {
$max_value = 'max(min(' . $layout_for_styles['minimumColumnWidth'] . ', 100%), (100% - (' . $responsive_gap_value . ' * (' . $layout_for_styles['columnCount'] . ' - 1))) /' . $layout_for_styles['columnCount'] . ')';
if ( $should_output_grid_columns && ! empty( $column_count ) && ! empty( $layout_for_styles['minimumColumnWidth'] ) ) {
$max_value = 'max(min(' . $layout_for_styles['minimumColumnWidth'] . ', 100%), (100% - (' . $responsive_gap_value . ' * (' . $column_count . ' - 1))) /' . $column_count . ')';
$grid_declarations['grid-template-columns'] = 'repeat(' . $auto_placement . ', minmax(' . $max_value . ', 1fr))';
} elseif ( $should_output_grid_columns && ! empty( $layout_for_styles['columnCount'] ) ) {
$grid_declarations['grid-template-columns'] = 'repeat(' . $layout_for_styles['columnCount'] . ', minmax(0, 1fr))';
} elseif ( $should_output_grid_columns && ! empty( $column_count ) ) {
$grid_declarations['grid-template-columns'] = 'repeat(' . $column_count . ', minmax(0, 1fr))';
} elseif ( $should_output_grid_columns ) {
$minimum_column_width = ! empty( $layout_for_styles['minimumColumnWidth'] ) ? $layout_for_styles['minimumColumnWidth'] : '12rem';
$grid_declarations['grid-template-columns'] = 'repeat(' . $auto_placement . ', minmax(min(' . $minimum_column_width . ', 100%), 1fr))';
}

if ( ! empty( $grid_declarations ) ) {
$base_has_container_type = empty( $base_layout['columnCount'] ) || ( ! empty( $base_layout['columnCount'] ) && ! empty( $base_layout['minimumColumnWidth'] ) );
if ( empty( $layout_for_styles['columnCount'] ) || ! empty( $layout_for_styles['minimumColumnWidth'] ) ) {
if ( empty( $column_count ) || ! empty( $layout_for_styles['minimumColumnWidth'] ) ) {
if ( null === $viewport_overrides || ! $base_has_container_type ) {
$grid_declarations['container-type'] = 'inline-size';
}
Expand All @@ -906,7 +926,7 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support
if ( $should_output_grid_rows ) {
$layout_styles[] = array(
'selector' => $selector,
'declarations' => array( 'grid-template-rows' => 'repeat(' . $layout_for_styles['rowCount'] . ', minmax(1rem, auto))' ),
'declarations' => array( 'grid-template-rows' => 'repeat(' . $row_count . ', minmax(1rem, auto))' ),
);
}

Expand Down
121 changes: 121 additions & 0 deletions phpunit/block-supports/layout-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1414,4 +1414,125 @@ public function test_restore_group_inner_container_with_non_string_tag_name() {
'Group inner container restore should not fatal when tagName is not a string.'
);
}

/**
* Tests that non-numeric grid placement values are dropped rather than being
* interpolated into the `grid-column` and `grid-row` declarations.
*
* @covers ::gutenberg_get_child_layout_style_rules
*/
public function test_gutenberg_get_child_layout_style_rules_with_non_numeric_grid_placement() {
$actual_output = gutenberg_get_child_layout_style_rules(
'.wp-container-content-test',
array(
'columnStart' => array( 2 ),
'columnSpan' => array( 3 ),
'rowStart' => array( 1 ),
'rowSpan' => array( 2 ),
),
array(),
null
);

$this->assertSame(
array(),
$actual_output,
'Non-numeric grid placement values should not produce any child layout rules.'
);
}

/**
* Tests that grid placement values saved as numeric strings (WordPress 6.3 to 6.6)
* produce the same declarations as numbers.
*
* @covers ::gutenberg_get_child_layout_style_rules
*/
public function test_gutenberg_get_child_layout_style_rules_with_numeric_string_grid_placement() {
$expected_output = array(
array(
'selector' => '.wp-container-content-test',
'declarations' => array(
'grid-column' => '2 / span 3',
'grid-row' => '1 / span 2',
),
),
);

$actual_output = gutenberg_get_child_layout_style_rules(
'.wp-container-content-test',
array(
'columnStart' => '2',
'columnSpan' => '3',
'rowStart' => '1',
'rowSpan' => '2',
),
array( 'columnCount' => '3' ),
null
);

$this->assertSame( $expected_output, $actual_output );
}

/**
* Tests that non-numeric grid counts are treated as absent instead of leaking into the
* CSS. The rowCount case keeps columnCount valid, because the row track rule is only
* reached when there is a column count.
*
* @dataProvider data_gutenberg_get_layout_style_with_non_numeric_grid_counts
*
* @covers ::gutenberg_get_layout_style
*
* @param array $layout Grid layout values.
* @param string $expected_output The expected output.
*/
public function test_gutenberg_get_layout_style_with_non_numeric_grid_counts( $layout, $expected_output ) {
$this->assertSame( $expected_output, gutenberg_get_layout_style( '.wp-layout', $layout ) );
}

/**
* Data provider for test_gutenberg_get_layout_style_with_non_numeric_grid_counts().
*
* @return array
*/
public function data_gutenberg_get_layout_style_with_non_numeric_grid_counts() {
return array(
'non-numeric columnCount falls back to the responsive default' => array(
'layout' => array(
'type' => 'grid',
'columnCount' => array( 3 ),
),
'expected_output' => '.wp-layout{grid-template-columns:repeat(auto-fill, minmax(min(12rem, 100%), 1fr));container-type:inline-size;}',
),
'non-numeric rowCount drops the row track rule' => array(
'layout' => array(
'type' => 'grid',
'columnCount' => 3,
'rowCount' => array( 2 ),
),
'expected_output' => '.wp-layout{grid-template-columns:repeat(3, minmax(0, 1fr));}',
),
);
}

/**
* Tests that grid counts saved as numeric strings (WordPress 6.3 to 6.6) produce the
* same CSS as numbers.
*
* @covers ::gutenberg_get_layout_style
*/
public function test_gutenberg_get_layout_style_with_numeric_string_grid_counts() {
$layout_styles = gutenberg_get_layout_style(
'.wp-layout',
array(
'type' => 'grid',
'columnCount' => '3',
'rowCount' => '2',
)
);

$this->assertSame(
'.wp-layout{grid-template-columns:repeat(3, minmax(0, 1fr));grid-template-rows:repeat(2, minmax(1rem, auto));}',
$layout_styles
);
}
}
Loading