Make WordPress Core


Ignore:
Timestamp:
04/24/2024 07:43:02 AM (2 years ago)
Author:
dmsnell
Message:

HTML API: Fix detection of single-length funky comments.

Since [60428] the Tag Processor has been misidentifying single-character
funky comments. It has been asserting that the full token-length for a
funky comment must be at least three characters after the opening (e.g.
</1>), but it has been starting to look for the closing > after
those same three characters. This means that it has been skipping the
actual close of these funky comments and swallowing up the next syntax
until it finds a >, often consuming the next tag in the process.

This patch fixes the detector and restores finding the following token.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/6412
Discussed in https://core.trac.wordpress.org/ticket/60170

Follow-up to [60428].
Fixes #60170.
Props dmsnell, gziolo, jonsurrell.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/html-api/wpHtmlTagProcessor-token-scanning.php

    r57508 r58040  
    762762
    763763        /**
     764         * Ensures that various funky comments are properly parsed.
     765         *
     766         * @ticket 60170
     767         *
     768         * @since 6.6.0
     769         *
     770         * @covers WP_HTML_Tag_Processor::next_token
     771         *
     772         * @dataProvider data_various_funky_comments
     773         *
     774         * @param string $funky_comment_html HTML containing a funky comment.
     775         * @param string $modifiable_text    Expected modifiable text of first funky comment in HTML.
     776         */
     777        public function test_various_funky_comments( $funky_comment_html, $modifiable_text ) {
     778                $processor = new WP_HTML_Tag_Processor( $funky_comment_html );
     779                while ( '#funky-comment' !== $processor->get_token_type() && $processor->next_token() ) {
     780                        continue;
     781                }
     782
     783                $this->assertSame(
     784                        '#funky-comment',
     785                        $processor->get_token_type(),
     786                        'Failed to find the expected funky comment.'
     787                );
     788
     789                $this->assertSame(
     790                        $modifiable_text,
     791                        $processor->get_modifiable_text(),
     792                        'Found the wrong modifiable text span inside a funky comment.'
     793                );
     794        }
     795
     796        /**
     797         * Data provider.
     798         *
     799         * @return array[].
     800         */
     801        public static function data_various_funky_comments() {
     802                return array(
     803                        'Space'          => array( '</ >', ' ' ),
     804                        'Short-bang'     => array( '</!>', '!' ),
     805                        'Question mark'  => array( '</?>', '?' ),
     806                        'Short-slash'    => array( '<//>', '/' ),
     807                        'Bit (no attrs)' => array( '<//wp:post-meta>', '/wp:post-meta' ),
     808                        'Bit (attrs)'    => array( '<//wp:post-meta key=isbn>', '/wp:post-meta key=isbn' ),
     809                        'Curly-wrapped'  => array( '</{json}>', '{json}' ),
     810                        'Before P'       => array( '</1><p>', '1' ),
     811                        'After P'        => array( '<p></__("Read more")></p>', '__("Read more")' ),
     812                        'Reference'      => array( '</&gt;>', '&gt;' ),
     813                );
     814        }
     815
     816        /**
    764817         * Test helper that wraps a string in double quotes.
    765818         *
Note: See TracChangeset for help on using the changeset viewer.