Make WordPress Core

Changeset 58893


Ignore:
Timestamp:
08/13/2024 10:12:01 PM (2 years ago)
Author:
dmsnell
Message:

HTML API: Only stop on full matches for requested tag name.

An optimization pass on the HTML API left a bug in the matches()
method, whereby it would falsely detect a tag name match if the
found tag were a lexical subset of the requested tag. This occurred
because of the use of substr_compare() without checking that the
outer lengths matched.

This patch resolves the bug by adding the length check.

Developed in ​https://github.com/wordpress/wordpress-develop/pull/7189
Discussed in https://core.trac.wordpress.org/ticket/61545

Follow-up to [58613].
Props dmsnell, westonruter.
See #61545.

Location:
trunk
Files:
2 edited

Legend:

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

    r58870 r58893  
    40104010
    40114011                // Does the tag name match the requested tag name in a case-insensitive manner?
    4012                 if ( isset( $this->sought_tag_name ) && 0 !== substr_compare( $this->html, $this->sought_tag_name, $this->tag_name_starts_at, $this->tag_name_length, true ) ) {
     4012                if (
     4013                        isset( $this->sought_tag_name ) &&
     4014                        (
     4015                                strlen( $this->sought_tag_name ) !== $this->tag_name_length ||
     4016                                0 !== substr_compare( $this->html, $this->sought_tag_name, $this->tag_name_starts_at, $this->tag_name_length, true )
     4017                        )
     4018                ) {
    40134019                        return false;
    40144020                }
  • trunk/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php

    r58858 r58893  
    600600
    601601                $this->assertFalse( $processor->next_tag( 'p' ), 'Querying a non-existing tag did not return false' );
     602        }
     603
     604        /**
     605         * @ticket 61545
     606         */
     607        public function test_next_tag_should_not_match_on_substrings_of_a_requested_tag() {
     608                $processor = new WP_HTML_Tag_Processor( '<p><pic><picture>' );
     609
     610                $this->assertTrue(
     611                        $processor->next_tag( 'PICTURE' ),
     612                        'Failed to find a tag when requested: check test setup.'
     613                );
     614
     615                $this->assertSame(
     616                        'PICTURE',
     617                        $processor->get_tag(),
     618                        'Should have skipped past substring tag matches, directly finding the PICTURE element.'
     619                );
     620
     621                $processor = new WP_HTML_Tag_Processor( '<p><pic>' );
     622
     623                $this->assertFalse(
     624                        $processor->next_tag( 'PICTURE' ),
     625                        "Should not have found any PICTURE element, but found '{$processor->get_token_name()}' instead."
     626                );
    602627        }
    603628
Note: See TracChangeset for help on using the changeset viewer.