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

WIP: HTML API: Allow extending input document for chunked processing. #5050

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
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
6 changes: 4 additions & 2 deletions src/wp-includes/html-api/class-wp-html-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,11 +438,13 @@ public function step( $node_to_process = self::PROCESS_NEXT_NODE ) {
$this->state->stack_of_open_elements->pop();
}

parent::next_tag( self::VISIT_EVERYTHING );
$found_tag = parent::next_tag( self::VISIT_EVERYTHING );
} else {
$found_tag = true;
}

// Finish stepping when there are no more tokens in the document.
if ( null === $this->get_tag() ) {
if ( ! $found_tag ) {
return false;
}

Expand Down
11 changes: 11 additions & 0 deletions src/wp-includes/html-api/class-wp-html-tag-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ class WP_HTML_Tag_Processor {
*/
private $bytes_already_parsed = 0;

private $previously_parsed = 0;

/**
* Byte offset in input document where current tag name starts.
*
Expand Down Expand Up @@ -507,6 +509,14 @@ public function __construct( $html ) {
$this->html = $html;
}

public function extend_input( $html ) {
if ( $this->bytes_already_parsed >= strlen( $this->html ) ) {
$this->bytes_already_parsed = $this->previously_parsed;
}

$this->html .= $html;
}

/**
* Finds the next tag matching the $query.
*
Expand All @@ -527,6 +537,7 @@ public function __construct( $html ) {
public function next_tag( $query = null ) {
$this->parse_query( $query );
$already_found = 0;
$this->previously_parsed = $this->bytes_already_parsed;

do {
if ( $this->bytes_already_parsed >= strlen( $this->html ) ) {
Expand Down
48 changes: 48 additions & 0 deletions tests/phpunit/tests/html-api/wpHtmlProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,52 @@ public function test_fails_to_reconstruct_formatting_elements() {
$this->assertTrue( $p->next_tag( 'EM' ), 'Could not find first EM.' );
$this->assertFalse( $p->next_tag( 'EM' ), 'Should have aborted before finding second EM as it required reconstructing the first EM.' );
}

/**
*
* @ticket {TICKET NUMBER}
*
* @covers WP_HTML_Processor::extend_input
*/
public function test_continues_processing_after_extending_input() {
$p = WP_HTML_Processor::createFragment( '<div><p><em>This</em> is' );

$p->next_tag( 'EM' );
$this->assertFalse( $p->next_tag(), "Expected to reach end of document and pause, but found {$p->get_tag()} instad." );
$this->assertNull( $p->get_last_error(), "Should not have encountered any errors, but found '{$p->get_last_error()}'." );

$p->extend_input( ' <strong>incomplete.</strong></p><img></div>' );

// Find the strong.
$this->assertTrue( $p->next_tag() );
$this->assertSame( array( 'HTML', 'BODY', 'DIV', 'P', 'STRONG' ), $p->get_breadcrumbs() );

// Find the image.
$this->assertTrue( $p->next_tag() );
$this->assertSame( array( 'HTML', 'BODY', 'DIV', 'IMG' ), $p->get_breadcrumbs() );
}

/**
*
* @ticket {TICKET NUMBER}
*
* @covers WP_HTML_Processor::extend_input
*/
public function test_continues_processing_after_extending_truncated_input() {
$p = WP_HTML_Processor::createFragment( '<div><p><em>This</em> is <str' );

$p->next_tag( 'EM' );
$this->assertFalse( $p->next_tag(), "Expected to reach end of document and pause, but found {$p->get_tag()} instad." );
$this->assertNull( $p->get_last_error(), "Should not have encountered any errors, but found '{$p->get_last_error()}'." );

$p->extend_input( 'ong>incomplete.</strong></p><img></div>' );

// Find the strong.
$this->assertTrue( $p->next_tag() );
$this->assertSame( array( 'HTML', 'BODY', 'DIV', 'P', 'STRONG' ), $p->get_breadcrumbs() );

// Find the image.
$this->assertTrue( $p->next_tag() );
$this->assertSame( array( 'HTML', 'BODY', 'DIV', 'IMG' ), $p->get_breadcrumbs() );
}
}
Loading