Make WordPress Core

Changeset 57506


Ignore:
Timestamp:
02/01/2024 12:10:19 AM (15 months ago)
Author:
dmsnell
Message:

HTML API: Fix CDATA lookalike matching invalid CDATA

When next_token() was introduced to the HTML Tag Processor, it started
classifying comments that look like they were intended to be CDATA sections.
In one of the changes made during development, however, a typo slipped
through code review that treated comments as CDATA even if they only
ended in ]> and not the required ]]>.

The consequences of this defect were minor because in all cases these are
treated as HTML comments from invalid syntax, but this patch adds the
missing check to ensure the proper reporting of CDATA-lookalikes.

Follow-up to [57348]

Props jonsurrell
Fixes #60406

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/html-api/class-wp-html-tag-processor.php

    r57489 r57506  
    17631763                    'A' === $html[ $this->token_starts_at + 7 ] &&
    17641764                    '[' === $html[ $this->token_starts_at + 8 ] &&
    1765                     ']' === $html[ $closer_at - 1 ]
     1765                    ']' === $html[ $closer_at - 1 ] &&
     1766                    ']' === $html[ $closer_at - 2 ]
    17661767                ) {
    17671768                    $this->parser_state    = self::STATE_COMMENT;
  • trunk/tests/phpunit/tests/html-api/wpHtmlTagProcessor-token-scanning.php

    r57348 r57506  
    349349
    350350    /**
     351     * Ensures that normative CDATA sections are properly parsed.
     352     *
     353     * @ticket 60406
     354     *
     355     * @since 6.5.0
     356     *
     357     * @covers WP_HTML_Tag_Processor::next_token
     358     */
     359    public function test_cdata_comment_with_incorrect_closer() {
     360        $processor = new WP_HTML_Tag_Processor( '<![CDATA[this is missing a closing square bracket]>' );
     361        $processor->next_token();
     362
     363        $this->assertSame(
     364            '#comment',
     365            $processor->get_token_name(),
     366            "Should have found comment token but found {$processor->get_token_name()} instead."
     367        );
     368
     369        $this->assertSame(
     370            WP_HTML_Processor::COMMENT_AS_INVALID_HTML,
     371            $processor->get_comment_type(),
     372            'Should have detected invalid HTML comment.'
     373        );
     374
     375        $this->assertSame(
     376            '[CDATA[this is missing a closing square bracket]',
     377            $processor->get_modifiable_text(),
     378            'Found incorrect modifiable text.'
     379        );
     380    }
     381
     382    /**
    351383     * Ensures that abruptly-closed CDATA sections are properly parsed as comments.
    352384     *
     
    365397            $processor->get_token_name(),
    366398            "Should have found a bogus comment but found {$processor->get_token_name()} instead."
     399        );
     400
     401        $this->assertSame(
     402            WP_HTML_Processor::COMMENT_AS_INVALID_HTML,
     403            $processor->get_comment_type(),
     404            'Should have detected invalid HTML comment.'
    367405        );
    368406
Note: See TracChangeset for help on using the changeset viewer.