Skip to content

Commit

Permalink
Rework with tests for fragment and full processors
Browse files Browse the repository at this point in the history
Use a dataprovider to get a factory function for processors
  • Loading branch information
sirreal committed Nov 6, 2024
1 parent f7af6e3 commit b1a6c00
Showing 1 changed file with 53 additions and 79 deletions.
132 changes: 53 additions & 79 deletions tests/phpunit/tests/html-api/wpHtmlProcessor-bookmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,77 +13,84 @@
*/
class Tests_HtmlApi_WpHtmlProcessor_Bookmark extends WP_UnitTestCase {
/**
* @dataProvider data_processor_constructors
*
* @ticket 62290
*/
public function test_full_processor_seek_same_location() {
$bookmark_name = 'the-bookmark';
$processor = WP_HTML_Processor::create_full_parser( '<html><body><div>' );
$this->assertTrue( $processor->next_tag( 'BODY' ) );
$this->assertTrue( $processor->set_bookmark( $bookmark_name ), 'Failed to set bookmark.' );
$this->assertTrue( $processor->has_bookmark( $bookmark_name ), 'Failed has_bookmark check.' );
public function test_processor_seek_same_location( callable $factory ) {
$processor = $factory( '<div><span>' );
$this->assertTrue( $processor->next_tag( 'DIV' ) );
$this->assertTrue( $processor->set_bookmark( 'mark' ), 'Failed to set bookmark.' );
$this->assertTrue( $processor->has_bookmark( 'mark' ), 'Failed has_bookmark check.' );

// Confirm the bookmark works.
$this->assertTrue( $processor->seek( $bookmark_name ), 'Failed to seek to bookmark.' );
$this->assertSame( 'BODY', $processor->get_tag() );
$this->assertTrue( $processor->next_tag() );
// Confirm the bookmark works and processing continues normally.
$this->assertTrue( $processor->seek( 'mark' ), 'Failed to seek to bookmark.' );
$this->assertSame( 'DIV', $processor->get_tag() );
$this->assertSame( array( 'HTML', 'BODY', 'DIV' ), $processor->get_breadcrumbs() );
$this->assertTrue( $processor->next_tag() );
$this->assertSame( 'SPAN', $processor->get_tag() );
$this->assertSame( array( 'HTML', 'BODY', 'DIV', 'SPAN' ), $processor->get_breadcrumbs() );
}

/**
* @dataProvider data_processor_constructors
*
* @ticket 62290
*/
public function test_full_processor_seek_backward() {
$bookmark_name = 'the-bookmark';
$processor = WP_HTML_Processor::create_full_parser( '<html><body><div>' );
$this->assertTrue( $processor->next_tag( 'BODY' ) );
$this->assertTrue( $processor->set_bookmark( $bookmark_name ), 'Failed to set bookmark.' );
$this->assertTrue( $processor->has_bookmark( $bookmark_name ), 'Failed has_bookmark check.' );
public function test_processor_seek_backward( callable $factory ) {
$processor = $factory( '<div><span>' );
$this->assertTrue( $processor->next_tag( 'DIV' ) );
$this->assertTrue( $processor->set_bookmark( 'mark' ), 'Failed to set bookmark.' );
$this->assertTrue( $processor->has_bookmark( 'mark' ), 'Failed has_bookmark check.' );

// Move past the bookmark so it must scan backwards.
$this->assertTrue( $processor->next_tag( 'DIV' ) );
$this->assertTrue( $processor->next_tag( 'SPAN' ) );

// Confirm the bookmark works.
$this->assertTrue( $processor->seek( $bookmark_name ), 'Failed to seek to bookmark.' );
$this->assertSame( 'BODY', $processor->get_tag() );
$this->assertTrue( $processor->seek( 'mark' ), 'Failed to seek to bookmark.' );
$this->assertSame( 'DIV', $processor->get_tag() );
}

/**
* @dataProvider data_processor_constructors
*
* @ticket 62290
*/
public function test_full_processor_seek_forward() {
$processor = WP_HTML_Processor::create_full_parser( '<html><body><div one></div><div two>' );
$this->assertTrue( $processor->next_tag( 'BODY' ) );
$this->assertTrue( $processor->set_bookmark( 'body' ), 'Failed to set bookmark "body".' );
$this->assertTrue( $processor->has_bookmark( 'body' ), 'Failed "body" has_bookmark check.' );

// Move past the bookmark so it must scan backwards.
public function test_processor_seek_forward( callable $factory ) {
$processor = $factory( '<div one></div><span two></span><a three>' );
$this->assertTrue( $processor->next_tag( 'DIV' ) );
$this->assertTrue( $processor->get_attribute( 'one' ) );
$this->assertTrue( $processor->set_bookmark( 'one' ), 'Failed to set bookmark "one".' );
$this->assertTrue( $processor->has_bookmark( 'one' ), 'Failed "one" has_bookmark check.' );

// Seek back.
$this->assertTrue( $processor->seek( 'body' ), 'Failed to seek to bookmark "body".' );
$this->assertSame( 'BODY', $processor->get_tag() );
// Move past the bookmark so it must scan backwards.
$this->assertTrue( $processor->next_tag( 'SPAN' ) );
$this->assertTrue( $processor->get_attribute( 'two' ) );
$this->assertTrue( $processor->set_bookmark( 'two' ), 'Failed to set bookmark "two".' );
$this->assertTrue( $processor->has_bookmark( 'two' ), 'Failed "two" has_bookmark check.' );

// Seek forward and continue processing.
// Seek back.
$this->assertTrue( $processor->seek( 'one' ), 'Failed to seek to bookmark "one".' );
$this->assertSame( 'DIV', $processor->get_tag() );
$this->assertTrue( $processor->get_attribute( 'one' ) );
$this->assertTrue( $processor->next_tag() );

$this->assertSame( 'DIV', $processor->get_tag() );
// Seek forward and continue processing.
$this->assertTrue( $processor->seek( 'two' ), 'Failed to seek to bookmark "two".' );
$this->assertSame( 'SPAN', $processor->get_tag() );
$this->assertTrue( $processor->get_attribute( 'two' ) );

$this->assertTrue( $processor->next_tag() );
$this->assertSame( 'A', $processor->get_tag() );
$this->assertTrue( $processor->get_attribute( 'three' ) );
}

/**
* Seeking into and out of foreign content requires updating the parsing namespace correctly.
* Ensure the parsing namespace is handled when seeking from foreign content.
*
* @dataProvider data_processor_constructors
*
* @ticket 62290
*/
public function test_full_processor_seek_back_from_foreign_content() {
$processor = WP_HTML_Processor::create_full_parser( '<html><body><custom-element /><svg><rect />' );
public function test_seek_back_from_foreign_content( callable $factory ) {
$processor = $factory( '<custom-element /><svg><rect />' );
$this->assertTrue( $processor->next_tag( 'CUSTOM-ELEMENT' ) );
$this->assertTrue( $processor->set_bookmark( 'mark' ), 'Failed to set bookmark "mark".' );
$this->assertTrue( $processor->has_bookmark( 'mark' ), 'Failed "mark" has_bookmark check.' );
Expand All @@ -95,7 +102,7 @@ public function test_full_processor_seek_back_from_foreign_content() {
* If the div were interpreted as foreign content, it would self-close.
*/
$this->assertTrue( $processor->has_self_closing_flag() );
$this->assertTrue( $processor->expects_closer(), 'Incorrectly interpreted custom-element with self-closing flag as self-closing element.' );
$this->assertTrue( $processor->expects_closer(), 'Incorrectly interpreted HTML custom-element with self-closing flag as self-closing element.' );

// Proceed into foreign content.
$this->assertTrue( $processor->next_tag( 'RECT' ) );
Expand All @@ -110,7 +117,7 @@ public function test_full_processor_seek_back_from_foreign_content() {
// If the parsing namespace were not correct here (html),
// then the self-closing flag would be misinterpreted.
$this->assertTrue( $processor->has_self_closing_flag() );
$this->assertTrue( $processor->expects_closer(), 'Incorrectly interpreted custom-element with self-closing flag as self-closing element.' );
$this->assertTrue( $processor->expects_closer(), 'Incorrectly interpreted HTML custom-element with self-closing flag as self-closing element.' );

// Proceed into foreign content again.
$this->assertTrue( $processor->next_tag( 'RECT' ) );
Expand All @@ -123,47 +130,14 @@ public function test_full_processor_seek_back_from_foreign_content() {
}

/**
* Seeking into and out of foreign content requires updating the parsing namespace correctly.
* Data provider.
*
* @ticket 62290
* @return array
*/
public function test_pragment_processor_seek_back_from_foreign_content() {
$processor = WP_HTML_Processor::create_fragment( '<custom-element /><svg><rect />' );
$this->assertTrue( $processor->next_tag( 'CUSTOM-ELEMENT' ) );
$this->assertTrue( $processor->set_bookmark( 'mark' ), 'Failed to set bookmark "mark".' );
$this->assertTrue( $processor->has_bookmark( 'mark' ), 'Failed "mark" has_bookmark check.' );

/*
* <custom-element /> has self-closing flag, but HTML elements (that are not void elements) cannot self-close,
* they must be closed by some means, usually a closing tag.
*
* If the div were interpreted as foreign content, it would self-close.
*/
$this->assertTrue( $processor->has_self_closing_flag() );
$this->assertTrue( $processor->expects_closer(), 'Incorrectly interpreted custom-element with self-closing flag as self-closing element.' );

// Proceed into foreign content.
$this->assertTrue( $processor->next_tag( 'RECT' ) );
$this->assertSame( 'svg', $processor->get_namespace() );
$this->assertTrue( $processor->has_self_closing_flag() );
$this->assertFalse( $processor->expects_closer() );
$this->assertSame( array( 'HTML', 'BODY', 'CUSTOM-ELEMENT', 'SVG', 'RECT' ), $processor->get_breadcrumbs() );

// Seek back.
$this->assertTrue( $processor->seek( 'mark' ), 'Failed to seek to bookmark "mark".' );
$this->assertSame( 'CUSTOM-ELEMENT', $processor->get_tag() );
// If the parsing namespace were not correct here (html),
// then the self-closing flag would be misinterpreted.
$this->assertTrue( $processor->has_self_closing_flag() );
$this->assertTrue( $processor->expects_closer(), 'Incorrectly interpreted custom-element with self-closing flag as self-closing element.' );

// Proceed into foreign content again.
$this->assertTrue( $processor->next_tag( 'RECT' ) );
$this->assertSame( 'svg', $processor->get_namespace() );
$this->assertTrue( $processor->has_self_closing_flag() );
$this->assertFalse( $processor->expects_closer() );

// The RECT should still descend from the CUSTOM-ELEMENT despite its self-closing flag.
$this->assertSame( array( 'HTML', 'BODY', 'CUSTOM-ELEMENT', 'SVG', 'RECT' ), $processor->get_breadcrumbs() );
public static function data_processor_constructors(): array {
return array(
'Full parser' => array( array( WP_HTML_Processor::class, 'create_full_parser' ) ),
'Fragment parser' => array( array( WP_HTML_Processor::class, 'create_fragment' ) ),
);
}
}

0 comments on commit b1a6c00

Please sign in to comment.