Skip to content

Conversation

@t-hamano
Copy link
Contributor

@t-hamano t-hamano commented Nov 7, 2025

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


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@t-hamano
Copy link
Contributor Author

t-hamano commented Nov 7, 2025

I'll prepare the commit message now, ready for when I commit this pull request.

Command Palette: Use WP_HTML_Processor and WP_HTML_Decoder for menu labels and URLs.

Replace regex-based HTML tag removal with WP_HTML_Processor to properly extract text nodes from menu labels. This ensures only root-level text nodes are
collected.

Additionally, replace html_entity_decode() with WP_HTML_Decoder::decode_attribute() for URL decoding to use the modern HTML API for consistent attribute decoding.

Follow-up to [61124], [61126], [61127], [61142].

Props: dmsnell, madhavishah01, peterwilsoncc, wildworks.
Fixes #64177, #64196.

@t-hamano t-hamano requested a review from dmsnell November 7, 2025 03:51
@t-hamano t-hamano marked this pull request as ready for review November 7, 2025 03:51
@github-actions
Copy link

github-actions bot commented Nov 7, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props wildworks, mukesh27, peterwilsoncc, dmsnell.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link

github-actions bot commented Nov 7, 2025

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@t-hamano
Copy link
Contributor Author

@dmsnell @mukeshpanchal27 @peterwilsoncc If there are no other blockers, I'd like to commit this before the RC1 release, but what do you think? This PR can be considered a code quality improvement, so if there are any blockers, we can punt it to a future release.

}

return trim( implode( '', $text_parts ) );
};
Copy link
Member

Choose a reason for hiding this comment

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

hey nice job @t-hamano getting this built. I hope it wasn’t too obscure to figure out.

this looks like it should be solid, but I can share a couple of points of feedback.

Finding root-level text nodes

when creating a fragment (with the default <body> context) we will always have an open HTML element and BODY element, meaning that root-level text will always have a depth of 3 (and likewise, the breadcrumb depth will be three).

this means we can eliminate the nested loop and directly check if the depth is 3. we don’t have to capture the root depth. that open HTML and BODY are guarantees with how it works.

On the other hand, we can also test this via the breadcrumbs. I found an issue that we should probably change/fix on matches_breadcrumbs(), because that won’t work here, but for the time being this would.

while ( $processor->next_token() ) {
	if ( array( 'HTML', 'BODY', '#text' ) !== $processor->get_breadcrumbs() ) {
		continue;
	}

	$text_parts…
}

Efficiency and reliability

The use of the HTML Processor is particularly convenient because it provides depth automatically. On the other hand, if you find that it’s too slow or fails too frequently (because it receives the fraction of input documents it can’t parse) then we can still adjust the lever on the reliability/practicality spectrum. The Tag Processor will not fail with the same parsing issues the PCRE matches did, even though that can lead to some kinds of parsing failures (with, for example, mismatched tags).

Still, the Tag Processor won’t fail a parse each token and is considerably faster than the fully-fledged HTML Processor. If we were to choose this approach, we’d want to manually track depth, which again, could be wrong because HTML is so wonderfully complex (vs. the HTML Processor which will not be wrong here).

$processor = new WP_HTML_Tag_Processor( $label );
$depth     = 0;
while ( $processor->next_token() ) {
	$token_name = $processor->get_token_name();

	if ( '#text' === $token_name && 0 === $depth ) {
		$text_parts…
		continue;
	}

	if ( $processor->is_closing_tag() ) {
		--$depth;
	} else if ( ! WP_HTML_Processor::is_void( $token_name ) ) {
		++$depth;
	}
}

The choice is up to you. The only thing I’d watch out for is that occasionally we get things like “nested” A tags, and those can cause the HTML Processor to abort out of caution.

@dmsnell
Copy link
Member

dmsnell commented Nov 12, 2025

@t-hamano let me know your preferences on this based on my feedback. I’m happy to approve the work if we want to get it in still, understanding that it’s now after the RC1 deadline. I believe that with a couple of sign-offs we can still do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants