HTML API: Avoid calling subclass method while internally scanning in Tag Processor.

After modifying tags in the HTML API, the Tag Processor backs up to before the tag being modified and then re-parses its attributes. This saves on the code complexity involved in applying updates, which have already been transformed to “lexical updates” by the time they are applied.

In order to do that, `::get_updated_html()` called `::next_tag()` to reuse its logic. However, as a public method, subclasses may change the behavior of that method, and the HTML Processor does just this. It maintains an HTML stack of open elements and when the Tag Processor calls this method to re-scan a tag and its attributes, it leads to a broken stack.

This commit replaces the call to `::next_tag()` with a more appropriate reapplication of its internal parsing logic to rescan the tag name and its attributes. Given the limited nature of what's occurring in `::get_updated_html()`, this should bring with it certain guarantees that no HTML structure is being changed (that structure will only be changed by subclasses like the HTML Processor).

Follow-up to [56274], [56702].

Props dmsnell, zieladam, nicolefurlan.
Fixes #59607.

git-svn-id: https://develop.svn.wordpress.org/trunk@56941 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2023-10-16 14:00:01 +00:00
parent 9225964063
commit 0f81f441dc
2 changed files with 64 additions and 15 deletions

View File

@@ -410,6 +410,53 @@ class Tests_HtmlApi_WpHtmlProcessorBreadcrumbs extends WP_UnitTestCase {
);
}
/**
* Ensures that updating tag's attributes doesn't shift the current position
* in the input HTML document.
*
* @since 6.4.0
*
* @ticket 59607
*
* @covers WP_HTML_Tag_Processor::get_updated_html
*/
public function test_remains_stable_when_editing_attributes() {
$p = WP_HTML_Processor::create_fragment( '<div><button>First<button><b here>Second' );
$p->next_tag( array( 'breadcrumbs' => array( 'BUTTON', 'B' ) ) );
$this->assertSame(
array( 'HTML', 'BODY', 'DIV', 'BUTTON', 'B' ),
$p->get_breadcrumbs(),
'Found the wrong nested structure at the matched tag.'
);
$p->set_attribute( 'a-name', 'a-value' );
$this->assertTrue(
$p->get_attribute( 'here' ),
'Should have found the B tag but could not find expected "here" attribute.'
);
$this->assertSame(
array( 'HTML', 'BODY', 'DIV', 'BUTTON', 'B' ),
$p->get_breadcrumbs(),
'Found the wrong nested structure at the matched tag.'
);
$p->get_updated_html();
$this->assertTrue(
$p->get_attribute( 'here' ),
'Should have stayed at the B tag but could not find expected "here" attribute.'
);
$this->assertSame(
array( 'HTML', 'BODY', 'DIV', 'BUTTON', 'B' ),
$p->get_breadcrumbs(),
'Found the wrong nested structure at the matched tag after updating attributes.'
);
}
/**
* Ensures that the ability to set attributes isn't broken by the HTML Processor.
*
@@ -417,7 +464,7 @@ class Tests_HtmlApi_WpHtmlProcessorBreadcrumbs extends WP_UnitTestCase {
*
* @ticket 58517
*
* @covers WP_HTML_Processor::set_attribute
* @covers WP_HTML_Tag_Processor::set_attribute
*/
public function test_can_modify_attributes_after_finding_tag() {
$p = WP_HTML_Processor::create_fragment( '<div><figure><img><figcaption>test</figcaption></figure>' );