Make WordPress Core

Changeset 58233

Timestamp:
05/29/2024 11:40:16 AM (2 months ago)
Author:
dmsnell
Message:

HTML API: Fix token length bug in Tag Processor.

The Tag Processor stores the byte-offsets into its HTML document where
the current token starts and ends, and also for every bookmark. In some
cases for tags, the end offset has been off by one.

In this patch the offset is fixed so that a bookmark always properly
refers to the full span of the token it's bookmarking. Also the current
token byte offsets are properly recorded.

While this is a defect in the Tag Processor, it hasn't been exposed
through the public interface and has not affected any of the working
of the processor. Only subclasses which rely on the length of a bookmark
have been potentially affected, and these are not supported environments
in the ongoing work.

This fix is important for future work and for ensuring that subclasses
performing custom behaviors remain as reliable as the public interface.

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

Props dmsnell, gziolo, jonsurrell, westonruter.
Fixes #61301.

Location:
trunk
Files:
3 edited

Legend:

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

    r58040 r58233  
    927927        }
    928928        $this->parser_state         = self::STATE_MATCHED_TAG;
    929         $this->token_length         = $tag_ends_at - $this->token_starts_at;
    930929        $this->bytes_already_parsed = $tag_ends_at + 1;
     930
    931931
    932932        /*
     
    10141014        $this->token_starts_at      = $was_at;
    10151015        $this->token_length         = $this->bytes_already_parsed - $this->token_starts_at;
    1016         $this->text_starts_at       = $tag_ends_at + 1;
     1016        $this->text_starts_at       = $tag_ends_at;
    10171017        $this->text_length          = $this->tag_name_starts_at - $this->text_starts_at;
    10181018        $this->tag_name_starts_at   = $tag_name_starts_at;
     
    26882688         *             ^ this appears one character before the end of the closing ">".
    26892689         */
    2690         return '/' === $this->html[ $this->token_starts_at + $this->token_length - 1 ];
     2690        return '/' === $this->html[ $this->token_starts_at + $this->token_length - ];
    26912691    }
    26922692
  • trunk/src/wp-includes/interactivity-api/class-wp-interactivity-api-directives-processor.php

    r57649 r58233  
    108108        $bookmark = 'append_content_after_template_tag_closer';
    109109        $this->set_bookmark( $bookmark );
    110         $after_closing_tag = $this->bookmarks[ $bookmark ]->start + $this->bookmarks[ $bookmark ]->length + 1;
     110        $after_closing_tag = $this->bookmarks[ $bookmark ]->start + $this->bookmarks[ $bookmark ]->length;
    111111        $this->release_bookmark( $bookmark );
    112112
     
    141141        list( $opener_tag, $closer_tag ) = $bookmarks;
    142142
    143         $after_opener_tag  = $this->bookmarks[ $opener_tag ]->start + $this->bookmarks[ $opener_tag ]->length + 1;
     143        $after_opener_tag  = $this->bookmarks[ $opener_tag ]->start + $this->bookmarks[ $opener_tag ]->length;
    144144        $before_closer_tag = $this->bookmarks[ $closer_tag ]->start;
    145145
  • trunk/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php

    r57805 r58233  
    475475
    476476        $this->assertSame( '<div wonky><img hidden></div>', $processor->get_updated_html() );
     477
     478
     479
     480
     481
     482
     483
     484
     485
     486
     487
     488
     489
     490
     491
     492
     493
     494
     495
     496
     497
     498
     499
     500
     501
     502
     503
     504
     505
     506
     507
     508
     509
     510
     511
     512
     513
     514
     515
     516
     517
     518
     519
     520
     521
     522
     523
     524
     525
     526
     527
     528
     529
     530
     531
     532
     533
     534
     535
     536
     537
     538
     539
     540
     541
     542
     543
     544
     545
     546
     547
     548
     549
     550
     551
     552
     553
     554
     555
     556
     557
     558
     559
     560
     561
     562
     563
     564
     565
     566
     567
     568
     569
     570
     571
     572
     573
     574
     575
     576
     577
     578
     579
    477580    }
    478581
     
    27472850                $this->set_bookmark( 'here' );
    27482851                $this->lexical_updates[] = new WP_HTML_Text_Replacement(
    2749                     $this->bookmarks['here']->start + $this->bookmarks['here']->length + 1,
     2852                    $this->bookmarks['here']->start + $this->bookmarks['here']->length,
    27502853                    0,
    27512854                    $new_html
Note: See TracChangeset for help on using the changeset viewer.