Skip to content

Commit 2021ed8

Browse files
SantosGuillamottalldancbravobernal
authored
Block Bindings: Simplify the HTML replacement logic until the HTML API is ready (#61236)
* Use hardcoded selectors and regex * Support <button> or <a> tags Co-authored-by: SantosGuillamot <santosguillamot@git.wordpress.org> Co-authored-by: talldan <talldanwp@git.wordpress.org> Co-authored-by: cbravobernal <cbravobernal@git.wordpress.org>
1 parent ab49143 commit 2021ed8

File tree

1 file changed

+17
-58
lines changed

1 file changed

+17
-58
lines changed

lib/compat/wordpress-6.5/blocks.php

+17-58
Original file line numberDiff line numberDiff line change
@@ -65,68 +65,27 @@ function gutenberg_block_bindings_replace_html( $block_content, $block_name, str
6565
switch ( $block_type->attributes[ $attribute_name ]['source'] ) {
6666
case 'html':
6767
case 'rich-text':
68-
$block_reader = new WP_HTML_Tag_Processor( $block_content );
69-
70-
// TODO: Support for CSS selectors whenever they are ready in the HTML API.
71-
// In the meantime, support comma-separated selectors by exploding them into an array.
72-
$selectors = explode( ',', $block_type->attributes[ $attribute_name ]['selector'] );
73-
// Add a bookmark to the first tag to be able to iterate over the selectors.
74-
$block_reader->next_tag();
75-
$block_reader->set_bookmark( 'iterate-selectors' );
76-
77-
// TODO: This shouldn't be needed when the `set_inner_html` function is ready.
78-
// Store the parent tag and its attributes to be able to restore them later in the button.
79-
// The button block has a wrapper while the paragraph and heading blocks don't.
80-
if ( 'core/button' === $block_name ) {
81-
$button_wrapper = $block_reader->get_tag();
82-
$button_wrapper_attribute_names = $block_reader->get_attribute_names_with_prefix( '' );
83-
$button_wrapper_attrs = array();
84-
foreach ( $button_wrapper_attribute_names as $name ) {
85-
$button_wrapper_attrs[ $name ] = $block_reader->get_attribute( $name );
86-
}
68+
// Hardcode the selectors and processing until the HTML API is able to read CSS selectors and replace inner HTML.
69+
// TODO: Use the HTML API instead.
70+
if ( 'core/paragraph' === $block_name && 'content' === $attribute_name ) {
71+
$selector = 'p';
8772
}
88-
89-
foreach ( $selectors as $selector ) {
90-
// If the parent tag, or any of its children, matches the selector, replace the HTML.
91-
if ( strcasecmp( $block_reader->get_tag( $selector ), $selector ) === 0 || $block_reader->next_tag(
92-
array(
93-
'tag_name' => $selector,
94-
)
95-
) ) {
96-
$block_reader->release_bookmark( 'iterate-selectors' );
97-
98-
// TODO: Use `set_inner_html` method whenever it's ready in the HTML API.
99-
// Until then, it is hardcoded for the paragraph, heading, and button blocks.
100-
// Store the tag and its attributes to be able to restore them later.
101-
$selector_attribute_names = $block_reader->get_attribute_names_with_prefix( '' );
102-
$selector_attrs = array();
103-
foreach ( $selector_attribute_names as $name ) {
104-
$selector_attrs[ $name ] = $block_reader->get_attribute( $name );
105-
}
106-
$selector_markup = "<$selector>" . wp_kses_post( $source_value ) . "</$selector>";
107-
$amended_content = new WP_HTML_Tag_Processor( $selector_markup );
108-
$amended_content->next_tag();
109-
foreach ( $selector_attrs as $attribute_key => $attribute_value ) {
110-
$amended_content->set_attribute( $attribute_key, $attribute_value );
111-
}
112-
if ( 'core/paragraph' === $block_name || 'core/heading' === $block_name ) {
113-
return $amended_content->get_updated_html();
114-
}
115-
if ( 'core/button' === $block_name ) {
116-
$button_markup = "<$button_wrapper>{$amended_content->get_updated_html()}</$button_wrapper>";
117-
$amended_button = new WP_HTML_Tag_Processor( $button_markup );
118-
$amended_button->next_tag();
119-
foreach ( $button_wrapper_attrs as $attribute_key => $attribute_value ) {
120-
$amended_button->set_attribute( $attribute_key, $attribute_value );
121-
}
122-
return $amended_button->get_updated_html();
123-
}
73+
if ( 'core/heading' === $block_name && 'content' === $attribute_name ) {
74+
$selector = 'h[1-6]';
75+
}
76+
if ( 'core/button' === $block_name && 'text' === $attribute_name ) {
77+
// Check if it is a <button> or <a> tag.
78+
if ( preg_match( '/<button[^>]*>.*?<\/button>/', $block_content ) ) {
79+
$selector = 'button';
12480
} else {
125-
$block_reader->seek( 'iterate-selectors' );
81+
$selector = 'a';
12682
}
12783
}
128-
$block_reader->release_bookmark( 'iterate-selectors' );
129-
return $block_content;
84+
if ( empty( $selector ) ) {
85+
return $block_content;
86+
}
87+
$pattern = '/(<' . $selector . '[^>]*>).*?(<\/' . $selector . '>)/i';
88+
return preg_replace( $pattern, '$1' . wp_kses_post( $source_value ) . '$2', $block_content );
13089

13190
case 'attribute':
13291
$amended_content = new WP_HTML_Tag_Processor( $block_content );

0 commit comments

Comments
 (0)