diff --git a/backport-changelog/7.1/11598.md b/backport-changelog/7.1/11598.md new file mode 100644 index 00000000000000..9af91ca5769748 --- /dev/null +++ b/backport-changelog/7.1/11598.md @@ -0,0 +1,3 @@ +https://github.com/WordPress/wordpress-develop/pull/11598 + +* https://github.com/WordPress/gutenberg/pull/77408 \ No newline at end of file diff --git a/lib/block-supports/layout.php b/lib/block-supports/layout.php index 61208294f7a22a..09a40261296449 100644 --- a/lib/block-supports/layout.php +++ b/lib/block-supports/layout.php @@ -1050,10 +1050,29 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) { $first_chunk = $block['innerContent'][0] ?? null; if ( is_string( $first_chunk ) && count( $block['innerContent'] ) > 1 ) { $first_chunk_processor = new WP_HTML_Tag_Processor( $first_chunk ); - while ( $first_chunk_processor->next_tag() ) { - $class_attribute = $first_chunk_processor->get_attribute( 'class' ); + /* + * Use a stack to track open elements as tags are visited. Void elements + * (those without a matching closing tag) are excluded so they don't + * accumulate on the stack. At the end of the chunk, every element still + * on the stack is unclosed — meaning its closing tag lives in a later + * innerContent entry alongside the inner blocks, which makes it the + * inner-block container. Elements that open and close within this chunk + * are siblings that precede the inner blocks and should be ignored. + * The last unclosed element with a class attribute is the best candidate + * for the inner-block wrapper. + */ + $tag_stack = array(); + while ( $first_chunk_processor->next_tag( array( 'tag_closers' => 'visit' ) ) ) { + if ( $first_chunk_processor->is_tag_closer() ) { + array_pop( $tag_stack ); + } elseif ( ! WP_HTML_Processor::is_void( $first_chunk_processor->get_tag() ) ) { + $tag_stack[] = $first_chunk_processor->get_attribute( 'class' ); + } + } + foreach ( array_reverse( $tag_stack ) as $class_attribute ) { if ( is_string( $class_attribute ) && ! empty( $class_attribute ) ) { $inner_block_wrapper_classes = $class_attribute; + break; } } } diff --git a/phpunit/block-supports/layout-test.php b/phpunit/block-supports/layout-test.php index ced43c418f1457..bb2dd790c60ac0 100644 --- a/phpunit/block-supports/layout-test.php +++ b/phpunit/block-supports/layout-test.php @@ -564,6 +564,33 @@ public function data_layout_support_flag_renders_classnames_on_wrapper() { ), 'expected_output' => '
', ), + /* + * When the first innerContent chunk contains a sibling element (one that fully opens + * and closes before the inner blocks), the layout classes must be added to the outer + * container — not to the sibling. The sibling's class was incorrectly chosen by the + * previous logic because it was the last class encountered while scanning the chunk. + */ + 'outer wrapper targeted when sibling element precedes inner blocks' => array( + 'args' => array( + 'block_content' => 'Inner block
Inner block
Inner block