diff --git a/lib/block-supports/index.php b/lib/block-supports/index.php index 958197abf4f009..4d91d6f9cc77e5 100644 --- a/lib/block-supports/index.php +++ b/lib/block-supports/index.php @@ -52,17 +52,21 @@ function gutenberg_apply_block_supports( $block_content, $block ) { return $block_content; } + // We need to wrap the block in order to handle UTF-8 properly. + $wrapper_left = ''; + $wrapper_right = ''; + $dom = new DOMDocument( '1.0', 'utf-8' ); // Suppress warnings from this method from polluting the front-end. // @codingStandardsIgnoreStart - if ( ! @$dom->loadHTML( mb_convert_encoding( $block_content, 'HTML-ENTITIES', 'UTF-8' ), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_COMPACT ) ) { + if ( ! @$dom->loadHTML( $wrapper_left . $block_content . $wrapper_right , LIBXML_HTML_NODEFDTD | LIBXML_COMPACT ) ) { // @codingStandardsIgnoreEnd return $block_content; } $xpath = new DOMXPath( $dom ); - $block_root = $xpath->query( '/*' )[0]; + $block_root = $xpath->query( '/html/body/*' )[0]; if ( empty( $block_root ) ) { return $block_content; @@ -86,7 +90,7 @@ function gutenberg_apply_block_supports( $block_content, $block ) { $block_root->setAttribute( 'style', esc_attr( implode( '; ', $new_styles ) . ';' ) ); } - return mb_convert_encoding( $dom->saveHtml(), 'UTF-8', 'HTML-ENTITIES' ); + return str_replace( array( $wrapper_left, $wrapper_right ), '', $dom->saveHtml() ); } 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 77c1225074c37f..9e6e3cba767ef4 100644 --- a/phpunit/class-block-supported-styles-test.php +++ b/phpunit/class-block-supported-styles-test.php @@ -72,10 +72,10 @@ private function get_attribute_from_block( $attribute, $block ) { * @param string $block String of rendered block to check. */ private function get_content_from_block( $block ) { - $start_index = strpos( $block, '>' ) + 1; + $start_index = strpos( $block, '>' ) + 1; // First occurrence of '>'. $split_arr = substr( $block, $start_index ); - $end_index = strpos( $split_arr, '<' ); - return substr( $split_arr, 0, $end_index ); + $end_index = strrpos( $split_arr, '<' ); // Last occurrence of '<'. + return substr( $split_arr, 0, $end_index ); // String between first '>' and last '<'. } /** @@ -117,7 +117,13 @@ private function assert_content_and_styles_and_classes_match( $block, $expected_ * * @var string */ - const BLOCK_CONTENT = 'Some non-Latin chärs to make sure DOM öperations don\'t mess them up: こんにちは'; + const BLOCK_CONTENT = ' +

Test

+

äöü

+

ß

+

系の家庭に

+

Example <p>Test!</p>

+ '; /** * Example block markup string to test with.