Commit 058f45d23dc for woocommerce
commit 058f45d23dc56028351f4d6c2ba002ca88a0a127
Author: Allison Levine <1689238+allilevine@users.noreply.github.com>
Date: Thu Dec 3 09:50:06 2026 -0500
Email editor: Cap resized images at their column width so Columns blocks keep their split (#71676)
* Email editor: Cap resized images at their column width
An Image block resized to a fixed px width kept that width in a narrower
column. Outlook sizes images from the width attribute and ignores
max-width, so the column split broke. The width preprocessor now caps a
px width at the space the block sits in, and the image renderer caps the
saved width at that space, scaling the height attribute and any fixed
height in the image style with it.
Also drop the saved height of a Media & Text image whose saved width is
0 or less, as for an unknown width.
* Email editor: Floor the capped image width and test percent widths in narrow columns
Column widths can be fractional, which would put a non-integer width on
the img and its wrapper table.
* Email editor: Scale a capped image's style only when the cap fires
Use the saved block width as the baseline, so a fixed height is scaled
even without a width in the style, non-pixel widths are left alone, and
a width that already fits a fractional column is not floored.
* Email editor: Leave column widths to the row and keep capped images fluid on phones
The px cap in the width preprocessor also shrank columns that take root
padding, leaving an unresized image in a full-width single column short
of the email width. Columns are already fitted to their row, so skip them.
A capped image without a fixed height now gets width:100% up to its
saved width in its style, so it fills a stacked column on phones. The
width attribute keeps the capped value for Outlook.
* Email editor: Keep the saved image width in one variable instead of two block copies
* Email editor: Edit a capped image's parsed style declarations instead of matching them with a regex
* Email editor: Explain why the width cap skips columns
diff --git a/packages/php/email-editor/changelog/fix-nl-1197-cap-resized-images b/packages/php/email-editor/changelog/fix-nl-1197-cap-resized-images
new file mode 100644
index 00000000000..febbd48141f
--- /dev/null
+++ b/packages/php/email-editor/changelog/fix-nl-1197-cap-resized-images
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Cap images resized wider than their column at the column width, so Columns blocks keep their split in emails.
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 bb7c6c02f7a..0790ea15954 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
@@ -86,6 +86,11 @@ class Blocks_Width_Preprocessor implements Preprocessor {
// 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 );
+ // A pixel width wider than the space the block sits in, like a resized image in a narrow column, is capped to fit.
+ // Columns keep the width their row gives them; one that takes root padding would otherwise shrink below it.
+ if ( 'core/column' !== $block['blockName'] && strpos( $width_input, 'px' ) !== false && $layout_width > 0 ) {
+ $width = min( $width, $layout_width );
+ }
if ( 'core/columns' === $block['blockName'] ) {
// Calculate width of the columns based on the layout width and padding.
diff --git a/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-image.php b/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-image.php
index 763a4a0d5ba..99cea077402 100644
--- a/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-image.php
+++ b/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-image.php
@@ -41,8 +41,13 @@ class Image extends Abstract_Block_Renderer {
$anchor_data_link_href = $parsed_html['anchor_data_link_href'];
$parsed_block = $this->add_image_size_when_missing( $parsed_block, $image_url );
+ $saved_width = $parsed_block['attrs']['width'];
+ $parsed_block = $this->cap_width_at_available_width( $parsed_block );
$image = $this->add_image_dimensions( $image, $parsed_block );
- $image = $this->apply_server_crop( $image, $parsed_block );
+ if ( $parsed_block['attrs']['width'] !== $saved_width ) {
+ $image = $this->scale_style_size( $image, Styles_Helper::parse_value( $saved_width ), Styles_Helper::parse_value( $parsed_block['attrs']['width'] ) );
+ }
+ $image = $this->apply_server_crop( $image, $parsed_block );
$image_with_wrapper = str_replace(
array( '{image_content}', '{caption_content}' ),
@@ -156,6 +161,28 @@ class Image extends Abstract_Block_Renderer {
return $parsed_block;
}
+ /**
+ * Cap a pixel width the author set on the block at the width available to the image.
+ *
+ * Outlook sizes an image from its width attribute and ignores `max-width`, so a resized image
+ * wider than its column would push the column split apart. Other units are left unchanged.
+ *
+ * @param array $parsed_block Parsed block.
+ */
+ private function cap_width_at_available_width( array $parsed_block ): array {
+ $block_width = is_scalar( $parsed_block['attrs']['width'] ?? null ) ? trim( (string) $parsed_block['attrs']['width'] ) : '';
+ if ( ! isset( $parsed_block['email_attrs']['width'] ) || ! preg_match( '/^\d+(?:\.\d+)?px$/', $block_width ) ) {
+ return $parsed_block;
+ }
+
+ $max_width = Styles_Helper::parse_value( $parsed_block['email_attrs']['width'] );
+ if ( $max_width >= 1 && Styles_Helper::parse_value( $block_width ) > $max_width ) {
+ // Whole pixels keep the img width attribute an integer.
+ $parsed_block['attrs']['width'] = floor( $max_width ) . 'px';
+ }
+ return $parsed_block;
+ }
+
/**
* Get the width of an image stored locally in the uploads directory.
*
@@ -361,6 +388,54 @@ class Image extends Abstract_Block_Renderer {
return $block_content;
}
+ /**
+ * Fit the size a resized image saves in its style to the capped width.
+ *
+ * Without a fixed height the image becomes fluid up to its saved width, so it fills a stacked column
+ * on phones while the capped wrapper and width attribute size it on desktop and in Outlook.
+ * A fixed pixel height is scaled with the width instead, so the image keeps its shape.
+ *
+ * @param string $block_content Image HTML.
+ * @param float $saved_width Width in pixels saved on the block, larger than the capped width.
+ * @param float $width Capped width in pixels.
+ */
+ private function scale_style_size( string $block_content, float $saved_width, float $width ): string {
+ $html = new \WP_HTML_Tag_Processor( $block_content );
+ if ( ! $html->next_tag( array( 'tag_name' => 'img' ) ) ) {
+ return $block_content;
+ }
+
+ /** @var string $style */ // phpcs:ignore Generic.Commenting.DocComment.MissingShort -- used for phpstan
+ $style = $html->get_attribute( 'style' ) ?? '';
+ $styles = Styles_Helper::parse_styles_to_array( $style );
+ $style_height = $styles['height'] ?? '';
+ if ( ! is_string( $style_height ) || ! str_ends_with( trim( $style_height ), 'px' ) ) {
+ unset( $styles['width'], $styles['max-width'], $styles['height'] );
+ $styles['width'] = '100%';
+ $styles['max-width'] = $saved_width . 'px';
+ $styles['height'] = 'auto';
+ $fluid_style = '';
+ foreach ( $styles as $property => $value ) {
+ $fluid_style .= $property . ':' . $value . ';';
+ }
+ $html->set_attribute( 'style', esc_attr( $fluid_style ) );
+ return $html->get_updated_html();
+ }
+
+ $height = max( 1, (int) round( Styles_Helper::parse_value( $style_height ) * $width / $saved_width ) );
+ $html->set_attribute( 'height', (string) $height );
+ $sizes = array( 'height' => $height );
+ $style_width = $styles['width'] ?? '';
+ if ( is_string( $style_width ) && str_ends_with( trim( $style_width ), 'px' ) ) {
+ $sizes['width'] = (int) $width;
+ }
+ foreach ( $sizes as $property => $value ) {
+ $style = (string) preg_replace( '/(^|;)(\s*)' . $property . '\s*:[^;]*/i', '${1}${2}' . $property . ':' . $value . 'px', $style );
+ }
+ $html->set_attribute( 'style', esc_attr( $style ) );
+ return $html->get_updated_html();
+ }
+
/**
* Swap a cropped image for a server-cropped file when an integration provides one.
*
diff --git a/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-media-text.php b/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-media-text.php
index a841ffb21c8..d770bd52588 100644
--- a/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-media-text.php
+++ b/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-media-text.php
@@ -184,7 +184,7 @@ class Media_Text extends Abstract_Block_Renderer {
// Without both saved dimensions there's no ratio to scale the height by, so drop it.
$saved_width = $html->get_attribute( 'width' );
$saved_height = $html->get_attribute( 'height' );
- if ( is_numeric( $saved_width ) && is_numeric( $saved_height ) ) {
+ if ( is_numeric( $saved_width ) && (float) $saved_width > 0 && is_numeric( $saved_height ) ) {
Html_Processing_Helper::scale_height_attribute( $html, $max_width );
} else {
$html->remove_attribute( 'height' );
diff --git a/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Image_Test.php b/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Image_Test.php
index 4c3567c4750..1d001bc35f6 100644
--- a/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Image_Test.php
+++ b/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Image_Test.php
@@ -11,6 +11,7 @@ namespace Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks;
use Automattic\WooCommerce\EmailEditor\Engine\Email_Editor;
use Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Rendering_Context;
use Automattic\WooCommerce\EmailEditor\Engine\Theme_Controller;
+use Automattic\WooCommerce\EmailEditor\Integrations\Utils\Styles_Helper;
/**
* Integration test for Image class
@@ -611,6 +612,129 @@ class Image_Test extends \Email_Editor_Integration_Test_Case {
$this->assertSame( '200', $this->get_image_attribute( $rendered, 'height' ) );
}
+ /**
+ * Test a width the author resized the image to is capped at the width available to it.
+ *
+ * Outlook sizes the image from its width attribute and ignores `max-width`, so an image wider
+ * than its column would squeeze the next column. A capped image is fluid up to its saved width in its
+ * style, so it fills a stacked column on phones. Widths that fit are kept.
+ *
+ * @dataProvider provide_resized_image_widths
+ * @param string $block_width Width saved on the block.
+ * @param string $available_width Width available to the image.
+ * @param string $expected_width Expected width attribute.
+ * @param string $expected_height Expected height attribute.
+ * @param string $expected_wrapper_width Expected width of the table wrapping the image.
+ * @param string $expected_style_width Expected width in the img style.
+ * @param string|null $expected_style_max_width Expected max-width in the img style.
+ */
+ public function testItCapsAResizedImageAtTheAvailableWidth( string $block_width, string $available_width, string $expected_width, string $expected_height, string $expected_wrapper_width, string $expected_style_width, ?string $expected_style_max_width ): void {
+ $image_content = sprintf(
+ '<figure class="wp-block-image size-large is-resized"><img src="https://test.com/wp-content/uploads/2023/05/image.jpg" alt="" width="1000" height="500" style="width:%s"/></figure>',
+ $block_width
+ );
+ $parsed_image = $this->parsed_image;
+ $parsed_image['attrs']['width'] = $block_width;
+ $parsed_image['email_attrs']['width'] = $available_width;
+ $parsed_image['innerHTML'] = $image_content;
+
+ $rendered = $this->image_renderer->render( $image_content, $parsed_image, $this->rendering_context );
+
+ $this->assertSame( $expected_width, $this->get_image_attribute( $rendered, 'width' ) );
+ $this->assertSame( $expected_height, $this->get_image_attribute( $rendered, 'height' ) );
+ $styles = Styles_Helper::parse_styles_to_array( (string) $this->get_image_attribute( $rendered, 'style' ) );
+ $this->assertSame( $expected_style_width, $styles['width'] ?? null );
+ $this->assertSame( $expected_style_max_width, $styles['max-width'] ?? null );
+ $html = new \WP_HTML_Tag_Processor( $rendered );
+ $this->assertTrue( $html->next_tag( array( 'class_name' => 'email-table-with-width' ) ) );
+ $this->assertSame( $expected_wrapper_width, $html->get_attribute( 'width' ) );
+ }
+
+ /**
+ * Test making a capped image fluid only replaces its size declarations, even when another value contains a size-like string.
+ */
+ public function testItKeepsOtherStylesWhenItMakesACappedImageFluid(): void {
+ $image_content = '<figure class="wp-block-image size-large is-resized"><img src="https://test.com/wp-content/uploads/2023/05/image.jpg" alt="" style="background-image:url(https://example.test/width:500px.png);width:500px"/></figure>';
+ $parsed_image = $this->parsed_image;
+ $parsed_image['attrs']['width'] = '500px';
+ $parsed_image['email_attrs']['width'] = '300px';
+ $parsed_image['innerHTML'] = $image_content;
+
+ $rendered = $this->image_renderer->render( $image_content, $parsed_image, $this->rendering_context );
+ $styles = Styles_Helper::parse_styles_to_array( (string) $this->get_image_attribute( $rendered, 'style' ) );
+
+ $this->assertSame( 'url(https://example.test/width:500px.png)', $styles['background-image'] ?? null );
+ $this->assertSame( '100%', $styles['width'] ?? null );
+ $this->assertSame( '500px', $styles['max-width'] ?? null );
+ }
+
+ /**
+ * Test a percentage width is left to the column even when its number is larger than the column's pixel width.
+ */
+ public function testItKeepsAPercentageWidthInANarrowColumn(): void {
+ $image_content = '<figure class="wp-block-image size-large is-resized"><img src="https://test.com/wp-content/uploads/2023/05/image.jpg" alt="" style="width:50%"/></figure>';
+ $parsed_image = $this->parsed_image;
+ $parsed_image['attrs']['width'] = '50%';
+ $parsed_image['email_attrs']['width'] = '40px';
+ $parsed_image['innerHTML'] = $image_content;
+
+ $rendered = $this->image_renderer->render( $image_content, $parsed_image, $this->rendering_context );
+
+ $html = new \WP_HTML_Tag_Processor( $rendered );
+ $this->assertTrue( $html->next_tag( array( 'class_name' => 'email-table-with-width' ) ) );
+ $this->assertSame( '50%', $html->get_attribute( 'width' ) );
+ }
+
+ /**
+ * Test a fixed height saved in the style of a resized image is scaled with the capped width, so the image keeps its shape.
+ *
+ * @dataProvider provide_fixed_height_styles
+ * @param string $style Style saved on the image.
+ * @param string|null $expected_style_width Expected width in the image style.
+ */
+ public function testItScalesAFixedHeightWithTheCappedWidth( string $style, ?string $expected_style_width ): void {
+ $image_content = sprintf( '<figure class="wp-block-image size-large is-resized"><img src="https://test.com/wp-content/uploads/2023/05/image.jpg" alt="" style="%s"/></figure>', $style );
+ $parsed_image = $this->parsed_image;
+ $parsed_image['attrs']['width'] = '500px';
+ $parsed_image['email_attrs']['width'] = '300px';
+ $parsed_image['innerHTML'] = $image_content;
+
+ $rendered = $this->image_renderer->render( $image_content, $parsed_image, $this->rendering_context );
+ $styles = Styles_Helper::parse_styles_to_array( (string) $this->get_image_attribute( $rendered, 'style' ) );
+
+ $this->assertSame( '300', $this->get_image_attribute( $rendered, 'width' ) );
+ // 400 * ( 300 / 500 ) = 240.
+ $this->assertSame( '240', $this->get_image_attribute( $rendered, 'height' ) );
+ $this->assertSame( $expected_style_width, $styles['width'] ?? null );
+ $this->assertSame( '240px', $styles['height'] ?? null );
+ }
+
+ /**
+ * Styles of a 500px image with a fixed 400px height.
+ *
+ * @return array<string, array{0: string, 1: string|null}>
+ */
+ public function provide_fixed_height_styles(): array {
+ return array(
+ 'width and height in the style' => array( 'width:500px;height:400px', '300px' ),
+ 'height only in the style' => array( 'height:400px', null ),
+ );
+ }
+
+ /**
+ * Widths saved on an image block and the width available to it.
+ *
+ * @return array<string, array{0: string, 1: string, 2: string, 3: string, 4: string, 5: string, 6: string|null}>
+ */
+ public function provide_resized_image_widths(): array {
+ return array(
+ 'wider than the column' => array( '500px', '300px', '300', '150', '300px', '100%', '500px' ),
+ 'narrower than the column' => array( '200px', '300px', '200', '100', '200px', '200px', null ),
+ 'wider than a fractional column' => array( '500px', '206.67px', '206', '103', '206px', '100%', '500px' ),
+ 'as wide as a fractional column' => array( '200.5px', '200.5px', '200.5', '100', '200.5px', '200.5px', null ),
+ );
+ }
+
/**
* Test a fixed height in the image's style wins over the scaled height.
*/
diff --git a/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Media_Text_Test.php b/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Media_Text_Test.php
index 3a26758404e..90892c1693b 100644
--- a/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Media_Text_Test.php
+++ b/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Media_Text_Test.php
@@ -364,7 +364,7 @@ class Media_Text_Test extends \Email_Editor_Integration_Test_Case {
}
/**
- * Media images saved without a width.
+ * Media images saved without a usable width.
*
* @return array<string, array{0: string}>
*/
@@ -374,6 +374,7 @@ class Media_Text_Test extends \Email_Editor_Integration_Test_Case {
'external image' => array( 'src="https://example.com/remote-image.jpg"' ),
'external image with w query' => array( 'src="https://example.com/remote-image.jpg?w=150"' ),
'saved height only' => array( 'src="https://example.com/remote-image.jpg" height="800"' ),
+ 'zero saved width' => array( 'src="https://example.com/remote-image.jpg" width="0" height="800"' ),
);
}
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 af83786d765..81b97077089 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
@@ -763,6 +763,50 @@ class Blocks_Width_Preprocessor_Test extends \Email_Editor_Unit_Test {
$this->assertEquals( '193px', $columns[1]['email_attrs']['column-width'] );
}
+ /**
+ * Test a block with a pixel width gets no more than its column's width, so an image resized wider than its column can't push the row apart
+ */
+ public function testItCapsAPixelWidthAtTheColumnWidth(): void {
+ $blocks = $this->get_columns_blocks( array( '50%', '50%' ) );
+
+ $blocks[0]['innerBlocks'][0]['innerBlocks'] = array(
+ array(
+ 'blockName' => 'core/image',
+ 'attrs' => array( 'width' => '500px' ),
+ 'innerBlocks' => array(),
+ ),
+ );
+
+ $result = $this->preprocessor->preprocess( $blocks, array( 'contentSize' => '600px' ), $this->styles );
+
+ $this->assertSame( '300px', $result[0]['innerBlocks'][0]['innerBlocks'][0]['email_attrs']['width'] );
+ }
+
+ /**
+ * Test a column that takes root padding keeps the width its row gives it, so an image in a full-width single column fills the email
+ */
+ public function testItDoesNotCapAColumnThatTakesRootPadding(): void {
+ $blocks = $this->get_columns_blocks( array( null ) );
+
+ $blocks[0]['attrs']['align'] = 'full';
+ $blocks[0]['innerBlocks'][0]['email_attrs'] = array(
+ 'root-padding-left' => '24px',
+ 'root-padding-right' => '24px',
+ );
+ $blocks[0]['innerBlocks'][0]['innerBlocks'] = array(
+ array(
+ 'blockName' => 'core/image',
+ 'attrs' => array(),
+ 'innerBlocks' => array(),
+ ),
+ );
+
+ $column = $this->preprocessor->preprocess( $blocks, array( 'contentSize' => '600px' ), $this->styles )[0]['innerBlocks'][0];
+
+ $this->assertSame( '600px', $column['email_attrs']['width'] );
+ $this->assertSame( '600px', $column['innerBlocks'][0]['email_attrs']['width'] );
+ }
+
/**
* Test a row with no room left (padding wider than the parent) is left alone instead of shrunk to negative widths
*/