Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML API: HTML Processor step all nodes #6

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions src/wp-includes/html-api/class-wp-html-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@
public function next_tag( $query = null ) {
if ( null === $query ) {
while ( $this->step() ) {
if ( ! $this->is_tag_closer() ) {
if ( $this->get_token_type() === '#tag' ) {
return true;
}
}
Expand All @@ -384,6 +384,9 @@

if ( ! ( array_key_exists( 'breadcrumbs', $query ) && is_array( $query['breadcrumbs'] ) ) ) {
while ( $this->step() ) {
if ( $this->get_token_type() !== '#tag' ) {
continue;
}
if ( ! $this->is_tag_closer() ) {
return true;
}
Expand All @@ -405,6 +408,9 @@
$match_offset = isset( $query['match_offset'] ) ? (int) $query['match_offset'] : 1;

while ( $match_offset > 0 && $this->step() ) {
if ( $this->get_token_type() !== '#tag' ) {
continue;
}
if ( $this->matches_breadcrumbs( $breadcrumbs ) && 0 === --$match_offset ) {
return true;
}
Expand All @@ -428,13 +434,7 @@
* @return bool
*/
public function next_token() {
$found_a_token = parent::next_token();

if ( '#tag' === $this->get_token_type() ) {
$this->step( self::PROCESS_CURRENT_NODE );
}

return $found_a_token;
return $this->step();
}

/**
Expand Down Expand Up @@ -535,13 +535,14 @@
}

if ( self::PROCESS_NEXT_NODE === $node_to_process ) {
while ( parent::next_token() && '#tag' !== $this->get_token_type() ) {
continue;
}
parent::next_token();
}

// Finish stepping when there are no more tokens in the document.
if ( null === $this->get_tag() ) {
if (
$this->parser_state === self::STATE_COMPLETE ||

Check failure on line 543 in src/wp-includes/html-api/class-wp-html-processor.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Use Yoda Condition checks, you must.
$this->parser_state === self::STATE_INCOMPLETE_INPUT

Check failure on line 544 in src/wp-includes/html-api/class-wp-html-processor.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Use Yoda Condition checks, you must.
) {
return false;
}

Expand Down Expand Up @@ -619,6 +620,19 @@
* @return bool Whether an element was found.
*/
private function step_in_body() {
switch ( $this->get_token_type() ) {
case '#text':
$this->reconstruct_active_formatting_elements();
return true;

case '#cdata-section':
case '#comment':
case '#doctype':
case '#presumptuous-tag':
case '#funky-comment':
return true;
}

$tag_name = $this->get_tag();
$op_sigil = $this->is_tag_closer() ? '-' : '+';
$op = "{$op_sigil}{$tag_name}";
Expand Down Expand Up @@ -1251,6 +1265,9 @@
case 'forward':
// When moving forwards, reparse the document until reaching the same location as the original bookmark.
while ( $this->step() ) {
if ( $this->get_token_type() !== '#tag' ) {
continue;
}
if ( $bookmark_starts_at === $this->bookmarks[ $this->state->current_token->bookmark_name ]->start ) {
return true;
}
Expand Down
8 changes: 7 additions & 1 deletion tests/phpunit/tests/html-api/wpHtmlProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,15 @@ public function test_warns_that_the_static_creator_methods_should_be_called_inst
*/
public function test_get_tag_is_null_once_document_is_finished() {
$processor = WP_HTML_Processor::create_fragment( '<div class="test">Test</div>' );
$processor->next_tag();
// Opener
$this->assertTrue( $processor->next_tag() );
$this->assertSame( 'DIV', $processor->get_tag() );

// Closer
$this->assertTrue( $processor->next_tag() );
$this->assertSame( 'DIV', $processor->get_tag() );

// End of document
$this->assertFalse( $processor->next_tag() );
$this->assertNull( $processor->get_tag() );
}
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/html-api/wpHtmlProcessorSemanticRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ public static function data_article_container_group() {
public function test_in_body_skips_unexpected_button_closer() {
$processor = WP_HTML_Processor::create_fragment( '<div>Test</button></div>' );

$processor->step();
$processor->next_tag();
$this->assertSame( 'DIV', $processor->get_tag(), 'Did not stop at initial DIV tag.' );
$this->assertFalse( $processor->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( $processor->step(), 'Found no further tags when it should have found the closing DIV' );
$this->assertTrue( $processor->next_tag(), 'Found no further tags when it should have found the closing DIV' );
$this->assertSame( 'DIV', $processor->get_tag(), "Did not skip unexpected BUTTON; stopped at {$processor->get_tag()}." );
$this->assertTrue( $processor->is_tag_closer(), 'Did not find that the terminal DIV tag is a closer.' );
}
Expand Down
Loading