Skip to content

Commit 13b5b66

Browse files
committed
Implement naive set_inner_html
1 parent 2ae4473 commit 13b5b66

File tree

1 file changed

+105
-1
lines changed

1 file changed

+105
-1
lines changed

src/wp-includes/html-api/class-wp-html-processor.php

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,109 @@ public static function create_full_parser( $html, $known_definite_encoding = 'UT
355355
return $processor;
356356
}
357357

358+
public function set_inner_html( $html ) {
359+
if ( $this->is_virtual() ) {
360+
return false;
361+
}
362+
363+
if ( $this->get_token_type() !== '#tag' ) {
364+
return false;
365+
}
366+
367+
if ( $this->is_tag_closer() ) {
368+
return false;
369+
}
370+
371+
if ( ! $this->expects_closer() ) {
372+
return false;
373+
}
374+
375+
if (
376+
'html' !== $this->state->current_token->namespace &&
377+
$this->state->current_token->has_self_closing_flag
378+
) {
379+
return false;
380+
}
381+
382+
$html_for_replacement = $this->normalize( $html );
383+
if ( empty( $html_for_replacement ) ) {
384+
return false;
385+
}
386+
387+
// @todo apply modifications if there are any???
388+
389+
if ( ! parent::set_bookmark( 'SET_INNER_HTML: opener' ) ) {
390+
return false;
391+
}
392+
393+
if ( ! $this->seek_to_matching_closer() ) {
394+
parent::seek( 'SET_INNER_HTML: opener' );
395+
return false;
396+
}
397+
398+
if ( ! parent::set_bookmark( 'SET_INNER_HTML: closer' ) ) {
399+
return false;
400+
}
401+
402+
$inner_html_start = $this->bookmarks['SET_INNER_HTML: opener']->start + $this->bookmarks['SET_INNER_HTML: opener']->length;
403+
$inner_html_length = $this->bookmarks['SET_INNER_HTML: closer']->start - $inner_html_start;
404+
405+
echo 'INNER HTML: ' . substr( $this->html, $inner_html_start, $inner_html_length ) . "\n";
406+
407+
echo "BEFORE:\n";
408+
var_dump( $this->get_updated_html() );
409+
410+
$this->lexical_updates['innerHTML'] = new WP_HTML_Text_Replacement(
411+
$inner_html_start,
412+
$inner_html_length,
413+
$html_for_replacement
414+
);
415+
416+
parent::seek( 'SET_INNER_HTML: opener' );
417+
parent::release_bookmark( 'SET_INNER_HTML: opener' );
418+
parent::release_bookmark( 'SET_INNER_HTML: closer' );
419+
echo "AFTER:\n";
420+
var_dump( $this->get_updated_html() );
421+
422+
// @todo check for whether that html will make a mess!
423+
// Will it break out of tags?
424+
425+
return true;
426+
}
427+
428+
public function seek_to_matching_closer(): bool {
429+
$tag_name = $this->get_tag();
430+
431+
if ( null === $tag_name ) {
432+
return false;
433+
}
434+
435+
if ( $this->is_tag_closer() ) {
436+
return false;
437+
}
438+
439+
if ( ! $this->expects_closer() ) {
440+
return false;
441+
}
442+
443+
$breadcrumbs = $this->breadcrumbs;
444+
array_pop( $breadcrumbs );
445+
446+
// @todo Can't use these queries together
447+
while ( $this->next_tag(
448+
array(
449+
'tag_name' => $this->get_tag(),
450+
'tag_closers' => 'visit',
451+
)
452+
) ) {
453+
if ( $this->get_breadcrumbs() === $breadcrumbs ) {
454+
return true;
455+
}
456+
}
457+
return false;
458+
}
459+
460+
358461
/**
359462
* Constructor.
360463
*
@@ -522,6 +625,7 @@ public function get_unsupported_exception() {
522625
* 1 for "first" tag, 3 for "third," etc.
523626
* Defaults to first tag.
524627
* @type string|null $class_name Tag must contain this whole class name to match.
628+
* @type string $tag_name Tag name to match.
525629
* @type string[] $breadcrumbs DOM sub-path at which element is found, e.g. `array( 'FIGURE', 'IMG' )`.
526630
* May also contain the wildcard `*` which matches a single element, e.g. `array( 'SECTION', '*' )`.
527631
* }
@@ -545,7 +649,7 @@ public function next_tag( $query = null ): bool {
545649
}
546650

547651
if ( is_string( $query ) ) {
548-
$query = array( 'breadcrumbs' => array( $query ) );
652+
$query = array( 'tag_name' => $query );
549653
}
550654

551655
if ( ! is_array( $query ) ) {

0 commit comments

Comments
 (0)