mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-07 18:50:12 +00:00
HTML API: Accumulate shift for internal parsing pointer.
A bug was discovered where where the parser wasn't returning to the start of the affected tag after making some updates. In few words, the Tag Processor has not been treating its own internal pointer `bytes_already_parsed` the same way it treats its bookmarks. That is, when updates are applied to the input document and then `get_updated_html()` is called, the internal pointer transfers to the newly-updated content as if no updates had been applied since the previous call to `get_updated_html()`. In this patch we're creating a new "shift accumulator" to account for all of the updates that accrue before calling `get_updated_html()`. This accumulated shift will be applied when swapping the input document with the output buffer, which should result in the pointer pointing to the same logical spot in the document it did before the udpate. In effect this patch adds a single workaround for treating the internal pointer like a bookmark, plus a temporary pointer which points to the beginning of the current tag when calling `get_updated_html()`. This will preserve the assumption that updating a document doesn't move that pointer, or shift which tag is currently matched. Props dmsnell, zieladam. Fixes #58179. git-svn-id: https://develop.svn.wordpress.org/trunk@55706 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -571,6 +571,32 @@ class Tests_HtmlApi_wpHtmlTagProcessor extends WP_UnitTestCase {
|
||||
$this->assertTrue( $p->next_tag( array( 'tag_closers' => 'visit' ) ), 'Did not find the </title> tag closer when there was no tag opener' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that updates to a document before calls to `get_updated_html()` don't
|
||||
* lead to the Tag Processor jumping to the wrong tag after the updates.
|
||||
*
|
||||
* @ticket 58179
|
||||
*
|
||||
* @covers WP_HTML_Tag_Processor::get_updated_html
|
||||
*/
|
||||
public function test_internal_pointer_returns_to_original_spot_after_inserting_content_before_cursor() {
|
||||
$tags = new WP_HTML_Tag_Processor( '<div>outside</div><section><div><img>inside</div></section>' );
|
||||
|
||||
$tags->next_tag();
|
||||
$tags->add_class( 'foo' );
|
||||
$tags->next_tag( 'section' );
|
||||
|
||||
// Return to this spot after moving ahead.
|
||||
$tags->set_bookmark( 'here' );
|
||||
|
||||
// Move ahead.
|
||||
$tags->next_tag( 'img' );
|
||||
$tags->seek( 'here' );
|
||||
$this->assertSame( '<div class="foo">outside</div><section><div><img>inside</div></section>', $tags->get_updated_html() );
|
||||
$this->assertSame( 'SECTION', $tags->get_tag() );
|
||||
$this->assertFalse( $tags->is_tag_closer() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 56299
|
||||
*
|
||||
@@ -1522,7 +1548,7 @@ HTML;
|
||||
HTML;
|
||||
|
||||
$p = new WP_HTML_Tag_Processor( $input );
|
||||
$this->assertTrue( $p->next_tag( 'div' ), 'Querying an existing tag did not return true' );
|
||||
$this->assertTrue( $p->next_tag( 'div' ), 'Did not find first DIV tag in input.' );
|
||||
$p->set_attribute( 'data-details', '{ "key": "value" }' );
|
||||
$p->add_class( 'is-processed' );
|
||||
$this->assertTrue(
|
||||
@@ -1532,7 +1558,7 @@ HTML;
|
||||
'class_name' => 'BtnGroup',
|
||||
)
|
||||
),
|
||||
'Querying an existing tag did not return true'
|
||||
'Did not find the first BtnGroup DIV tag'
|
||||
);
|
||||
$p->remove_class( 'BtnGroup' );
|
||||
$p->add_class( 'button-group' );
|
||||
@@ -1544,7 +1570,7 @@ HTML;
|
||||
'class_name' => 'BtnGroup',
|
||||
)
|
||||
),
|
||||
'Querying an existing tag did not return true'
|
||||
'Did not find the second BtnGroup DIV tag'
|
||||
);
|
||||
$p->remove_class( 'BtnGroup' );
|
||||
$p->add_class( 'button-group' );
|
||||
@@ -1557,10 +1583,10 @@ HTML;
|
||||
'match_offset' => 3,
|
||||
)
|
||||
),
|
||||
'Querying an existing tag did not return true'
|
||||
'Did not find third BUTTON tag with "btn" CSS class'
|
||||
);
|
||||
$p->remove_attribute( 'class' );
|
||||
$this->assertFalse( $p->next_tag( 'non-existent' ), 'Querying a non-existing tag did not return false' );
|
||||
$this->assertFalse( $p->next_tag( 'non-existent' ), "Found a {$p->get_tag()} tag when none should have been found." );
|
||||
$p->set_attribute( 'class', 'test' );
|
||||
$this->assertSame( $expected_output, $p->get_updated_html(), 'Calling get_updated_html after updating the attributes did not return the expected HTML' );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user