Skip to content
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Move anonymous class inside image conditional
  • Loading branch information
SantosGuillamot committed Jul 1, 2024
commit 731d8699c34eef6f141e366c1dcb0300cf1ced56
65 changes: 33 additions & 32 deletions src/wp-includes/class-wp-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,46 +333,47 @@ private function replace_html( string $block_content, string $attribute_name, $s
switch ( $block_type->attributes[ $attribute_name ]['source'] ) {
case 'html':
case 'rich-text':
// Create private anonymous class until the HTML API provides `set_inner_html` method.
$bindings_processor = new class( $block_content, WP_HTML_Processor::CONSTRUCTOR_UNLOCK_CODE ) extends WP_HTML_Processor {
public function set_inner_text( $new_content ) {
$tag_name = $this->get_tag();
// Get position of the opener tag.
$this->set_bookmark( 'opener_tag' );
$opener_tag_bookmark = $this->bookmarks['_opener_tag'];

// Visit the closing tag.
if ( ! $this->next_tag(
array(
'tag_name' => $tag_name,
'tag_closers' => 'visit',
)
) || ! $this->is_tag_closer() ) {
$this->release_bookmark( 'opener_tag' );
return null;
}

// Get position of the closer tag.
$this->set_bookmark( 'closer_tag' );
$closer_tag_bookmark = $this->bookmarks['_closer_tag'];
if ( 'core/image' === $this->name && 'caption' === $attribute_name ) {
// Create private anonymous class until the HTML API provides `set_inner_html` method.
$bindings_processor = new class( $block_content, WP_HTML_Processor::CONSTRUCTOR_UNLOCK_CODE ) extends WP_HTML_Processor {
Copy link
Member

Choose a reason for hiding this comment

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

this is an interesting side-effect of that choice. I think we should rename that constant to something scarier, like what its value is.

I kind of like that it makes anonymous classes stand out more.

Copy link
Member

Choose a reason for hiding this comment

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

@sirreal I think this is a really good example of a part of the HTML Processor we're going to have to improve.

not only is the unlock code ugly here (ugly, because it shouts how this is work-around territory), but also by calling the constructor directly we bypass setting up the context node and the HTML node. in effect, this is starting as a full parser instead of a fragment parser.

I wonder how we can better allow subclassing without this hassle and risk.

public function set_inner_text( $new_content ) {
$tag_name = $this->get_tag();
// Get position of the opener tag.
$this->set_bookmark( 'opener_tag' );
$opener_tag_bookmark = $this->bookmarks['_opener_tag'];

// Visit the closing tag.
if ( ! $this->next_tag(
array(
'tag_name' => $tag_name,
'tag_closers' => 'visit',
)
) || ! $this->is_tag_closer() ) {
$this->release_bookmark( 'opener_tag' );
return null;
}

// Appends the new content.
$after_opener_tag = $opener_tag_bookmark->start + $opener_tag_bookmark->length;
$inner_content_length = $closer_tag_bookmark->start - $after_opener_tag;
$this->lexical_updates[] = new WP_HTML_Text_Replacement( $after_opener_tag, $inner_content_length, $new_content );
$this->release_bookmark( 'opener_tag' );
$this->release_bookmark( 'closer_tag' );
}
};
$block_reader = $bindings_processor::create_fragment( $block_content );
// Get position of the closer tag.
$this->set_bookmark( 'closer_tag' );
$closer_tag_bookmark = $this->bookmarks['_closer_tag'];

if ( 'core/image' === $this->name && 'caption' === $attribute_name ) {
// Appends the new content.
$after_opener_tag = $opener_tag_bookmark->start + $opener_tag_bookmark->length;
$inner_content_length = $closer_tag_bookmark->start - $after_opener_tag;
$this->lexical_updates[] = new WP_HTML_Text_Replacement( $after_opener_tag, $inner_content_length, $new_content );
$this->release_bookmark( 'opener_tag' );
$this->release_bookmark( 'closer_tag' );
}
};
$block_reader = $bindings_processor::create_fragment( $block_content );
if ( $block_reader->next_tag( 'figcaption' ) ) {
$block_reader->set_inner_text( wp_kses_post( $source_value ) );
}
return $block_reader->get_updated_html();
}

$block_reader = new WP_HTML_Tag_Processor( $block_content );

// TODO: Support for CSS selectors whenever they are ready in the HTML API.
// In the meantime, support comma-separated selectors by exploding them into an array.
$selectors = explode( ',', $block_type->attributes[ $attribute_name ]['selector'] );
Expand Down