Make WordPress Core

Changeset 65560


Ignore:
Timestamp:
11/17/2026 06:55:10 PM (less than one hour ago)
Author:
dmsnell
Message:

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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r65317 r65560  
    23572357
    23582358/**
    2359  * Parses footnotes markup out of a content string,
    2360  * and renders those appropriate for the excerpt.
     2359 * Removes recognized footnote reference markup from a given string.
     2360 *
     2361 * Example:
     2362 *
     2363 *    $content = 'Never<sup data-fn="abc-123" class="fn"><a href="#abc-123" id="abc-123">1</a></sup> say never.';
     2364 *    'Never say never' === excerpt_remove_footnotes( $content );
    23612365 *
    23622366 * @since 6.3.0
    23632367 *
    2364  * @param string $content The content to parse.
    2365  * @return string The parsed and filtered content.
     2368 * @param string $content HTML which might contain footnote references from the block editor.
     2369 * @return string Given input HTML without the footnote references.
    23662370 */
    23672371function excerpt_remove_footnotes( $content ) {
    2368         if ( ! str_contains( $content, 'data-fn=' ) ) {
     2372        if ( ! str_contains( $content, 'data-fn' ) ) {
    23692373                return $content;
    23702374        }
    23712375
    2372         return preg_replace(
    2373                 '_<sup data-fn="[^"]+" class="[^"]+">\s*<a href="[^"]+" id="[^"]+">\d+</a>\s*</sup>_',
    2374                 '',
    2375                 $content
    2376         );
     2376        $processor = new class( $content ) extends WP_HTML_Tag_Processor {
     2377                public function is_whitespace_text_node(): bool {
     2378                        if ( self::STATE_TEXT_NODE !== $this->parser_state ) {
     2379                                return false;
     2380                        }
     2381
     2382                        $this->subdivide_text_appropriately();
     2383                        return self::TEXT_IS_WHITESPACE === $this->text_node_classification;
     2384                }
     2385
     2386                public function get_span(): WP_HTML_Span {
     2387                        $this->set_bookmark( 'here' );
     2388
     2389                        return $this->bookmarks['here'];
     2390                }
     2391        };
     2392
     2393        $output          = '';
     2394        $at              = 0;
     2395        $reprocess_token = false;
     2396
     2397        while ( $reprocess_token || $processor->next_token() ) {
     2398                $reprocess_token = false;
     2399
     2400                // Match the <sup data-fn="..." class="..."> opening tag.
     2401                if (
     2402                        $processor->is_tag_closer() ||
     2403                        'SUP' !== $processor->get_tag() ||
     2404                        null === $processor->get_attribute( 'data-fn' ) ||
     2405                        null === $processor->get_attribute( 'class' )
     2406                ) {
     2407                        continue;
     2408                }
     2409
     2410                $footnote_opening = $processor->get_span();
     2411
     2412                if ( ! $processor->next_token() ) {
     2413                        break;
     2414                }
     2415
     2416                // Skip any whitespace-only text between the tags.
     2417                if ( '#text' === $processor->get_token_name() ) {
     2418                        if ( ! $processor->is_whitespace_text_node() ) {
     2419                                $reprocess_token = true;
     2420                                continue;
     2421                        }
     2422
     2423                        $processor->next_token();
     2424                }
     2425
     2426                // Match the <a href="..." id="..."> opening tag.
     2427                if (
     2428                        $processor->is_tag_closer() ||
     2429                        'A' !== $processor->get_token_name() ||
     2430                        null === $processor->get_attribute( 'href' ) ||
     2431                        null === $processor->get_attribute( 'id' )
     2432                ) {
     2433                        $reprocess_token = true;
     2434                        continue;
     2435                }
     2436
     2437                // Match an all-digit text node.
     2438                if ( ! $processor->next_token() || '#text' !== $processor->get_token_name() ) {
     2439                        $reprocess_token = true;
     2440                        continue;
     2441                }
     2442
     2443                $footnote_number = $processor->get_modifiable_text();
     2444                $digit_count     = strspn( $footnote_number, '0123456789' );
     2445                if ( 0 === $digit_count || strlen( $footnote_number ) !== $digit_count ) {
     2446                        $reprocess_token = true;
     2447                        continue;
     2448                }
     2449
     2450                // Match the closing </a> tag.
     2451                if (
     2452                        ! $processor->next_token() ||
     2453                        'A' !== $processor->get_token_name() ||
     2454                        ! $processor->is_tag_closer()
     2455                ) {
     2456                        $reprocess_token = true;
     2457                        continue;
     2458                }
     2459
     2460                if ( ! $processor->next_token() ) {
     2461                        break;
     2462                }
     2463
     2464                // Skip any whitespace-only text between the tags.
     2465                if ( '#text' === $processor->get_token_name() ) {
     2466                        if ( ! $processor->is_whitespace_text_node() ) {
     2467                                $reprocess_token = true;
     2468                                continue;
     2469                        }
     2470
     2471                        $processor->next_token();
     2472                }
     2473
     2474                // Match the closing </sup> tag.
     2475                if (
     2476                        'SUP' !== $processor->get_token_name() ||
     2477                        ! $processor->is_tag_closer()
     2478                ) {
     2479                        $reprocess_token = true;
     2480                        continue;
     2481                }
     2482
     2483                $footnote_closing = $processor->get_span();
     2484
     2485                // Remove the section and continue.
     2486                $output .= substr( $content, $at, $footnote_opening->start - $at );
     2487                $at      = $footnote_closing->start + $footnote_closing->length;
     2488        }
     2489
     2490        // No footnotes were found and removed: return the original input.
     2491        if ( 0 === $at ) {
     2492                return $content;
     2493        }
     2494
     2495        if ( $at < strlen( $content ) ) {
     2496                $output .= substr( $content, $at );
     2497        }
     2498
     2499        return $output;
    23772500}
    23782501
  • trunk/src/wp-includes/html-api/class-wp-html-tag-processor.php

    r65388 r65560  
    957957         *
    958958         * @return bool Whether a token was parsed.
     959         *
     960         * @phpstan-impure
    959961         */
    960962        public function next_token(): bool {
  • trunk/tests/phpunit/tests/formatting/excerptRemoveFootnotes.php

    r64439 r65560  
    1717         */
    1818        public function test_remove_footnotes( $expected, $content ) {
    19                 $this->assertSame( $expected, excerpt_remove_footnotes( $content ) );
     19                $this->assertEqualHTML( $expected, excerpt_remove_footnotes( $content ) );
    2020        }
    2121
Note: See TracChangeset for help on using the changeset viewer.