Make WordPress Core

Changeset 55469


Ignore:
Timestamp:
03/06/2023 06:53:38 PM (17 months ago)
Author:
hellofromTonya
Message:

HTML API: Fix finding RCData and Script tag closers.

Fixes finding the following tag closers </script>, </textarea>, and </title> in WP_HTML_Tag_Processor.

Follow-up to [55407], [55203].

Props zieladam, dmsnell, hellofromTonya.
Fixes #57852.
See #57575.

Location:
trunk
Files:
2 edited

Legend:

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

    r55407 r55469  
    776776            }
    777777
    778             $at += 2;
     778            $closer_potentially_starts_at = $at;
     779            $at                          += 2;
    779780
    780781            /*
     
    819820
    820821            if ( '>' === $html[ $at ] || '/' === $html[ $at ] ) {
    821                 ++$this->bytes_already_parsed;
     822                $this->bytes_already_parsed = $closer_potentially_starts_at;
    822823                return true;
    823824            }
     
    888889
    889890            if ( '/' === $html[ $at ] ) {
    890                 $is_closing = true;
     891                $closer_potentially_starts_at = $at - 1;
     892                $is_closing                   = true;
    891893                ++$at;
    892894            } else {
     
    939941
    940942            if ( $is_closing ) {
    941                 $this->bytes_already_parsed = $at;
     943                $this->bytes_already_parsed = $closer_potentially_starts_at;
    942944                if ( $this->bytes_already_parsed >= $doc_length ) {
    943945                    return false;
     
    949951
    950952                if ( '>' === $html[ $this->bytes_already_parsed ] ) {
    951                     ++$this->bytes_already_parsed;
     953                    $this->bytes_already_parsed = $closer_potentially_starts_at;
    952954                    return true;
    953955                }
  • trunk/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php

    r55315 r55469  
    431431    /**
    432432     * @ticket 56299
     433     * @ticket 57852
    433434     *
    434435     * @covers WP_HTML_Tag_Processor::next_tag
     
    460461        );
    461462        $this->assertTrue( $p->is_tag_closer(), 'Indicated a tag closer is a tag opener' );
     463
     464        $p = new WP_HTML_Tag_Processor( '<div>' );
     465        $this->assertTrue( $p->next_tag( array( 'tag_closers' => 'visit' ) ), "Did not find a tag opener when tag_closers was set to 'visit'" );
     466        $this->assertFalse( $p->next_tag( array( 'tag_closers' => 'visit' ) ), "Found a closer where there wasn't one" );
     467    }
     468
     469    /**
     470     * @ticket 57852
     471     *
     472     * @covers WP_HTML_Tag_Processor::next_tag
     473     * @covers WP_HTML_Tag_Processor::is_tag_closer
     474     */
     475    public function test_next_tag_should_stop_on_rcdata_and_script_tag_closers_when_requested() {
     476        $p = new WP_HTML_Tag_Processor( '<script>abc</script>' );
     477
     478        $p->next_tag();
     479        $this->assertTrue( $p->next_tag( array( 'tag_closers' => 'visit' ) ), 'Did not find the </script> tag closer' );
     480        $this->assertTrue( $p->is_tag_closer(), 'Indicated a <script> tag opener is a tag closer' );
     481
     482        $p = new WP_HTML_Tag_Processor( 'abc</script>' );
     483        $this->assertTrue( $p->next_tag( array( 'tag_closers' => 'visit' ) ), 'Did not find the </script> tag closer when there was no tag opener' );
     484
     485        $p = new WP_HTML_Tag_Processor( '<textarea>abc</textarea>' );
     486
     487        $p->next_tag();
     488        $this->assertTrue( $p->next_tag( array( 'tag_closers' => 'visit' ) ), 'Did not find the </textarea> tag closer' );
     489        $this->assertTrue( $p->is_tag_closer(), 'Indicated a <textarea> tag opener is a tag closer' );
     490
     491        $p = new WP_HTML_Tag_Processor( 'abc</textarea>' );
     492        $this->assertTrue( $p->next_tag( array( 'tag_closers' => 'visit' ) ), 'Did not find the </textarea> tag closer when there was no tag opener' );
     493
     494        $p = new WP_HTML_Tag_Processor( '<title>abc</title>' );
     495
     496        $p->next_tag();
     497        $this->assertTrue( $p->next_tag( array( 'tag_closers' => 'visit' ) ), 'Did not find the </title> tag closer' );
     498        $this->assertTrue( $p->is_tag_closer(), 'Indicated a <title> tag opener is a tag closer' );
     499
     500        $p = new WP_HTML_Tag_Processor( 'abc</title>' );
     501        $this->assertTrue( $p->next_tag( array( 'tag_closers' => 'visit' ) ), 'Did not find the </title> tag closer when there was no tag opener' );
    462502    }
    463503
Note: See TracChangeset for help on using the changeset viewer.