Skip to content

Commit

Permalink
Replace usage in block supports elements too
Browse files Browse the repository at this point in the history
  • Loading branch information
dmsnell committed Dec 16, 2022
1 parent 85eeeb4 commit 7e7b3b8
Showing 1 changed file with 5 additions and 20 deletions.
25 changes: 5 additions & 20 deletions lib/block-supports/elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,14 @@ function gutenberg_render_elements_support( $block_content, $block ) {
return $block_content;
}

$class_name = gutenberg_get_elements_class_name( $block );

// Like the layout hook this assumes the hook only applies to blocks with a single wrapper.
// Retrieve the opening tag of the first HTML element.
$html_element_matches = array();
preg_match( '/<[^>]+>/', $block_content, $html_element_matches, PREG_OFFSET_CAPTURE );
$first_element = $html_element_matches[0][0];
// If the first HTML element has a class attribute just add the new class
// as we do on layout and duotone.
if ( str_contains( $first_element, 'class="' ) ) {
$content = preg_replace(
'/' . preg_quote( 'class="', '/' ) . '/',
'class="' . $class_name . ' ',
$block_content,
1
);
} else {
// If the first HTML element has no class attribute we should inject the attribute before the attribute at the end.
$first_element_offset = $html_element_matches[0][1];
$content = substr_replace( $block_content, ' class="' . $class_name . '"', $first_element_offset + strlen( $first_element ) - 1, 0 );
// Add the class name to the first element, presuming it's the wrapper, if it exists.
$tags = new WP_HTML_Tag_Processor( $block_content );
if ( $tags->next_tag() ) {
$tags->add_class( gutenberg_get_elements_class_name( $block ) );
}

return $content;
return $tags->get_updated_html();
}

/**
Expand Down

1 comment on commit 7e7b3b8

@github-actions
Copy link

@github-actions github-actions bot commented on 7e7b3b8 Dec 16, 2022

Choose a reason for hiding this comment

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

Flaky tests detected.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3716442762
📝 Reported issues:

Please sign in to comment.