Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 133 additions & 10 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() ) {

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

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;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/wp-includes/html-api/class-wp-html-tag-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/formatting/excerptRemoveFootnotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}

/**
Expand Down
Loading