Commit 278bcc6fc06 for woocommerce
commit 278bcc6fc06589df52ae016d8071fb357fec0ef9
Author: Allison Levine <1689238+allilevine@users.noreply.github.com>
Date: Tue Dec 1 09:47:13 2026 -0500
[Email Editor] Fix oversized images in Media & Text blocks widening emails (#71054)
* fix(email-editor): cap Media & Text images at their column width
The Media & Text renderer kept the image's saved width (often 1024px) inside
a column of about 300px. Outlook ignores max-width and other clients size the
table by the width attribute, so the email grew past its layout and Gmail on
Android shrank the whole message, leaving tiny text.
Size the image to the media cell (block email width, less the block's own
padding and border, times mediaWidth, less cell padding), scale its height,
and add max-width/width:100% so it still fills the cell when stacked.
Move the theme variables map parsing into Preset_Variable_Resolver so the
renderer can resolve preset padding without a Theme_Controller.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(email-editor): size Media & Text images from attachment metadata
Core saves the media image without width/height. wp_filter_content_tags()
adds them from the attachment metadata, but only after do_blocks() has run
the Media & Text renderer, so the cap never saw a width and real sends still
came out at width="1024". Look the dimensions up the same way core does when
the image has none.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* refactor(email-editor): share image height scaling between renderers
Move Image::scale_height_attribute() to Html_Processing_Helper so the
Media & Text cap uses the same Outlook height rule. Correct the cap's
docblock (core adds the width later; it isn't saved), and note at the width
preprocessor that Media & Text relies on email_attrs width including the
block's own padding and border.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(email-editor): convert rem and em block padding to px for the Media & Text cap
parse_value() drops the unit, so 2rem padding was taken off as 2px and the
image could still overflow its cell.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(email-editor): resolve percentage padding against the block width for the Media & Text cap
convert_to_px() scales % against a 16px base, so 10% padding counted as
2px. CSS resolves percentage padding against the containing block's width,
so use the block's email width instead.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Fit Media & Text images of unknown size to their column
An image inserted from a URL has no attachment to read its size from,
so it kept no width and Outlook showed it at its natural size. It now
fills the media cell as it does in the editor, and any lone height is
dropped since there's no width to scale it against.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Measure Media & Text images from a URL before filling the cell
Before falling back to the cell width, read the size from a `w` query
parameter or from the file when the URL is in the uploads directory, as
the Image renderer does. Small images there keep their natural size
instead of being stretched. Both lookups move to Html_Processing_Helper
so the two renderers share them.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Pin looked-up image sizes and ignore query strings on uploads
When a Media & Text image's size comes from a lookup and fits the cell,
write it to the width and height attributes. Outlook reads only those,
so a host that ignores the `w` parameter can't widen the email.
Strip the query string and fragment from an uploads URL before reading
the file, so `image.png?ver=1` is measured instead of skipped.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Always fill the Media & Text cell with the media image
Core's Media & Text CSS sets the image to `width: 100%`, so set the email
image's width to the media cell width whatever its size, and scale the
height only when both saved dimensions are known. Small library and
external images now render the same, as they do on the site.
This drops the attachment, `w` query and file lookups from Media & Text,
so the `w` query and file lookups go back to being private to the Image
renderer. The Image renderer still strips the query string before
looking for the file in the uploads directory.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Fold the Media & Text sizing tests into data providers
Hard-code the spacing preset's expected width instead of resolving it the
way the renderer does, and drop the test without an email width, which the
width preprocessor always sets and other tests already cover.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Simplify the per-side border read and assert a removed height is absent
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Cap the Media & Text image when its cell is narrower than its padding
A media cell with no room left inside its 10px padding returned the image
unchanged, so a 1024px image kept its width attribute and widened the email
in clients that ignore max-width. Cap it at the cell's full width instead.
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-985-media-text-image-width b/packages/php/email-editor/changelog/fix-nl-985-media-text-image-width
new file mode 100644
index 00000000000..67502250ba9
--- /dev/null
+++ b/packages/php/email-editor/changelog/fix-nl-985-media-text-image-width
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Keep Media & Text images within their column so emails no longer render too wide or with tiny text in Gmail on Android.
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 88ca4483518..bb7c6c02f7a 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
@@ -109,6 +109,7 @@ class Blocks_Width_Preprocessor implements Preprocessor {
$modified_styles['spacing']['padding']['left'] = $block_padding_left;
$modified_styles['spacing']['padding']['right'] = $block_padding_right;
+ // Includes the block's own padding and border. Media_Text::get_horizontal_box_width() relies on that.
$block['email_attrs']['width'] = "{$width}px";
$block['innerBlocks'] = $this->calculate_widths( $block['innerBlocks'], $modified_layout, $modified_styles, $variables_map );
$parsed_blocks[ $key ] = $block;
diff --git a/packages/php/email-editor/src/Engine/Renderer/ContentRenderer/class-preset-variable-resolver.php b/packages/php/email-editor/src/Engine/Renderer/ContentRenderer/class-preset-variable-resolver.php
index c3d4e9cbac6..a5bfdcf42ad 100644
--- a/packages/php/email-editor/src/Engine/Renderer/ContentRenderer/class-preset-variable-resolver.php
+++ b/packages/php/email-editor/src/Engine/Renderer/ContentRenderer/class-preset-variable-resolver.php
@@ -8,6 +8,8 @@
declare(strict_types = 1);
namespace Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer;
+use WP_Theme_JSON;
+
/**
* Resolves WordPress preset variable references to their actual values.
*
@@ -63,6 +65,25 @@ class Preset_Variable_Resolver {
return $variables_map[ $css_var_name ] ?? $value;
}
+ /**
+ * Build the map of CSS variable names to values that a theme defines.
+ *
+ * @param WP_Theme_JSON $theme Theme to read the variables from.
+ * @return array<string, string> Map such as `array( '--wp--preset--spacing--20' => '20px' )`.
+ */
+ public static function get_variables_map( WP_Theme_JSON $theme ): array {
+ $variables_css = $theme->get_stylesheet( array( 'variables' ) );
+ $map = array();
+
+ if ( preg_match_all( '/--(.*?):\s*(.*?);/', $variables_css, $matches, PREG_SET_ORDER ) ) {
+ foreach ( $matches as $match ) {
+ $map[ '--' . $match[1] ] = $match[2];
+ }
+ }
+
+ return $map;
+ }
+
/**
* Convert a preset variable reference to CSS var() syntax.
*
diff --git a/packages/php/email-editor/src/Engine/class-theme-controller.php b/packages/php/email-editor/src/Engine/class-theme-controller.php
index aaf523fce8d..e8ac66703f2 100644
--- a/packages/php/email-editor/src/Engine/class-theme-controller.php
+++ b/packages/php/email-editor/src/Engine/class-theme-controller.php
@@ -304,18 +304,6 @@ class Theme_Controller {
* @return array
*/
public function get_variables_values_map(): array {
- $variables_css = $this->get_theme()->get_stylesheet( array( 'variables' ) );
- $map = array();
- // Regular expression to match CSS variable definitions.
- $pattern = '/--(.*?):\s*(.*?);/';
-
- if ( preg_match_all( $pattern, $variables_css, $matches, PREG_SET_ORDER ) ) {
- foreach ( $matches as $match ) {
- // '--' . $match[1] is the variable name, $match[2] is the variable value.
- $map[ '--' . $match[1] ] = $match[2];
- }
- }
-
- return $map;
+ return Preset_Variable_Resolver::get_variables_map( $this->get_theme() );
}
}
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 ebf2e5e5a4a..763a4a0d5ba 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
@@ -175,7 +175,8 @@ class Image extends Abstract_Block_Renderer {
}
$base_dir = realpath( $upload_dir['basedir'] );
- $image_path = realpath( $upload_dir['basedir'] . '/' . substr( $image_url, strlen( $base_url ) ) );
+ $relative = (string) strtok( substr( $image_url, strlen( $base_url ) ), '?#' );
+ $image_path = realpath( $upload_dir['basedir'] . '/' . $relative );
if ( false === $base_dir || false === $image_path ) {
return null;
@@ -349,7 +350,7 @@ class Image extends Abstract_Block_Renderer {
$width = Styles_Helper::parse_value( $block_width );
// A percentage can't be scaled against, and a fixed height is the author's choice.
if ( ! $has_fixed_height && strpos( $block_width, '%' ) === false ) {
- $this->scale_height_attribute( $html, $width );
+ Html_Processing_Helper::scale_height_attribute( $html, $width );
}
/* @phpstan-ignore-next-line Wrong annotation for parameter in WP. */
$html->set_attribute( 'width', esc_attr( $width ) );
@@ -421,35 +422,6 @@ class Image extends Abstract_Block_Renderer {
return $html->get_updated_html();
}
- /**
- * Scale the image's saved height attribute to the width the email renders it at.
- *
- * Outlook renders with Word's engine, which sizes an image from its width and height
- * attributes and ignores the inlined `height: auto`. A saved height left beside a rewritten
- * width therefore stretches the image, so the height is scaled by the same factor.
- *
- * The height is left alone when either saved dimension is missing or non-numeric, since there
- * is then no ratio to scale by.
- *
- * @param \WP_HTML_Tag_Processor $html Processor positioned on the img tag.
- * @param float $new_width Width in pixels the image is rendered at.
- */
- private function scale_height_attribute( \WP_HTML_Tag_Processor $html, float $new_width ): void {
- $saved_width = $html->get_attribute( 'width' );
- $saved_height = $html->get_attribute( 'height' );
-
- if ( ! is_string( $saved_width ) || ! is_numeric( $saved_width ) || ! is_string( $saved_height ) || ! is_numeric( $saved_height ) ) {
- return;
- }
-
- if ( (float) $saved_width <= 0 ) {
- return;
- }
-
- $scaled_height = max( 1, (int) round( (float) $saved_height * ( $new_width / (float) $saved_width ) ) );
- $html->set_attribute( 'height', esc_attr( (string) $scaled_height ) );
- }
-
/**
* Add core's caption class to the caption element when it is missing.
*
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 1246018f7d9..a841ffb21c8 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
@@ -8,7 +8,9 @@
declare( strict_types = 1 );
namespace Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks;
+use Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Preset_Variable_Resolver;
use Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Rendering_Context;
+use Automattic\WooCommerce\EmailEditor\Integrations\Utils\Html_Processing_Helper;
use Automattic\WooCommerce\EmailEditor\Integrations\Utils\Table_Wrapper_Helper;
use Automattic\WooCommerce\EmailEditor\Integrations\Utils\Styles_Helper;
use Automattic\WooCommerce\EmailEditor\Integrations\Utils\Dom_Document_Helper;
@@ -18,6 +20,11 @@ use Automattic\WooCommerce\EmailEditor\Integrations\Utils\Dom_Document_Helper;
* This renderer handles core/media-text blocks with proper email-friendly HTML layout.
*/
class Media_Text extends Abstract_Block_Renderer {
+ /**
+ * Padding in pixels on each side of the media cell.
+ */
+ private const MEDIA_CELL_PADDING = 10;
+
/**
* Renders the media-text block content using a direct table-based layout.
*
@@ -44,6 +51,8 @@ class Media_Text extends Abstract_Block_Renderer {
return '';
}
+ $media_content = $this->fit_media_image_to_cell( $media_content, $parsed_block, $rendering_context );
+
// Build the email-friendly layout.
return $this->build_email_layout( $media_content, $text_content, $block_attrs, $block_content, $rendering_context );
}
@@ -116,7 +125,7 @@ class Media_Text extends Abstract_Block_Renderer {
// Build individual table cells.
$media_cell_attrs = array(
- 'style' => sprintf( 'width: %d%%; padding: 10px; vertical-align: %s;', $media_width, $vertical_alignment ),
+ 'style' => sprintf( 'width: %d%%; padding: %dpx; vertical-align: %s;', $media_width, self::MEDIA_CELL_PADDING, $vertical_alignment ),
'valign' => $vertical_alignment,
);
$text_cell_attrs = array(
@@ -140,6 +149,100 @@ class Media_Text extends Abstract_Block_Renderer {
return Table_Wrapper_Helper::render_table_wrapper( $cells, $table_attrs, array(), array(), false );
}
+ /**
+ * Size the media image to fill the media cell, as core's Media & Text CSS (`width: 100%`) does.
+ *
+ * The width is written as pixels because Outlook ignores `max-width` and lays the image out at its
+ * saved width (often 1024px), which makes the email wider than its layout and Gmail on Android
+ * then shrinks the whole message to fit the screen.
+ *
+ * @param string $media_content Media HTML content.
+ * @param array $parsed_block Parsed block.
+ * @param Rendering_Context $rendering_context Rendering context.
+ * @return string Media HTML content.
+ */
+ private function fit_media_image_to_cell( string $media_content, array $parsed_block, Rendering_Context $rendering_context ): string {
+ if ( ! isset( $parsed_block['email_attrs']['width'] ) ) {
+ return $media_content;
+ }
+
+ $block_attrs = $parsed_block['attrs'] ?? array();
+ $email_width = Styles_Helper::parse_value( $parsed_block['email_attrs']['width'] );
+ $block_width = $email_width - $this->get_horizontal_box_width( $block_attrs, $email_width, $rendering_context );
+ $cell_width = (int) floor( $block_width * $this->get_media_width_from_attributes( $block_attrs ) / 100 );
+ $max_width = $cell_width - 2 * self::MEDIA_CELL_PADDING;
+ if ( $max_width <= 0 ) {
+ // The cell is too narrow for its padding. Cap at its full width so the image still can't widen the email.
+ $max_width = max( 1, $cell_width );
+ }
+
+ $html = new \WP_HTML_Tag_Processor( $media_content );
+ if ( ! $html->next_tag( array( 'tag_name' => 'img' ) ) ) {
+ return $media_content;
+ }
+
+ // 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 ) ) {
+ Html_Processing_Helper::scale_height_attribute( $html, $max_width );
+ } else {
+ $html->remove_attribute( 'height' );
+ }
+ $html->set_attribute( 'width', (string) $max_width );
+
+ $style = $html->get_attribute( 'style' );
+ $style = is_string( $style ) ? rtrim( trim( $style ), ';' ) . ';' : '';
+ $html->set_attribute( 'style', $style . sprintf( 'max-width:%dpx;width:100%%;height:auto;', $max_width ) );
+
+ return $html->get_updated_html();
+ }
+
+ /**
+ * Get the block's own left and right padding plus border width, in pixels.
+ *
+ * The block's email width includes these, but they're applied to the wrapper table, so the
+ * cells get what's left. Percentages resolve against the block's email width, as CSS padding does.
+ *
+ * @param array $block_attrs Block attributes.
+ * @param float $email_width Block's email width in pixels.
+ * @param Rendering_Context $rendering_context Rendering context.
+ * @return float
+ */
+ private function get_horizontal_box_width( array $block_attrs, float $email_width, Rendering_Context $rendering_context ): float {
+ $padding = $block_attrs['style']['spacing']['padding'] ?? array();
+ $border = $block_attrs['style']['border'] ?? array();
+ $padding = is_array( $padding ) ? $padding : array();
+ $border = is_array( $border ) ? $border : array();
+
+ $values = array( $padding['left'] ?? '0px', $padding['right'] ?? '0px' );
+ foreach ( array( 'left', 'right' ) as $side ) {
+ $side_border = is_array( $border[ $side ] ?? null ) ? $border[ $side ] : array();
+ $values[] = $side_border['width'] ?? $border['width'] ?? '0px';
+ }
+
+ $variables_map = null;
+ $total = 0.0;
+ foreach ( $values as $value ) {
+ if ( ! is_string( $value ) && ! is_numeric( $value ) ) {
+ continue;
+ }
+ $value = (string) $value;
+ if ( Preset_Variable_Resolver::is_preset_reference( $value ) ) {
+ $variables_map = $variables_map ?? Preset_Variable_Resolver::get_variables_map( $rendering_context->get_theme_json() );
+ $value = Preset_Variable_Resolver::resolve( $value, $variables_map );
+ }
+ if ( '%' === substr( trim( $value ), -1 ) ) {
+ $total += $email_width * Styles_Helper::parse_value( $value ) / 100;
+ continue;
+ }
+ $px = Styles_Helper::convert_to_px( $value, false );
+ $total += Styles_Helper::parse_value( $px ?? $value );
+ }
+
+ return $total;
+ }
+
/**
* Get the vertical alignment value from block attributes.
*
diff --git a/packages/php/email-editor/src/Integrations/Utils/class-html-processing-helper.php b/packages/php/email-editor/src/Integrations/Utils/class-html-processing-helper.php
index c4acba54bde..907c6f00f17 100644
--- a/packages/php/email-editor/src/Integrations/Utils/class-html-processing-helper.php
+++ b/packages/php/email-editor/src/Integrations/Utils/class-html-processing-helper.php
@@ -58,6 +58,35 @@ class Html_Processing_Helper {
return trim( $classes );
}
+ /**
+ * Scale the image's saved height attribute to the width the email renders it at.
+ *
+ * Outlook renders with Word's engine, which sizes an image from its width and height
+ * attributes and ignores the inlined `height: auto`. A saved height left beside a rewritten
+ * width therefore stretches the image, so the height is scaled by the same factor.
+ *
+ * The height is left alone when either saved dimension is missing or non-numeric, since there
+ * is then no ratio to scale by.
+ *
+ * @param \WP_HTML_Tag_Processor $html Processor positioned on the img tag.
+ * @param float $new_width Width in pixels the image is rendered at.
+ */
+ public static function scale_height_attribute( \WP_HTML_Tag_Processor $html, float $new_width ): void {
+ $saved_width = $html->get_attribute( 'width' );
+ $saved_height = $html->get_attribute( 'height' );
+
+ if ( ! is_string( $saved_width ) || ! is_numeric( $saved_width ) || ! is_string( $saved_height ) || ! is_numeric( $saved_height ) ) {
+ return;
+ }
+
+ if ( (float) $saved_width <= 0 ) {
+ return;
+ }
+
+ $scaled_height = max( 1, (int) round( (float) $saved_height * ( $new_width / (float) $saved_width ) ) );
+ $html->set_attribute( 'height', esc_attr( (string) $scaled_height ) );
+ }
+
/**
* Remove from an element the class names whose styles the renderer applies to the wrapping table cell.
*
diff --git a/packages/php/email-editor/tests/integration/Engine/Renderer/Snapshots/snapshots/core-blocks.html b/packages/php/email-editor/tests/integration/Engine/Renderer/Snapshots/snapshots/core-blocks.html
index 43381e3b930..a1f0e7124fc 100644
--- a/packages/php/email-editor/tests/integration/Engine/Renderer/Snapshots/snapshots/core-blocks.html
+++ b/packages/php/email-editor/tests/integration/Engine/Renderer/Snapshots/snapshots/core-blocks.html
@@ -698,7 +698,7 @@
<tbody>
<tr>
<td valign="middle" style="border-collapse: collapse; mso-table-lspace: 0; mso-table-rspace: 0; width: 50%; padding: 10px; vertical-align: middle;">
-<img src="https://example.com/media.jpg" alt="" style="border: 0; height: auto; -ms-interpolation-mode: bicubic; line-height: 100%; max-width: 100%; outline: none; text-decoration: none;">
+<img width="286" src="https://example.com/media.jpg" alt="" style="border: 0; -ms-interpolation-mode: bicubic; line-height: 100%; outline: none; text-decoration: none; max-width: 286px; width: 100%; height: auto;">
</td>
<td valign="middle" style="border-collapse: collapse; mso-table-lspace: 0; mso-table-rspace: 0; width: 50%; padding: 0 8%; vertical-align: middle;">
<!--[if mso | IE]>
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 a1c96b01d12..4c3567c4750 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
@@ -434,6 +434,38 @@ class Image_Test extends \Email_Editor_Integration_Test_Case {
$this->assertStringContainsString( 'width:100%;', $rendered );
}
+ /**
+ * An image in the uploads directory is measured from its file. The query string must be
+ * removed first, or the file isn't found and a small image is stretched to the column.
+ */
+ public function testItReadsTheWidthOfAnUploadedImageWhoseUrlHasAQueryString(): void {
+ if ( ! function_exists( 'imagecreatetruecolor' ) ) {
+ $this->markTestSkipped( 'GD is needed to create a test image.' );
+ }
+ $upload_dir = wp_upload_dir();
+ $path = trailingslashit( $upload_dir['path'] ) . 'image-test-query-string.png';
+ $image = imagecreatetruecolor( 120, 60 );
+ $this->assertNotFalse( $image );
+ imagepng( $image, $path );
+
+ $image_content = sprintf(
+ '<figure class="wp-block-image size-full"><img src="%s" alt=""/></figure>',
+ esc_url( trailingslashit( $upload_dir['url'] ) . 'image-test-query-string.png?ver=1' )
+ );
+ $parsed_image = $this->parsed_image;
+ unset( $parsed_image['attrs']['width'], $parsed_image['attrs']['id'] );
+ $parsed_image['email_attrs']['width'] = '600px';
+ $parsed_image['innerHTML'] = $image_content;
+
+ try {
+ $rendered = $this->image_renderer->render( $image_content, $parsed_image, $this->rendering_context );
+ } finally {
+ wp_delete_file( $path );
+ }
+
+ $this->assertSame( '120', $this->get_image_attribute( $rendered, 'width' ) );
+ }
+
/**
* Test it ignores invalid URL width parameters
*/
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 9e7c883f69b..3a26758404e 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
@@ -222,4 +222,197 @@ class Media_Text_Test extends \Email_Editor_Integration_Test_Case {
$this->assertStringContainsString( 'media-image.jpg', $rendered );
$this->assertStringNotContainsString( 'text-image.jpg', $rendered );
}
+
+ /**
+ * The image fills the media cell's inner width: the block's 612px times the media share, minus
+ * 10px of cell padding on each side. Its saved height is scaled to keep the aspect ratio.
+ *
+ * @dataProvider provide_saved_image_sizes
+ * @param int $width Saved image width.
+ * @param int $height Saved image height.
+ * @param int $media_width Media width share in percent.
+ * @param string $expected_width Expected width attribute.
+ * @param string $expected_height Expected height attribute.
+ */
+ public function testItFitsImageToMediaCell( int $width, int $height, int $media_width, string $expected_width, string $expected_height ): void {
+ $img = $this->render_and_get_img( $this->get_block_with_image( $width, $height, $media_width ) );
+
+ $this->assertSame( $expected_width, $img['width'] );
+ $this->assertSame( $expected_height, $img['height'] );
+ $this->assertStringContainsString( "max-width:{$expected_width}px;width:100%;height:auto;", $img['style'] );
+ }
+
+ /**
+ * Saved image sizes and media shares.
+ *
+ * @return array<string, array{0: int, 1: int, 2: int, 3: string, 4: string}>
+ */
+ public function provide_saved_image_sizes(): array {
+ return array(
+ // 612 * 50% - 20 = 286.
+ 'oversized image is capped' => array( 1200, 800, 50, '286', '191' ),
+ // Core's Media & Text CSS sets the image to `width: 100%`, so a small one fills the cell too.
+ 'small image is stretched' => array( 200, 100, 50, '286', '143' ),
+ // 612 * 30% - 20 = 163.
+ 'media share sets the cell size' => array( 1200, 800, 30, '163', '109' ),
+ );
+ }
+
+ /**
+ * A cell too narrow for its 10px padding on each side (120px * 15% = 18px) still caps the image,
+ * at the cell's full width, so a 1200px image can't widen the email.
+ */
+ public function testItCapsImageAtCellWidthWhenCellIsNarrowerThanItsPadding(): void {
+ $parsed_block = $this->get_block_with_image( 1200, 800, 15 );
+ $parsed_block['email_attrs']['width'] = '120px';
+
+ $img = $this->render_and_get_img( $parsed_block );
+
+ $this->assertSame( '18', $img['width'] );
+ $this->assertSame( '12', $img['height'] );
+ $this->assertStringContainsString( 'max-width:18px;width:100%;height:auto;', $img['style'] );
+ }
+
+ /**
+ * The block's own padding and border sit on the wrapper table, so they're taken off its 612px
+ * before the media share: ( 612 - padding and border ) * 50% - 20px.
+ *
+ * @dataProvider provide_block_box_styles
+ * @param array $style Block style attribute.
+ * @param string $expected_width Expected width attribute.
+ */
+ public function testItSubtractsBlockPaddingAndBorderFromCellWidth( array $style, string $expected_width ): void {
+ $parsed_block = $this->get_block_with_image( 1200, 800, 50 );
+ $parsed_block['attrs']['style'] = $style;
+
+ $img = $this->render_and_get_img( $parsed_block );
+
+ $this->assertSame( $expected_width, $img['width'] );
+ }
+
+ /**
+ * Block padding and border styles.
+ *
+ * @return array<string, array{0: array, 1: string}>
+ */
+ public function provide_block_box_styles(): array {
+ $padding = function ( string $value ): array {
+ return array(
+ 'spacing' => array(
+ 'padding' => array(
+ 'left' => $value,
+ 'right' => $value,
+ ),
+ ),
+ );
+ };
+
+ return array(
+ // ( 612 - 2 * 20px - 2 * 2px ) * 50% - 20 = 264.
+ 'px padding and border' => array( array_merge( $padding( '20px' ), array( 'border' => array( 'width' => '2px' ) ) ), '264' ),
+ // 2rem is 32px: ( 612 - 64 ) * 50% - 20 = 254.
+ 'rem padding' => array( $padding( '2rem' ), '254' ),
+ // 5% of the block's 612px is 30.6px: ( 612 - 61.2 ) * 50% - 20 = 255.
+ 'percentage padding' => array( $padding( '5%' ), '255' ),
+ // The email theme's spacing preset 20 is 24px: ( 612 - 48 ) * 50% - 20 = 262.
+ 'spacing preset padding' => array( $padding( 'var:preset|spacing|20' ), '262' ),
+ );
+ }
+
+ /**
+ * Core saves the media image without `width` and `height`, whether it's from the media library
+ * or another site, so it fills the cell whatever size the file is. A saved height alone can't be
+ * scaled, so it's dropped rather than distorting the image in Outlook.
+ *
+ * @dataProvider provide_images_without_saved_size
+ * @param string $img_attrs Attributes of the media image.
+ */
+ public function testItFillsCellWhenImageHasNoSavedSize( string $img_attrs ): void {
+ $attachment_id = self::factory()->attachment->create_object(
+ array(
+ 'file' => '2026/11/small.jpg',
+ 'post_mime_type' => 'image/jpeg',
+ )
+ );
+ $this->assertIsInt( $attachment_id );
+ wp_update_attachment_metadata(
+ $attachment_id,
+ array(
+ 'width' => 120,
+ 'height' => 80,
+ 'file' => '2026/11/small.jpg',
+ 'sizes' => array(),
+ )
+ );
+
+ $parsed_block = $this->get_block_with_image( 1, 1, 50 );
+ $parsed_block['attrs']['mediaId'] = $attachment_id;
+ $parsed_block['innerHTML'] = sprintf(
+ '<figure class="wp-block-media-text__media"><img %s alt=""/></figure><div class="wp-block-media-text__content"><p>Media content</p></div>',
+ str_replace(
+ array( '{library_url}', '{id}' ),
+ array( esc_url( (string) wp_get_attachment_url( $attachment_id ) ), (string) $attachment_id ),
+ $img_attrs
+ )
+ );
+
+ $img = $this->render_and_get_img( $parsed_block );
+
+ $this->assertSame( '286', $img['width'] );
+ $this->assertNull( $img['height'] );
+ $this->assertStringContainsString( 'max-width:286px;width:100%;height:auto;', $img['style'] );
+ }
+
+ /**
+ * Media images saved without a width.
+ *
+ * @return array<string, array{0: string}>
+ */
+ public function provide_images_without_saved_size(): array {
+ return array(
+ 'small library image' => array( 'src="{library_url}" class="wp-image-{id} size-full"' ),
+ '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"' ),
+ );
+ }
+
+ /**
+ * Build a media & text block whose media is an image with the given saved dimensions.
+ *
+ * @param int $width Saved image width.
+ * @param int $height Saved image height.
+ * @param int $media_width Media width share in percent.
+ * @return array
+ */
+ private function get_block_with_image( int $width, int $height, int $media_width ): array {
+ $parsed_block = $this->parsed_media;
+ $parsed_block['attrs']['mediaWidth'] = $media_width;
+ $parsed_block['email_attrs'] = array( 'width' => '612px' );
+ $parsed_block['innerHTML'] = sprintf(
+ '<figure class="wp-block-media-text__media"><img src="test-image.jpg" alt="Test Image" width="%d" height="%d" style="max-width:100%%" /></figure><div class="wp-block-media-text__content"><p>Media content</p></div>',
+ $width,
+ $height
+ );
+ return $parsed_block;
+ }
+
+ /**
+ * Render the block and return the media image's width, height and style attributes.
+ *
+ * @param array $parsed_block Parsed block.
+ * @return array{width: string, height: string|true|null, style: string}
+ */
+ private function render_and_get_img( array $parsed_block ): array {
+ $rendered = $this->media_renderer->render( '', $parsed_block, $this->rendering_context );
+
+ $processor = new \WP_HTML_Tag_Processor( $rendered );
+ $this->assertTrue( $processor->next_tag( array( 'tag_name' => 'img' ) ), 'Expected the media image in the rendered output.' );
+
+ return array(
+ 'width' => (string) $processor->get_attribute( 'width' ),
+ 'height' => $processor->get_attribute( 'height' ),
+ 'style' => (string) $processor->get_attribute( 'style' ),
+ );
+ }
}