diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index ba63ddc331636..3b6ee11138000 100644 --- a/src/wp-includes/blocks.php +++ b/src/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 = '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; + $reprocess_token = false; + + while ( $reprocess_token || $processor->next_token() ) { + $reprocess_token = false; + + // 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() ) { + $reprocess_token = true; + 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' ) + ) { + $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 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 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/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(); 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 ) ); } /**