From 2e78d3a09061dea937ab59b76302cff27a13e04d Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Thu, 16 Apr 2026 14:36:11 +1000 Subject: [PATCH 1/4] Ensure layout classnames are applied to the inner blocks wrapper and not to its siblings --- lib/block-supports/layout.php | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/block-supports/layout.php b/lib/block-supports/layout.php index 61208294f7a22a..9eb469e845a9d0 100644 --- a/lib/block-supports/layout.php +++ b/lib/block-supports/layout.php @@ -1050,10 +1050,30 @@ 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(); + $html_void_elements = array( 'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr' ); + while ( $first_chunk_processor->next_tag( array( 'tag_closers' => 'visit' ) ) ) { + if ( $first_chunk_processor->is_tag_closer() ) { + array_pop( $tag_stack ); + } elseif ( ! in_array( strtolower( $first_chunk_processor->get_tag() ), $html_void_elements, true ) ) { + $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; } } } From 5ce4f945cd6a9249bc41e1f1500af2dfcf148009 Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Thu, 16 Apr 2026 15:29:53 +1000 Subject: [PATCH 2/4] Add test --- phpunit/block-supports/layout-test.php | 27 ++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) 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' => '
Header

Inner block

', + 'block' => array( + 'blockName' => 'core/group', + 'attrs' => array( + 'layout' => array( + 'type' => 'default', + ), + ), + 'innerBlocks' => array(), + 'innerHTML' => '
Header

Inner block

', + 'innerContent' => array( + '
Header
', + null, + '
', + ), + ), + ), + 'expected_output' => '
Header

Inner block

', + ), ); } From bed4e36b6e593cafe34c8a2122729abb03e373d7 Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Fri, 17 Apr 2026 11:59:45 +1000 Subject: [PATCH 3/4] use is_void --- lib/block-supports/layout.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/block-supports/layout.php b/lib/block-supports/layout.php index 9eb469e845a9d0..09a40261296449 100644 --- a/lib/block-supports/layout.php +++ b/lib/block-supports/layout.php @@ -1061,12 +1061,11 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) { * The last unclosed element with a class attribute is the best candidate * for the inner-block wrapper. */ - $tag_stack = array(); - $html_void_elements = array( 'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr' ); + $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 ( ! in_array( strtolower( $first_chunk_processor->get_tag() ), $html_void_elements, true ) ) { + } elseif ( ! WP_HTML_Processor::is_void( $first_chunk_processor->get_tag() ) ) { $tag_stack[] = $first_chunk_processor->get_attribute( 'class' ); } } From f795b39048f31a85fd139371998874b89daf3aa2 Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Mon, 20 Apr 2026 10:21:06 +1000 Subject: [PATCH 4/4] add changelog --- backport-changelog/7.1/11598.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 backport-changelog/7.1/11598.md 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