Commit 9e47b825ae for wordpress.org

commit 9e47b825aed7383403e8f6ae9c1c42aebeffea06
Author: dmsnell <dmsnell@git.wordpress.org>
Date:   Tue Nov 17 18:57:56 2026 +0000

    HTML API: Refactor `excerpt_remove_footnotes()`.

    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: https://github.com/WordPress/wordpress-develop/pull/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

diff --git a/wp-includes/blocks.php b/wp-includes/blocks.php
index ba63ddc331..3b6ee11138 100644
--- a/wp-includes/blocks.php
+++ b/wp-includes/blocks.php
@@ -2356,24 +2356,147 @@ 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 = 'Never<sup data-fn="abc-123" class="fn"><a href="#abc-123" id="abc-123">1</a></sup> 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(
-		'_<sup data-fn="[^"]+" class="[^"]+">\s*<a href="[^"]+" id="[^"]+">\d+</a>\s*</sup>_',
-		'',
-		$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;
+	$reprocess_token = false;
+
+	while ( $reprocess_token || $processor->next_token() ) {
+		$reprocess_token = false;
+
+		// Match the <sup data-fn="..." class="..."> 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() ) {
+				$reprocess_token = true;
+				continue;
+			}
+
+			$processor->next_token();
+		}
+
+		// Match the <a href="..." id="..."> opening tag.
+		if (
+			$processor->is_tag_closer() ||
+			'A' !== $processor->get_token_name() ||
+			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;
+		}
+
+		// Match the closing </a> tag.
+		if (
+			! $processor->next_token() ||
+			'A' !== $processor->get_token_name() ||
+			! $processor->is_tag_closer()
+		) {
+			$reprocess_token = true;
+			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() ) {
+				$reprocess_token = true;
+				continue;
+			}
+
+			$processor->next_token();
+		}
+
+		// Match the closing </sup> tag.
+		if (
+			'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 - $at );
+		$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;
 }

 /**
diff --git a/wp-includes/html-api/class-wp-html-tag-processor.php b/wp-includes/html-api/class-wp-html-tag-processor.php
index 7e047c4808..34429f2b31 100644
--- a/wp-includes/html-api/class-wp-html-tag-processor.php
+++ b/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();
diff --git a/wp-includes/version.php b/wp-includes/version.php
index 3bf8dd975c..2058456f56 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -16,7 +16,7 @@
  *
  * @global string $wp_version
  */
-$wp_version = '7.2-RC1-65559';
+$wp_version = '7.2-RC1-65560';

 /**
  * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.