Skip to content

Commit 6b684b7

Browse files
Ensure layout classnames are applied to the inner blocks wrapper
1 parent bccb9c1 commit 6b684b7

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

src/wp-includes/block-supports/layout.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,10 +1008,29 @@ function wp_render_layout_support_flag( $block_content, $block ) {
10081008
$first_chunk = $block['innerContent'][0] ?? null;
10091009
if ( is_string( $first_chunk ) && count( $block['innerContent'] ) > 1 ) {
10101010
$first_chunk_processor = new WP_HTML_Tag_Processor( $first_chunk );
1011-
while ( $first_chunk_processor->next_tag() ) {
1012-
$class_attribute = $first_chunk_processor->get_attribute( 'class' );
1011+
/*
1012+
* Use a stack to track open elements as tags are visited. Void elements
1013+
* (those without a matching closing tag) are excluded so they don't
1014+
* accumulate on the stack. At the end of the chunk, every element still
1015+
* on the stack is unclosed — meaning its closing tag lives in a later
1016+
* innerContent entry alongside the inner blocks, which makes it the
1017+
* inner-block container. Elements that open and close within this chunk
1018+
* are siblings that precede the inner blocks and should be ignored.
1019+
* The last unclosed element with a class attribute is the best candidate
1020+
* for the inner-block wrapper.
1021+
*/
1022+
$tag_stack = array();
1023+
while ( $first_chunk_processor->next_tag( array( 'tag_closers' => 'visit' ) ) ) {
1024+
if ( $first_chunk_processor->is_tag_closer() ) {
1025+
array_pop( $tag_stack );
1026+
} elseif ( ! WP_HTML_Processor::is_void( $first_chunk_processor->get_tag() ) ) {
1027+
$tag_stack[] = $first_chunk_processor->get_attribute( 'class' );
1028+
}
1029+
}
1030+
foreach ( array_reverse( $tag_stack ) as $class_attribute ) {
10131031
if ( is_string( $class_attribute ) && ! empty( $class_attribute ) ) {
10141032
$inner_block_wrapper_classes = $class_attribute;
1033+
break;
10151034
}
10161035
}
10171036
}

tests/phpunit/tests/block-supports/layout.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,27 @@ public function data_layout_support_flag_renders_classnames_on_wrapper() {
354354
),
355355
'expected_output' => '<p>A paragraph</p>',
356356
),
357+
'outer wrapper targeted when sibling element precedes inner blocks' => array(
358+
'args' => array(
359+
'block_content' => '<div class="wp-block-group"><div class="wp-block-group__header">Header</div><p>Inner block</p></div>',
360+
'block' => array(
361+
'blockName' => 'core/group',
362+
'attrs' => array(
363+
'layout' => array(
364+
'type' => 'default',
365+
),
366+
),
367+
'innerBlocks' => array(),
368+
'innerHTML' => '<div class="wp-block-group"><div class="wp-block-group__header">Header</div><p>Inner block</p></div>',
369+
'innerContent' => array(
370+
'<div class="wp-block-group"><div class="wp-block-group__header">Header</div>',
371+
null,
372+
'</div>',
373+
),
374+
),
375+
),
376+
'expected_output' => '<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow"><div class="wp-block-group__header">Header</div><p>Inner block</p></div>',
377+
),
357378
);
358379
}
359380

0 commit comments

Comments
 (0)