Commit 89033e6e59a for woocommerce
commit 89033e6e59a684fadb732978bae6a151542614c9
Author: Allison Levine <1689238+allilevine@users.noreply.github.com>
Date: Mon Nov 30 14:30:17 2026 -0500
[Email Editor] Leave out blocks that render nothing, so they add no gap (#71285)
* Leave out email blocks that render nothing, so they add no gap
A block whose email output shows nothing (for example a Custom HTML
block holding only a <style> tag) was still wrapped in an
email-block-layout div with the block gap, so a post opening with a few
of them got a blank band above its content.
- Abstract_Block_Renderer::render() skips the spacer when the rendered
content has no visible element or text.
- Fallback passes such content through without its table wrapper, and
root padding is skipped for it too. The <style> itself still reaches
the CSS inliner, as before.
- Spacing_Preprocessor doesn't count leading style-only HTML blocks as
the first child, so the first visible block gets no gap.
NL-1209
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Tighten the NL-1209 tests after a test audit
- Reuse create_email_post() and count padding wrappers with the tag
processor instead of matching raw markup.
- Show the rendered HTML in every assertion message.
- Name the helper test after its behavior, correct its docblock, and
split the empty-HTML row so a failure names the rule that broke.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Ignore doctypes and stray closers when checking for visible email content
A doctype, a bogus closer such as </2>, or </> renders nothing, but
has_visible_content() counted them as visible, so a style-only block
pasted with one still got a wrapper and a gap.
Also document that the spacing preprocessor reads saved HTML, so a
pre_render_block replacement can't be seen there.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Ignore stray closing tags when checking for visible email content
A closing tag with no opener before it, such as a lone </div>, renders
nothing: browsers ignore it and the CSS inliner's parser drops it. The
check counted it as visible, so the block kept a wrapper and a gap.
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-1209-skip-empty-blocks b/packages/php/email-editor/changelog/fix-nl-1209-skip-empty-blocks
new file mode 100644
index 00000000000..2c073a2b8da
--- /dev/null
+++ b/packages/php/email-editor/changelog/fix-nl-1209-skip-empty-blocks
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Leave out email blocks that show nothing, such as Custom HTML holding only a style tag, so they add no blank gap.
diff --git a/packages/php/email-editor/src/Engine/Renderer/ContentRenderer/Preprocessors/class-spacing-preprocessor.php b/packages/php/email-editor/src/Engine/Renderer/ContentRenderer/Preprocessors/class-spacing-preprocessor.php
index 4a8269bb065..83c4537cdf6 100644
--- a/packages/php/email-editor/src/Engine/Renderer/ContentRenderer/Preprocessors/class-spacing-preprocessor.php
+++ b/packages/php/email-editor/src/Engine/Renderer/ContentRenderer/Preprocessors/class-spacing-preprocessor.php
@@ -10,6 +10,7 @@ namespace Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Pre
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;
/**
* This preprocessor is responsible for setting default spacing values for blocks.
@@ -136,6 +137,7 @@ class Spacing_Preprocessor implements Context_Aware_Preprocessor {
* @return array
*/
private function add_block_gaps( array $parsed_blocks, string $gap = '', $parent_block = null, array $root_padding = array(), bool $apply_root_padding = false, array $container_padding = array(), array $variables_map = array(), string $gap_padding_side = 'padding-left' ): array {
+ $follows_visible_block = false;
foreach ( $parsed_blocks as $key => $block ) {
$block_name = $block['blockName'] ?? '';
$parent_block_name = $parent_block['blockName'] ?? '';
@@ -144,12 +146,13 @@ class Spacing_Preprocessor implements Context_Aware_Preprocessor {
/**
* Do not add a gap to:
- * - first child
+ * - first child, or one preceded only by HTML that shows nothing (e.g. a `<style>` tag)
* - parent block is a buttons block (where buttons are side by side).
*/
- if ( 0 !== $key && $gap && 'core/buttons' !== $parent_block_name ) {
+ if ( $follows_visible_block && $gap && 'core/buttons' !== $parent_block_name ) {
$block['email_attrs']['margin-top'] = $gap;
}
+ $follows_visible_block = $follows_visible_block || ! $this->is_html_without_visible_content( $block );
// Handle horizontal gap for columns: apply physical padding to column children (except the first).
// Only an explicitly defined column blockGap is applied. We intentionally do not
@@ -223,6 +226,20 @@ class Spacing_Preprocessor implements Context_Aware_Preprocessor {
return $parsed_blocks;
}
+ /**
+ * Check whether a block is raw HTML that shows nothing in the email, such as only a `<style>` tag.
+ *
+ * This reads the saved HTML, before rendering. A `pre_render_block` callback that replaces such a
+ * block with visible HTML leaves the next block without its gap.
+ *
+ * @param array $block Parsed block.
+ * @return bool
+ */
+ private function is_html_without_visible_content( array $block ): bool {
+ return in_array( $block['blockName'] ?? null, array( null, 'core/html' ), true )
+ && ! Html_Processing_Helper::has_visible_content( $block['innerHTML'] ?? '' );
+ }
+
/**
* Returns the list of block names treated as "post content" for padding delegation.
*
diff --git a/packages/php/email-editor/src/Engine/Renderer/ContentRenderer/class-content-renderer.php b/packages/php/email-editor/src/Engine/Renderer/ContentRenderer/class-content-renderer.php
index 8a1a4978010..d3a3d1767ff 100644
--- a/packages/php/email-editor/src/Engine/Renderer/ContentRenderer/class-content-renderer.php
+++ b/packages/php/email-editor/src/Engine/Renderer/ContentRenderer/class-content-renderer.php
@@ -474,6 +474,10 @@ class Content_Renderer {
* @return string The content wrapped with horizontal padding, or unchanged if no padding.
*/
private function add_root_horizontal_padding( string $content, array $email_attrs ): string {
+ if ( ! Html_Processing_Helper::has_visible_content( $content ) ) {
+ return $content;
+ }
+
$padding_left = $this->sum_padding_values(
$email_attrs['root-padding-left'] ?? null,
$email_attrs['container-padding-left'] ?? null
diff --git a/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-abstract-block-renderer.php b/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-abstract-block-renderer.php
index 9a35e35f507..f7518893f4c 100644
--- a/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-abstract-block-renderer.php
+++ b/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-abstract-block-renderer.php
@@ -11,6 +11,7 @@ namespace Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks;
use Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Block_Renderer;
use Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Rendering_Context;
use Automattic\WooCommerce\EmailEditor\Integrations\Utils\Dom_Document_Helper;
+use Automattic\WooCommerce\EmailEditor\Integrations\Utils\Html_Processing_Helper;
use Automattic\WooCommerce\EmailEditor\Integrations\Utils\Styles_Helper;
use Automattic\WooCommerce\EmailEditor\Integrations\Utils\Table_Wrapper_Helper;
use WP_Style_Engine;
@@ -121,14 +122,22 @@ abstract class Abstract_Block_Renderer implements Block_Renderer {
/**
* Render the block.
*
+ * Content with nothing visible, such as a Custom HTML block holding only a `<style>` tag,
+ * is returned without the spacer so it adds no gap to the email.
+ *
* @param string $block_content The block content.
* @param array $parsed_block The parsed block.
* @param Rendering_Context $rendering_context The rendering context.
* @return string
*/
public function render( string $block_content, array $parsed_block, Rendering_Context $rendering_context ): string {
+ $content = $this->render_content( $block_content, $parsed_block, $rendering_context );
+ if ( ! Html_Processing_Helper::has_visible_content( $content ) ) {
+ return $content;
+ }
+
return $this->add_spacer_with_context(
- $this->render_content( $block_content, $parsed_block, $rendering_context ),
+ $content,
$parsed_block['email_attrs'] ?? array(),
$rendering_context
);
diff --git a/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-fallback.php b/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-fallback.php
index 8d01bb2714d..99f7bfbe538 100644
--- a/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-fallback.php
+++ b/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-fallback.php
@@ -9,6 +9,7 @@ declare( strict_types = 1 );
namespace Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks;
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;
/**
@@ -30,6 +31,11 @@ class Fallback extends Abstract_Block_Renderer {
* @return string
*/
protected function render_content( $block_content, array $parsed_block, Rendering_Context $rendering_context ): string {
+ // Content that shows nothing, e.g. only a `<style>` tag, is left unwrapped so the block adds no gap.
+ if ( ! Html_Processing_Helper::has_visible_content( $block_content ) ) {
+ return $block_content;
+ }
+
$block_attrs = $parsed_block['attrs'] ?? array();
$table_attrs = array(
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 5d947f376a5..c4acba54bde 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
@@ -775,4 +775,36 @@ class Html_Processing_Helper {
return implode( '; ', $sanitized_styles );
}
+
+ /**
+ * Check whether HTML has anything an email shows: an element or non-whitespace text.
+ *
+ * Comments, doctypes, stray closing tags such as `</div>` or `</2>`, and `<style>`/`<script>` elements
+ * don't count, since the email doesn't show them.
+ *
+ * @param string $html HTML to check.
+ * @return bool
+ */
+ public static function has_visible_content( string $html ): bool {
+ $processor = new \WP_HTML_Tag_Processor( $html );
+ while ( $processor->next_token() ) {
+ $token_type = $processor->get_token_type();
+ if ( in_array( $token_type, array( '#comment', '#doctype', '#funky-comment', '#presumptuous-tag' ), true ) ) {
+ continue;
+ }
+ if ( '#text' === $token_type ) {
+ if ( '' !== trim( $processor->get_modifiable_text() ) ) {
+ return true;
+ }
+ continue;
+ }
+ // A closer reached here has no opener before it, so the parser drops it.
+ if ( '#tag' === $token_type && ( $processor->is_tag_closer() || in_array( $processor->get_tag(), array( 'STYLE', 'SCRIPT' ), true ) ) ) {
+ continue;
+ }
+ return true;
+ }
+ // Keep HTML the processor couldn't finish reading rather than risk dropping content.
+ return $processor->paused_at_incomplete_token();
+ }
}
diff --git a/packages/php/email-editor/tests/integration/Engine/Renderer/ContentRenderer/Content_Renderer_Test.php b/packages/php/email-editor/tests/integration/Engine/Renderer/ContentRenderer/Content_Renderer_Test.php
index e255784b0cc..af328b921ad 100644
--- a/packages/php/email-editor/tests/integration/Engine/Renderer/ContentRenderer/Content_Renderer_Test.php
+++ b/packages/php/email-editor/tests/integration/Engine/Renderer/ContentRenderer/Content_Renderer_Test.php
@@ -648,6 +648,48 @@ class Content_Renderer_Test extends \Email_Editor_Integration_Test_Case {
\WP_Block_Type_Registry::get_instance()->unregister( 'test/no-padding-block' );
}
+ /**
+ * A Custom HTML block holding only a `<style>` tag shows nothing, so it must add no wrapper and no gap.
+ *
+ * Catches the regression where each such block left an empty wrapper with the block gap (NL-1209):
+ * three of them opening a post put a blank band above the content. The first visible block gets no
+ * gap, the next one gets a single gap, and the `<style>` itself still reaches the CSS inliner.
+ */
+ public function testItLeavesNoWrapperOrGapForHtmlBlocksThatShowNothing(): void {
+ // Custom HTML authors can post unfiltered HTML. Without that capability, kses strips the `<style>` tag.
+ $author_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
+ $this->assertIsInt( $author_id );
+ wp_set_current_user( $author_id );
+ $style_block = '<!-- wp:html --><style>.elementor-hidden { display: none; }</style><!-- /wp:html -->';
+ $email_post = $this->create_email_post(
+ $style_block . $style_block . $style_block
+ . '<!-- wp:paragraph --><p>First</p><!-- /wp:paragraph -->'
+ . $style_block
+ . '<!-- wp:paragraph --><p>Second</p><!-- /wp:paragraph -->'
+ );
+ $template = new \WP_Block_Template();
+ $template->id = 'template-id';
+ $template->content = '<!-- wp:post-content /-->';
+
+ $html = $this->renderer->render_without_css_inline( $email_post, $template )['html'];
+
+ $wrapper_styles = array();
+ $processor = new \WP_HTML_Tag_Processor( $html );
+ while ( $processor->next_tag( array( 'class_name' => 'email-block-layout' ) ) ) {
+ $wrapper_styles[] = $processor->get_attribute( 'style' );
+ }
+ // The wrappers are post-content's, then the two paragraphs'.
+ $this->assertSame( array( '', '', 'margin-top:16px;' ), $wrapper_styles, $html );
+
+ $padding_wrappers = 0;
+ $processor = new \WP_HTML_Tag_Processor( $html );
+ while ( $processor->next_tag( array( 'class_name' => 'email-root-padding' ) ) ) {
+ ++$padding_wrappers;
+ }
+ $this->assertSame( 2, $padding_wrappers, 'Only the two paragraphs should get the root padding wrapper. ' . $html );
+ $this->assertSame( 4, substr_count( $html, '.elementor-hidden { display: none; }</style>' ), 'Every <style> should still reach the CSS inliner. ' . $html );
+ }
+
/**
* Test preprocess_parsed_blocks treats a group with its own padding wrapping
* post-content as a box (WooCommerce template pattern): the box takes the root
diff --git a/packages/php/email-editor/tests/integration/Integrations/Utils/Html_Processing_Helper_Test.php b/packages/php/email-editor/tests/integration/Integrations/Utils/Html_Processing_Helper_Test.php
index e6f5e25eb69..c6011663076 100644
--- a/packages/php/email-editor/tests/integration/Integrations/Utils/Html_Processing_Helper_Test.php
+++ b/packages/php/email-editor/tests/integration/Integrations/Utils/Html_Processing_Helper_Test.php
@@ -337,4 +337,41 @@ class Html_Processing_Helper_Test extends \Email_Editor_Integration_Test_Case {
),
);
}
+
+ /**
+ * Only HTML that shows nothing in an email counts as empty, so its block can be left out without a gap.
+ *
+ * Catches a check that treats image, separator or spacer markup as empty, which strips their wrapper,
+ * padding and gap, or one that misses `<style>`/`<script>`-only HTML (or a doctype or stray closer
+ * pasted with it), whose block then leaves a blank gap.
+ *
+ * @dataProvider visible_content_provider
+ * @param string $html HTML to check.
+ * @param bool $expected Whether the HTML shows anything.
+ */
+ public function test_has_visible_content_ignores_only_style_script_comments_and_whitespace( string $html, bool $expected ): void {
+ $this->assertSame( $expected, Html_Processing_Helper::has_visible_content( $html ) );
+ }
+
+ /**
+ * Data provider for test_has_visible_content_ignores_only_style_script_comments_and_whitespace.
+ *
+ * @return array<string, array{string, bool}>
+ */
+ public function visible_content_provider(): array {
+ return array(
+ 'style only' => array( '<style>.a { color: red; }</style>', false ),
+ 'script, comment and whitespace' => array( "\n<!-- note --><script>var a = 1;</script>\n", false ),
+ 'doctype and stray closers' => array( '<!DOCTYPE html></2></><style>p { color: red; }</style>', false ),
+ 'text after a doctype' => array( '<!DOCTYPE html>Hello', true ),
+ // Browsers turn a lone </p> into an empty <p>, but the CSS inliner's parser drops it like any other.
+ 'stray closing tags' => array( '</div></p></span>', false ),
+ 'text after a stray closing tag' => array( '</div>Hello', true ),
+ 'text' => array( '<style>p { margin: 0; }</style>Hello', true ),
+ 'non-breaking space' => array( ' ', true ),
+ 'image' => array( '<img src="https://example.com/a.jpg" alt="">', true ),
+ 'separator' => array( '<hr class="wp-block-separator">', true ),
+ 'spacer' => array( '<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>', true ),
+ );
+ }
}