Skip to content
Open
Show file tree
Hide file tree
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
Use alternative method
  • Loading branch information
SantosGuillamot committed Jul 1, 2024
commit ac434829403543765ede11db4e56f36a4ef628db
60 changes: 23 additions & 37 deletions src/wp-includes/class-wp-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,55 +348,41 @@ private function replace_html( string $block_content, string $attribute_name, $s
* @param string $new_content New content to insert in the figcaption element.
* @return bool Whether the inner content was properly replaced.
*/
public function set_content_between_figcaption_balanced_tags( $new_content ) {
/*
* THIS IS A STOP-GAP MEASURE NOT TO BE EMULATED.
*
* Check that the processor is paused on an opener tag.
*
*/
if (
WP_HTML_Processor::STATE_MATCHED_TAG !== $this->parser_state ||
'FIGCAPTION' !== $this->get_tag() ||
$this->is_tag_closer()
) {
public function set_figcaption_inner_text( $new_content ) {
// Check that the processor is paused on an opener tag.
if ( 'FIGCAPTION' !== $this->get_tag() || $this->is_tag_closer() ) {
return false;
}

// Set position of the opener tag.
$this->set_bookmark( 'opener_tag' );

/*
* This is a best-effort guess to visit the closer tag and check it exists.
* In the future, this code should rely on the HTML Processor for this kind of operation.
*/
$tag_name = $this->get_tag();
if ( ! $this->next_tag(
array(
'tag_name' => $tag_name,
'tag_closers' => 'visit',
)
) || ! $this->is_tag_closer() ) {
// Once this element closes the depth will be one shallower than it is now.
$depth = $this->get_current_depth();
while ( $this->next_token() && $this->get_current_depth() >= $depth ) {
// This is inside the FIGCAPTION element.
}

if ( null !== $this->get_last_error() || $this->paused_at_incomplete_token() ) {
return false;
}

// Set position of the closer tag.
$this->set_bookmark( 'closer_tag' );
$this->set_bookmark( 'here' );

// Get opener and closer tag bookmarks.
$opener_tag_bookmark = $this->bookmarks['_opener_tag'];
$closer_tag_bookmark = $this->bookmarks['_closer_tag'];
$opening = $this->bookmarks[ $this->current_element->token->bookmark_name ];
$closing = $this->bookmarks['_here'];
$start = $opening->start + $opening->length;

$this->lexical_updates[] = new WP_HTML_Text_Replacement(
$start,
$closing->start - $start,
wp_kses_post( $new_content )
);

// 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 );
return true;
}
};
$block_reader = $bindings_processor::create_fragment( $block_content );

$block_reader = $bindings_processor::create_fragment( $block_content );
Copy link
Member

Choose a reason for hiding this comment

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

whoah. this is unrelated to your changes @SantosGuillamot, but this line really confuses me, purely based on how PHP's anonymous classes work. @sirreal maybe what I wrote above is fine, and when we call new class( ...$args ) they don't matter?

yes, I just tested and this is odd. calling $bindings_processor->next_token() will crash because it never finds the context node. this means that it looks like an HTML Processor but isn't. it's better as a HTML Processor Builder.

@SantosGuillamot I wonder if we could take advantage of this in line 338 and instead of passing $block_content pass something like "do not use this, it will not work. it's only here to create a subclass and call the static creator method"

Copy link
Author

Choose a reason for hiding this comment

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

I am not 100% this is what you meant, but I made this change to address the feedback. Let me know if that's not what you were suggesting.

if ( $block_reader->next_tag( 'figcaption' ) ) {
$block_reader->set_content_between_figcaption_balanced_tags( wp_kses_post( $source_value ) );
$block_reader->set_figcaption_inner_text( $source_value );
}
return $block_reader->get_updated_html();
}
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/html-api/class-wp-html-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
*
* @var ?WP_HTML_Stack_Event
*/
private $current_element = null;
public $current_element = null;

/**
* Context node if created as a fragment parser.
Expand Down