mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-10 21:40:08 +00:00
This patch adds support to process the BUTTON element. This requires adding some additional semantic rules to handle situations where a BUTTON element is already in scope. Also included is a fixup to enforce that `WP_HTML_Processor::next_tag()` never returns for a tag closer. This is useful with the Tag Processor, but not for the HTML Processor. There were tests relying on this behavior to assert that internal processes were working as they should, but those tests have been updated to use the semi-private `step()` function, which does stop on tag closers. This patch is one in a series of changes to expand support within the HTML API, moving gradually to allow for more focused changes that are easier to review and test. The HTML Processor is a work in progress with a certain set of features slated to be ready and tested by 6.4.0, but it will only contain partial support of the HTML5 specification even after that. Whenever it cannot positively recognize and process its input it bails, and certain function stubs and logical stubs exist to structure future expansions of support. Props dmsnell. Fixes #58961. git-svn-id: https://develop.svn.wordpress.org/trunk@56380 602fd350-edb4-49c9-b593-d223f7449a82
190 lines
9.4 KiB
PHP
190 lines
9.4 KiB
PHP
<?php
|
|
/**
|
|
* Unit tests covering WP_HTML_Processor compliance with HTML5 semantic parsing rules.
|
|
*
|
|
* @package WordPress
|
|
* @subpackage HTML-API
|
|
*
|
|
* @since 6.4.0
|
|
*
|
|
* @group html-api
|
|
*
|
|
* @coversDefaultClass WP_HTML_Processor
|
|
*/
|
|
class Tests_HtmlApi_WpHtmlProcessorSemanticRules extends WP_UnitTestCase {
|
|
/*******************************************************************
|
|
* RULES FOR "IN BODY" MODE
|
|
*******************************************************************/
|
|
|
|
/**
|
|
* Verifies that when encountering an end tag for which there is no corresponding
|
|
* element in scope, that it skips the tag entirely.
|
|
*
|
|
* @ticket 58961
|
|
*
|
|
* @since 6.4.0
|
|
*
|
|
* @throws Exception
|
|
*/
|
|
public function test_in_body_skips_unexpected_button_closer() {
|
|
$p = WP_HTML_Processor::createFragment( '<div>Test</button></div>' );
|
|
|
|
$p->step();
|
|
$this->assertEquals( 'DIV', $p->get_tag(), 'Did not stop at initial DIV tag.' );
|
|
$this->assertFalse( $p->is_tag_closer(), 'Did not find that initial DIV tag is an opener.' );
|
|
|
|
/*
|
|
* When encountering the BUTTON closing tag, there is no BUTTON in the stack of open elements.
|
|
* It should be ignored as there's no BUTTON to close.
|
|
*/
|
|
$this->assertTrue( $p->step(), 'Found no further tags when it should have found the closing DIV' );
|
|
$this->assertEquals( 'DIV', $p->get_tag(), "Did not skip unexpected BUTTON; stopped at {$p->get_tag()}." );
|
|
$this->assertTrue( $p->is_tag_closer(), 'Did not find that the terminal DIV tag is a closer.' );
|
|
}
|
|
|
|
/**
|
|
* Verifies insertion of a BUTTON element when no existing BUTTON is already in scope.
|
|
*
|
|
* @ticket 58961
|
|
*
|
|
* @since 6.4.0
|
|
*
|
|
* @throws WP_HTML_Unsupported_Exception
|
|
*/
|
|
public function test_in_body_button_with_no_button_in_scope() {
|
|
$p = WP_HTML_Processor::createFragment( '<div><p>Click the button <button one>here</button>!</p></div><button two>not here</button>' );
|
|
|
|
$this->assertTrue( $p->next_tag( 'BUTTON' ), 'Could not find expected first button.' );
|
|
$this->assertTrue( $p->get_attribute( 'one' ), 'Failed to match expected attribute on first button.' );
|
|
$this->assertSame( array( 'HTML', 'BODY', 'DIV', 'P', 'BUTTON' ), $p->get_breadcrumbs(), 'Failed to produce expected DOM nesting for first button.' );
|
|
|
|
/*
|
|
* There's nothing special about this HTML construction, but it's important to verify that
|
|
* the HTML Processor can find a BUTTON under normal and normative scenarios, not just the
|
|
* malformed and unexpected ones.
|
|
*/
|
|
$this->assertTrue( $p->next_tag( 'BUTTON' ), 'Could not find expected second button.' );
|
|
$this->assertTrue( $p->get_attribute( 'two' ), 'Failed to match expected attribute on second button.' );
|
|
$this->assertSame( array( 'HTML', 'BODY', 'BUTTON' ), $p->get_breadcrumbs(), 'Failed to produce expected DOM nesting for second button.' );
|
|
}
|
|
|
|
/**
|
|
* Verifies what when inserting a BUTTON element, when a BUTTON is already in scope,
|
|
* that the open button is closed with all other elements inside of it.
|
|
*
|
|
* @ticket 58961
|
|
*
|
|
* @since 6.4.0
|
|
*
|
|
* @throws WP_HTML_Unsupported_Exception
|
|
*/
|
|
public function test_in_body_button_with_button_in_scope_as_parent() {
|
|
$p = WP_HTML_Processor::createFragment( '<div><p>Click the button <button one>almost<button two>here</button>!</p></div><button three>not here</button>' );
|
|
|
|
$this->assertTrue( $p->next_tag( 'BUTTON' ), 'Could not find expected first button.' );
|
|
$this->assertTrue( $p->get_attribute( 'one' ), 'Failed to match expected attribute on first button.' );
|
|
$this->assertSame( array( 'HTML', 'BODY', 'DIV', 'P', 'BUTTON' ), $p->get_breadcrumbs(), 'Failed to produce expected DOM nesting for first button.' );
|
|
|
|
/*
|
|
* A naive parser might skip the second BUTTON because it's looking for the close of the first one,
|
|
* or it may place it as a child of the first one, but it implicitly closes the open BUTTON.
|
|
*/
|
|
$this->assertTrue( $p->next_tag( 'BUTTON' ), 'Could not find expected second button.' );
|
|
$this->assertTrue( $p->get_attribute( 'two' ), 'Failed to match expected attribute on second button.' );
|
|
$this->assertSame( array( 'HTML', 'BODY', 'DIV', 'P', 'BUTTON' ), $p->get_breadcrumbs(), 'Failed to produce expected DOM nesting for second button.' );
|
|
|
|
/*
|
|
* This is another form of the test for the second button, but from a different side. The test is
|
|
* looking for proper handling of the open and close sequence for the BUTTON tags.
|
|
*/
|
|
$this->assertTrue( $p->next_tag( 'BUTTON' ), 'Could not find expected third button.' );
|
|
$this->assertTrue( $p->get_attribute( 'three' ), 'Failed to match expected attribute on third button.' );
|
|
$this->assertSame( array( 'HTML', 'BODY', 'BUTTON' ), $p->get_breadcrumbs(), 'Failed to produce expected DOM nesting for third button.' );
|
|
}
|
|
|
|
/**
|
|
* Verifies what when inserting a BUTTON element, when a BUTTON is already in scope,
|
|
* that the open button is closed with all other elements inside of it, even if the
|
|
* BUTTON in scope is not a direct parent of the new BUTTON element.
|
|
*
|
|
* @ticket 58961
|
|
*
|
|
* @since 6.4.0
|
|
*
|
|
* @throws WP_HTML_Unsupported_Exception
|
|
*/
|
|
public function test_in_body_button_with_button_in_scope_as_ancestor() {
|
|
$p = WP_HTML_Processor::createFragment( '<div><button one><p>Click the button <span><button two>here</button>!</span></p></div><button three>not here</button>' );
|
|
|
|
// This button finds itself normally nesting inside the DIV.
|
|
$this->assertTrue( $p->next_tag( 'BUTTON' ), 'Could not find expected first button.' );
|
|
$this->assertTrue( $p->get_attribute( 'one' ), 'Failed to match expected attribute on first button.' );
|
|
$this->assertSame( array( 'HTML', 'BODY', 'DIV', 'BUTTON' ), $p->get_breadcrumbs(), 'Failed to produce expected DOM nesting for first button.' );
|
|
|
|
/*
|
|
* Because the second button appears while a BUTTON is in scope, it generates implied end tags and closes
|
|
* the BUTTON, P, and SPAN elements. It looks like the BUTTON is inside the SPAN, but it's another case
|
|
* of an unexpected closing SPAN tag because the SPAN was closed by the second BUTTON. This element finds
|
|
* itself a child of the most-recent open element above the most-recent BUTTON, or the DIV.
|
|
*/
|
|
$this->assertTrue( $p->next_tag( 'BUTTON' ), 'Could not find expected second button.' );
|
|
$this->assertTrue( $p->get_attribute( 'two' ), 'Failed to match expected attribute on second button.' );
|
|
$this->assertSame( array( 'HTML', 'BODY', 'DIV', 'BUTTON' ), $p->get_breadcrumbs(), 'Failed to produce expected DOM nesting for second button.' );
|
|
|
|
// The third button is back to normal, because everything has been implicitly or explicitly closed by now.
|
|
$this->assertTrue( $p->next_tag( 'BUTTON' ), 'Could not find expected third button.' );
|
|
$this->assertTrue( $p->get_attribute( 'three' ), 'Failed to match expected attribute on third button.' );
|
|
$this->assertSame( array( 'HTML', 'BODY', 'BUTTON' ), $p->get_breadcrumbs(), 'Failed to produce expected DOM nesting for third button.' );
|
|
}
|
|
|
|
/*
|
|
* Verifies that when "in body" and encountering "any other end tag"
|
|
* that the HTML processor ignores the end tag if there's a special
|
|
* element on the stack of open elements before the matching opening.
|
|
*
|
|
* @ticket 58907
|
|
*
|
|
* @since 6.4.0
|
|
*
|
|
* @covers WP_HTML_Processor::step_in_body
|
|
*/
|
|
public function test_in_body_any_other_end_tag_with_unclosed_special_element() {
|
|
$p = WP_HTML_Processor::createFragment( '<div><span><p></span><div>' );
|
|
|
|
$p->next_tag( 'P' );
|
|
$this->assertSame( 'P', $p->get_tag(), "Expected to start test on P element but found {$p->get_tag()} instead." );
|
|
$this->assertSame( array( 'HTML', 'BODY', 'DIV', 'SPAN', 'P' ), $p->get_breadcrumbs(), 'Failed to produce expected DOM nesting.' );
|
|
|
|
$this->assertTrue( $p->next_tag(), 'Failed to advance past P tag to expected DIV opener.' );
|
|
$this->assertSame( 'DIV', $p->get_tag(), "Expected to find DIV element, but found {$p->get_tag()} instead." );
|
|
$this->assertSame( array( 'HTML', 'BODY', 'DIV', 'SPAN', 'DIV' ), $p->get_breadcrumbs(), 'Failed to produce expected DOM nesting: SPAN should still be open and DIV should be its child.' );
|
|
}
|
|
|
|
/*
|
|
* Verifies that when "in body" and encountering "any other end tag"
|
|
* that the HTML processor closes appropriate elements on the stack of
|
|
* open elements up to the matching opening.
|
|
*
|
|
* @ticket 58907
|
|
*
|
|
* @since 6.4.0
|
|
*
|
|
* @covers WP_HTML_Processor::step_in_body
|
|
*/
|
|
public function test_in_body_any_other_end_tag_with_unclosed_non_special_element() {
|
|
$p = WP_HTML_Processor::createFragment( '<div><span><code></span><div>' );
|
|
|
|
$p->next_tag( 'CODE' );
|
|
$this->assertSame( 'CODE', $p->get_tag(), "Expected to start test on CODE element but found {$p->get_tag()} instead." );
|
|
$this->assertSame( array( 'HTML', 'BODY', 'DIV', 'SPAN', 'CODE' ), $p->get_breadcrumbs(), 'Failed to produce expected DOM nesting.' );
|
|
|
|
$this->assertTrue( $p->step(), 'Failed to advance past CODE tag to expected SPAN closer.' );
|
|
$this->assertTrue( $p->is_tag_closer(), 'Expected to find closing SPAN, but found opener instead.' );
|
|
$this->assertSame( array( 'HTML', 'BODY', 'DIV' ), $p->get_breadcrumbs(), 'Failed to advance past CODE tag to expected DIV opener.' );
|
|
|
|
$this->assertTrue( $p->next_tag(), 'Failed to advance past SPAN closer to expected DIV opener.' );
|
|
$this->assertSame( 'DIV', $p->get_tag(), "Expected to find DIV element, but found {$p->get_tag()} instead." );
|
|
$this->assertSame( array( 'HTML', 'BODY', 'DIV', 'DIV' ), $p->get_breadcrumbs(), 'Failed to produce expected DOM nesting: SPAN should be closed and DIV should be its sibling.' );
|
|
}
|
|
}
|