Changeset 55235
- Timestamp:
- 02/06/2023 07:41:25 PM (22 months ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/block-supports/elements.php
r54874 r55235 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 -
trunk/tests/phpunit/tests/block-supports/elements.php
r55034 r55235 74 74 $this->assertSame( 75 75 $result, 76 '<p class=" wp-elements-1 has-dark-gray-background-color has-background">Hello <a href="http://www.wordpress.org/">WordPress</a>!</p>'76 '<p class="has-dark-gray-background-color has-background wp-elements-1">Hello <a href="http://www.wordpress.org/">WordPress</a>!</p>' 77 77 ); 78 78 } … … 104 104 $this->assertSame( 105 105 $result, 106 '<p id="anchor" class="wp-elements-1">Hello <a href="http://www.wordpress.org/">WordPress</a>!</p>'106 '<p class="wp-elements-1" id="anchor">Hello <a href="http://www.wordpress.org/">WordPress</a>!</p>' 107 107 ); 108 108 }
Note: See TracChangeset
for help on using the changeset viewer.