Make WordPress Core

Changeset 59502


Ignore:
Timestamp:
12/10/2024 09:11:35 AM (22 months ago)
Author:
gziolo
Message:

HTML API: Prevent bookmarks from being set on virtual tokens

Fixes the issue when an HTML_Processor bookmark was set at a virtual token (a node in the resulting document that does not correspond to an HTML token present in the input string), seek behavior was unreliable.

Props jonsurrell, gziolo.
Fixes #62521.

Location:
trunk
Files:
2 edited

Legend:

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

    r59500 r59502  
    304304
    305305                while ( $context_processor->next_tag() ) {
    306                         $context_processor->set_bookmark( 'final_node' );
     306                        if ( ! $context_processor->is_virtual() ) {
     307                                $context_processor->set_bookmark( 'final_node' );
     308                        }
    307309                }
    308310
    … …  
    56745676         * HTML structure or unwanted processing overhead.
    56755677         *
     5678         * Bookmarks cannot be set on tokens that do no appear in the original
     5679         * HTML text. For example, the HTML `<table><td>` stops at tags `TABLE`,
     5680         * `TBODY`, `TR`, and `TD`. The `TBODY` and `TR` tags do not appear in
     5681         * the original HTML and cannot be used as bookmarks.
     5682         *
    56765683         * @since 6.4.0
    56775684         *
    … …  
    56805687         */
    56815688        public function set_bookmark( $bookmark_name ): bool {
     5689                if ( $this->is_virtual() ) {
     5690                        _doing_it_wrong(
     5691                                __METHOD__,
     5692                                __( 'Cannot set bookmarks on tokens that do no appear in the original HTML text.' ),
     5693                                '6.8.0'
     5694                        );
     5695                        return false;
     5696                }
    56825697                return parent::set_bookmark( "_{$bookmark_name}" );
    56835698        }
  • trunk/tests/phpunit/tests/html-api/wpHtmlProcessor-bookmark.php

    r59391 r59502  
    158158                );
    159159        }
     160
     161        /**
     162         * @ticket 62521
     163         *
     164         * @expectedIncorrectUsage WP_HTML_Processor::set_bookmark
     165         */
     166        public function test_bookmarks_not_allowed_on_virtual_nodes() {
     167                $processor = WP_HTML_Processor::create_full_parser( 'text' );
     168                $this->assertTrue( $processor->next_tag( 'BODY' ) );
     169                $this->assertFalse( $processor->set_bookmark( 'mark' ) );
     170                $this->assertTrue( $processor->next_token() );
     171                $this->assertTrue( $processor->set_bookmark( 'mark' ) );
     172        }
    160173}
Note: See TracChangeset for help on using the changeset viewer.