Make WordPress Core

Opened 9 months ago

Closed 9 months ago

Last modified 9 months ago

#62241 closed defect (bug) (fixed)

HTML API: Fix the position update after changing the modifiable text when length differs

Reported by: gziolo's profile gziolo Owned by: gziolo's profile gziolo
Milestone: 6.7 Priority: normal
Severity: normal Version: 6.7
Component: HTML API Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

Reported by @zieladam on GitHub: https://github.com/WordPress/wordpress-develop/pull/7007#issuecomment-2412540433

I just isolated the problem:

$p = new WP_HTML_Tag_Processor('Hello there');
$p->next_token();
$p->set_modifiable_text('Short');
echo $p->get_updated_html();

It's not occurring with this patch:

-               $this->bytes_already_parsed = $before_current_tag;
+               if ($this->get_token_type() === '#tag') {
+                       $this->bytes_already_parsed = $before_current_tag;
+               }

The actual fix will be more nuanced because the text node might appear in the HTML in the middle or at the end of the HTML provided, examples:

<p>Hello</p>String to replace
<p>Hello</p>String to replace<p>Hello</p>

Change History (6)

This ticket was mentioned in PR #7577 on WordPress/wordpress-develop by @gziolo.


9 months ago
#1

  • Keywords has-patch has-unit-tests added

Reported by @adamziel in https://github.com/WordPress/wordpress-develop/pull/7007#issuecomment-2412540433:

I just isolated the problem:

$p = new WP_HTML_Tag_Processor('Hello there');
$p->next_token();
$p->set_modifiable_text('Short');
echo $p->get_updated_html();

It's not occurring with this patch:

-               $this->bytes_already_parsed = $before_current_tag;
+               if ($this->get_token_type() === '#tag') {
+                       $this->bytes_already_parsed = $before_current_tag;
+               }

Trac ticket: https://core.trac.wordpress.org/ticket/62241

@jonsurrell commented on PR #7577:


9 months ago
#2

@gziolo I've proposed a fix for this in https://github.com/gziolo/wordpress-develop/pull/2 that can be merged into this PR.

@gziolo commented on PR #7577:


9 months ago
#3

@sirreal, I would appreciate another look after changes applied to unit tests.

#4 @gziolo
9 months ago

  • Owner set to gziolo
  • Resolution set to fixed
  • Status changed from new to closed

In 59250:

HTML API: Fix the position update after changing the modifiable text when length differs

There was an edge case detected for updating the text placed without the wrapping HTML tag with set_modifiable_text.

Props gziolo, jonsurrell.
Fixes #62241.

@zieladam commented on PR #7577:


9 months ago
#6

Thank you so much for a timely fix!

Note: See TracTickets for help on using tickets.