Make WordPress Core


Ignore:
Timestamp:
03/05/2024 05:32:19 AM (16 months ago)
Author:
dmsnell
Message:

HTML API: Ensure that breadcrumbs are properly retained after seeking.

In some cases, it's possible to seek back into a location found inside
an element which has been closed before the point in the document where
the seek() was made. In these cases the breadcrumb stack is lost, and
calling get_breadcrumbs() after the seek will return the wrong information.

In this patch, the HTML Processor takes a conservative approach and
moves to the front of the document, then reparses the document until
it reaches the sought-after location. This ensures consistency on
the stack of open elements and active formats, and preserves
breadcrumbs.

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

Props jonsurrell.
Follow-up to [60687].
See #58517.
Fixes #60687.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/html-api/wpHtmlProcessorBreadcrumbs.php

    r57508 r57768  
    546546        $this->assertTrue( $processor->get_attribute( 'two' ) );
    547547    }
     548
     549    /**
     550     * Ensures that breadcrumbs are properly reported after seeking backward to a location
     551     * inside an element which has been fully closed before the seek.
     552     *
     553     * @ticket 60687
     554     */
     555    public function test_retains_proper_bookmarks_after_seeking_back_to_closed_element() {
     556        $processor = WP_HTML_Processor::create_fragment( '<div><img></div><div><hr></div>' );
     557
     558        $processor->next_tag( 'IMG' );
     559        $processor->set_bookmark( 'first' );
     560
     561        $processor->next_tag( 'HR' );
     562
     563        $processor->seek( 'first' );
     564        $this->assertSame(
     565            array( 'HTML', 'BODY', 'DIV', 'IMG' ),
     566            $processor->get_breadcrumbs(),
     567            'Should have retained breadcrumbs from bookmarked location after seeking backwards to it.'
     568        );
     569    }
    548570}
Note: See TracChangeset for help on using the changeset viewer.