Repository navigation
Ensure layout classnames are applied to the inner blocks wrapper and not to its siblings #77408
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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() ) ) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we also have “special atomic elements” like a more proper solution would use the 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 this could be further refined by limiting these to names that start with some prefix, like |
||
| $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; | ||
| } | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
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 😄
There was a problem hiding this comment.
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].