diff --git a/lib/block-supports/index.php b/lib/block-supports/index.php index 599ef9276acc69..3b0f0d1540771f 100644 --- a/lib/block-supports/index.php +++ b/lib/block-supports/index.php @@ -104,12 +104,15 @@ function gutenberg_apply_block_supports( $block_content, $block ) { $block_root->setAttribute( 'style', implode( '; ', $new_styles ) . ';' ); } - $result = ''; - // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase - foreach ( $body_element->childNodes as $child_node ) { - $result .= $dom->saveHtml( $child_node ); - } - return $result; + // Avoid using `$dom->saveHtml( $node )` because the node results may not produce consistent + // whitespace for PHP < 7.3. Saving the root HTML `$dom->saveHtml()` prevents this behavior. + $full_html = $dom->saveHtml(); + + // Find the open/close tags. The open tag needs to be adjusted so we get inside the tag + // and not the tag itself. + $start = strpos( $full_html, '', 0 ) + strlen( '' ); + $end = strpos( $full_html, '', $start ); + return trim( substr( $full_html, $start, $end - $start ) ); } add_filter( 'render_block', 'gutenberg_apply_block_supports', 10, 2 ); diff --git a/phpunit/class-block-supported-styles-test.php b/phpunit/class-block-supported-styles-test.php index aebd7c20060933..fef5e2bf45d1a8 100644 --- a/phpunit/class-block-supported-styles-test.php +++ b/phpunit/class-block-supported-styles-test.php @@ -880,6 +880,20 @@ public function test_render_block_includes_appended_html() { $this->assertEquals( '

Hello from the block content!

Appended
', $result ); } + /** + * Ensure that HTML is correctly extracted with multibyte contents. + */ + public function test_render_block_mb_html() { + $this->register_block_type( + 'core/example', + array( 'render_callback' => true ) + ); + + $result = do_blocks( '' ); + + $this->assertEquals( '', $result ); + } + /** * Should not error when the rendered block is text only. */