Make WordPress Core

Changeset 57582


Ignore:
Timestamp:
02/10/2024 10:52:28 PM (7 months ago)
Author:
dmsnell
Message:

HTML API: Add subclassed has_bookmark() and fix seek()

The WP_HTML_Processor::has_bookmark() method has not correctly reported bookmarks
which have been set, because it wraps the given bookmark names when setting them.
Additionally, WP_HTML_Processor::seek() does not seek to correct location if HTML
has been updated because it wasn't flushing enqueued updates to the document.

In this patch both problems are resolved and added tests guard these behaviors
against future regressions.

Developed in https://github.com/WordPress/wordpress-develop/pull/6039
Discussed in https://core.trac.wordpress.org/ticket/60474

Follow-up to [56274].
Props dmsnell, jonsurrell.
Fixes #60474.

File:
1 edited

Legend:

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

    r57528 r57582  
    12381238     */
    12391239    public function seek( $bookmark_name ) {
     1240        // Flush any pending updates to the document before beginning.
     1241        $this->get_updated_html();
     1242
    12401243        $actual_bookmark_name = "_{$bookmark_name}";
    12411244        $processor_started_at = $this->state->current_token
     
    12471250        switch ( $direction ) {
    12481251            case 'forward':
    1249                 // When moving forwards, re-parse the document until reaching the same location as the original bookmark.
     1252                // When moving forwards, reparse the document until reaching the same location as the original bookmark.
    12501253                while ( $this->step() ) {
    12511254                    if ( $bookmark_starts_at === $this->bookmarks[ $this->state->current_token->bookmark_name ]->start ) {
     
    13671370    public function set_bookmark( $bookmark_name ) {
    13681371        return parent::set_bookmark( "_{$bookmark_name}" );
     1372    }
     1373
     1374    /**
     1375     * Checks whether a bookmark with the given name exists.
     1376     *
     1377     * @since 6.5.0
     1378     *
     1379     * @param string $bookmark_name Name to identify a bookmark that potentially exists.
     1380     * @return bool Whether that bookmark exists.
     1381     */
     1382    public function has_bookmark( $bookmark_name ) {
     1383        return parent::has_bookmark( "_{$bookmark_name}" );
    13691384    }
    13701385
Note: See TracChangeset for help on using the changeset viewer.