From 0a92f0dc5232d5af98968bfc450048529aeb6c41 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Mon, 7 Sep 2020 11:51:51 +0200 Subject: [PATCH] Access body element directly via id attribute --- lib/block-supports/index.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/block-supports/index.php b/lib/block-supports/index.php index dacacda38598a..e5291cddc3a2f 100644 --- a/lib/block-supports/index.php +++ b/lib/block-supports/index.php @@ -52,16 +52,22 @@ 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 DOMDocument::loadHTML warnings from polluting the front-end. $previous = libxml_use_internal_errors( true ); - $success = $dom->loadHTML( $wrapper_left . $block_content . $wrapper_right, LIBXML_HTML_NODEFDTD | LIBXML_COMPACT ); + $body_id = '__BLOCK_SUPPORTS_INJECTED_BODY_ID__'; + + // We need to wrap the block in order to handle UTF-8 properly. + $wrapped_block_html = + '' + . $block_content + . ''; + + $success = $dom->loadHTML( $wrapped_block_html, LIBXML_HTML_NODEFDTD | LIBXML_COMPACT ); // Clear errors and reset the use_errors setting. libxml_clear_errors(); @@ -71,7 +77,7 @@ function gutenberg_apply_block_supports( $block_content, $block ) { return $block_content; } - $body_element = $dom->getElementsByTagName( 'body' )[0]; + $body_element = $dom->getElementByID( $body_id ); $block_root = $body_element->childNodes[0]; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase if ( empty( $block_root ) ) {