From b187df818db92099c12803fe58a9622e53f0d37b Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 16 Dec 2022 13:37:42 -0700 Subject: [PATCH] Block Settings/Support: Use Tag Processor to inject class name on wrapper. When we introduced #42124 new block supports behavior we did so with a PCRE replacement that opened the possibility for a few bugs related to processing the HTML attributes. It was noted in that PR that this would be a good candidate for the `WP_HTML_Tag_Processor`. In this patch we're performing that replacement as follow-up work. This should improve the reliability and hopefully the readability of what is being done to the HTML as it renders. --- lib/block-supports/elements.php | 25 +++++-------------------- lib/block-supports/settings.php | 25 +++++-------------------- 2 files changed, 10 insertions(+), 40 deletions(-) diff --git a/lib/block-supports/elements.php b/lib/block-supports/elements.php index 0b6e31db3d66d3..f55f087b0be82c 100644 --- a/lib/block-supports/elements.php +++ b/lib/block-supports/elements.php @@ -49,29 +49,14 @@ function gutenberg_render_elements_support( $block_content, $block ) { return $block_content; } - $class_name = gutenberg_get_elements_class_name( $block ); - // Like the layout hook this assumes the hook only applies to blocks with a single wrapper. - // Retrieve the opening tag of the first HTML element. - $html_element_matches = array(); - preg_match( '/<[^>]+>/', $block_content, $html_element_matches, PREG_OFFSET_CAPTURE ); - $first_element = $html_element_matches[0][0]; - // If the first HTML element has a class attribute just add the new class - // as we do on layout and duotone. - if ( str_contains( $first_element, 'class="' ) ) { - $content = preg_replace( - '/' . preg_quote( 'class="', '/' ) . '/', - 'class="' . $class_name . ' ', - $block_content, - 1 - ); - } else { - // If the first HTML element has no class attribute we should inject the attribute before the attribute at the end. - $first_element_offset = $html_element_matches[0][1]; - $content = substr_replace( $block_content, ' class="' . $class_name . '"', $first_element_offset + strlen( $first_element ) - 1, 0 ); + // Add the class name to the first element, presuming it's the wrapper, if it exists. + $tags = new WP_HTML_Tag_Processor( $block_content ); + if ( $tags->next_tag() ) { + $tags->add_class( gutenberg_get_elements_class_name( $block ) ); } - return $content; + return $tags->get_updated_html(); } /** diff --git a/lib/block-supports/settings.php b/lib/block-supports/settings.php index 60900629c04cf1..89b47f2e9a062b 100644 --- a/lib/block-supports/settings.php +++ b/lib/block-supports/settings.php @@ -43,29 +43,14 @@ function _gutenberg_add_block_level_presets_class( $block_content, $block ) { return $block_content; } - $class_name = _gutenberg_get_presets_class_name( $block ); - // Like the layout hook this assumes the hook only applies to blocks with a single wrapper. - // Retrieve the opening tag of the first HTML element. - $html_element_matches = array(); - preg_match( '/<[^>]+>/', $block_content, $html_element_matches, PREG_OFFSET_CAPTURE ); - $first_element = $html_element_matches[0][0]; - // If the first HTML element has a class attribute just add the new class - // as we do on layout and duotone. - if ( strpos( $first_element, 'class="' ) !== false ) { - $content = preg_replace( - '/' . preg_quote( 'class="', '/' ) . '/', - 'class="' . $class_name . ' ', - $block_content, - 1 - ); - } else { - // If the first HTML element has no class attribute we should inject the attribute before the attribute at the end. - $first_element_offset = $html_element_matches[0][1]; - $content = substr_replace( $block_content, ' class="' . $class_name . '"', $first_element_offset + strlen( $first_element ) - 1, 0 ); + // Add the class name to the first element, presuming it's the wrapper, if it exists. + $tags = new WP_HTML_Tag_Processor( $block_content ); + if ( $tags->next_tag() ) { + $tags->add_class( _gutenberg_get_presets_class_name( $block ) ); } - return $content; + return $tags->get_updated_html(); } /**