From 3ce013f1f0ec0831761941c3f3d09af06601c39a Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 11 Sep 2020 13:07:40 +0200 Subject: [PATCH 1/3] Get DOM root and extract substring contents In some versions of PHP (< 7.3) the `DOMDocument::saveHtml( $node )` method would format HTML introducing whitespace that could result in different rendered results in the browser. Avoid using the `DOMDocument::saveHtml( $node )` to ensure consistent behavior with supported PHP versions. Mentioned here: https://github.com/WordPress/gutenberg/pull/25028#discussion_r484484183 --- lib/block-supports/index.php | 15 +++++++++------ phpunit/class-block-supported-styles-test.php | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/block-supports/index.php b/lib/block-supports/index.php index 599ef9276acc69..a23a02a5203011 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 = mb_strpos( $full_html, '', 0, 'UTF-8' ) + mb_strlen( '', 'UTF-8' ); + $end = mb_strpos( $full_html, '', $start, 'UTF-8' ); + return mb_substr( $full_html, $start, $end - $start, 'UTF-8' ); } 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. */ From 20646ab077c6e5415bc39a43fb5a138263ee547e Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 11 Sep 2020 21:23:10 +0200 Subject: [PATCH 2/3] Drop mb_ versions --- lib/block-supports/index.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/block-supports/index.php b/lib/block-supports/index.php index a23a02a5203011..a334634ca36f99 100644 --- a/lib/block-supports/index.php +++ b/lib/block-supports/index.php @@ -110,9 +110,9 @@ function gutenberg_apply_block_supports( $block_content, $block ) { // Find the open/close tags. The open tag needs to be adjusted so we get inside the tag // and not the tag itself. - $start = mb_strpos( $full_html, '', 0, 'UTF-8' ) + mb_strlen( '', 'UTF-8' ); - $end = mb_strpos( $full_html, '', $start, 'UTF-8' ); - return mb_substr( $full_html, $start, $end - $start, 'UTF-8' ); + $start = strpos( $full_html, '', 0 ) + strlen( '' ); + $end = strpos( $full_html, '', $start ); + return substr( $full_html, $start, $end - $start ); } add_filter( 'render_block', 'gutenberg_apply_block_supports', 10, 2 ); From d104c750df92804e81c1f98da04792a9aea9eb88 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 11 Sep 2020 21:23:20 +0200 Subject: [PATCH 3/3] Trim result --- lib/block-supports/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/block-supports/index.php b/lib/block-supports/index.php index a334634ca36f99..3b0f0d1540771f 100644 --- a/lib/block-supports/index.php +++ b/lib/block-supports/index.php @@ -112,7 +112,7 @@ function gutenberg_apply_block_supports( $block_content, $block ) { // and not the tag itself. $start = strpos( $full_html, '', 0 ) + strlen( '' ); $end = strpos( $full_html, '', $start ); - return substr( $full_html, $start, $end - $start ); + return trim( substr( $full_html, $start, $end - $start ) ); } add_filter( 'render_block', 'gutenberg_apply_block_supports', 10, 2 );