Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions backport-changelog/7.1/11598.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/11598

* https://github.com/WordPress/gutenberg/pull/77408
23 changes: 21 additions & 2 deletions lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this is the "last" as in last in a list, or "Innermost"? Just getting my head around things 😄

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the last open tag in the string of markup that is $block['innerContent'][0].

* 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() ) ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we also have “special atomic elements” like SCRIPT and STYLE and others which have no closing tag. they wouldn’t be likely, but some could show up, such as TEXTAREA.

a more proper solution would use the WP_HTML_Processor->expects_closer() method to see if a closing tag is meant to appear. that will handle all of the wild edge cases, including knowing that IMG is a void element in HTML, but not if it appears within an SVG or MATH element.

we can use depth to track which elements are open and closed and limit that to elements with a class because the depth increases before visiting and opening tag and decreases before visiting a closing tag.

$first_chunk_processor = WP_HTML_Processor::create_fragment( $first_chunk );
$open_classes = array();
$open_depths  = array();
while ( $first_chunk_processor->next_token() ) {
	if ( '#tag' !== $first_chunk_processor->get_token_type() ) {
		continue;
	}

	$depth = $first_chunk_processor->get_current_depth();

	if ( $first_chunk_processor->is_tag_closer() ) {
		if ( $depth < ( end( $open_depths ) ?? 0 ) ) {
			array_pop( $open_depths );
		}
		continue;
	}

	if ( null === ( $classes = $first_chunk_processor->class_list() ) ) {
		continue;
	}

	$open_classes[] = iterator_to_array( $classes );
	$open_depths[]  = $depth;
}

$inner_block_wrapper_classes = end( $open_classes );

this highlights another existing issue, which is that we’re looking for exact string matches of the class attribute. in this snippet I’ve tossed in a change which gives us the ability to match any element containing all of the class names in the class attribute, not just exact matches.

this could be further refined by limiting these to names that start with some prefix, like wp-

$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;
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions phpunit/block-supports/layout-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,33 @@ public function data_layout_support_flag_renders_classnames_on_wrapper() {
),
'expected_output' => '<div class="wp-block-group is-layout-grid wp-container-core-group-is-layout-9d260ee2 wp-block-group-is-layout-grid"></div>',
),
/*
* 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' => '<div class="wp-block-group"><div class="wp-block-group__header">Header</div><p>Inner block</p></div>',
'block' => array(
'blockName' => 'core/group',
'attrs' => array(
'layout' => array(
'type' => 'default',
),
),
'innerBlocks' => array(),
'innerHTML' => '<div class="wp-block-group"><div class="wp-block-group__header">Header</div><p>Inner block</p></div>',
'innerContent' => array(
'<div class="wp-block-group"><div class="wp-block-group__header">Header</div>',
null,
'</div>',
),
),
),
'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>',
),
);
}

Expand Down
Loading