Skip to content

Commit cc96ed2

Browse files
committed
HTML API: Avoid processing incomplete syntax elements.
The HTML Tag Processor is able to know if it starts parsing a syntax element and reaches the end of the document before it reaches the end of the element. In these cases, after this patch, the processor will indicate this condition. For example, when processing `<div><input type="te` there is an incomplete INPUT element. The processor will fail to find the INPUT, it will pause right after the DIV, and `paused_at_incomplete_token()` will return `true`. This patch doesn't change any existing behaviors, but it adds the new method to report on the final failure condition. It provides a mechanism for later use to add chunked parsing to the class, wherein it will be possible to process a document without having the entire document loaded in memory, for example when processing unbuffered output. This is also a necessary change for adding the ability to scan every token in the document. Currently the Tag Processor only exposes tags as tokens, but it will need to process `#text` nodes, HTML comments, and other markup in order to enable behaviors in the HTML Processor and in refactors of existing HTML processing in Core.
1 parent 7aa146d commit cc96ed2

File tree

3 files changed

+530
-88
lines changed

3 files changed

+530
-88
lines changed

src/wp-includes/html-api/class-wp-html-processor.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,6 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
145145
*/
146146
const MAX_BOOKMARKS = 100;
147147

148-
/**
149-
* Static query for instructing the Tag Processor to visit every token.
150-
*
151-
* @access private
152-
*
153-
* @since 6.4.0
154-
*
155-
* @var array
156-
*/
157-
const VISIT_EVERYTHING = array( 'tag_closers' => 'visit' );
158-
159148
/**
160149
* Holds the working state of the parser, including the stack of
161150
* open elements and the stack of active formatting elements.
@@ -516,7 +505,9 @@ public function step( $node_to_process = self::PROCESS_NEXT_NODE ) {
516505
$this->state->stack_of_open_elements->pop();
517506
}
518507

519-
parent::next_tag( self::VISIT_EVERYTHING );
508+
while ( parent::next_token() && '#tag' !== $this->get_token_type() ) {
509+
continue;
510+
}
520511
}
521512

522513
// Finish stepping when there are no more tokens in the document.

0 commit comments

Comments
 (0)