Skip to content

Commit

Permalink
Implement naive set_inner_html
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed Oct 9, 2024
1 parent 2ae4473 commit 13b5b66
Showing 1 changed file with 105 additions and 1 deletion.
106 changes: 105 additions & 1 deletion src/wp-includes/html-api/class-wp-html-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,109 @@ public static function create_full_parser( $html, $known_definite_encoding = 'UT
return $processor;
}

public function set_inner_html( $html ) {
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;
}

if (
'html' !== $this->state->current_token->namespace &&
$this->state->current_token->has_self_closing_flag
) {
return false;
}

$html_for_replacement = $this->normalize( $html );
if ( empty( $html_for_replacement ) ) {
return false;
}

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

if ( ! $this->seek_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;

echo 'INNER HTML: ' . substr( $this->html, $inner_html_start, $inner_html_length ) . "\n";

echo "BEFORE:\n";
var_dump( $this->get_updated_html() );

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

parent::seek( 'SET_INNER_HTML: opener' );
parent::release_bookmark( 'SET_INNER_HTML: opener' );
parent::release_bookmark( 'SET_INNER_HTML: closer' );
echo "AFTER:\n";
var_dump( $this->get_updated_html() );

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

public function seek_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->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;
}


/**
* Constructor.
*
Expand Down Expand Up @@ -522,6 +625,7 @@ public function get_unsupported_exception() {
* 1 for "first" tag, 3 for "third," etc.
* Defaults to first tag.
* @type string|null $class_name Tag must contain this whole class name to match.
* @type string $tag_name Tag name to match.
* @type string[] $breadcrumbs DOM sub-path at which element is found, e.g. `array( 'FIGURE', 'IMG' )`.
* May also contain the wildcard `*` which matches a single element, e.g. `array( 'SECTION', '*' )`.
* }
Expand All @@ -545,7 +649,7 @@ public function next_tag( $query = null ): bool {
}

if ( is_string( $query ) ) {
$query = array( 'breadcrumbs' => array( $query ) );
$query = array( 'tag_name' => $query );
}

if ( ! is_array( $query ) ) {
Expand Down

0 comments on commit 13b5b66

Please sign in to comment.