Skip to content

HTML API: Refactor `excerpt_remove_footnotes(). - #15917

Closed
dmsnell wants to merge 5 commits into
WordPress:trunkfrom
dmsnell:html-api/refactor-excerpt-remove-footnotes
Closed

dmsnell wants to merge 5 commits into
WordPress:trunkfrom
dmsnell:html-api/refactor-excerpt-remove-footnotes

Conversation

@dmsnell

@dmsnell dmsnell commented Nov 12, 2026

Copy link
Copy Markdown
Member

Trac ticket: Core-66668.

Follow-up to r64233.

Attribute ordering in HTML is not guaranteed, but the PCRE pattern in excerpt_remove_footnotes() was hard-coded to expect this. The wp_kses() refactor exposed this as it happened to reorder attributes that are saved on footnote references in the block editor.

This patch replaces the PCRE match in excerpt_remove_footnotes() with the HTML API and matching code to detect the intended pattern from the original implementation, making it more robust against lexical variations of equivalent HTML structure.

@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

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

Props dmsnell, westonruter.

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@github-actions

This comment has been minimized.

@dmsnell

dmsnell commented Nov 12, 2026

Copy link
Copy Markdown
Member Author

@tellthemachines assuming that I’ll make sure all the tests pass, would you be interested in giving this a look-over? we had some failures due to reordering of the attributes on <sup> with the wp_kses() rewrite in #9248

@github-actions

Copy link
Copy Markdown

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

  • 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.

@dmsnell

dmsnell commented Nov 12, 2026

Copy link
Copy Markdown
Member Author

the level of failure of PHPStan here to analyze this change is impressive, and yet I realize that somehow that’s the intended behavior, which is baffling that any imaginable type system for an object-oriented language would assume that method calls are pure and disconnected from internal state on their object.

@dmsnell

dmsnell commented Nov 12, 2026

Copy link
Copy Markdown
Member Author

@westonruter here is another case where the following config change would improve the default effectiveness of the type checker.

parameters:
    rememberPossiblyImpureFunctionValues: false

if we switched that, it would seem like the type check would actually lead us to consider cases we might assume a type is one thing when it could be more than one thing, which would be safety by default.

the analysis of this PR is another example where the type checker is instructing us to enter defects into code and encouraging loss of trust in its utility.

maybe we would have a big PR removing all those @phpstan-impure comments (a huge boon IMO), and have to deal with all sorts of new potential type errors that are really potential errors instead of false positives, but that seems like the kind of thing we want in a type system.

@westonruter

Copy link
Copy Markdown
Member

@dmsnell:

maybe we would have a big PR removing all those @phpstan-impure comments (a huge boon IMO), and have to deal with all sorts of new potential type errors that are really potential errors instead of false positives, but that seems like the kind of thing we want in a type system.

I've got two PRs open to explore two ends of this, the first one you'll hate and the second one I think you'll love:

Trac ticket: Core-66668.

Follow-up to r64233.

Attribute ordering in HTML is not guaranteed, but the PCRE pattern in
`excerpt_remove_footnotes()` was hard-coded to expect this. The
`wp_kses()` refactor exposed this as it happened to reorder attributes
that are saved on footnote references in the block editor.

This patch replaces the PCRE match in `excerpt_remove_footnotes()` with
the HTML API and matching code to detect the intended pattern from the
original implementation, making it more robust against lexical
variations of equivalent HTML structure.
@dmsnell
dmsnell force-pushed the html-api/refactor-excerpt-remove-footnotes branch from 6c0d460 to 91dee96 Compare November 17, 2026 15:58

$footnote_opening = $processor->get_span();

if ( ! $processor->next_token() ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The PHPStan errors can all be resolved in the short term via:

diff --git a/src/wp-includes/html-api/class-wp-html-tag-processor.php b/src/wp-includes/html-api/class-wp-html-tag-processor.php
index 7e047c4808..34429f2b31 100644
--- a/src/wp-includes/html-api/class-wp-html-tag-processor.php
+++ b/src/wp-includes/html-api/class-wp-html-tag-processor.php
@@ -956,6 +956,8 @@ class WP_HTML_Tag_Processor {
 	 * @since 7.1.0 Recognizes processing instructions.
 	 *
 	 * @return bool Whether a token was parsed.
+	 *
+	 * @phpstan-impure
 	 */
 	public function next_token(): bool {
 		return $this->base_class_next_token();

Since \WP_HTML_Tag_Processor::parse_next_tag() and \WP_HTML_Tag_Processor::next_tag() both have this annotation, it's not really out of the norm. If we flip off rememberPossiblyImpureFunctionValues then they can all be removed together.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I held back adding another goto. I will bite my tongue and add this and try to hide my shame.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

thanks @westonruter

@cbravobernal cbravobernal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. I compared it with the old regex on several inputs, and it accepts other attribute orders, quotes and casing.

Not blocking: a test case with class before data-fn would fail with the old regex.

wporg-sync pushed a commit that referenced this pull request Nov 17, 2026
Attribute ordering in HTML is not guaranteed, but the PCRE pattern in `excerpt_remove_footnotes()` was hard-coded to expect a specific order. The `wp_kses()` refactor reorders attributes saved in footnote references from the block editor, triggering a failure to remove them from excerpts.

This patch replaces the PCRE match in `excerpt_remove_footnotes()` with HTML API matching code, which detects the intended pattern from the original implementation, making it more robust against lexical variations of equivalent HTML structure.

Developed in: #15917
Discussed in: https://core.trac.wordpress.org/ticket/66668

Follow-up to r64233.

Props cbravobernal, dmsnell, tbshiki, westonruter.
Fixes #66668. See #66208.


git-svn-id: https://develop.svn.wordpress.org/trunk@65560 602fd350-edb4-49c9-b593-d223f7449a82
@github-actions

Copy link
Copy Markdown

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 65560
GitHub commit: 0bab8d2

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions Bot closed this Nov 17, 2026
wporg-sync pushed a commit to WordPress/WordPress that referenced this pull request Nov 17, 2026
Attribute ordering in HTML is not guaranteed, but the PCRE pattern in `excerpt_remove_footnotes()` was hard-coded to expect a specific order. The `wp_kses()` refactor reorders attributes saved in footnote references from the block editor, triggering a failure to remove them from excerpts.

This patch replaces the PCRE match in `excerpt_remove_footnotes()` with HTML API matching code, which detects the intended pattern from the original implementation, making it more robust against lexical variations of equivalent HTML structure.

Developed in: WordPress/wordpress-develop#15917
Discussed in: https://core.trac.wordpress.org/ticket/66668

Follow-up to r64233.

Props cbravobernal, dmsnell, tbshiki, westonruter.
Fixes #66668. See #66208.

Built from https://develop.svn.wordpress.org/trunk@65560


git-svn-id: http://core.svn.wordpress.org/trunk@64633 1a063a9b-81f0-0310-95a4-ce76da25c4cd
@dmsnell
dmsnell deleted the html-api/refactor-excerpt-remove-footnotes branch November 17, 2026 19:00
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.

3 participants