Commit 19b4051ddbc for woocommerce

commit 19b4051ddbce37b1b091b960e942aa977ffc6c2c
Author: Allison Levine <1689238+allilevine@users.noreply.github.com>
Date:   Wed Nov 25 16:00:11 2026 -0500

    Email editor: shrink Columns blocks wider than 100% to fit the email (#71063)

    * fix(email-editor): shrink Columns blocks wider than 100% to fit the email

    Columns whose saved widths add up to more than the row (e.g. two columns
    both at 100%) kept their full widths, so the email rendered a row twice
    the content width. Outlook and desktop Apple Mail don't stack the columns
    and show the email too wide.

    Scale every column by the same factor when the row overflows, as
    flex-shrink does on the web. The shrunk width goes in
    email_attrs['column-width'], which sizes inner blocks and the column cell.
    Rows that fit render unchanged.

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

    * fix(email-editor): guard column shrinking against rows with no room

    Skip shrinking when the row width is zero or negative (a Columns block in
    a heavily padded parent), which produced negative widths and could divide
    by zero. Floor shrunk widths to whole pixels so the cell width attribute
    stays an integer and the row never exceeds its width.

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

    * Fix changelog type for email editor columns fix

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

    * Describe the zero-width column test in the present tense

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

    ---------

    Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

diff --git a/packages/php/email-editor/changelog/fix-nl-1192-overwide-columns b/packages/php/email-editor/changelog/fix-nl-1192-overwide-columns
new file mode 100644
index 00000000000..e3c209056f7
--- /dev/null
+++ b/packages/php/email-editor/changelog/fix-nl-1192-overwide-columns
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fit Columns blocks whose column widths add up to more than 100% within the email, instead of rendering too wide.
diff --git a/packages/php/email-editor/src/Engine/Renderer/ContentRenderer/Preprocessors/class-blocks-width-preprocessor.php b/packages/php/email-editor/src/Engine/Renderer/ContentRenderer/Preprocessors/class-blocks-width-preprocessor.php
index ecdd47394a0..88ca4483518 100644
--- a/packages/php/email-editor/src/Engine/Renderer/ContentRenderer/Preprocessors/class-blocks-width-preprocessor.php
+++ b/packages/php/email-editor/src/Engine/Renderer/ContentRenderer/Preprocessors/class-blocks-width-preprocessor.php
@@ -83,11 +83,8 @@ class Blocks_Width_Preprocessor implements Preprocessor {
 			// block instead of forcing the surrounding table to expand.
 			$block_border_horizontal = $this->get_block_horizontal_border( $block );

-			$width_input = $block['attrs']['width'] ?? '100%';
-			// Currently we support only % and px units in case only the number is provided we assume it's %
-			// because editor saves percent values as a number.
-			$width_input = is_numeric( $width_input ) ? "$width_input%" : $width_input;
-			$width_input = is_string( $width_input ) ? $width_input : '100%';
+			// A column shrunk to fit its row carries its shrunk width in email_attrs; the saved width would overflow.
+			$width_input = $this->normalize_width_input( $block['email_attrs']['column-width'] ?? $block['attrs']['width'] ?? '100%' );
 			$width       = $this->convert_width_to_pixels( $width_input, $layout_width );

 			if ( 'core/columns' === $block['blockName'] ) {
@@ -119,6 +116,21 @@ class Blocks_Width_Preprocessor implements Preprocessor {
 		return $parsed_blocks;
 	}

+	/**
+	 * Normalize a saved width to a string with a unit.
+	 *
+	 * Only % and px are supported. A bare number is treated as %, because the editor saves percent values as numbers.
+	 *
+	 * @param mixed $width Saved width.
+	 * @return string
+	 */
+	private function normalize_width_input( $width ): string {
+		if ( is_numeric( $width ) ) {
+			return "{$width}%";
+		}
+		return is_string( $width ) ? $width : '100%';
+	}
+
 	// TODO: We could add support for other units like em, rem, etc.
 	/**
 	 * Convert width to pixels
@@ -182,7 +194,7 @@ class Blocks_Width_Preprocessor implements Preprocessor {
 		foreach ( $columns as $column ) {
 			if ( isset( $column['attrs']['width'] ) && ! empty( $column['attrs']['width'] ) ) {
 				++$columns_count_with_defined_width;
-				$defined_column_width += $this->convert_width_to_pixels( $column['attrs']['width'], $columns_width );
+				$defined_column_width += $this->convert_width_to_pixels( $this->normalize_width_input( $column['attrs']['width'] ), $columns_width );
 			} else {
 				// When width is not set we need to add padding to the defined column width for better ratio accuracy.
 				$defined_column_width += $this->parse_number_from_string_with_pixels( Preset_Variable_Resolver::resolve( $column['attrs']['style']['spacing']['padding']['left'] ?? '0px', $variables_map ) );
@@ -195,6 +207,11 @@ class Blocks_Width_Preprocessor implements Preprocessor {

 		if ( $columns_count - $columns_count_with_defined_width > 0 ) {
 			$default_columns_width = round( ( $columns_width - $defined_column_width ) / ( $columns_count - $columns_count_with_defined_width ), 2 );
+			// When the defined widths already fill the row, give each unset column an equal share
+			// so it isn't zero or negative; shrink_overflowing_columns() then fits the row.
+			if ( $default_columns_width <= 0 ) {
+				$default_columns_width = round( $columns_width / $columns_count, 2 );
+			}
 			foreach ( $columns as $key => $column ) {
 				if ( ! isset( $column['attrs']['width'] ) || empty( $column['attrs']['width'] ) ) {
 					// Add padding to the specific column width because it's not included in the default width.
@@ -208,6 +225,39 @@ class Blocks_Width_Preprocessor implements Preprocessor {
 				}
 			}
 		}
+		return $this->shrink_overflowing_columns( $columns, $columns_width );
+	}
+
+	/**
+	 * Scale every column down by the same factor when their widths add up to more than the row.
+	 *
+	 * Matches flex-shrink on the web, e.g. two columns saved at 100% each become 50%.
+	 * The shrunk width goes in email_attrs['column-width']; rows that fit are left untouched.
+	 *
+	 * @param array $columns Columns, each with a width set.
+	 * @param float $columns_width Columns width.
+	 * @return array
+	 */
+	private function shrink_overflowing_columns( array $columns, float $columns_width ): array {
+		$pixel_widths = array();
+		foreach ( $columns as $key => $column ) {
+			$pixel_widths[ $key ] = $this->convert_width_to_pixels( $this->normalize_width_input( $column['attrs']['width'] ), $columns_width );
+		}
+		$total_width = array_sum( $pixel_widths );
+
+		// Percent widths are rounded to whole pixels, so allow half a pixel per column
+		// before treating the row as overflowing (e.g. 3 x 33.33% of 620px is 621px).
+		// A row with no room left (heavily padded parent) has nothing sensible to shrink to.
+		if ( $columns_width <= 0 || $total_width <= $columns_width + count( $columns ) * 0.5 ) {
+			return $columns;
+		}
+
+		$ratio = $columns_width / $total_width;
+		foreach ( $pixel_widths as $key => $pixel_width ) {
+			// Whole pixels keep the cell width attribute an integer; flooring keeps the row within its width.
+			$shrunk_width                                   = floor( $pixel_width * $ratio );
+			$columns[ $key ]['email_attrs']['column-width'] = "{$shrunk_width}px";
+		}
 		return $columns;
 	}
 }
diff --git a/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-column.php b/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-column.php
index 4077292f94a..865e62b3a4f 100644
--- a/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-column.php
+++ b/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-column.php
@@ -121,7 +121,7 @@ class Column extends Abstract_Block_Renderer {
 		$wrapper_cell_attrs = array(
 			'class' => $wrapper_classname,
 			'style' => $wrapper_styles['css'],
-			'width' => $this->get_width_attribute_value( $block_attributes['width'] ),
+			'width' => $this->get_width_attribute_value( $parsed_block['email_attrs']['column-width'] ?? $block_attributes['width'] ),
 		);

 		return Table_Wrapper_Helper::render_table_cell( $inner_table, $wrapper_cell_attrs );
diff --git a/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Column_Test.php b/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Column_Test.php
index dcd5f40a80c..b00a16a126e 100644
--- a/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Column_Test.php
+++ b/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Column_Test.php
@@ -205,6 +205,31 @@ class Column_Test extends \Email_Editor_Integration_Test_Case {
 		$this->assertStringNotContainsString( 'width="220px"', $rendered );
 	}

+	/**
+	 * Test it uses the shrunk width set by Blocks_Width_Preprocessor instead of the saved width.
+	 */
+	public function testItUsesShrunkColumnWidthFromEmailAttrs(): void {
+		$parsed_column                                = $this->parsed_column;
+		$parsed_column['attrs']['width']              = '100%';
+		$parsed_column['email_attrs']['column-width'] = '290px';
+		$rendered                                     = $this->column_renderer->render( '<p>Column content</p>', $parsed_column, $this->rendering_context );
+		$this->checkValidHTML( $rendered );
+		$this->assertStringContainsString( 'width="290"', $rendered );
+		$this->assertStringNotContainsString( 'width="100%"', $this->get_column_cell_tag( $rendered ) );
+	}
+
+	/**
+	 * Return the opening tag of the column's outer cell.
+	 *
+	 * @param string $rendered Rendered column.
+	 * @return string
+	 */
+	private function get_column_cell_tag( string $rendered ): string {
+		preg_match( '/<td[^>]*wp-block-column[^>]*>/', $rendered, $matches );
+		$this->assertNotEmpty( $matches, 'Column cell not found.' );
+		return $matches[0];
+	}
+
 	/**
 	 * Test it applies padding-left from email_attrs (set by Spacing_Preprocessor for columns blockGap)
 	 */
diff --git a/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Renderer_Test.php b/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Renderer_Test.php
index b233bc6c942..bff1d349846 100644
--- a/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Renderer_Test.php
+++ b/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Renderer_Test.php
@@ -23,6 +23,7 @@ class Renderer_Test extends \Email_Editor_Integration_Test_Case {
 		'core/columns',
 		'core/column',
 		'core/heading',
+		'core/image',
 		'core/paragraph',
 		'core/list',
 		'core/list-item',
@@ -287,6 +288,34 @@ class Renderer_Test extends \Email_Editor_Integration_Test_Case {
 		$this->assertStringContainsString( 'background-color: #000', $style ); // black is #000.
 	}

+	/**
+	 * Test columns whose saved widths add up to more than 100% are shrunk to fit the email, images included.
+	 */
+	public function testItShrinksColumnsThatAddUpToMoreThanTheEmailWidth(): void {
+		$column        = '<!-- wp:column {"width":"100%"} --><div class="wp-block-column" style="flex-basis:100%"><!-- wp:image {"sizeSlug":"large"} --><figure class="wp-block-image size-large"><img src="https://example.com/image.jpg" alt=""/></figure><!-- /wp:image --></div><!-- /wp:column -->';
+		$email_post_id = $this->factory->post->create(
+			array(
+				'post_content' => '<!-- wp:columns --><div class="wp-block-columns">' . $column . $column . '</div><!-- /wp:columns -->',
+			)
+		);
+		$this->assertIsInt( $email_post_id );
+		$email_post = get_post( $email_post_id );
+		$this->assertInstanceOf( WP_Post::class, $email_post );
+		$rendered = $this->renderer->render( $email_post, 'Subject', '', 'en' );
+
+		preg_match_all( '/<td[^>]*class="[^"]*wp-block-column[^"]*"[^>]*width="([^"]+)"/', $rendered['html'], $column_widths );
+		preg_match_all( '/<img(?=[^>]*example\.com\/image\.jpg)[^>]*width="(\d+)"/', $rendered['html'], $image_widths );
+		$this->assertCount( 2, $column_widths[1] );
+		$this->assertCount( 2, $image_widths[1] );
+
+		$this->assertMatchesRegularExpression( '/^\d+$/', $column_widths[1][0], 'The column cell declares the shrunk width in whole pixels, not the saved 100%.' );
+		$column_width = (float) $column_widths[1][0];
+		$this->assertSame( $column_widths[1][0], $column_widths[1][1] );
+		$this->assertLessThanOrEqual( 660, $column_width * 2, 'Both columns fit within the 660px email.' );
+		$this->assertLessThanOrEqual( $column_width, (float) $image_widths[1][0], 'The image is no wider than its column.' );
+		$this->assertLessThanOrEqual( $column_width, (float) $image_widths[1][1], 'The image is no wider than its column.' );
+	}
+
 	/**
 	 * Test a site palette color email clients can’t render is left out of the sent email.
 	 *
diff --git a/packages/php/email-editor/tests/unit/Engine/Renderer/ContentRenderer/Preprocessors/Blocks_Width_Preprocessor_Test.php b/packages/php/email-editor/tests/unit/Engine/Renderer/ContentRenderer/Preprocessors/Blocks_Width_Preprocessor_Test.php
index b00856072fa..af83786d765 100644
--- a/packages/php/email-editor/tests/unit/Engine/Renderer/ContentRenderer/Preprocessors/Blocks_Width_Preprocessor_Test.php
+++ b/packages/php/email-editor/tests/unit/Engine/Renderer/ContentRenderer/Preprocessors/Blocks_Width_Preprocessor_Test.php
@@ -681,4 +681,136 @@ class Blocks_Width_Preprocessor_Test extends \Email_Editor_Unit_Test {
 		$result = $this->preprocessor->preprocess( $blocks, $this->layout, $styles );
 		$this->assertEquals( '660px', $result[0]['email_attrs']['width'] ); // 100% of 660
 	}
+
+	/**
+	 * Test it shrinks two columns saved at 100% each to half the row
+	 */
+	public function testItShrinksColumnsThatAddUpToMoreThanTheRow(): void {
+		$result  = $this->preprocessor->preprocess( $this->get_columns_blocks( array( '100%', '100%' ) ), array( 'contentSize' => '580px' ), $this->styles );
+		$columns = $result[0]['innerBlocks'];
+
+		$this->assertEquals( '290px', $columns[0]['email_attrs']['column-width'] );
+		$this->assertEquals( '290px', $columns[0]['email_attrs']['width'] );
+		$this->assertEquals( '290px', $columns[0]['innerBlocks'][0]['email_attrs']['width'] );
+		$this->assertEquals( '290px', $columns[1]['email_attrs']['column-width'] );
+		$this->assertEquals( '290px', $columns[1]['email_attrs']['width'] );
+		$this->assertEquals( '290px', $columns[1]['innerBlocks'][0]['email_attrs']['width'] );
+		$this->assertEquals( '100%', $columns[0]['attrs']['width'], 'Saved attributes are left untouched.' );
+	}
+
+	/**
+	 * Test it shrinks three columns at 50% each to a third of the row
+	 */
+	public function testItShrinksThreeColumnsAtHalfWidthToThirds(): void {
+		$result  = $this->preprocessor->preprocess( $this->get_columns_blocks( array( '50%', '50%', '50%' ) ), array( 'contentSize' => '600px' ), $this->styles );
+		$columns = $result[0]['innerBlocks'];
+
+		$this->assertCount( 3, $columns );
+		foreach ( $columns as $column ) {
+			$this->assertEquals( '200px', $column['email_attrs']['column-width'] );
+			$this->assertEquals( '200px', $column['email_attrs']['width'] );
+		}
+	}
+
+	/**
+	 * Test it compares mixed px and % widths in pixels
+	 */
+	public function testItShrinksMixedPixelAndPercentColumns(): void {
+		$result  = $this->preprocessor->preprocess( $this->get_columns_blocks( array( '400px', '100%' ) ), array( 'contentSize' => '580px' ), $this->styles );
+		$columns = $result[0]['innerBlocks'];
+
+		// 400 + 580 = 980, scaled by 580 / 980 and floored to whole pixels.
+		$this->assertEquals( '236px', $columns[0]['email_attrs']['column-width'] );
+		$this->assertEquals( '343px', $columns[1]['email_attrs']['column-width'] );
+	}
+
+	/**
+	 * Test it leaves columns that fit the row unchanged
+	 */
+	public function testItDoesNotShrinkColumnsThatFit(): void {
+		$result  = $this->preprocessor->preprocess( $this->get_columns_blocks( array( '30%', '70%' ) ), array( 'contentSize' => '580px' ), $this->styles );
+		$columns = $result[0]['innerBlocks'];
+
+		$this->assertArrayNotHasKey( 'column-width', $columns[0]['email_attrs'] );
+		$this->assertArrayNotHasKey( 'column-width', $columns[1]['email_attrs'] );
+		$this->assertEquals( '174px', $columns[0]['email_attrs']['width'] );
+		$this->assertEquals( '406px', $columns[1]['email_attrs']['width'] );
+	}
+
+	/**
+	 * Test rounding percent widths to whole pixels doesn't count as overflowing
+	 */
+	public function testItDoesNotShrinkColumnsThatOverflowOnlyByRounding(): void {
+		$result  = $this->preprocessor->preprocess( $this->get_columns_blocks( array( '33.33%', '33.33%', '33.33%' ) ), array( 'contentSize' => '620px' ), $this->styles );
+		$columns = $result[0]['innerBlocks'];
+
+		$this->assertCount( 3, $columns );
+		foreach ( $columns as $column ) {
+			$this->assertArrayNotHasKey( 'column-width', $column['email_attrs'] );
+			$this->assertEquals( '207px', $column['email_attrs']['width'] );
+		}
+	}
+
+	/**
+	 * Test a column without a width next to a full-width column gets a positive share of the row
+	 */
+	public function testItGivesUnsetColumnAShareWhenDefinedColumnsFillTheRow(): void {
+		$result  = $this->preprocessor->preprocess( $this->get_columns_blocks( array( '100%', null ) ), array( 'contentSize' => '580px' ), $this->styles );
+		$columns = $result[0]['innerBlocks'];
+
+		// The unset column gets an equal share (290px), then 580 + 290 is scaled to fit 580 and floored.
+		$this->assertEquals( '386px', $columns[0]['email_attrs']['column-width'] );
+		$this->assertEquals( '193px', $columns[1]['email_attrs']['column-width'] );
+	}
+
+	/**
+	 * Test a row with no room left (padding wider than the parent) is left alone instead of shrunk to negative widths
+	 */
+	public function testItDoesNotShrinkColumnsInARowWithNoRoom(): void {
+		$blocks = $this->get_columns_blocks( array( '100px', '100px' ) );
+
+		$blocks[0]['attrs']['style']['spacing']['padding'] = array(
+			'left'  => '80px',
+			'right' => '80px',
+		);
+		$columns = $this->preprocessor->preprocess( $blocks, array( 'contentSize' => '100px' ), $this->styles )[0]['innerBlocks'];
+		$this->assertArrayNotHasKey( 'column-width', $columns[0]['email_attrs'] );
+		$this->assertEquals( '100px', $columns[0]['email_attrs']['width'] );
+
+		// Zero widths in that row would otherwise divide by zero.
+		$blocks[0]['innerBlocks'] = $this->get_columns_blocks( array( '0%', '0px' ) )[0]['innerBlocks'];
+		$columns                  = $this->preprocessor->preprocess( $blocks, array( 'contentSize' => '100px' ), $this->styles )[0]['innerBlocks'];
+		$this->assertArrayNotHasKey( 'column-width', $columns[0]['email_attrs'] );
+		$this->assertArrayNotHasKey( 'column-width', $columns[1]['email_attrs'] );
+	}
+
+	/**
+	 * Build a columns block with one paragraph per column.
+	 *
+	 * @param array<string|null> $widths Saved width per column; null leaves the width unset.
+	 * @return array
+	 */
+	private function get_columns_blocks( array $widths ): array {
+		$columns = array();
+		foreach ( $widths as $width ) {
+			$columns[] = array(
+				'blockName'   => 'core/column',
+				'attrs'       => null === $width ? array() : array( 'width' => $width ),
+				'innerBlocks' => array(
+					array(
+						'blockName'   => 'core/paragraph',
+						'attrs'       => array(),
+						'innerBlocks' => array(),
+					),
+				),
+			);
+		}
+		return array(
+			array(
+				'blockName'   => 'core/columns',
+				'attrs'       => array(),
+				'innerBlocks' => $columns,
+			),
+		);
+	}
 }