Commit 9a881a92a37 for woocommerce

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

    Email Editor: Add space between Classic and HTML paragraphs in emails (#71386)

    * Email Editor: Space paragraphs that are not paragraph blocks

    Paragraphs inside one chunk of HTML (Classic content, Custom HTML blocks,
    shortcodes) get margin 0 from content.css, so they ran together in emails.
    Give consecutive paragraphs the email's block gap with p + p, which paragraph
    blocks never match because each one sits in its own table.

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

    * Email Editor: Drop redundant filter cleanup in paragraph gap test

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

    * Email Editor: Check the block gap before writing it into the paragraph rule

    A blockGap holding a closing brace could end the p + p rule and add its own
    rules to the email. Pass it through safecss_filter_attr() as a margin-top
    declaration first.

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

    * Email Editor: Skip the paragraph gap rule when the block gap holds a ';'

    safecss_filter_attr() keeps every allowed declaration, so a blockGap such as
    '16px; font-size: 0' added font-size: 0 to every second paragraph. Only a single
    value now becomes the margin-top.

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

    * Email Editor: Accept only a single length as the paragraph gap

    Validate blockGap with Html_Processing_Helper::sanitize_dimension_value()
    instead of filtering it as a declaration. Spacing presets are already resolved
    by get_styles(), so a preset becomes its length; any other value, such as one
    holding extra declarations or a brace, adds no rule.

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

    * Email Editor: Cover a brace inside var() in the paragraph gap test

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

    * Email Editor: Accept lengths without a leading zero as dimensions

    sanitize_dimension_value() rejected valid lengths such as .5rem, so a block gap
    written that way added no paragraph 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-998-classic-paragraph-gap b/packages/php/email-editor/changelog/fix-nl-998-classic-paragraph-gap
new file mode 100644
index 00000000000..7ac03f87c2b
--- /dev/null
+++ b/packages/php/email-editor/changelog/fix-nl-998-classic-paragraph-gap
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Add space between paragraphs from the Classic editor, Custom HTML blocks, and shortcodes in emails.
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 6d333e733bf..8a1a4978010 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
@@ -13,6 +13,7 @@ use Automattic\WooCommerce\EmailEditor\Engine\Renderer\Css_Inliner;
 use Automattic\WooCommerce\EmailEditor\Engine\Theme_Controller;
 use Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks\Fallback;
 use Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks\Post_Content;
+use Automattic\WooCommerce\EmailEditor\Integrations\Utils\Html_Processing_Helper;
 use Automattic\WooCommerce\EmailEditor\Integrations\Utils\Table_Wrapper_Helper;
 use WP_Block_Template;
 use WP_Block_Type_Registry;
@@ -687,6 +688,14 @@ class Content_Renderer {
 			$layout['wideSize'] ?? $layout['contentSize']
 		);

+		// Paragraph blocks are each wrapped in their own table, so this only reaches paragraphs
+		// in one chunk of HTML (Classic, Custom HTML, shortcodes), which content.css resets to margin 0.
+		// get_styles() has already resolved spacing presets, so only a single length is accepted here.
+		$block_gap = Html_Processing_Helper::sanitize_dimension_value( $this->theme_controller->get_styles()['spacing']['blockGap'] ?? null );
+		if ( '' !== $block_gap ) {
+			$styles .= sprintf( 'p + p { margin-top: %s; }', $block_gap );
+		}
+
 		// Get styles from theme.
 		$styles              .= $this->theme_controller->get_stylesheet_for_rendering();
 		$block_support_styles = $this->theme_controller->get_stylesheet_from_context( 'block-supports', 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 d14919d0759..5d947f376a5 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
@@ -222,7 +222,7 @@ class Html_Processing_Helper {
 		$sanitized_value = self::sanitize_css_value( $value );

 		// Additional validation for dimension-specific units.
-		if ( ! empty( $sanitized_value ) && preg_match( '/^(\d+(?:\.\d+)?)(px|em|rem|%|vh|vw|ex|ch|in|cm|mm|pt|pc)$/', $sanitized_value ) ) {
+		if ( ! empty( $sanitized_value ) && preg_match( '/^(\d*\.?\d+)(px|em|rem|%|vh|vw|ex|ch|in|cm|mm|pt|pc)$/', $sanitized_value ) ) {
 			return $sanitized_value;
 		}

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 bfd7cb727a3..e255784b0cc 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
@@ -1109,4 +1109,92 @@ class Content_Renderer_Test extends \Email_Editor_Integration_Test_Case {
 		$this->assertSame( 1, preg_match( '/<strong[^>]*>Bold text<\/strong>/', $content, $strong ) );
 		$this->assertMatchesRegularExpression( '/style="[^"]*font-weight:\s*bold\s*(;|")/', $strong[0] );
 	}
+
+	/**
+	 * Paragraphs in one chunk of HTML (Classic content, a Custom HTML block) are separated by the
+	 * email's block gap, as paragraph blocks are.
+	 *
+	 * @testWith ["<p>First</p>\n<p>Second</p>"]
+	 *           ["<!-- wp:html --><p>First</p><p>Second</p><!-- /wp:html -->"]
+	 *
+	 * @param string $post_content Post content holding two paragraphs that are not paragraph blocks.
+	 */
+	public function testItSpacesParagraphsThatAreNotParagraphBlocks( string $post_content ): void {
+		$paragraphs = $this->render_paragraph_styles( $post_content );
+
+		$this->assertSame( array( 'First', 'Second' ), array_keys( $paragraphs ) );
+		$this->assertStringNotContainsString( 'margin-top', $paragraphs['First'] );
+		$this->assertStringEndsWith( 'margin-top: 16px;', $paragraphs['Second'] );
+	}
+
+	/**
+	 * The gap between paragraphs that are not paragraph blocks follows the email's block gap setting.
+	 * A gap that is not a single length or spacing preset adds no rule, so it can't add other styles to the email.
+	 *
+	 * @testWith ["24px", "display: block; margin: 0; margin-top: 24px;"]
+	 *           ["1.5rem", "display: block; margin: 0; margin-top: 1.5rem;"]
+	 *           [".5rem", "display: block; margin: 0; margin-top: .5rem;"]
+	 *           ["var:preset|spacing|20", "display: block; margin: 0; margin-top: 24px;"]
+	 *           ["16px; font-size: 0", "display: block; margin: 0;"]
+	 *           ["16px } p { display: none", "display: block; margin: 0;"]
+	 *           ["var(--x} p { display:none)", "display: block; margin: 0;"]
+	 *           ["junk", "display: block; margin: 0;"]
+	 *
+	 * @param string $block_gap      Block gap set in the email's theme.json.
+	 * @param string $expected_style Expected style of the second paragraph.
+	 */
+	public function testItSpacesParagraphsThatAreNotParagraphBlocksByTheBlockGap( string $block_gap, string $expected_style ): void {
+		$set_block_gap = static function ( \WP_Theme_JSON $theme ) use ( $block_gap ): \WP_Theme_JSON {
+			$theme->merge(
+				new \WP_Theme_JSON(
+					array(
+						'version' => 3,
+						'styles'  => array( 'spacing' => array( 'blockGap' => $block_gap ) ),
+					)
+				)
+			);
+			return $theme;
+		};
+		add_filter( 'woocommerce_email_editor_theme_json', $set_block_gap );
+
+		$paragraphs = $this->render_paragraph_styles( '<!-- wp:html --><p>First</p><p>Second</p><!-- /wp:html -->' );
+
+		$this->assertSame( $expected_style, $paragraphs['Second'] );
+	}
+
+	/**
+	 * Paragraph blocks already get the block gap on their wrappers, so the paragraphs themselves get no margin.
+	 */
+	public function testItAddsNoMarginToParagraphBlocks(): void {
+		$paragraphs = $this->render_paragraph_styles( '<!-- wp:paragraph --><p>First</p><!-- /wp:paragraph --><!-- wp:paragraph --><p>Second</p><!-- /wp:paragraph -->' );
+
+		$this->assertSame( array( 'First', 'Second' ), array_keys( $paragraphs ) );
+		$this->assertStringNotContainsString( 'margin-top', $paragraphs['First'] );
+		$this->assertStringNotContainsString( 'margin-top', $paragraphs['Second'] );
+	}
+
+	/**
+	 * Renders the post content and returns the style attribute of each paragraph, keyed by its text.
+	 *
+	 * @param string $post_content Post content.
+	 * @return array<string, string>
+	 */
+	private function render_paragraph_styles( string $post_content ): array {
+		$email_post_id = $this->factory->post->create( array( 'post_content' => $post_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 /-->';
+		$rendered          = $this->renderer->render( $email_post, $template );
+
+		preg_match_all( '/<p[^>]*style="([^"]*)"[^>]*>(\w+)<\/p>/', $rendered, $matches, PREG_SET_ORDER );
+		$styles = array();
+		foreach ( $matches as $match ) {
+			$styles[ $match[2] ] = $match[1];
+		}
+		return $styles;
+	}
 }
diff --git a/packages/php/email-editor/tests/unit/Integrations/Utils/Html_Processing_Helper_Test.php b/packages/php/email-editor/tests/unit/Integrations/Utils/Html_Processing_Helper_Test.php
index 14aabd6e7ce..bd4190b3e4d 100644
--- a/packages/php/email-editor/tests/unit/Integrations/Utils/Html_Processing_Helper_Test.php
+++ b/packages/php/email-editor/tests/unit/Integrations/Utils/Html_Processing_Helper_Test.php
@@ -781,6 +781,7 @@ class Html_Processing_Helper_Test extends \Email_Editor_Unit_Test {
 			'50%'    => '50%',      // Percentage.
 			'2em'    => '2em',      // Em units.
 			'1.5rem' => '1.5rem',   // Rem units.
+			'.5rem'  => '.5rem',    // No leading zero.
 			'100vh'  => '100vh',    // Viewport height.
 			'50vw'   => '50vw',     // Viewport width.
 			'12pt'   => '12pt',     // Points.