Make WordPress Core

Changeset 55407


Ignore:
Timestamp:
02/22/2023 08:53:41 PM (13 months ago)
Author:
hellofromTonya
Message:

HTML API: Fix finding bookmarks set on closing tag WP_HTML_Tag_Processor.

Setting a bookmark on a tag should set its "start" position before the opening "<", e.g.:

<div> Testing a <b>Bookmark</b>
----------------^

The previous calculation assumed this is always one byte to the left from $tag_name_starts_at.

However, in a closing tag that index points to a solidus symbol "/":

<div> Testing a <b>Bookmark</b>
----------------------------^

The bookmark should therefore start two bytes before the tag name:

<div> Testing a <b>Bookmark</b>
---------------------------^

This changeset achieves this by:

  • Using the correct starting index for closing tag bookmarks.
  • Adding array( 'tag_closers' => 'visit' ) in WP_HTML_Tag_Processor::seek().

Follow-up to [55203].

Props zieladam, dmsnell, flixos90.
Fixes #57787.
See #57575.

Location:
trunk
Files:
2 edited

Legend:

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

    r55402 r55407  
    723723
    724724        $this->bookmarks[ $name ] = new WP_HTML_Span(
    725             $this->tag_name_starts_at - 1,
     725            $this->tag_name_starts_at - ( $this->is_closing_tag ? 2 : 1 ),
    726726            $this->tag_ends_at
    727727        );
     
    15051505        $this->bytes_already_copied = $this->bytes_already_parsed;
    15061506        $this->output_buffer        = substr( $this->html, 0, $this->bytes_already_copied );
    1507         return $this->next_tag();
     1507        return $this->next_tag( array( 'tag_closers' => 'visit' ) );
    15081508    }
    15091509
  • trunk/tests/phpunit/tests/html-api/wpHtmlTagProcessor-bookmark.php

    r55315 r55407  
    6161            $p->get_updated_html(),
    6262            'Did not seek to the intended bookmark locations'
     63        );
     64    }
     65
     66    /**
     67     * @ticket 57787
     68     *
     69     * @covers WP_HTML_Tag_Processor::seek
     70     */
     71    public function test_seeks_to_tag_closer_bookmark() {
     72        $p = new WP_HTML_Tag_Processor( '<div>First</div><span>Second</span>' );
     73        $p->next_tag( array( 'tag_closers' => 'visit' ) );
     74        $p->set_bookmark( 'first' );
     75        $p->next_tag( array( 'tag_closers' => 'visit' ) );
     76        $p->set_bookmark( 'second' );
     77
     78        $p->seek( 'first' );
     79        $p->seek( 'second' );
     80
     81        $this->assertSame(
     82            'DIV',
     83            $p->get_tag(),
     84            'Did not seek to the intended bookmark location'
    6385        );
    6486    }
Note: See TracChangeset for help on using the changeset viewer.