From 586166be81b03b2ef094af90b50d9f598ee89649 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Wed, 11 Nov 2026 22:31:04 -0700 Subject: [PATCH 1/5] HTML API: Refactor `excerpt_remove_footnotes(). 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. --- src/wp-includes/blocks.php | 134 ++++++++++++++++++++++++++++++++++--- 1 file changed, 124 insertions(+), 10 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index ba63ddc331636..bd719d41d3511 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -2356,24 +2356,138 @@ function excerpt_remove_blocks( $content ) { } /** - * Parses footnotes markup out of a content string, - * and renders those appropriate for the excerpt. + * Removes recognized footnote reference markup from a given string. + * + * Example: + * + * $content = 'Never1 say never.'; + * 'Never say never' === excerpt_remove_footnotes( $content ); * * @since 6.3.0 * - * @param string $content The content to parse. - * @return string The parsed and filtered content. + * @param string $content HTML which might contain footnote references from the block editor. + * @return string Given input HTML without the footnote references. */ function excerpt_remove_footnotes( $content ) { - if ( ! str_contains( $content, 'data-fn=' ) ) { + if ( ! str_contains( $content, 'data-fn' ) ) { return $content; } - return preg_replace( - '_\s*\d+\s*_', - '', - $content - ); + $processor = new class( $content ) extends WP_HTML_Tag_Processor { + public function is_whitespace_text_node(): bool { + if ( self::STATE_TEXT_NODE !== $this->parser_state ) { + return false; + } + + $this->subdivide_text_appropriately(); + return self::TEXT_IS_WHITESPACE === $this->text_node_classification; + } + + public function get_span(): WP_HTML_Span { + $this->set_bookmark( 'here' ); + + return $this->bookmarks['here']; + } + }; + + $output = ''; + $at = 0; + + while ( $processor->next_token() ) { + // Match the opening tag. + if ( + $processor->is_tag_closer() || + 'SUP' !== $processor->get_tag() || + null === $processor->get_attribute( 'data-fn' ) || + null === $processor->get_attribute( 'class' ) + ) { + continue; + } + + $footnote_opening = $processor->get_span(); + + if ( ! $processor->next_token() ) { + break; + } + + // Skip any whitespace-only text between the tags. + if ( '#text' === $processor->get_token_name() ) { + if ( ! $processor->is_whitespace_text_node() ) { + continue; + } + + $processor->next_token(); + } + + // Match the opening tag. + if ( + $processor->is_tag_closer() || + 'A' !== $processor->get_token_name() || + null === $processor->get_attribute( 'href' ) || + null === $processor->get_attribute( 'id' ) + ) { + continue; + } + + // Skip an all-digit text node. + if ( ! $processor->next_token() || '#text' !== $processor->get_token_name() ) { + continue; + } + + $footnote_number = $processor->get_modifiable_text(); + $digit_count = strspn( $footnote_number, '0123456789' ); + if ( 0 === $digit_count || strlen( $footnote_number ) !== $digit_count ) { + continue; + } + + // Match the closing tag. + if ( + ! $processor->next_token() || + 'A' !== $processor->get_token_name() || + ! $processor->is_tag_closer() + ) { + continue; + } + + if ( ! $processor->next_token() ) { + break; + } + + // Skip any whitespace-only text between the tags. + if ( '#text' === $processor->get_token_name() ) { + if ( ! $processor->is_whitespace_text_node() ) { + continue; + } + + $processor->next_token(); + } + + // Match the closing tag. + if ( + ! $processor->next_token() || + 'SUP' !== $processor->get_token_name() || + ! $processor->is_tag_closer() + ) { + continue; + } + + $footnote_closing = $processor->get_span(); + + // Remove the section and continue. + $output .= substr( $content, $at, $footnote_opening->start ); + $at = $footnote_closing->start + $footnote_closing->length; + } + + // No footnotes were found and removed: return the original input. + if ( 0 === $at ) { + return $content; + } + + if ( $at < strlen( $content ) ) { + $output .= substr( $content, $at ); + } + + return $output; } /** From 91dee96d305e09bbc996722510249837ff95418f Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Tue, 17 Nov 2026 08:57:02 -0700 Subject: [PATCH 2/5] Assert equivalent HTML in tests. --- tests/phpunit/tests/formatting/excerptRemoveFootnotes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/formatting/excerptRemoveFootnotes.php b/tests/phpunit/tests/formatting/excerptRemoveFootnotes.php index eabf3bfc2f47d..97066c5e407db 100644 --- a/tests/phpunit/tests/formatting/excerptRemoveFootnotes.php +++ b/tests/phpunit/tests/formatting/excerptRemoveFootnotes.php @@ -16,7 +16,7 @@ class Tests_Formatting_ExcerptRemoveFootnotes extends WP_UnitTestCase { * @param string $content Content to run strip_shortcodes() on. */ public function test_remove_footnotes( $expected, $content ) { - $this->assertSame( $expected, excerpt_remove_footnotes( $content ) ); + $this->assertEqualHTML( $expected, excerpt_remove_footnotes( $content ) ); } /** From 2a8284eaa793ee2e49fdc2547ede32a17d8446ae Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Tue, 17 Nov 2026 09:18:31 -0700 Subject: [PATCH 3/5] Need not advance twice into closing tags. --- src/wp-includes/blocks.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index bd719d41d3511..68a676690097f 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -2429,7 +2429,7 @@ public function get_span(): WP_HTML_Span { continue; } - // Skip an all-digit text node. + // Match an all-digit text node. if ( ! $processor->next_token() || '#text' !== $processor->get_token_name() ) { continue; } @@ -2464,7 +2464,6 @@ public function get_span(): WP_HTML_Span { // Match the closing tag. if ( - ! $processor->next_token() || 'SUP' !== $processor->get_token_name() || ! $processor->is_tag_closer() ) { From 9717f408a0b66f97c9f14d3cfa2220a77efde0bf Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Tue, 17 Nov 2026 09:51:00 -0700 Subject: [PATCH 4/5] Copy only required content; reprocess token when matches fail. --- src/wp-includes/blocks.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 68a676690097f..3b6ee11138000 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -2390,10 +2390,13 @@ public function get_span(): WP_HTML_Span { } }; - $output = ''; - $at = 0; + $output = ''; + $at = 0; + $reprocess_token = false; + + while ( $reprocess_token || $processor->next_token() ) { + $reprocess_token = false; - while ( $processor->next_token() ) { // Match the opening tag. if ( $processor->is_tag_closer() || @@ -2413,6 +2416,7 @@ public function get_span(): WP_HTML_Span { // Skip any whitespace-only text between the tags. if ( '#text' === $processor->get_token_name() ) { if ( ! $processor->is_whitespace_text_node() ) { + $reprocess_token = true; continue; } @@ -2426,17 +2430,20 @@ public function get_span(): WP_HTML_Span { null === $processor->get_attribute( 'href' ) || null === $processor->get_attribute( 'id' ) ) { + $reprocess_token = true; continue; } // Match an all-digit text node. if ( ! $processor->next_token() || '#text' !== $processor->get_token_name() ) { + $reprocess_token = true; continue; } $footnote_number = $processor->get_modifiable_text(); $digit_count = strspn( $footnote_number, '0123456789' ); if ( 0 === $digit_count || strlen( $footnote_number ) !== $digit_count ) { + $reprocess_token = true; continue; } @@ -2446,6 +2453,7 @@ public function get_span(): WP_HTML_Span { 'A' !== $processor->get_token_name() || ! $processor->is_tag_closer() ) { + $reprocess_token = true; continue; } @@ -2456,6 +2464,7 @@ public function get_span(): WP_HTML_Span { // Skip any whitespace-only text between the tags. if ( '#text' === $processor->get_token_name() ) { if ( ! $processor->is_whitespace_text_node() ) { + $reprocess_token = true; continue; } @@ -2467,13 +2476,14 @@ public function get_span(): WP_HTML_Span { 'SUP' !== $processor->get_token_name() || ! $processor->is_tag_closer() ) { + $reprocess_token = true; continue; } $footnote_closing = $processor->get_span(); // Remove the section and continue. - $output .= substr( $content, $at, $footnote_opening->start ); + $output .= substr( $content, $at, $footnote_opening->start - $at ); $at = $footnote_closing->start + $footnote_closing->length; } From 67d6fc9341b8ae444adefeb6baa97609733cf09f Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Tue, 17 Nov 2026 09:52:42 -0700 Subject: [PATCH 5/5] =?UTF-8?q?=EF=BF=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/wp-includes/html-api/class-wp-html-tag-processor.php | 2 ++ 1 file changed, 2 insertions(+) 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 7e047c4808081..34429f2b31aed 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 @@ public function next_tag( $query = null ): bool { * @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();