Commit d1eb1a73468 for woocommerce
commit d1eb1a7346860423ff15f4512f2781a8c9b9e6e9
Author: Allison Levine <1689238+allilevine@users.noreply.github.com>
Date: Mon Nov 16 13:05:25 2026 -0500
fix: wrap long lines in code, verse and table blocks (#70892)
* fix: wrap long lines in code, verse and table blocks
A post with a code, verse or table block made the email scroll sideways. The
content stylesheet gives every <pre> white-space: pre-wrap, which breaks at a
space but never inside a word, so one long identifier or URL set the width of
the whole email. core/code and core/verse have no email renderer, so that rule
was the only wrapping they got.
core/table had a related problem from the other end: its renderer emits
width: 100% on an auto-layout table, and auto layout resolves to min-content
when min-content is wider. A cell holding an unbreakable word therefore pushed
the table past the 660px content width regardless of the declared 100%.
Add word-break: break-word to the pre reset and to the table cells the core/table
renderer writes. Breaking a word only when it does not fit leaves ordinary prose
and the line breaks a code block exists to keep untouched. core/preformatted
already had this on its wrapper cell via the Text renderer; this brings the rest
in line rather than introducing a new approach.
Verified by rendering an email with all four blocks through Renderer::render()
and measuring it in WebKit at a 700px viewport: 2342px wide before, 700px after,
with every element inside the 660px column.
Fixes NL-1007.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* fix: keep a floor under narrow table cells in emails
word-break: break-word lets a cell break mid-word, which is what stops a long
identifier widening the email. It also drops the cell's minimum width to a single
character, and auto table layout hands width out in proportion to content. A
short column sharing a row with a long one is therefore squeezed to that floor,
so a "Code" header renders one letter per line down the side of the table.
Add min-width: 4em to the cells the renderer writes. Measured through
Renderer::render() in WebKit, which is what iOS Gmail uses: the "Code" cell goes
from 29x114px to 81x42px at a 390px viewport, and the table still fits, 390px
document width either way. The same cell was already collapsing at 700px
(43x66px), so this was never only a phone problem.
A table wide enough that every column wants the floor can still exceed the
content width, but by a bounded amount rather than the 864px an unfloored table
reached in the same test.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
* fix: lower the table cell floor so short columns still fit
The 4em floor applied to every cell, so a table of short columns that fitted
before it existed no longer did. Measured through Renderer::render() in WebKit
at a 390px viewport, a five column table of one-character values went from 342px
to 454px, scrolling sideways — the symptom this branch set out to remove.
3em is the largest floor that leaves five columns inside the content width, and
it still keeps a short label readable beside a long unbreakable value: the "Code"
cell renders 65x42px rather than collapsing to 29x114px with no floor at all.
Raised by CodeRabbit on the pull request.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
* test: measure the cell floor against the width a phone gives the table
The regression assertion compared against the 612px desktop content width, which
the 4em floor it was written to reject clears comfortably: 5 columns at 4em comes
to 400px. The test passed with the exact value that caused the reported overflow,
so it guarded nothing.
Measure against 342px instead, the width left after the email's root padding on a
390px phone, and count the collapsed borders. 4em now computes 406px and fails,
which matches the 406px the table actually rendered at in WebKit. A second
assertion pins that threshold so the limit can't quietly stop biting.
Raised by CodeRabbit on the pull request.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
* fix: share the block wrapping rules with the editor
The cell rules were set inline by the table renderer, so the email wrapped a long
identifier and the editor did not. Move them, and the word-break for code and
verse, into content-shared.css, which both the editor and the renderer load.
The selector is the one core uses, `.wp-block-table` cells, so it reaches the
block's own cells in both places and leaves the layout tables an email is built
from alone. Core breaks words only on fixed-layout tables, where the page can
scroll the block instead; an email has nowhere to scroll, so every table needs it.
Measured at 342px, the width a 390px phone leaves the content: the table goes
from 846px, scrolling inside its block, to 262px, and the short column beside the
long one settles at 65px, the same width the email gives it.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
* docs: cite the core rule the table cell floor follows
Read against WordPress 7.1.4 rather than the copy in node_modules, which is a
different version: core pairs word-break with a min-width floor on the verse
block for the same reason a table cell needs one.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
* fix: give email tables a fixed layout instead of a per-cell minimum
A per-cell minimum accumulates: six short columns at 3em need 391px where a
390px phone, less the email's root padding, leaves 342px. No value escapes
that, because six content-sized short columns already fill 342px exactly, so
any floor above content size overflows.
Equal columns are the only arrangement that neither overflows, whatever the
column count, nor leaves a short label squeezed beside a long value until it
reads one letter per line. Core uses the same mechanism behind has-fixed-layout
and keeps auto layout elsewhere because overflow-x can scroll the block; an
email has nowhere to scroll.
Measured in the editor's mobile preview and through the renderer: two, six and
eight column tables all fit, with no horizontal scroll at any count. The cost is
that a short label now takes an equal share of the width.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
diff --git a/packages/php/email-editor/changelog/fix-nl-1007-wrap-code-table-verse b/packages/php/email-editor/changelog/fix-nl-1007-wrap-code-table-verse
new file mode 100644
index 00000000000..85725225fcd
--- /dev/null
+++ b/packages/php/email-editor/changelog/fix-nl-1007-wrap-code-table-verse
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Wrap long lines in code, verse and table blocks, and give table columns an equal share of the width, so an email no longer scrolls sideways.
diff --git a/packages/php/email-editor/src/Engine/content-shared.css b/packages/php/email-editor/src/Engine/content-shared.css
index 32eee0ecc02..6f3fe48ffb5 100644
--- a/packages/php/email-editor/src/Engine/content-shared.css
+++ b/packages/php/email-editor/src/Engine/content-shared.css
@@ -15,8 +15,27 @@ pre.has-background {
padding: 16px 24px;
}
+/* pre-wrap breaks at spaces, so a long identifier or URL would still widen the block */
pre {
font-family: 'Courier New', Courier, 'Lucida Sans Typewriter', 'Lucida Typewriter', monospace;
+ word-break: break-word;
+}
+
+/*
+ * Core breaks words only on fixed-layout tables, where overflow-x scrolls the block instead.
+ * An email has nowhere to scroll, so every table gets both. Equal columns are the only
+ * arrangement that neither overflows, whatever the column count, nor leaves a short label
+ * squeezed beside a long value until it reads one letter per line. The class sits on the
+ * table in the email and on the figure around it in the editor.
+ */
+table.wp-block-table,
+.wp-block-table > table {
+ table-layout: fixed;
+}
+
+.wp-block-table td,
+.wp-block-table th {
+ word-break: break-word;
}
/* With a light text weight such as 300, the browser's "bolder" gives 400 and looks like normal text. */
diff --git a/packages/php/email-editor/tests/integration/Engine/Renderer/Snapshots/fixtures/core-blocks.html b/packages/php/email-editor/tests/integration/Engine/Renderer/Snapshots/fixtures/core-blocks.html
index ce90132ee75..fe150ff3d17 100644
--- a/packages/php/email-editor/tests/integration/Engine/Renderer/Snapshots/fixtures/core-blocks.html
+++ b/packages/php/email-editor/tests/integration/Engine/Renderer/Snapshots/fixtures/core-blocks.html
@@ -50,6 +50,10 @@
<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>Product</th><th>Price</th></tr></thead><tbody><tr><td>T-shirt</td><td>$20</td></tr></tbody></table></figure>
<!-- /wp:table -->
+<!-- wp:table -->
+<figure class="wp-block-table"><table><thead><tr><th>Code</th><th>Value</th></tr></thead><tbody><tr><td>GR-1</td><td>EACMP_BREAKING_UNRECOVERABLE_INVARIANT_WITH_A_NAME_FAR_WIDER_THAN_THE_EMAIL</td></tr></tbody></table></figure>
+<!-- /wp:table -->
+
<!-- wp:group {"layout":{"type":"constrained"}} -->
<div class="wp-block-group"><!-- wp:paragraph -->
<p>Inside a group</p>
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 91d39642812..43381e3b930 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
@@ -479,14 +479,64 @@
<table border="1" cellpadding="8" cellspacing="0" role="presentation" width="100%" class="has-fixed-layout wp-block-table" style="mso-table-lspace: 0; mso-table-rspace: 0; margin-top: 0; margin-bottom: 0; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; table-layout: fixed; border-collapse: collapse; width: 100%; min-width: 100%; color: #1e1e1e; text-align: left;">
<thead>
<tr>
-<th valign="top" style="vertical-align: top; border: 1px solid #1e1e1e; padding: 8px; text-align: center; border-bottom: 3px solid #1e1e1e;">Product</th>
-<th valign="top" style="vertical-align: top; border: 1px solid #1e1e1e; padding: 8px; text-align: center; border-bottom: 3px solid #1e1e1e;">Price</th>
+<th valign="top" style="word-break: break-word; vertical-align: top; border: 1px solid #1e1e1e; padding: 8px; text-align: center; border-bottom: 3px solid #1e1e1e;">Product</th>
+<th valign="top" style="word-break: break-word; vertical-align: top; border: 1px solid #1e1e1e; padding: 8px; text-align: center; border-bottom: 3px solid #1e1e1e;">Price</th>
</tr>
</thead>
<tbody>
<tr>
-<td valign="top" style="border-collapse: collapse; mso-table-lspace: 0; mso-table-rspace: 0; vertical-align: top; border: 1px solid #1e1e1e; padding: 8px; text-align: left;">T-shirt</td>
-<td valign="top" style="border-collapse: collapse; mso-table-lspace: 0; mso-table-rspace: 0; vertical-align: top; border: 1px solid #1e1e1e; padding: 8px; text-align: left;">$20</td>
+<td valign="top" style="border-collapse: collapse; mso-table-lspace: 0; mso-table-rspace: 0; word-break: break-word; vertical-align: top; border: 1px solid #1e1e1e; padding: 8px; text-align: left;">T-shirt</td>
+<td valign="top" style="border-collapse: collapse; mso-table-lspace: 0; mso-table-rspace: 0; word-break: break-word; vertical-align: top; border: 1px solid #1e1e1e; padding: 8px; text-align: left;">$20</td>
+</tr>
+</tbody>
+</table>
+</td>
+ </tr>
+ </tbody>
+ </table>
+</div>
+<!--[if mso | IE]>
+</td>
+ </tr>
+ </tbody>
+ </table>
+<![endif]-->
+</div>
+<!--[if mso | IE]>
+</td>
+ </tr>
+ </tbody>
+ </table>
+<![endif]-->
+<!--[if mso | IE]>
+<table border="0" cellpadding="0" cellspacing="0" role="presentation" align="left" width="100%">
+ <tbody>
+ <tr>
+ <td style="padding-left:24px;padding-right:24px;">
+<![endif]-->
+<div class="email-root-padding" style="padding-left: 24px; padding-right: 24px;">
+<!--[if mso | IE]>
+<table border="0" cellpadding="0" cellspacing="0" role="presentation" align="left" width="100%" style="margin-top:16px;">
+ <tbody>
+ <tr>
+ <td>
+<![endif]-->
+<div class="email-block-layout" style="margin-top: 16px;">
+<table border="0" cellpadding="0" cellspacing="0" role="presentation" width="100%" style="mso-table-lspace: 0; mso-table-rspace: 0; border-collapse: separate;">
+ <tbody>
+ <tr>
+ <td class="email-table-block" align="left" style="border-collapse: collapse; mso-table-lspace: 0; mso-table-rspace: 0; background: transparent;">
+<table border="1" cellpadding="8" cellspacing="0" role="presentation" width="100%" class="wp-block-table" style="mso-table-lspace: 0; mso-table-rspace: 0; margin-top: 0; margin-bottom: 0; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; table-layout: fixed; border-collapse: collapse; width: 100%; min-width: 100%; color: #1e1e1e; text-align: left;">
+<thead>
+<tr>
+<th valign="top" style="word-break: break-word; vertical-align: top; border: 1px solid #1e1e1e; padding: 8px; text-align: center; border-bottom: 3px solid #1e1e1e;">Code</th>
+<th valign="top" style="word-break: break-word; vertical-align: top; border: 1px solid #1e1e1e; padding: 8px; text-align: center; border-bottom: 3px solid #1e1e1e;">Value</th>
+</tr>
+</thead>
+<tbody>
+<tr>
+<td valign="top" style="border-collapse: collapse; mso-table-lspace: 0; mso-table-rspace: 0; word-break: break-word; vertical-align: top; border: 1px solid #1e1e1e; padding: 8px; text-align: left;">GR-1</td>
+<td valign="top" style="border-collapse: collapse; mso-table-lspace: 0; mso-table-rspace: 0; word-break: break-word; vertical-align: top; border: 1px solid #1e1e1e; padding: 8px; text-align: left;">EACMP_BREAKING_UNRECOVERABLE_INVARIANT_WITH_A_NAME_FAR_WIDER_THAN_THE_EMAIL</td>
</tr>
</tbody>
</table>
diff --git a/packages/php/email-editor/tests/integration/Engine/Renderer/Snapshots/snapshots/core-blocks.txt b/packages/php/email-editor/tests/integration/Engine/Renderer/Snapshots/snapshots/core-blocks.txt
index af863d254bf..64cb2674fe9 100644
--- a/packages/php/email-editor/tests/integration/Engine/Renderer/Snapshots/snapshots/core-blocks.txt
+++ b/packages/php/email-editor/tests/integration/Engine/Renderer/Snapshots/snapshots/core-blocks.txt
@@ -23,6 +23,9 @@ A customer
Product Price
T-shirt $20
+Code Value
+GR-1 EACMP_BREAKING_UNRECOVERABLE_INVARIANT_WITH_A_NAME_FAR_WIDER_THAN_THE_EMAIL
+
Inside a group
Text on a cover
diff --git a/packages/php/email-editor/tests/integration/Engine/Settings_Controller_Test.php b/packages/php/email-editor/tests/integration/Engine/Settings_Controller_Test.php
index 4f96491be4a..c17540d99bb 100644
--- a/packages/php/email-editor/tests/integration/Engine/Settings_Controller_Test.php
+++ b/packages/php/email-editor/tests/integration/Engine/Settings_Controller_Test.php
@@ -59,6 +59,27 @@ class Settings_Controller_Test extends \Email_Editor_Integration_Test_Case {
$this->assertDoesNotMatchRegularExpression( '/\{[^}]*\{/', $expected_css, 'Nested rules must be flattened so selector prefixing keeps them valid' );
}
+ /**
+ * Test the editor gets the shared stylesheet unchanged, so the rules that keep a long word
+ * from widening the email wrap the same way in the editor.
+ */
+ public function testItAddsSharedContentStylesToEditorStyles(): void {
+ $shared_css = (string) file_get_contents( dirname( __DIR__, 3 ) . '/src/Engine/content-shared.css' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local package file.
+
+ $settings = $this->settings_controller->get_settings();
+
+ $shared_styles = array_filter(
+ $settings['styles'],
+ static function ( array $style ) use ( $shared_css ): bool {
+ return $style['css'] === $shared_css;
+ }
+ );
+
+ $this->assertCount( 1, $shared_styles, 'The iframe must receive content-shared.css unchanged' );
+ $this->assertStringContainsString( 'word-break: break-word', $shared_css, 'The shared stylesheet must hold the wrapping rules the email relies on' );
+ $this->assertStringContainsString( '.wp-block-table td', $shared_css );
+ }
+
/**
* Test it keeps the admin color scheme stylesheet in the iframe assets.
*/
diff --git a/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Code_Test.php b/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Code_Test.php
new file mode 100644
index 00000000000..72d46d8573f
--- /dev/null
+++ b/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Code_Test.php
@@ -0,0 +1,99 @@
+<?php
+/**
+ * This file is part of the WooCommerce Email Editor package
+ *
+ * @package Automattic\WooCommerce\EmailEditor
+ */
+
+declare(strict_types = 1);
+namespace Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks;
+
+use Automattic\WooCommerce\EmailEditor\Engine\Email_Editor;
+use Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Content_Renderer;
+use Automattic\WooCommerce\EmailEditor\Integrations\Core\Initializer;
+
+/**
+ * Integration test for the core/code and core/verse blocks, through the whole content renderer.
+ *
+ * Neither block has an email renderer, so both reach the email as the `<pre>` element core saves
+ * and take their styles from the content stylesheet.
+ */
+class Code_Test extends \Email_Editor_Integration_Test_Case {
+ /**
+ * A word no email client can break on whitespace, which is what widens the email.
+ */
+ private const UNBREAKABLE_WORD = 'EACMP_BREAKING_UNRECOVERABLE_INVARIANT_WITH_A_NAME_FAR_WIDER_THAN_THE_EMAIL';
+
+ /**
+ * Set up the renderer.
+ */
+ public function setUp(): void {
+ parent::setUp();
+ $this->di_container->get( Email_Editor::class )->initialize();
+ $this->di_container->get( Initializer::class )->initialize();
+ }
+
+ /**
+ * Test a long unbreakable word in a code block wraps instead of widening the email.
+ */
+ public function testItWrapsLongWordsInACodeBlock(): void {
+ $html = $this->render_block( '<!-- wp:code --><pre class="wp-block-code"><code>' . self::UNBREAKABLE_WORD . '</code></pre><!-- /wp:code -->' );
+
+ $style = $this->get_pre_element( $html )->getAttribute( 'style' );
+ $this->assertStringContainsString( 'word-break: break-word', $style );
+ $this->assertStringContainsString( 'white-space: pre-wrap', $style );
+ }
+
+ /**
+ * Test a long unbreakable word in a verse block wraps instead of widening the email.
+ */
+ public function testItWrapsLongWordsInAVerseBlock(): void {
+ $html = $this->render_block( '<!-- wp:verse --><pre class="wp-block-verse">' . self::UNBREAKABLE_WORD . '</pre><!-- /wp:verse -->' );
+
+ $style = $this->get_pre_element( $html )->getAttribute( 'style' );
+ $this->assertStringContainsString( 'word-break: break-word', $style );
+ $this->assertStringContainsString( 'white-space: pre-wrap', $style );
+ }
+
+ /**
+ * Test wrapping a long word does not cost the line breaks a code block exists to keep.
+ */
+ public function testItKeepsLineBreaksInACodeBlock(): void {
+ $html = $this->render_block( '<!-- wp:code --><pre class="wp-block-code"><code>first line' . "\n\t" . 'indented line</code></pre><!-- /wp:code -->' );
+
+ $this->assertStringContainsString( "first line\n\tindented line", $html );
+ }
+
+ /**
+ * Render an email with the given content and return the HTML.
+ *
+ * @param string $content Block markup.
+ */
+ private function render_block( string $content ): string {
+ $email_post_id = $this->factory->post->create( array( 'post_content' => $content ) );
+ $this->assertIsInt( $email_post_id );
+ $email_post = get_post( $email_post_id );
+ $this->assertInstanceOf( \WP_Post::class, $email_post );
+
+ $template = new \WP_Block_Template();
+ $template->id = 'template-id';
+ $template->content = '<!-- wp:post-content /-->';
+
+ return $this->di_container->get( Content_Renderer::class )->render( $email_post, $template );
+ }
+
+ /**
+ * Return the pre element of the rendered HTML.
+ *
+ * @param string $html Rendered HTML.
+ */
+ private function get_pre_element( string $html ): \DOMElement {
+ $document = new \DOMDocument();
+ libxml_use_internal_errors( true );
+ $document->loadHTML( '<?xml encoding="utf-8" ?>' . $html );
+ libxml_clear_errors();
+ $pre = $document->getElementsByTagName( 'pre' )->item( 0 );
+ $this->assertInstanceOf( \DOMElement::class, $pre );
+ return $pre;
+ }
+}
diff --git a/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Preformatted_Test.php b/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Preformatted_Test.php
index 69cbde9344b..a722aa4823d 100644
--- a/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Preformatted_Test.php
+++ b/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Preformatted_Test.php
@@ -61,6 +61,7 @@ class Preformatted_Test extends \Email_Editor_Integration_Test_Case {
$this->assertStringContainsString( "first line\n\tindented line</pre>", $html );
$style = $this->get_pre_element( $html )->getAttribute( 'style' );
$this->assertStringContainsString( 'white-space: pre-wrap', $style );
+ $this->assertStringContainsString( 'word-break: break-word', $style );
$this->assertStringContainsString( 'margin: 0', $style );
$this->assertStringContainsString( 'font-family:', $style );
$this->assertStringContainsString( 'monospace', $style );
diff --git a/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Table_Test.php b/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Table_Test.php
index 87f5258c5dc..e34e65a318b 100644
--- a/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Table_Test.php
+++ b/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Table_Test.php
@@ -9,13 +9,20 @@ declare( strict_types = 1 );
namespace Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks;
use Automattic\WooCommerce\EmailEditor\Engine\Email_Editor;
+use Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Content_Renderer;
use Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Rendering_Context;
use Automattic\WooCommerce\EmailEditor\Engine\Theme_Controller;
+use Automattic\WooCommerce\EmailEditor\Integrations\Core\Initializer;
/**
* Integration test for Table class
*/
class Table_Test extends \Email_Editor_Integration_Test_Case {
+ /**
+ * A word no email client can break on whitespace, which is what widens the email.
+ */
+ private const UNBREAKABLE_WORD = 'EACMP_BREAKING_UNRECOVERABLE_INVARIANT_WITH_A_NAME_FAR_WIDER_THAN_THE_EMAIL';
+
/**
* Table renderer instance
*
@@ -100,6 +107,20 @@ class Table_Test extends \Email_Editor_Integration_Test_Case {
*/
private $rendering_context;
+ /**
+ * Block type that was given an email render callback, so tearDown can restore it.
+ *
+ * @var \WP_Block_Type|null
+ */
+ private $wired_block_type = null;
+
+ /**
+ * The email render callback core/table had before the test set one.
+ *
+ * @var mixed
+ */
+ private $previous_email_callback = null;
+
/**
* Set up before each test
*/
@@ -111,6 +132,17 @@ class Table_Test extends \Email_Editor_Integration_Test_Case {
$this->rendering_context = new Rendering_Context( $theme_controller->get_theme() );
}
+ /**
+ * Clean up after each test.
+ */
+ public function tearDown(): void {
+ if ( $this->wired_block_type ) {
+ $this->wired_block_type->__set( 'render_email_callback', $this->previous_email_callback );
+ $this->wired_block_type = null;
+ }
+ parent::tearDown();
+ }
+
/**
* Test it renders table content
*/
@@ -737,6 +769,58 @@ class Table_Test extends \Email_Editor_Integration_Test_Case {
$this->assertStringContainsString( 'table-layout: fixed;', $rendered );
}
+ /**
+ * Test every table gets a fixed layout, so a short label keeps a readable column beside a
+ * long value instead of being squeezed to one letter per line.
+ */
+ public function testItGivesEveryTableAFixedLayout(): void {
+ $rendered = $this->render_table(
+ '<thead><tr><th>Code</th><th>Value</th></tr></thead>
+ <tbody><tr><td>GR-1</td><td>' . self::UNBREAKABLE_WORD . '</td></tr></tbody>'
+ );
+
+ $this->assertStringContainsString( 'table-layout: fixed', $this->get_table_style( $rendered ) );
+ }
+
+ /**
+ * Test the column count cannot push a table past the width a phone gives it.
+ *
+ * A per-cell minimum would: they accumulate, so six columns at 3em need 391px where a
+ * 390px phone, less the email's 24px of root padding either side, leaves 342px. Equal
+ * columns divide that width however many there are.
+ */
+ public function testASixColumnTableStillFitsThePhone(): void {
+ $rendered = $this->render_table(
+ '<thead><tr><th>A</th><th>B</th><th>C</th><th>D</th><th>E</th><th>F</th></tr></thead>
+ <tbody><tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr></tbody>'
+ );
+
+ $this->assertStringContainsString( 'table-layout: fixed', $this->get_table_style( $rendered ) );
+
+ $styles = $this->get_cell_styles( $rendered );
+ $this->assertCount( 12, $styles );
+ foreach ( $styles as $style ) {
+ $this->assertStringNotContainsString( 'min-width', $style, 'A per-cell minimum would accumulate across columns.' );
+ }
+ }
+
+ /**
+ * Test every cell wraps long unbreakable words, so a wide cell can't widen the email.
+ */
+ public function testItWrapsLongWordsInCells(): void {
+ $styles = $this->get_cell_styles(
+ $this->render_table(
+ '<thead><tr><th>' . self::UNBREAKABLE_WORD . '</th></tr></thead>
+ <tbody><tr><td>' . self::UNBREAKABLE_WORD . '</td></tr></tbody>'
+ )
+ );
+
+ $this->assertCount( 2, $styles );
+ foreach ( $styles as $style ) {
+ $this->assertStringContainsString( 'word-break: break-word', $style );
+ }
+ }
+
/**
* Test it does not apply fixed layout when class is not present
*/
@@ -816,4 +900,104 @@ class Table_Test extends \Email_Editor_Integration_Test_Case {
$rendered = $this->table_renderer->render( $input, $parsed, $this->rendering_context );
$this->assertStringContainsString( '<table', $rendered );
}
+
+ /**
+ * Give core/table the email render callback Bootstrap gives it in production.
+ *
+ * The block settings filter only runs when a block is registered, and core blocks are
+ * already registered by the time a test boots the editor.
+ */
+ private function wire_table_renderer(): void {
+ $initializer = $this->di_container->get( Initializer::class );
+ $initializer->initialize();
+
+ $block_type = \WP_Block_Type_Registry::get_instance()->get_registered( 'core/table' );
+ $this->assertInstanceOf( \WP_Block_Type::class, $block_type );
+
+ $settings = $initializer->update_block_settings( array( 'name' => 'core/table' ) );
+ $this->assertArrayHasKey( 'render_email_callback', $settings );
+
+ $this->previous_email_callback = get_object_vars( $block_type )['render_email_callback'] ?? null;
+ $this->wired_block_type = $block_type;
+ $block_type->__set( 'render_email_callback', $settings['render_email_callback'] );
+ }
+
+ /**
+ * Render an email holding one table block and return the HTML.
+ *
+ * The cell wrapping rules live in the shared stylesheet, so they only reach the cells once
+ * the content renderer has inlined it. Rendering the block on its own would miss them.
+ *
+ * @param string $rows Table rows.
+ */
+ private function render_table( string $rows ): string {
+ $this->wire_table_renderer();
+
+ $table = '<figure class="wp-block-table"><table>' . $rows . '</table></figure>';
+ $content = '<!-- wp:table -->' . $table . '<!-- /wp:table -->';
+
+ $email_post_id = $this->factory->post->create( array( 'post_content' => $content ) );
+ $this->assertIsInt( $email_post_id );
+ $email_post = get_post( $email_post_id );
+ $this->assertInstanceOf( \WP_Post::class, $email_post );
+
+ $template = new \WP_Block_Template();
+ $template->id = 'template-id';
+ $template->content = '<!-- wp:post-content /-->';
+
+ $html = $this->di_container->get( Content_Renderer::class )->render( $email_post, $template );
+
+ // Only this renderer sets cellpadding, so without it the block fell back to the engine's
+ // generic renderer and the test would be measuring the wrong markup.
+ $this->assertStringContainsString( 'cellpadding="8"', $html, 'The table block should reach the email through its own renderer.' );
+
+ return $html;
+ }
+
+ /**
+ * Return the style attribute of every cell of the table block in the rendered HTML.
+ *
+ * An email is built out of layout tables, so the cells are selected from the table block
+ * rather than from the document, which holds many cells that are not the block's.
+ *
+ * @param string $html Rendered HTML.
+ * @return string[]
+ */
+ private function get_cell_styles( string $html ): array {
+ $document = new \DOMDocument();
+ libxml_use_internal_errors( true );
+ $document->loadHTML( '<?xml encoding="utf-8" ?>' . $html );
+ libxml_clear_errors();
+
+ $cells = ( new \DOMXPath( $document ) )->query(
+ "//*[contains(concat(' ', normalize-space(@class), ' '), ' wp-block-table ')]//*[self::td or self::th]"
+ );
+ $this->assertInstanceOf( \DOMNodeList::class, $cells );
+
+ $styles = array();
+ foreach ( $cells as $cell ) {
+ $this->assertInstanceOf( \DOMElement::class, $cell );
+ $styles[] = $cell->getAttribute( 'style' );
+ }
+
+ return $styles;
+ }
+
+ /**
+ * Return the style attribute of the table block's own table element.
+ *
+ * @param string $html Rendered HTML.
+ */
+ private function get_table_style( string $html ): string {
+ $processor = new \WP_HTML_Tag_Processor( $html );
+
+ while ( $processor->next_tag( array( 'tag_name' => 'TABLE' ) ) ) {
+ $class = (string) $processor->get_attribute( 'class' );
+ if ( false !== strpos( $class, 'wp-block-table' ) ) {
+ return (string) $processor->get_attribute( 'style' );
+ }
+ }
+
+ $this->fail( 'The rendered email holds no table block.' );
+ }
}