#61301 closed defect (bug) (fixed)
HTML API: Tag bookmark length is incorrect
Reported by: | jonsurrell | Owned by: | dmsnell |
---|---|---|---|
Milestone: | 6.6 | Priority: | normal |
Severity: | normal | Version: | 6.5 |
Component: | HTML API | Keywords: | has-patch has-unit-tests needs-dev-note |
Focuses: | Cc: |
Description
Opening tag tokens in the HTML API are incorrect. I'd expect that substr( $html, $token_start, $token_length );
to produce the entire tag. Currently, this produces the tags up to (but not including) the final >
. A tag like <div>
has a length 4
, so the result would be <div
.
This is largely internal, although classes extending the tag processor observe this bug through bookmarks.
Change History (15)
This ticket was mentioned in PR #6625 on WordPress/wordpress-develop by @jonsurrell.
4 months ago
#1
- Keywords has-patch has-unit-tests added
4 months ago
#3
@sirreal can you look into the failure in the linked PR? I think it's related to seeking with updates and getting the wrong $this->bytes_already_processed
@jonsurrell commented on PR #6625:
4 months ago
#4
The test failure in https://github.com/WordPress/wordpress-develop/pull/6651 makes me nervous.
Can you look into the failure in the linked PR? I think it's related to seeking with updates and getting the wrong$this->bytes_already_processed
68a3251d307c39467ab85bf739578d15da378d75 was missing from that branch. There was a test that used the broken offset. The offset adjustment was removed in this branch, I pushed to #6651.
@jonsurrell commented on PR #6625:
4 months ago
#5
@westonruter This is a bugfix for incorrect token lengths in tag bookmarks in the Tag Processor. Subclasses might see and be affected by the bug (and the fix) if they ever work directly with tag bookmarks lengths. I believe you have some plugins that may be doing advanced things with the Tag Processor so wanted to make sure you were aware.
Below is an example fix from this PR, plugins would be impacted in the same way and would need to remove the off-by-one correction on the bookmark lengths:
-
src/wp-includes/interactivity-api/class-wp-interactivity-api-directives-processor.php
diff --git a/src/wp-includes/interactivity-api/class-wp-interactivity-api-directives-processor.php b/src/wp-includes/interactivity-api/class-wp-interactivity-api-directives-processor.php index 3b2dcb123797..b12dcb4b3b15 100644
a b public function append_content_after_template_tag_closer( string $new_content ): 107 107 108 108 $bookmark = 'append_content_after_template_tag_closer'; 109 109 $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; 111 111 $this->release_bookmark( $bookmark ); 112 112 113 113 // Appends the new content. … … private function get_after_opener_tag_and_before_closer_tag_positions( bool $rew 140 140 } 141 141 list( $opener_tag, $closer_tag ) = $bookmarks; 142 142 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; 144 144 $before_closer_tag = $this->bookmarks[ $closer_tag ]->start; 145 145 146 146 if ( $rewind ) {
#6
@
4 months ago
- Keywords needs-dev-note added
It will need a small section in the miscellaneous dev note for developers that extend the WP_HTML_Tag_Processor
.
@westonruter commented on PR #6625:
4 months ago
#8
@sirreal See the following method in this subclass of `WP_HTML_Tag_Processor`:
/**
* Appends HTML to the provided bookmark.
*
* @param string $bookmark Bookmark.
* @param string $html HTML to inject.
* @return bool Whether the HTML was appended.
*/
public function append_html( string $bookmark, string $html ): bool {
if ( ! $this->has_bookmark( $bookmark ) ) {
return false;
}
$start = $this->bookmarks[ $bookmark ]->start;
$this->lexical_updates[] = new WP_HTML_Text_Replacement(
$start,
$this->old_text_replacement_signature_needed ? $start : 0,
$html
);
return true;
}
So since it's not dealing with length
then is a patch needed?
4 months ago
#9
So since it's not dealing with length then is a patch needed?
this should be correct, @westonruter
#10
@
4 months ago
I don't think this needs a full dev note but should definitely have a mention in the release like the following:
A bug was fixed in the
WP_HTML_Tag_Processor
where tag token bookmarks. This bug affected classes that extend theWP_HTML_Tag_Processor
orWP_HTML_Processor
classes and access tag bookmark or token lengths directly. Affected classes would have needed to correct the token length by adding 1. The tag length has been fixed and this correction should be removed in WordPress 6.6.
#11
@
4 months ago
- Owner set to dmsnell
- Resolution set to fixed
- Status changed from new to closed
In 58233:
This ticket was mentioned in Slack in #core by juanmaguitar. View the logs.
4 months ago
This ticket was mentioned in Slack in #core by dmsnell. View the logs.
4 months ago
#15
@
4 months ago
Dev note incorporated into the Updates to the HTML API in 6.6 post
It seems like the open tags have incorrect length in the tag processor. I'd expect that
substr( $html, $token_start, $token_length );
to produce the entire tag. This patch fixes that, before it would exclude the final>
on tags.This is mostly internal and it seems to have adjustments in several places to fix it, but I'd like to have a consistent internal representation. Other types of tokens behave as I'd expect (with
substr( $html, $start, $length )
).@dmsnell thoughts?
<details>
<summary>I'd expect something like this to hold:</summary>
</details>
Trac ticket: https://core.trac.wordpress.org/ticket/61301