Skip to content

Commit

Permalink
Only add layout classes to inner wrapper if block is a container. (#4…
Browse files Browse the repository at this point in the history
  • Loading branch information
tellthemachines authored Mar 1, 2023
1 parent 00e25b4 commit 58809f8
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,23 +301,6 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support
return '';
}

/**
* Gets classname from last tag in a string of HTML.
*
* @param string $html markup to be processed.
* @return string String of inner wrapper classnames.
*/
function gutenberg_get_classnames_from_last_tag( $html ) {
$tags = new WP_HTML_Tag_Processor( $html );
$last_classnames = '';

while ( $tags->next_tag() ) {
$last_classnames = $tags->get_attribute( 'class' );
}

return (string) $last_classnames;
}

/**
* Renders the layout config to the block wrapper.
*
Expand Down Expand Up @@ -502,7 +485,17 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {
* The first chunk of innerContent contains the block markup up until the inner blocks start.
* We want to target the opening tag of the inner blocks wrapper, which is the last tag in that chunk.
*/
$inner_content_classnames = isset( $block['innerContent'][0] ) && 'string' === gettype( $block['innerContent'][0] ) ? gutenberg_get_classnames_from_last_tag( $block['innerContent'][0] ) : '';
$inner_content_classnames = '';

if ( isset( $block['innerContent'][0] ) && 'string' === gettype( $block['innerContent'][0] ) && count( $block['innerContent'] ) > 1 ) {
$tags = new WP_HTML_Tag_Processor( $block['innerContent'][0] );
$last_classnames = '';
while ( $tags->next_tag() ) {
$last_classnames = $tags->get_attribute( 'class' );
}

$inner_content_classnames = (string) $last_classnames;
}

$content = $content_with_outer_classnames ? new WP_HTML_Tag_Processor( $content_with_outer_classnames ) : new WP_HTML_Tag_Processor( $block_content );

Expand Down

0 comments on commit 58809f8

Please sign in to comment.