@@ -56,29 +56,14 @@ function wp_render_elements_support( $block_content, $block ) {
56
56
return $ block_content ;
57
57
}
58
58
59
- $ class_name = wp_get_elements_class_name ( $ block );
60
-
61
59
// Like the layout hook this assumes the hook only applies to blocks with a single wrapper.
62
- // Retrieve the opening tag of the first HTML element.
63
- $ html_element_matches = array ();
64
- preg_match ( '/<[^>]+>/ ' , $ block_content , $ html_element_matches , PREG_OFFSET_CAPTURE );
65
- $ first_element = $ html_element_matches [0 ][0 ];
66
- // If the first HTML element has a class attribute just add the new class
67
- // as we do on layout and duotone.
68
- if ( strpos ( $ first_element , 'class=" ' ) !== false ) {
69
- $ content = preg_replace (
70
- '/ ' . preg_quote ( 'class=" ' , '/ ' ) . '/ ' ,
71
- 'class=" ' . $ class_name . ' ' ,
72
- $ block_content ,
73
- 1
74
- );
75
- } else {
76
- // If the first HTML element has no class attribute we should inject the attribute before the attribute at the end.
77
- $ first_element_offset = $ html_element_matches [0 ][1 ];
78
- $ content = substr_replace ( $ block_content , ' class=" ' . $ class_name . '" ' , $ first_element_offset + strlen ( $ first_element ) - 1 , 0 );
60
+ // Add the class name to the first element, presuming it's the wrapper, if it exists.
61
+ $ tags = new WP_HTML_Tag_Processor ( $ block_content );
62
+ if ( $ tags ->next_tag () ) {
63
+ $ tags ->add_class ( wp_get_elements_class_name ( $ block ) );
79
64
}
80
65
81
- return $ content ;
66
+ return $ tags -> get_updated_html () ;
82
67
}
83
68
84
69
/**
0 commit comments