Make WordPress Core

Opened 4 weeks ago

Closed 7 days ago

Last modified 5 days ago

#61617 closed enhancement (fixed)

HTML API: Add method to replace modifiable text.

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

Description

The HTML API has provided get_modifiable_text() since WordPress 6.5, but it has never provided a method to change the modifiable text for a given token in the document.

It should provide this function to make it possible to safely replace HTML text nodes.

Change History (7)

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


4 weeks ago
#1

  • Keywords has-unit-tests added

Trac ticket: Core-61617

This patch introduces a new method, set_modifiable_text() to the Tag Processor, which makes it possible and safe to replace text nodes within an HTML document, performing the appropriate escaping.

This method can be used in conjunction with other code to modify the text content of a document, and can be used for transforming HTML in a streaming fashion.

@zieladam commented on PR #7007:


3 weeks ago
#2

get_updated_html() might need this adjustment:

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

Without it, replacing a longer string with a shorter one can make $this->bytes_already_parsed negative.

@dmsnell commented on PR #7007:


10 days ago
#3

get_updated_html() might need this adjustment:

Thanks @adamziel - this level of review is so helpful! Unfortunately, when I added a test to try and trigger the condition, I couldn't. What do you think? Do you see any problem with this?

https://github.com/WordPress/wordpress-develop/pull/7007/files#diff-ef817e9b61dcdc18d5e1a84bfbb55370916b179cfed01eab03121456d7ce847cR42-R86

#4 @dmsnell
7 days ago

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

In 58829:

HTML API: Add set_modifiable_text() for replacing text nodes.

This patch introduces a new method, set_modifiable_text() to the
Tag Processor, which makes it possible and safe to replace text nodes
within an HTML document, performing the appropriate escaping.

This method can be used in conjunction with other code to modify the
text content of a document, and can be used for transforming HTML
in a streaming fashion.

Developed in https://github.com/wordpress/wordpress-develop/pull/7007
Discussed in https://core.trac.wordpress.org/ticket/61617

Props: dmsnell, gziolo, zieladam.
Fixes #61617.

@zieladam commented on PR #7007:


5 days ago
#6

Thanks @adamziel - this level of review is so helpful! Unfortunately, when I added a test to try and trigger the condition, I couldn't. What do you think? Do you see any problem with this?

That test looks good. I'd also check for regular tags like DIV and then for HTML comments. I ran into that in https://github.com/adamziel/site-transfer-protocol, although now can't find the commit where I did it. Eventually I've switched to a different method of setting modifiable text, and now I'm happy to migrate to this now that it's merged.

@dmsnell commented on PR #7007:


5 days ago
#7

@adamziel if you ever find this problem again I'll be happy to fix it ASAP

Note: See TracTickets for help on using tickets.