Skip to content
Draft
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
Next Next commit
Add html api processor for setting inner html
  • Loading branch information
sirreal committed Nov 27, 2024
commit b49b3174a27e677d35fc87b035add949745a9edc
111 changes: 111 additions & 0 deletions src/wp-includes/class-wp-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,3 +607,114 @@ public function render( $options = array() ) {
return $block_content;
}
}

// phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound
class PrivateProcessor 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.

#6838 uses a pattern for a private processor class:

$bindings_processor_builder = new class(
'Do not use this, it will not work. It is only here to create a subclass and call the static creator method',
WP_HTML_Processor::CONSTRUCTOR_UNLOCK_CODE
) extends WP_HTML_Processor {


public function set_inner_html( ?string $html ) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This method allows does "naive" inner HTML replacement, where the content that should be inside the node can change the HTML tree beyond the node.

This is also true with the existing implementation so it's not a regression and can be dealt with now or in follow-up work.

if ( $this->is_virtual() ) {
return false;
}

if ( $this->get_token_type() !== '#tag' ) {
return false;
}

if ( $this->is_tag_closer() ) {
return false;
}

if ( ! $this->expects_closer() ) {
return false;
}

// @todo check if this is necessary
/*if (*/
/* 'html' !== $this->state->current_token->namespace &&*/
/* $this->state->current_token->has_self_closing_flag*/
/*) {*/
/* return false;*/
/*}*/

if ( null === $html ) {
$html = '';
}
if ( '' !== $html ) {
$fragment_parser = $this->spawn_fragment_parser( $html );
if (
null === $fragment_parser
) {
return false;
}

try {
$html = $fragment_parser->serialize();
} catch ( Exception $e ) {
return false;
}
}

// @todo apply modifications if there are any???
if ( ! parent::set_bookmark( 'SET_INNER_HTML: opener' ) ) {
return false;
}

if ( ! $this->proceed_to_matching_closer() ) {
parent::seek( 'SET_INNER_HTML: opener' );
return false;
}

if ( ! parent::set_bookmark( 'SET_INNER_HTML: closer' ) ) {
return false;
}

$inner_html_start = $this->bookmarks['SET_INNER_HTML: opener']->start + $this->bookmarks['SET_INNER_HTML: opener']->length;
$inner_html_length = $this->bookmarks['SET_INNER_HTML: closer']->start - $inner_html_start;

$this->lexical_updates['innerHTML'] = new WP_HTML_Text_Replacement(
$inner_html_start,
$inner_html_length,
$html
);

parent::seek( 'SET_INNER_HTML: opener' );
parent::release_bookmark( 'SET_INNER_HTML: opener' );
parent::release_bookmark( 'SET_INNER_HTML: closer' );

// @todo check for whether that html will make a mess!
// Will it break out of tags?
return true;
}

public function proceed_to_matching_closer(): bool {
$tag_name = $this->get_tag();

if ( null === $tag_name ) {
return false;
}

if ( $this->is_tag_closer() ) {
return false;
}

if ( ! $this->expects_closer() ) {
return false;
}

$breadcrumbs = $this->get_breadcrumbs();
array_pop( $breadcrumbs );

// @todo Can't use these queries together
while ( $this->next_tag(
array(
'tag_name' => $this->get_tag(),
'tag_closers' => 'visit',
)
) ) {
if ( $this->get_breadcrumbs() === $breadcrumbs ) {
return true;
}
}
return false;
}
}
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 @@ -844,7 +844,7 @@ public function is_tag_closer(): bool {
*
* @return bool Whether the current token is virtual.
*/
private function is_virtual(): bool {
protected function is_virtual(): bool {
return (
isset( $this->current_element->provenance ) &&
'virtual' === $this->current_element->provenance
Expand Down