' );
+ * false === $processor->next_tag();
+ * WP_HTML_Processor::ERROR_UNSUPPORTED === $processor->get_last_error();
+ *
+ * @since 6.4.0
+ *
+ * @see self::ERROR_UNSUPPORTED
+ * @see self::ERROR_EXCEEDED_MAX_BOOKMARKS
+ *
+ * @return string|null The last error, if one exists, otherwise null.
+ */
+ public function get_last_error() {
+ return $this->last_error;
+ }
+
+ /**
+ * Finds the next tag matching the $query.
+ *
+ * @todo Support matching the class name and tag name.
+ *
+ * @since 6.4.0
+ *
+ * @throws Exception When unable to allocate a bookmark for the next token in the input HTML document.
+ *
+ * @param array|string|null $query {
+ * Optional. Which tag name to find, having which class, etc. Default is to find any tag.
+ *
+ * @type string|null $tag_name Which tag to find, or `null` for "any tag."
+ * @type int|null $match_offset Find the Nth tag matching all search criteria.
+ * 1 for "first" tag, 3 for "third," etc.
+ * Defaults to first tag.
+ * @type string|null $class_name Tag must contain this whole class name to match.
+ * @type string[] $breadcrumbs DOM sub-path at which element is found, e.g. `array( 'FIGURE', 'IMG' )`.
+ * May also contain the wildcard `*` which matches a single element, e.g. `array( 'SECTION', '*' )`.
+ * }
+ * @return bool Whether a tag was matched.
+ */
+ public function next_tag( $query = null ) {
+ if ( null === $query ) {
+ while ( $this->step() ) {
+ if ( ! $this->is_tag_closer() ) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ if ( is_string( $query ) ) {
+ $query = array( 'breadcrumbs' => array( $query ) );
+ }
+
+ if ( ! is_array( $query ) ) {
+ _doing_it_wrong(
+ __METHOD__,
+ __( 'Please pass a query array to this function.' ),
+ '6.4.0'
+ );
+ return false;
+ }
+
+ if ( ! ( array_key_exists( 'breadcrumbs', $query ) && is_array( $query['breadcrumbs'] ) ) ) {
+ while ( $this->step() ) {
+ if ( ! $this->is_tag_closer() ) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ if ( isset( $query['tag_closers'] ) && 'visit' === $query['tag_closers'] ) {
+ _doing_it_wrong(
+ __METHOD__,
+ __( 'Cannot visit tag closers in HTML Processor.' ),
+ '6.4.0'
+ );
+ return false;
+ }
+
+ $breadcrumbs = $query['breadcrumbs'];
+ $match_offset = isset( $query['match_offset'] ) ? (int) $query['match_offset'] : 1;
+
+ while ( $match_offset > 0 && $this->step() ) {
+ if ( $this->matches_breadcrumbs( $breadcrumbs ) && 0 === --$match_offset ) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Indicates if the currently-matched tag matches the given breadcrumbs.
+ *
+ * A "*" represents a single tag wildcard, where any tag matches, but not no tags.
+ *
+ * At some point this function _may_ support a `**` syntax for matching any number
+ * of unspecified tags in the breadcrumb stack. This has been intentionally left
+ * out, however, to keep this function simple and to avoid introducing backtracking,
+ * which could open up surprising performance breakdowns.
+ *
+ * Example:
+ *
+ * $processor = WP_HTML_Processor::create_fragment( '' );
+ * $processor->next_tag( 'img' );
+ * true === $processor->matches_breadcrumbs( array( 'figure', 'img' ) );
+ * true === $processor->matches_breadcrumbs( array( 'span', 'figure', 'img' ) );
+ * false === $processor->matches_breadcrumbs( array( 'span', 'img' ) );
+ * true === $processor->matches_breadcrumbs( array( 'span', '*', 'img' ) );
+ *
+ * @since 6.4.0
+ *
+ * @param string[] $breadcrumbs DOM sub-path at which element is found, e.g. `array( 'FIGURE', 'IMG' )`.
+ * May also contain the wildcard `*` which matches a single element, e.g. `array( 'SECTION', '*' )`.
+ * @return bool Whether the currently-matched tag is found at the given nested structure.
+ */
+ public function matches_breadcrumbs( $breadcrumbs ) {
+ if ( ! $this->get_tag() ) {
+ return false;
+ }
+
+ // Everything matches when there are zero constraints.
+ if ( 0 === count( $breadcrumbs ) ) {
+ return true;
+ }
+
+ // Start at the last crumb.
+ $crumb = end( $breadcrumbs );
+
+ if ( '*' !== $crumb && $this->get_tag() !== strtoupper( $crumb ) ) {
+ return false;
+ }
+
+ foreach ( $this->state->stack_of_open_elements->walk_up() as $node ) {
+ $crumb = strtoupper( current( $breadcrumbs ) );
+
+ if ( '*' !== $crumb && $node->node_name !== $crumb ) {
+ return false;
+ }
+
+ if ( false === prev( $breadcrumbs ) ) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Steps through the HTML document and stop at the next tag, if any.
+ *
+ * @since 6.4.0
+ *
+ * @throws Exception When unable to allocate a bookmark for the next token in the input HTML document.
+ *
+ * @see self::PROCESS_NEXT_NODE
+ * @see self::REPROCESS_CURRENT_NODE
+ *
+ * @param string $node_to_process Whether to parse the next node or reprocess the current node.
+ * @return bool Whether a tag was matched.
+ */
+ public function step( $node_to_process = self::PROCESS_NEXT_NODE ) {
+ // Refuse to proceed if there was a previous error.
+ if ( null !== $this->last_error ) {
+ return false;
+ }
+
+ if ( self::PROCESS_NEXT_NODE === $node_to_process ) {
+ /*
+ * Void elements still hop onto the stack of open elements even though
+ * there's no corresponding closing tag. This is important for managing
+ * stack-based operations such as "navigate to parent node" or checking
+ * on an element's breadcrumbs.
+ *
+ * When moving on to the next node, therefore, if the bottom-most element
+ * on the stack is a void element, it must be closed.
+ *
+ * @todo Once self-closing foreign elements and BGSOUND are supported,
+ * they must also be implicitly closed here too. BGSOUND is
+ * special since it's only self-closing if the self-closing flag
+ * is provided in the opening tag, otherwise it expects a tag closer.
+ */
+ $top_node = $this->state->stack_of_open_elements->current_node();
+ if ( $top_node && self::is_void( $top_node->node_name ) ) {
+ $this->state->stack_of_open_elements->pop();
+ }
+
+ parent::next_tag( self::VISIT_EVERYTHING );
+ }
+
+ // Finish stepping when there are no more tokens in the document.
+ if ( null === $this->get_tag() ) {
+ return false;
+ }
+
+ $this->state->current_token = new WP_HTML_Token(
+ $this->bookmark_tag(),
+ $this->get_tag(),
+ $this->is_tag_closer(),
+ $this->release_internal_bookmark_on_destruct
+ );
+
+ try {
+ switch ( $this->state->insertion_mode ) {
+ case WP_HTML_Processor_State::INSERTION_MODE_IN_BODY:
+ return $this->step_in_body();
+
+ default:
+ $this->last_error = self::ERROR_UNSUPPORTED;
+ throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
+ }
+ } catch ( WP_HTML_Unsupported_Exception $e ) {
+ /*
+ * Exceptions are used in this class to escape deep call stacks that
+ * otherwise might involve messier calling and return conventions.
+ */
+ return false;
+ }
+ }
+
+ /**
+ * Computes the HTML breadcrumbs for the currently-matched node, if matched.
+ *
+ * Breadcrumbs start at the outermost parent and descend toward the matched element.
+ * They always include the entire path from the root HTML node to the matched element.
+ *
+ * @todo It could be more efficient to expose a generator-based version of this function
+ * to avoid creating the array copy on tag iteration. If this is done, it would likely
+ * be more useful to walk up the stack when yielding instead of starting at the top.
+ *
+ * Example
+ *
+ * $processor = WP_HTML_Processor::create_fragment( '
' );
+ * $processor->next_tag( 'IMG' );
+ * $processor->get_breadcrumbs() === array( 'HTML', 'BODY', 'P', 'STRONG', 'EM', 'IMG' );
+ *
+ * @since 6.4.0
+ *
+ * @return string[]|null Array of tag names representing path to matched node, if matched, otherwise NULL.
+ */
+ public function get_breadcrumbs() {
+ if ( ! $this->get_tag() ) {
+ return null;
+ }
+
+ $breadcrumbs = array();
+ foreach ( $this->state->stack_of_open_elements->walk_down() as $stack_item ) {
+ $breadcrumbs[] = $stack_item->node_name;
+ }
+
+ return $breadcrumbs;
+ }
+
+ /**
+ * Parses next element in the 'in body' insertion mode.
+ *
+ * This internal function performs the 'in body' insertion mode
+ * logic for the generalized WP_HTML_Processor::step() function.
+ *
+ * @since 6.4.0
+ *
+ * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input.
+ *
+ * @see https://html.spec.whatwg.org/#parsing-main-inbody
+ * @see WP_HTML_Processor::step
+ *
+ * @return bool Whether an element was found.
+ */
+ private function step_in_body() {
+ $tag_name = $this->get_tag();
+ $op_sigil = $this->is_tag_closer() ? '-' : '+';
+ $op = "{$op_sigil}{$tag_name}";
+
+ switch ( $op ) {
+ /*
+ * > A start tag whose tag name is "button"
+ */
+ case '+BUTTON':
+ if ( $this->state->stack_of_open_elements->has_element_in_scope( 'BUTTON' ) ) {
+ // @todo Indicate a parse error once it's possible. This error does not impact the logic here.
+ $this->generate_implied_end_tags();
+ $this->state->stack_of_open_elements->pop_until( 'BUTTON' );
+ }
+
+ $this->reconstruct_active_formatting_elements();
+ $this->insert_html_element( $this->state->current_token );
+ $this->state->frameset_ok = false;
+
+ return true;
+
+ /*
+ * > A start tag whose tag name is one of: "address", "article", "aside",
+ * > "blockquote", "center", "details", "dialog", "dir", "div", "dl",
+ * > "fieldset", "figcaption", "figure", "footer", "header", "hgroup",
+ * > "main", "menu", "nav", "ol", "p", "search", "section", "summary", "ul"
+ */
+ case '+ADDRESS':
+ case '+ARTICLE':
+ case '+ASIDE':
+ case '+BLOCKQUOTE':
+ case '+CENTER':
+ case '+DETAILS':
+ case '+DIALOG':
+ case '+DIR':
+ case '+DIV':
+ case '+DL':
+ case '+FIELDSET':
+ case '+FIGCAPTION':
+ case '+FIGURE':
+ case '+FOOTER':
+ case '+HEADER':
+ case '+HGROUP':
+ case '+MAIN':
+ case '+MENU':
+ case '+NAV':
+ case '+OL':
+ case '+P':
+ case '+SEARCH':
+ case '+SECTION':
+ case '+SUMMARY':
+ case '+UL':
+ if ( $this->state->stack_of_open_elements->has_p_in_button_scope() ) {
+ $this->close_a_p_element();
+ }
+
+ $this->insert_html_element( $this->state->current_token );
+ return true;
+
+ /*
+ * > An end tag whose tag name is one of: "address", "article", "aside", "blockquote",
+ * > "button", "center", "details", "dialog", "dir", "div", "dl", "fieldset",
+ * > "figcaption", "figure", "footer", "header", "hgroup", "listing", "main",
+ * > "menu", "nav", "ol", "pre", "search", "section", "summary", "ul"
+ */
+ case '-ADDRESS':
+ case '-ARTICLE':
+ case '-ASIDE':
+ case '-BLOCKQUOTE':
+ case '-BUTTON':
+ case '-CENTER':
+ case '-DETAILS':
+ case '-DIALOG':
+ case '-DIR':
+ case '-DIV':
+ case '-DL':
+ case '-FIELDSET':
+ case '-FIGCAPTION':
+ case '-FIGURE':
+ case '-FOOTER':
+ case '-HEADER':
+ case '-HGROUP':
+ case '-MAIN':
+ case '-MENU':
+ case '-NAV':
+ case '-OL':
+ case '-SEARCH':
+ case '-SECTION':
+ case '-SUMMARY':
+ case '-UL':
+ if ( ! $this->state->stack_of_open_elements->has_element_in_scope( $tag_name ) ) {
+ // @todo Report parse error.
+ // Ignore the token.
+ return $this->step();
+ }
+
+ $this->generate_implied_end_tags();
+ if ( $this->state->stack_of_open_elements->current_node()->node_name !== $tag_name ) {
+ // @todo Record parse error: this error doesn't impact parsing.
+ }
+ $this->state->stack_of_open_elements->pop_until( $tag_name );
+ return true;
+
+ /*
+ * > A start tag whose tag name is one of: "h1", "h2", "h3", "h4", "h5", "h6"
+ */
+ case '+H1':
+ case '+H2':
+ case '+H3':
+ case '+H4':
+ case '+H5':
+ case '+H6':
+ if ( $this->state->stack_of_open_elements->has_p_in_button_scope() ) {
+ $this->close_a_p_element();
+ }
+
+ if (
+ in_array(
+ $this->state->stack_of_open_elements->current_node()->node_name,
+ array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' ),
+ true
+ )
+ ) {
+ // @todo Indicate a parse error once it's possible.
+ $this->state->stack_of_open_elements->pop();
+ }
+
+ $this->insert_html_element( $this->state->current_token );
+ return true;
+
+ /*
+ * > An end tag whose tag name is one of: "h1", "h2", "h3", "h4", "h5", "h6"
+ */
+ case '-H1':
+ case '-H2':
+ case '-H3':
+ case '-H4':
+ case '-H5':
+ case '-H6':
+ if ( ! $this->state->stack_of_open_elements->has_element_in_scope( '(internal: H1 through H6 - do not use)' ) ) {
+ /*
+ * This is a parse error; ignore the token.
+ *
+ * @todo Indicate a parse error once it's possible.
+ */
+ return $this->step();
+ }
+
+ $this->generate_implied_end_tags();
+
+ if ( $this->state->stack_of_open_elements->current_node()->node_name !== $tag_name ) {
+ // @todo Record parse error: this error doesn't impact parsing.
+ }
+
+ $this->state->stack_of_open_elements->pop_until( '(internal: H1 through H6 - do not use)' );
+ return true;
+
+ /*
+ * > A start tag whose tag name is "li"
+ * > A start tag whose tag name is one of: "dd", "dt"
+ */
+ case '+DD':
+ case '+DT':
+ case '+LI':
+ $this->state->frameset_ok = false;
+ $node = $this->state->stack_of_open_elements->current_node();
+ $is_li = 'LI' === $tag_name;
+
+ in_body_list_loop:
+ /*
+ * The logic for LI and DT/DD is the same except for one point: LI elements _only_
+ * close other LI elements, but a DT or DD element closes _any_ open DT or DD element.
+ */
+ if ( $is_li ? 'LI' === $node->node_name : ( 'DD' === $node->node_name || 'DT' === $node->node_name ) ) {
+ $node_name = $is_li ? 'LI' : $node->node_name;
+ $this->generate_implied_end_tags( $node_name );
+ if ( $node_name !== $this->state->stack_of_open_elements->current_node()->node_name ) {
+ // @todo Indicate a parse error once it's possible. This error does not impact the logic here.
+ }
+
+ $this->state->stack_of_open_elements->pop_until( $node_name );
+ goto in_body_list_done;
+ }
+
+ if (
+ 'ADDRESS' !== $node->node_name &&
+ 'DIV' !== $node->node_name &&
+ 'P' !== $node->node_name &&
+ $this->is_special( $node->node_name )
+ ) {
+ /*
+ * > If node is in the special category, but is not an address, div,
+ * > or p element, then jump to the step labeled done below.
+ */
+ goto in_body_list_done;
+ } else {
+ /*
+ * > Otherwise, set node to the previous entry in the stack of open elements
+ * > and return to the step labeled loop.
+ */
+ foreach ( $this->state->stack_of_open_elements->walk_up( $node ) as $item ) {
+ $node = $item;
+ break;
+ }
+ goto in_body_list_loop;
+ }
+
+ in_body_list_done:
+ if ( $this->state->stack_of_open_elements->has_p_in_button_scope() ) {
+ $this->close_a_p_element();
+ }
+
+ $this->insert_html_element( $this->state->current_token );
+ return true;
+
+ /*
+ * > An end tag whose tag name is "li"
+ * > An end tag whose tag name is one of: "dd", "dt"
+ */
+ case '-DD':
+ case '-DT':
+ case '-LI':
+ if (
+ /*
+ * An end tag whose tag name is "li":
+ * If the stack of open elements does not have an li element in list item scope,
+ * then this is a parse error; ignore the token.
+ */
+ (
+ 'LI' === $tag_name &&
+ ! $this->state->stack_of_open_elements->has_element_in_list_item_scope( 'LI' )
+ ) ||
+ /*
+ * An end tag whose tag name is one of: "dd", "dt":
+ * If the stack of open elements does not have an element in scope that is an
+ * HTML element with the same tag name as that of the token, then this is a
+ * parse error; ignore the token.
+ */
+ (
+ 'LI' !== $tag_name &&
+ ! $this->state->stack_of_open_elements->has_element_in_scope( $tag_name )
+ )
+ ) {
+ /*
+ * This is a parse error, ignore the token.
+ *
+ * @todo Indicate a parse error once it's possible.
+ */
+ return $this->step();
+ }
+
+ $this->generate_implied_end_tags( $tag_name );
+
+ if ( $tag_name !== $this->state->stack_of_open_elements->current_node()->node_name ) {
+ // @todo Indicate a parse error once it's possible. This error does not impact the logic here.
+ }
+
+ $this->state->stack_of_open_elements->pop_until( $tag_name );
+ return true;
+
+ /*
+ * > An end tag whose tag name is "p"
+ */
+ case '-P':
+ if ( ! $this->state->stack_of_open_elements->has_p_in_button_scope() ) {
+ $this->insert_html_element( $this->state->current_token );
+ }
+
+ $this->close_a_p_element();
+ return true;
+
+ // > A start tag whose tag name is "a"
+ case '+A':
+ foreach ( $this->state->active_formatting_elements->walk_up() as $item ) {
+ switch ( $item->node_name ) {
+ case 'marker':
+ break;
+
+ case 'A':
+ $this->run_adoption_agency_algorithm();
+ $this->state->active_formatting_elements->remove_node( $item );
+ $this->state->stack_of_open_elements->remove_node( $item );
+ break;
+ }
+ }
+
+ $this->reconstruct_active_formatting_elements();
+ $this->insert_html_element( $this->state->current_token );
+ $this->state->active_formatting_elements->push( $this->state->current_token );
+ return true;
+
+ /*
+ * > A start tag whose tag name is one of: "b", "big", "code", "em", "font", "i",
+ * > "s", "small", "strike", "strong", "tt", "u"
+ */
+ case '+B':
+ case '+BIG':
+ case '+CODE':
+ case '+EM':
+ case '+FONT':
+ case '+I':
+ case '+S':
+ case '+SMALL':
+ case '+STRIKE':
+ case '+STRONG':
+ case '+TT':
+ case '+U':
+ $this->reconstruct_active_formatting_elements();
+ $this->insert_html_element( $this->state->current_token );
+ $this->state->active_formatting_elements->push( $this->state->current_token );
+ return true;
+
+ /*
+ * > An end tag whose tag name is one of: "a", "b", "big", "code", "em", "font", "i",
+ * > "nobr", "s", "small", "strike", "strong", "tt", "u"
+ */
+ case '-A':
+ case '-B':
+ case '-BIG':
+ case '-CODE':
+ case '-EM':
+ case '-FONT':
+ case '-I':
+ case '-S':
+ case '-SMALL':
+ case '-STRIKE':
+ case '-STRONG':
+ case '-TT':
+ case '-U':
+ $this->run_adoption_agency_algorithm();
+ return true;
+
+ /*
+ * > A start tag whose tag name is one of: "area", "br", "embed", "img", "keygen", "wbr"
+ */
+ case '+IMG':
+ $this->reconstruct_active_formatting_elements();
+ $this->insert_html_element( $this->state->current_token );
+ return true;
+ }
+
+ /*
+ * These tags require special handling in the 'in body' insertion mode
+ * but that handling hasn't yet been implemented.
+ *
+ * As the rules for each tag are implemented, the corresponding tag
+ * name should be removed from this list. An accompanying test should
+ * help ensure this list is maintained.
+ *
+ * @see Tests_HtmlApi_WpHtmlProcessor::test_step_in_body_fails_on_unsupported_tags
+ *
+ * Since this switch structure throws a WP_HTML_Unsupported_Exception, it's
+ * possible to handle "any other start tag" and "any other end tag" below,
+ * as that guarantees execution doesn't proceed for the unimplemented tags.
+ *
+ * @see https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inbody
+ */
+ switch ( $tag_name ) {
+ case 'APPLET':
+ case 'AREA':
+ case 'BASE':
+ case 'BASEFONT':
+ case 'BGSOUND':
+ case 'BODY':
+ case 'BR':
+ case 'CAPTION':
+ case 'COL':
+ case 'COLGROUP':
+ case 'DD':
+ case 'DT':
+ case 'EMBED':
+ case 'FORM':
+ case 'FRAME':
+ case 'FRAMESET':
+ case 'HEAD':
+ case 'HR':
+ case 'HTML':
+ case 'IFRAME':
+ case 'INPUT':
+ case 'KEYGEN':
+ case 'LI':
+ case 'LINK':
+ case 'LISTING':
+ case 'MARQUEE':
+ case 'MATH':
+ case 'META':
+ case 'NOBR':
+ case 'NOEMBED':
+ case 'NOFRAMES':
+ case 'NOSCRIPT':
+ case 'OBJECT':
+ case 'OL':
+ case 'OPTGROUP':
+ case 'OPTION':
+ case 'PARAM':
+ case 'PLAINTEXT':
+ case 'PRE':
+ case 'RB':
+ case 'RP':
+ case 'RT':
+ case 'RTC':
+ case 'SARCASM':
+ case 'SCRIPT':
+ case 'SELECT':
+ case 'SOURCE':
+ case 'STYLE':
+ case 'SVG':
+ case 'TABLE':
+ case 'TBODY':
+ case 'TD':
+ case 'TEMPLATE':
+ case 'TEXTAREA':
+ case 'TFOOT':
+ case 'TH':
+ case 'THEAD':
+ case 'TITLE':
+ case 'TR':
+ case 'TRACK':
+ case 'UL':
+ case 'WBR':
+ case 'XMP':
+ $this->last_error = self::ERROR_UNSUPPORTED;
+ throw new WP_HTML_Unsupported_Exception( "Cannot process {$tag_name} element." );
+ }
+
+ if ( ! $this->is_tag_closer() ) {
+ /*
+ * > Any other start tag
+ */
+ $this->reconstruct_active_formatting_elements();
+ $this->insert_html_element( $this->state->current_token );
+ return true;
+ } else {
+ /*
+ * > Any other end tag
+ */
+
+ /*
+ * Find the corresponding tag opener in the stack of open elements, if
+ * it exists before reaching a special element, which provides a kind
+ * of boundary in the stack. For example, a `` should not
+ * close anything beyond its containing `P` or `DIV` element.
+ */
+ foreach ( $this->state->stack_of_open_elements->walk_up() as $node ) {
+ if ( $tag_name === $node->node_name ) {
+ break;
+ }
+
+ if ( self::is_special( $node->node_name ) ) {
+ // This is a parse error, ignore the token.
+ return $this->step();
+ }
+ }
+
+ $this->generate_implied_end_tags( $tag_name );
+ if ( $node !== $this->state->stack_of_open_elements->current_node() ) {
+ // @todo Record parse error: this error doesn't impact parsing.
+ }
+
+ foreach ( $this->state->stack_of_open_elements->walk_up() as $item ) {
+ $this->state->stack_of_open_elements->pop();
+ if ( $node === $item ) {
+ return true;
+ }
+ }
+ }
+ }
+
+ /*
+ * Internal helpers
+ */
+
+ /**
+ * Creates a new bookmark for the currently-matched tag and returns the generated name.
+ *
+ * @since 6.4.0
+ *
+ * @throws Exception When unable to allocate requested bookmark.
+ *
+ * @return string|false Name of created bookmark, or false if unable to create.
+ */
+ private function bookmark_tag() {
+ if ( ! $this->get_tag() ) {
+ return false;
+ }
+
+ if ( ! parent::set_bookmark( ++$this->bookmark_counter ) ) {
+ $this->last_error = self::ERROR_EXCEEDED_MAX_BOOKMARKS;
+ throw new Exception( 'could not allocate bookmark' );
+ }
+
+ return "{$this->bookmark_counter}";
+ }
+
+ /*
+ * HTML semantic overrides for Tag Processor
+ */
+
+ /**
+ * Returns the uppercase name of the matched tag.
+ *
+ * The semantic rules for HTML specify that certain tags be reprocessed
+ * with a different tag name. Because of this, the tag name presented
+ * by the HTML Processor may differ from the one reported by the HTML
+ * Tag Processor, which doesn't apply these semantic rules.
+ *
+ * Example:
+ *
+ * $processor = new WP_HTML_Tag_Processor( 'Test
' );
+ * $processor->next_tag() === true;
+ * $processor->get_tag() === 'DIV';
+ *
+ * $processor->next_tag() === false;
+ * $processor->get_tag() === null;
+ *
+ * @since 6.4.0
+ *
+ * @return string|null Name of currently matched tag in input HTML, or `null` if none found.
+ */
+ public function get_tag() {
+ if ( null !== $this->last_error ) {
+ return null;
+ }
+
+ $tag_name = parent::get_tag();
+
+ switch ( $tag_name ) {
+ case 'IMAGE':
+ /*
+ * > A start tag whose tag name is "image"
+ * > Change the token's tag name to "img" and reprocess it. (Don't ask.)
+ */
+ return 'IMG';
+
+ default:
+ return $tag_name;
+ }
+ }
+
+ /**
+ * Removes a bookmark that is no longer needed.
+ *
+ * Releasing a bookmark frees up the small
+ * performance overhead it requires.
+ *
+ * @since 6.4.0
+ *
+ * @param string $bookmark_name Name of the bookmark to remove.
+ * @return bool Whether the bookmark already existed before removal.
+ */
+ public function release_bookmark( $bookmark_name ) {
+ return parent::release_bookmark( "_{$bookmark_name}" );
+ }
+
+ /**
+ * Moves the internal cursor in the HTML Processor to a given bookmark's location.
+ *
+ * In order to prevent accidental infinite loops, there's a
+ * maximum limit on the number of times seek() can be called.
+ *
+ * @throws Exception When unable to allocate a bookmark for the next token in the input HTML document.
+ *
+ * @since 6.4.0
+ *
+ * @param string $bookmark_name Jump to the place in the document identified by this bookmark name.
+ * @return bool Whether the internal cursor was successfully moved to the bookmark's location.
+ */
+ public function seek( $bookmark_name ) {
+ $actual_bookmark_name = "_{$bookmark_name}";
+ $processor_started_at = $this->state->current_token
+ ? $this->bookmarks[ $this->state->current_token->bookmark_name ]->start
+ : 0;
+ $bookmark_starts_at = $this->bookmarks[ $actual_bookmark_name ]->start;
+ $direction = $bookmark_starts_at > $processor_started_at ? 'forward' : 'backward';
+
+ switch ( $direction ) {
+ case 'forward':
+ // When moving forwards, re-parse the document until reaching the same location as the original bookmark.
+ while ( $this->step() ) {
+ if ( $bookmark_starts_at === $this->bookmarks[ $this->state->current_token->bookmark_name ]->start ) {
+ return true;
+ }
+ }
+
+ return false;
+
+ case 'backward':
+ /*
+ * When moving backwards, clear out all existing stack entries which appear after the destination
+ * bookmark. These could be stored for later retrieval, but doing so would require additional
+ * memory overhead and also demand that references and bookmarks are updated as the document
+ * changes. In time this could be a valuable optimization, but it's okay to give up that
+ * optimization in exchange for more CPU time to recompute the stack, to re-parse the
+ * document that may have already been parsed once.
+ */
+ foreach ( $this->state->stack_of_open_elements->walk_up() as $item ) {
+ if ( $bookmark_starts_at >= $this->bookmarks[ $item->bookmark_name ]->start ) {
+ break;
+ }
+
+ $this->state->stack_of_open_elements->remove_node( $item );
+ }
+
+ foreach ( $this->state->active_formatting_elements->walk_up() as $item ) {
+ if ( $bookmark_starts_at >= $this->bookmarks[ $item->bookmark_name ]->start ) {
+ break;
+ }
+
+ $this->state->active_formatting_elements->remove_node( $item );
+ }
+
+ return parent::seek( $actual_bookmark_name );
+ }
+ }
+
+ /**
+ * Sets a bookmark in the HTML document.
+ *
+ * Bookmarks represent specific places or tokens in the HTML
+ * document, such as a tag opener or closer. When applying
+ * edits to a document, such as setting an attribute, the
+ * text offsets of that token may shift; the bookmark is
+ * kept updated with those shifts and remains stable unless
+ * the entire span of text in which the token sits is removed.
+ *
+ * Release bookmarks when they are no longer needed.
+ *
+ * Example:
+ *
+ * Surprising fact you may not know!
+ * ^ ^
+ * \-|-- this `H2` opener bookmark tracks the token
+ *
+ * Surprising fact you may no…
+ * ^ ^
+ * \-|-- it shifts with edits
+ *
+ * Bookmarks provide the ability to seek to a previously-scanned
+ * place in the HTML document. This avoids the need to re-scan
+ * the entire document.
+ *
+ * Example:
+ *
+ *
+ * ^^^^
+ * want to note this last item
+ *
+ * $p = new WP_HTML_Tag_Processor( $html );
+ * $in_list = false;
+ * while ( $p->next_tag( array( 'tag_closers' => $in_list ? 'visit' : 'skip' ) ) ) {
+ * if ( 'UL' === $p->get_tag() ) {
+ * if ( $p->is_tag_closer() ) {
+ * $in_list = false;
+ * $p->set_bookmark( 'resume' );
+ * if ( $p->seek( 'last-li' ) ) {
+ * $p->add_class( 'last-li' );
+ * }
+ * $p->seek( 'resume' );
+ * $p->release_bookmark( 'last-li' );
+ * $p->release_bookmark( 'resume' );
+ * } else {
+ * $in_list = true;
+ * }
+ * }
+ *
+ * if ( 'LI' === $p->get_tag() ) {
+ * $p->set_bookmark( 'last-li' );
+ * }
+ * }
+ *
+ * Bookmarks intentionally hide the internal string offsets
+ * to which they refer. They are maintained internally as
+ * updates are applied to the HTML document and therefore
+ * retain their "position" - the location to which they
+ * originally pointed. The inability to use bookmarks with
+ * functions like `substr` is therefore intentional to guard
+ * against accidentally breaking the HTML.
+ *
+ * Because bookmarks allocate memory and require processing
+ * for every applied update, they are limited and require
+ * a name. They should not be created with programmatically-made
+ * names, such as "li_{$index}" with some loop. As a general
+ * rule they should only be created with string-literal names
+ * like "start-of-section" or "last-paragraph".
+ *
+ * Bookmarks are a powerful tool to enable complicated behavior.
+ * Consider double-checking that you need this tool if you are
+ * reaching for it, as inappropriate use could lead to broken
+ * HTML structure or unwanted processing overhead.
+ *
+ * @since 6.4.0
+ *
+ * @param string $bookmark_name Identifies this particular bookmark.
+ * @return bool Whether the bookmark was successfully created.
+ */
+ public function set_bookmark( $bookmark_name ) {
+ return parent::set_bookmark( "_{$bookmark_name}" );
+ }
+
+ /*
+ * HTML Parsing Algorithms
+ */
+
+ /**
+ * Closes a P element.
+ *
+ * @since 6.4.0
+ *
+ * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input.
+ *
+ * @see https://html.spec.whatwg.org/#close-a-p-element
+ */
+ private function close_a_p_element() {
+ $this->generate_implied_end_tags( 'P' );
+ $this->state->stack_of_open_elements->pop_until( 'P' );
+ }
+
+ /**
+ * Closes elements that have implied end tags.
+ *
+ * @since 6.4.0
+ *
+ * @see https://html.spec.whatwg.org/#generate-implied-end-tags
+ *
+ * @param string|null $except_for_this_element Perform as if this element doesn't exist in the stack of open elements.
+ */
+ private function generate_implied_end_tags( $except_for_this_element = null ) {
+ $elements_with_implied_end_tags = array(
+ 'DD',
+ 'DT',
+ 'LI',
+ 'P',
+ );
+
+ $current_node = $this->state->stack_of_open_elements->current_node();
+ while (
+ $current_node && $current_node->node_name !== $except_for_this_element &&
+ in_array( $this->state->stack_of_open_elements->current_node(), $elements_with_implied_end_tags, true )
+ ) {
+ $this->state->stack_of_open_elements->pop();
+ }
+ }
+
+ /**
+ * Closes elements that have implied end tags, thoroughly.
+ *
+ * See the HTML specification for an explanation why this is
+ * different from generating end tags in the normal sense.
+ *
+ * @since 6.4.0
+ *
+ * @see WP_HTML_Processor::generate_implied_end_tags
+ * @see https://html.spec.whatwg.org/#generate-implied-end-tags
+ */
+ private function generate_implied_end_tags_thoroughly() {
+ $elements_with_implied_end_tags = array(
+ 'DD',
+ 'DT',
+ 'LI',
+ 'P',
+ );
+
+ while ( in_array( $this->state->stack_of_open_elements->current_node(), $elements_with_implied_end_tags, true ) ) {
+ $this->state->stack_of_open_elements->pop();
+ }
+ }
+
+ /**
+ * Reconstructs the active formatting elements.
+ *
+ * > This has the effect of reopening all the formatting elements that were opened
+ * > in the current body, cell, or caption (whichever is youngest) that haven't
+ * > been explicitly closed.
+ *
+ * @since 6.4.0
+ *
+ * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input.
+ *
+ * @see https://html.spec.whatwg.org/#reconstruct-the-active-formatting-elements
+ *
+ * @return bool Whether any formatting elements needed to be reconstructed.
+ */
+ private function reconstruct_active_formatting_elements() {
+ /*
+ * > If there are no entries in the list of active formatting elements, then there is nothing
+ * > to reconstruct; stop this algorithm.
+ */
+ if ( 0 === $this->state->active_formatting_elements->count() ) {
+ return false;
+ }
+
+ $last_entry = $this->state->active_formatting_elements->current_node();
+ if (
+
+ /*
+ * > If the last (most recently added) entry in the list of active formatting elements is a marker;
+ * > stop this algorithm.
+ */
+ 'marker' === $last_entry->node_name ||
+
+ /*
+ * > If the last (most recently added) entry in the list of active formatting elements is an
+ * > element that is in the stack of open elements, then there is nothing to reconstruct;
+ * > stop this algorithm.
+ */
+ $this->state->stack_of_open_elements->contains_node( $last_entry )
+ ) {
+ return false;
+ }
+
+ $this->last_error = self::ERROR_UNSUPPORTED;
+ throw new WP_HTML_Unsupported_Exception( 'Cannot reconstruct active formatting elements when advancing and rewinding is required.' );
+ }
+
+ /**
+ * Runs the adoption agency algorithm.
+ *
+ * @since 6.4.0
+ *
+ * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input.
+ *
+ * @see https://html.spec.whatwg.org/#adoption-agency-algorithm
+ */
+ private function run_adoption_agency_algorithm() {
+ $budget = 1000;
+ $subject = $this->get_tag();
+ $current_node = $this->state->stack_of_open_elements->current_node();
+
+ if (
+ // > If the current node is an HTML element whose tag name is subject
+ $current_node && $subject === $current_node->node_name &&
+ // > the current node is not in the list of active formatting elements
+ ! $this->state->active_formatting_elements->contains_node( $current_node )
+ ) {
+ $this->state->stack_of_open_elements->pop();
+ return;
+ }
+
+ $outer_loop_counter = 0;
+ while ( $budget-- > 0 ) {
+ if ( $outer_loop_counter++ >= 8 ) {
+ return;
+ }
+
+ /*
+ * > Let formatting element be the last element in the list of active formatting elements that:
+ * > - is between the end of the list and the last marker in the list,
+ * > if any, or the start of the list otherwise,
+ * > - and has the tag name subject.
+ */
+ $formatting_element = null;
+ foreach ( $this->state->active_formatting_elements->walk_up() as $item ) {
+ if ( 'marker' === $item->node_name ) {
+ break;
+ }
+
+ if ( $subject === $item->node_name ) {
+ $formatting_element = $item;
+ break;
+ }
+ }
+
+ // > If there is no such element, then return and instead act as described in the "any other end tag" entry above.
+ if ( null === $formatting_element ) {
+ $this->last_error = self::ERROR_UNSUPPORTED;
+ throw new WP_HTML_Unsupported_Exception( 'Cannot run adoption agency when "any other end tag" is required.' );
+ }
+
+ // > If formatting element is not in the stack of open elements, then this is a parse error; remove the element from the list, and return.
+ if ( ! $this->state->stack_of_open_elements->contains_node( $formatting_element ) ) {
+ $this->state->active_formatting_elements->remove_node( $formatting_element );
+ return;
+ }
+
+ // > If formatting element is in the stack of open elements, but the element is not in scope, then this is a parse error; return.
+ if ( ! $this->state->stack_of_open_elements->has_element_in_scope( $formatting_element->node_name ) ) {
+ return;
+ }
+
+ /*
+ * > Let furthest block be the topmost node in the stack of open elements that is lower in the stack
+ * > than formatting element, and is an element in the special category. There might not be one.
+ */
+ $is_above_formatting_element = true;
+ $furthest_block = null;
+ foreach ( $this->state->stack_of_open_elements->walk_down() as $item ) {
+ if ( $is_above_formatting_element && $formatting_element->bookmark_name !== $item->bookmark_name ) {
+ continue;
+ }
+
+ if ( $is_above_formatting_element ) {
+ $is_above_formatting_element = false;
+ continue;
+ }
+
+ if ( self::is_special( $item->node_name ) ) {
+ $furthest_block = $item;
+ break;
+ }
+ }
+
+ /*
+ * > If there is no furthest block, then the UA must first pop all the nodes from the bottom of the
+ * > stack of open elements, from the current node up to and including formatting element, then
+ * > remove formatting element from the list of active formatting elements, and finally return.
+ */
+ if ( null === $furthest_block ) {
+ foreach ( $this->state->stack_of_open_elements->walk_up() as $item ) {
+ $this->state->stack_of_open_elements->pop();
+
+ if ( $formatting_element->bookmark_name === $item->bookmark_name ) {
+ $this->state->active_formatting_elements->remove_node( $formatting_element );
+ return;
+ }
+ }
+ }
+
+ $this->last_error = self::ERROR_UNSUPPORTED;
+ throw new WP_HTML_Unsupported_Exception( 'Cannot extract common ancestor in adoption agency algorithm.' );
+ }
+
+ $this->last_error = self::ERROR_UNSUPPORTED;
+ throw new WP_HTML_Unsupported_Exception( 'Cannot run adoption agency when looping required.' );
+ }
+
+ /**
+ * Inserts an HTML element on the stack of open elements.
+ *
+ * @since 6.4.0
+ *
+ * @see https://html.spec.whatwg.org/#insert-a-foreign-element
+ *
+ * @param WP_HTML_Token $token Name of bookmark pointing to element in original input HTML.
+ */
+ private function insert_html_element( $token ) {
+ $this->state->stack_of_open_elements->push( $token );
+ }
+
+ /*
+ * HTML Specification Helpers
+ */
+
+ /**
+ * Returns whether an element of a given name is in the HTML special category.
+ *
+ * @since 6.4.0
+ *
+ * @see https://html.spec.whatwg.org/#special
+ *
+ * @param string $tag_name Name of element to check.
+ * @return bool Whether the element of the given name is in the special category.
+ */
+ public static function is_special( $tag_name ) {
+ $tag_name = strtoupper( $tag_name );
+
+ return (
+ 'ADDRESS' === $tag_name ||
+ 'APPLET' === $tag_name ||
+ 'AREA' === $tag_name ||
+ 'ARTICLE' === $tag_name ||
+ 'ASIDE' === $tag_name ||
+ 'BASE' === $tag_name ||
+ 'BASEFONT' === $tag_name ||
+ 'BGSOUND' === $tag_name ||
+ 'BLOCKQUOTE' === $tag_name ||
+ 'BODY' === $tag_name ||
+ 'BR' === $tag_name ||
+ 'BUTTON' === $tag_name ||
+ 'CAPTION' === $tag_name ||
+ 'CENTER' === $tag_name ||
+ 'COL' === $tag_name ||
+ 'COLGROUP' === $tag_name ||
+ 'DD' === $tag_name ||
+ 'DETAILS' === $tag_name ||
+ 'DIR' === $tag_name ||
+ 'DIV' === $tag_name ||
+ 'DL' === $tag_name ||
+ 'DT' === $tag_name ||
+ 'EMBED' === $tag_name ||
+ 'FIELDSET' === $tag_name ||
+ 'FIGCAPTION' === $tag_name ||
+ 'FIGURE' === $tag_name ||
+ 'FOOTER' === $tag_name ||
+ 'FORM' === $tag_name ||
+ 'FRAME' === $tag_name ||
+ 'FRAMESET' === $tag_name ||
+ 'H1' === $tag_name ||
+ 'H2' === $tag_name ||
+ 'H3' === $tag_name ||
+ 'H4' === $tag_name ||
+ 'H5' === $tag_name ||
+ 'H6' === $tag_name ||
+ 'HEAD' === $tag_name ||
+ 'HEADER' === $tag_name ||
+ 'HGROUP' === $tag_name ||
+ 'HR' === $tag_name ||
+ 'HTML' === $tag_name ||
+ 'IFRAME' === $tag_name ||
+ 'IMG' === $tag_name ||
+ 'INPUT' === $tag_name ||
+ 'KEYGEN' === $tag_name ||
+ 'LI' === $tag_name ||
+ 'LINK' === $tag_name ||
+ 'LISTING' === $tag_name ||
+ 'MAIN' === $tag_name ||
+ 'MARQUEE' === $tag_name ||
+ 'MENU' === $tag_name ||
+ 'META' === $tag_name ||
+ 'NAV' === $tag_name ||
+ 'NOEMBED' === $tag_name ||
+ 'NOFRAMES' === $tag_name ||
+ 'NOSCRIPT' === $tag_name ||
+ 'OBJECT' === $tag_name ||
+ 'OL' === $tag_name ||
+ 'P' === $tag_name ||
+ 'PARAM' === $tag_name ||
+ 'PLAINTEXT' === $tag_name ||
+ 'PRE' === $tag_name ||
+ 'SCRIPT' === $tag_name ||
+ 'SEARCH' === $tag_name ||
+ 'SECTION' === $tag_name ||
+ 'SELECT' === $tag_name ||
+ 'SOURCE' === $tag_name ||
+ 'STYLE' === $tag_name ||
+ 'SUMMARY' === $tag_name ||
+ 'TABLE' === $tag_name ||
+ 'TBODY' === $tag_name ||
+ 'TD' === $tag_name ||
+ 'TEMPLATE' === $tag_name ||
+ 'TEXTAREA' === $tag_name ||
+ 'TFOOT' === $tag_name ||
+ 'TH' === $tag_name ||
+ 'THEAD' === $tag_name ||
+ 'TITLE' === $tag_name ||
+ 'TR' === $tag_name ||
+ 'TRACK' === $tag_name ||
+ 'UL' === $tag_name ||
+ 'WBR' === $tag_name ||
+ 'XMP' === $tag_name ||
+
+ // MathML.
+ 'MI' === $tag_name ||
+ 'MO' === $tag_name ||
+ 'MN' === $tag_name ||
+ 'MS' === $tag_name ||
+ 'MTEXT' === $tag_name ||
+ 'ANNOTATION-XML' === $tag_name ||
+
+ // SVG.
+ 'FOREIGNOBJECT' === $tag_name ||
+ 'DESC' === $tag_name ||
+ 'TITLE' === $tag_name
+ );
+ }
+
+ /**
+ * Returns whether a given element is an HTML Void Element
+ *
+ * > area, base, br, col, embed, hr, img, input, link, meta, source, track, wbr
+ *
+ * @since 6.4.0
+ *
+ * @see https://html.spec.whatwg.org/#void-elements
+ *
+ * @param string $tag_name Name of HTML tag to check.
+ * @return bool Whether the given tag is an HTML Void Element.
+ */
+ public static function is_void( $tag_name ) {
+ $tag_name = strtoupper( $tag_name );
+
+ return (
+ 'AREA' === $tag_name ||
+ 'BASE' === $tag_name ||
+ 'BR' === $tag_name ||
+ 'COL' === $tag_name ||
+ 'EMBED' === $tag_name ||
+ 'HR' === $tag_name ||
+ 'IMG' === $tag_name ||
+ 'INPUT' === $tag_name ||
+ 'LINK' === $tag_name ||
+ 'META' === $tag_name ||
+ 'SOURCE' === $tag_name ||
+ 'TRACK' === $tag_name ||
+ 'WBR' === $tag_name
+ );
+ }
+
+ /*
+ * Constants that would pollute the top of the class if they were found there.
+ */
+
+ /**
+ * Indicates that the next HTML token should be parsed and processed.
+ *
+ * @since 6.4.0
+ *
+ * @var string
+ */
+ const PROCESS_NEXT_NODE = 'process-next-node';
+
+ /**
+ * Indicates that the current HTML token should be reprocessed in the newly-selected insertion mode.
+ *
+ * @since 6.4.0
+ *
+ * @var string
+ */
+ const REPROCESS_CURRENT_NODE = 'reprocess-current-node';
+
+ /**
+ * Indicates that the parser encountered unsupported markup and has bailed.
+ *
+ * @since 6.4.0
+ *
+ * @var string
+ */
+ const ERROR_UNSUPPORTED = 'unsupported';
+
+ /**
+ * Indicates that the parser encountered more HTML tokens than it
+ * was able to process and has bailed.
+ *
+ * @since 6.4.0
+ *
+ * @var string
+ */
+ const ERROR_EXCEEDED_MAX_BOOKMARKS = 'exceeded-max-bookmarks';
+
+ /**
+ * Unlock code that must be passed into the constructor to create this class.
+ *
+ * This class extends the WP_HTML_Tag_Processor, which has a public class
+ * constructor. Therefore, it's not possible to have a private constructor here.
+ *
+ * This unlock code is used to ensure that anyone calling the constructor is
+ * doing so with a full understanding that it's intended to be a private API.
+ *
+ * @access private
+ */
+ const CONSTRUCTOR_UNLOCK_CODE = 'Use WP_HTML_Processor::create_fragment() instead of calling the class constructor directly.';
+}
diff --git a/lib/compat/wordpress-6.5/html-api/class-gutenberg-html-processor-state-6-5.php b/lib/compat/wordpress-6.5/html-api/class-gutenberg-html-processor-state-6-5.php
new file mode 100644
index 0000000000000..890e19fb5da4d
--- /dev/null
+++ b/lib/compat/wordpress-6.5/html-api/class-gutenberg-html-processor-state-6-5.php
@@ -0,0 +1,143 @@
+ The frameset-ok flag is set to "ok" when the parser is created. It is set to "not ok" after certain tokens are seen.
+ *
+ * @since 6.4.0
+ *
+ * @see https://html.spec.whatwg.org/#frameset-ok-flag
+ *
+ * @var bool
+ */
+ public $frameset_ok = true;
+
+ /**
+ * Constructor - creates a new and empty state value.
+ *
+ * @since 6.4.0
+ *
+ * @see WP_HTML_Processor
+ */
+ public function __construct() {
+ $this->stack_of_open_elements = new Gutenberg_HTML_Open_Elements_6_5();
+ $this->active_formatting_elements = new WP_HTML_Active_Formatting_Elements();
+ }
+}
diff --git a/lib/compat/wordpress-6.5/html-api/class-gutenberg-html-tag-processor-6-5.php b/lib/compat/wordpress-6.5/html-api/class-gutenberg-html-tag-processor-6-5.php
index f14bc15adf999..de3823d2b2703 100644
--- a/lib/compat/wordpress-6.5/html-api/class-gutenberg-html-tag-processor-6-5.php
+++ b/lib/compat/wordpress-6.5/html-api/class-gutenberg-html-tag-processor-6-5.php
@@ -15,9 +15,6 @@
* - Prune the whitespace when removing classes/attributes: e.g. "a b c" -> "c" not " c".
* This would increase the size of the changes for some operations but leave more
* natural-looking output HTML.
- * - Decode HTML character references within class names when matching. E.g. match having
- * class `1<"2` needs to recognize `class="1<"2"`. Currently the Tag Processor
- * will fail to find the right tag if the class name is encoded as such.
* - Properly decode HTML character references in `get_attribute()`. PHP's
* `html_entity_decode()` is wrong in a couple ways: it doesn't account for the
* no-ambiguous-ampersand rule, and it improperly handles the way semicolons may
@@ -107,6 +104,56 @@
* given, it will return `true` (the only way to set `false` for an
* attribute is to remove it).
*
+ * #### When matching fails
+ *
+ * When `next_tag()` returns `false` it could mean different things:
+ *
+ * - The requested tag wasn't found in the input document.
+ * - The input document ended in the middle of an HTML syntax element.
+ *
+ * When a document ends in the middle of a syntax element it will pause
+ * the processor. This is to make it possible in the future to extend the
+ * input document and proceed - an important requirement for chunked
+ * streaming parsing of a document.
+ *
+ * Example:
+ *
+ * $processor = new WP_HTML_Tag_Processor( 'This
` inside an HTML comment.
+ * - STYLE content is raw text.
+ * - TITLE content is plain text but character references are decoded.
+ * - TEXTAREA content is plain text but character references are decoded.
+ * - XMP (deprecated) content is raw text.
+ *
* ### Modifying HTML attributes for a found tag
*
* Once you've found the start of an opening tag you can modify
@@ -241,9 +288,39 @@
* double-quoted strings, meaning that attributes on input with single-quoted or
* unquoted values will appear in the output with double-quotes.
*
+ * ### Scripting Flag
+ *
+ * The Tag Processor parses HTML with the "scripting flag" disabled. This means
+ * that it doesn't run any scripts while parsing the page. In a browser with
+ * JavaScript enabled, for example, the script can change the parse of the
+ * document as it loads. On the server, however, evaluating JavaScript is not
+ * only impractical, but also unwanted.
+ *
+ * Practically this means that the Tag Processor will descend into NOSCRIPT
+ * elements and process its child tags. Were the scripting flag enabled, such
+ * as in a typical browser, the contents of NOSCRIPT are skipped entirely.
+ *
+ * This allows the HTML API to process the content that will be presented in
+ * a browser when scripting is disabled, but it offers a different view of a
+ * page than most browser sessions will experience. E.g. the tags inside the
+ * NOSCRIPT disappear.
+ *
+ * ### Text Encoding
+ *
+ * The Tag Processor assumes that the input HTML document is encoded with a
+ * text encoding compatible with 7-bit ASCII's '<', '>', '&', ';', '/', '=',
+ * "'", '"', 'a' - 'z', 'A' - 'Z', and the whitespace characters ' ', tab,
+ * carriage-return, newline, and form-feed.
+ *
+ * In practice, this includes almost every single-byte encoding as well as
+ * UTF-8. Notably, however, it does not include UTF-16. If providing input
+ * that's incompatible, then convert the encoding beforehand.
+ *
* @since 6.2.0
* @since 6.2.1 Fix: Support for various invalid comments; attribute updates are case-insensitive.
* @since 6.3.2 Fix: Skip HTML-like content inside rawtext elements such as STYLE.
+ * @since 6.5.0 Pauses processor when input ends in an incomplete syntax token.
+ * Introduces "special" elements which act like void elements, e.g. STYLE.
*/
class Gutenberg_HTML_Tag_Processor_6_5 {
/**
@@ -316,6 +393,27 @@ class Gutenberg_HTML_Tag_Processor_6_5 {
*/
private $stop_on_tag_closers;
+ /**
+ * Specifies mode of operation of the parser at any given time.
+ *
+ * | State | Meaning |
+ * | --------------|----------------------------------------------------------------------|
+ * | *Ready* | The parser is ready to run. |
+ * | *Complete* | There is nothing left to parse. |
+ * | *Incomplete* | The HTML ended in the middle of a token; nothing more can be parsed. |
+ * | *Matched tag* | Found an HTML tag; it's possible to modify its attributes. |
+ *
+ * @since 6.5.0
+ *
+ * @see WP_HTML_Tag_Processor::STATE_READY
+ * @see WP_HTML_Tag_Processor::STATE_COMPLETE
+ * @see WP_HTML_Tag_Processor::STATE_INCOMPLETE
+ * @see WP_HTML_Tag_Processor::STATE_MATCHED_TAG
+ *
+ * @var string
+ */
+ private $parser_state = self::STATE_READY;
+
/**
* How many bytes from the original HTML document have been read and parsed.
*
@@ -544,6 +642,7 @@ public function __construct( $html ) {
* Finds the next tag matching the $query.
*
* @since 6.2.0
+ * @since 6.5.0 No longer processes incomplete tokens at end of document; pauses the processor at start of token.
*
* @param array|string|null $query {
* Optional. Which tag name to find, having which class, etc. Default is to find any tag.
@@ -562,90 +661,177 @@ public function next_tag( $query = null ) {
$already_found = 0;
do {
- if ( $this->bytes_already_parsed >= strlen( $this->html ) ) {
- return false;
- }
-
- // Find the next tag if it exists.
- if ( false === $this->parse_next_tag() ) {
- $this->bytes_already_parsed = strlen( $this->html );
-
+ if ( false === $this->next_token() ) {
return false;
}
- // Parse all of its attributes.
- while ( $this->parse_next_attribute() ) {
+ if ( self::STATE_MATCHED_TAG !== $this->parser_state ) {
continue;
}
- // Ensure that the tag closes before the end of the document.
- if ( $this->bytes_already_parsed >= strlen( $this->html ) ) {
- return false;
+ if ( $this->matches() ) {
+ ++$already_found;
}
+ } while ( $already_found < $this->sought_match_offset );
- $tag_ends_at = strpos( $this->html, '>', $this->bytes_already_parsed );
- if ( false === $tag_ends_at ) {
- return false;
- }
- $this->token_length = $tag_ends_at - $this->token_starts_at;
- $this->bytes_already_parsed = $tag_ends_at;
+ return true;
+ }
- // Finally, check if the parsed tag and its attributes match the search query.
- if ( $this->matches() ) {
- ++$already_found;
+ /**
+ * Finds the next token in the HTML document.
+ *
+ * An HTML document can be viewed as a stream of tokens,
+ * where tokens are things like HTML tags, HTML comments,
+ * text nodes, etc. This method finds the next token in
+ * the HTML document and returns whether it found one.
+ *
+ * If it starts parsing a token and reaches the end of the
+ * document then it will seek to the start of the last
+ * token and pause, returning `false` to indicate that it
+ * failed to find a complete token.
+ *
+ * Possible token types, based on the HTML specification:
+ *
+ * - an HTML tag, whether opening, closing, or void.
+ * - a text node - the plaintext inside tags.
+ * - an HTML comment.
+ * - a DOCTYPE declaration.
+ * - a processing instruction, e.g. ``.
+ *
+ * The Tag Processor currently only supports the tag token.
+ *
+ * @since 6.5.0
+ *
+ * @return bool Whether a token was parsed.
+ */
+ public function next_token() {
+ $this->get_updated_html();
+ $was_at = $this->bytes_already_parsed;
+
+ // Don't proceed if there's nothing more to scan.
+ if (
+ self::STATE_COMPLETE === $this->parser_state ||
+ self::STATE_INCOMPLETE === $this->parser_state
+ ) {
+ return false;
+ }
+
+ /*
+ * The next step in the parsing loop determines the parsing state;
+ * clear it so that state doesn't linger from the previous step.
+ */
+ $this->parser_state = self::STATE_READY;
+
+ if ( $this->bytes_already_parsed >= strlen( $this->html ) ) {
+ $this->parser_state = self::STATE_COMPLETE;
+ return false;
+ }
+
+ // Find the next tag if it exists.
+ if ( false === $this->parse_next_tag() ) {
+ if ( self::STATE_INCOMPLETE === $this->parser_state ) {
+ $this->bytes_already_parsed = $was_at;
}
- /*
- * For non-DATA sections which might contain text that looks like HTML tags but
- * isn't, scan with the appropriate alternative mode. Looking at the first letter
- * of the tag name as a pre-check avoids a string allocation when it's not needed.
- */
- $t = $this->html[ $this->tag_name_starts_at ];
- if (
- ! $this->is_closing_tag &&
+ return false;
+ }
+
+ // Parse all of its attributes.
+ while ( $this->parse_next_attribute() ) {
+ continue;
+ }
+
+ // Ensure that the tag closes before the end of the document.
+ if (
+ self::STATE_INCOMPLETE === $this->parser_state ||
+ $this->bytes_already_parsed >= strlen( $this->html )
+ ) {
+ // Does this appropriately clear state (parsed attributes)?
+ $this->parser_state = self::STATE_INCOMPLETE;
+ $this->bytes_already_parsed = $was_at;
+
+ return false;
+ }
+
+ $tag_ends_at = strpos( $this->html, '>', $this->bytes_already_parsed );
+ if ( false === $tag_ends_at ) {
+ $this->parser_state = self::STATE_INCOMPLETE;
+ $this->bytes_already_parsed = $was_at;
+
+ return false;
+ }
+ $this->parser_state = self::STATE_MATCHED_TAG;
+ $this->token_length = $tag_ends_at - $this->token_starts_at;
+ $this->bytes_already_parsed = $tag_ends_at;
+
+ /*
+ * For non-DATA sections which might contain text that looks like HTML tags but
+ * isn't, scan with the appropriate alternative mode. Looking at the first letter
+ * of the tag name as a pre-check avoids a string allocation when it's not needed.
+ */
+ $t = $this->html[ $this->tag_name_starts_at ];
+ if (
+ ! $this->is_closing_tag &&
+ (
+ 'i' === $t || 'I' === $t ||
+ 'n' === $t || 'N' === $t ||
+ 's' === $t || 'S' === $t ||
+ 't' === $t || 'T' === $t ||
+ 'x' === $t || 'X' === $t
+ )
+ ) {
+ $tag_name = $this->get_tag();
+
+ if ( 'SCRIPT' === $tag_name && ! $this->skip_script_data() ) {
+ $this->parser_state = self::STATE_INCOMPLETE;
+ $this->bytes_already_parsed = $was_at;
+
+ return false;
+ } elseif (
+ ( 'TEXTAREA' === $tag_name || 'TITLE' === $tag_name ) &&
+ ! $this->skip_rcdata( $tag_name )
+ ) {
+ $this->parser_state = self::STATE_INCOMPLETE;
+ $this->bytes_already_parsed = $was_at;
+
+ return false;
+ } elseif (
(
- 'i' === $t || 'I' === $t ||
- 'n' === $t || 'N' === $t ||
- 's' === $t || 'S' === $t ||
- 't' === $t || 'T' === $t
- ) ) {
- $tag_name = $this->get_tag();
-
- if ( 'SCRIPT' === $tag_name && ! $this->skip_script_data() ) {
- $this->bytes_already_parsed = strlen( $this->html );
- return false;
- } elseif (
- ( 'TEXTAREA' === $tag_name || 'TITLE' === $tag_name ) &&
- ! $this->skip_rcdata( $tag_name )
- ) {
- $this->bytes_already_parsed = strlen( $this->html );
- return false;
- } elseif (
- (
- 'IFRAME' === $tag_name ||
- 'NOEMBED' === $tag_name ||
- 'NOFRAMES' === $tag_name ||
- 'NOSCRIPT' === $tag_name ||
- 'STYLE' === $tag_name
- ) &&
- ! $this->skip_rawtext( $tag_name )
- ) {
- /*
- * "XMP" should be here too but its rules are more complicated and require the
- * complexity of the HTML Processor (it needs to close out any open P element,
- * meaning it can't be skipped here or else the HTML Processor will lose its
- * place). For now, it can be ignored as it's a rare HTML tag in practice and
- * any normative HTML should be using PRE instead.
- */
- $this->bytes_already_parsed = strlen( $this->html );
- return false;
- }
+ 'IFRAME' === $tag_name ||
+ 'NOEMBED' === $tag_name ||
+ 'NOFRAMES' === $tag_name ||
+ 'STYLE' === $tag_name ||
+ 'XMP' === $tag_name
+ ) &&
+ ! $this->skip_rawtext( $tag_name )
+ ) {
+ $this->parser_state = self::STATE_INCOMPLETE;
+ $this->bytes_already_parsed = $was_at;
+
+ return false;
}
- } while ( $already_found < $this->sought_match_offset );
+ }
return true;
}
+ /**
+ * Whether the processor paused because the input HTML document ended
+ * in the middle of a syntax element, such as in the middle of a tag.
+ *
+ * Example:
+ *
+ * $processor = new WP_HTML_Tag_Processor( '' === $html[ $at + 1 ] ) {
@@ -1276,6 +1502,8 @@ private function parse_next_tag() {
if ( '?' === $html[ $at + 1 ] ) {
$closer_at = strpos( $html, '>', $at + 2 );
if ( false === $closer_at ) {
+ $this->parser_state = self::STATE_INCOMPLETE;
+
return false;
}
@@ -1290,8 +1518,15 @@ private function parse_next_tag() {
* See https://html.spec.whatwg.org/#parse-error-invalid-first-character-of-tag-name
*/
if ( $this->is_closing_tag ) {
+ // No chance of finding a closer.
+ if ( $at + 3 > $doc_length ) {
+ return false;
+ }
+
$closer_at = strpos( $html, '>', $at + 3 );
if ( false === $closer_at ) {
+ $this->parser_state = self::STATE_INCOMPLETE;
+
return false;
}
@@ -1316,6 +1551,8 @@ private function parse_next_attribute() {
// Skip whitespace and slashes.
$this->bytes_already_parsed += strspn( $this->html, " \t\f\r\n/", $this->bytes_already_parsed );
if ( $this->bytes_already_parsed >= strlen( $this->html ) ) {
+ $this->parser_state = self::STATE_INCOMPLETE;
+
return false;
}
@@ -1338,11 +1575,15 @@ private function parse_next_attribute() {
$attribute_name = substr( $this->html, $attribute_start, $name_length );
$this->bytes_already_parsed += $name_length;
if ( $this->bytes_already_parsed >= strlen( $this->html ) ) {
+ $this->parser_state = self::STATE_INCOMPLETE;
+
return false;
}
$this->skip_whitespace();
if ( $this->bytes_already_parsed >= strlen( $this->html ) ) {
+ $this->parser_state = self::STATE_INCOMPLETE;
+
return false;
}
@@ -1351,6 +1592,8 @@ private function parse_next_attribute() {
++$this->bytes_already_parsed;
$this->skip_whitespace();
if ( $this->bytes_already_parsed >= strlen( $this->html ) ) {
+ $this->parser_state = self::STATE_INCOMPLETE;
+
return false;
}
@@ -1377,6 +1620,8 @@ private function parse_next_attribute() {
}
if ( $attribute_end >= strlen( $this->html ) ) {
+ $this->parser_state = self::STATE_INCOMPLETE;
+
return false;
}
@@ -1443,7 +1688,6 @@ private function skip_whitespace() {
* @since 6.2.0
*/
private function after_tag() {
- $this->get_updated_html();
$this->token_starts_at = null;
$this->token_length = null;
$this->tag_name_starts_at = null;
@@ -1786,6 +2030,10 @@ private static function sort_start_ascending( $a, $b ) {
* @return string|boolean|null Value of enqueued update if present, otherwise false.
*/
private function get_enqueued_attribute_value( $comparable_name ) {
+ if ( self::STATE_MATCHED_TAG !== $this->parser_state ) {
+ return false;
+ }
+
if ( ! isset( $this->lexical_updates[ $comparable_name ] ) ) {
return false;
}
@@ -1853,7 +2101,7 @@ private function get_enqueued_attribute_value( $comparable_name ) {
* @return string|true|null Value of attribute or `null` if not available. Boolean attributes return `true`.
*/
public function get_attribute( $name ) {
- if ( null === $this->tag_name_starts_at ) {
+ if ( self::STATE_MATCHED_TAG !== $this->parser_state ) {
return null;
}
@@ -1933,7 +2181,10 @@ public function get_attribute( $name ) {
* @return array|null List of attribute names, or `null` when no tag opener is matched.
*/
public function get_attribute_names_with_prefix( $prefix ) {
- if ( $this->is_closing_tag || null === $this->tag_name_starts_at ) {
+ if (
+ self::STATE_MATCHED_TAG !== $this->parser_state ||
+ $this->is_closing_tag
+ ) {
return null;
}
@@ -1965,7 +2216,7 @@ public function get_attribute_names_with_prefix( $prefix ) {
* @return string|null Name of currently matched tag in input HTML, or `null` if none found.
*/
public function get_tag() {
- if ( null === $this->tag_name_starts_at ) {
+ if ( self::STATE_MATCHED_TAG !== $this->parser_state ) {
return null;
}
@@ -1992,7 +2243,7 @@ public function get_tag() {
* @return bool Whether the currently matched tag contains the self-closing flag.
*/
public function has_self_closing_flag() {
- if ( ! $this->tag_name_starts_at ) {
+ if ( self::STATE_MATCHED_TAG !== $this->parser_state ) {
return false;
}
@@ -2024,7 +2275,10 @@ public function has_self_closing_flag() {
* @return bool Whether the current tag is a tag closer.
*/
public function is_tag_closer() {
- return $this->is_closing_tag;
+ return (
+ self::STATE_MATCHED_TAG === $this->parser_state &&
+ $this->is_closing_tag
+ );
}
/**
@@ -2044,7 +2298,10 @@ public function is_tag_closer() {
* @return bool Whether an attribute value was set.
*/
public function set_attribute( $name, $value ) {
- if ( $this->is_closing_tag || null === $this->tag_name_starts_at ) {
+ if (
+ self::STATE_MATCHED_TAG !== $this->parser_state ||
+ $this->is_closing_tag
+ ) {
return false;
}
@@ -2177,7 +2434,10 @@ public function set_attribute( $name, $value ) {
* @return bool Whether an attribute was removed.
*/
public function remove_attribute( $name ) {
- if ( $this->is_closing_tag ) {
+ if (
+ self::STATE_MATCHED_TAG !== $this->parser_state ||
+ $this->is_closing_tag
+ ) {
return false;
}
@@ -2254,13 +2514,14 @@ public function remove_attribute( $name ) {
* @return bool Whether the class was set to be added.
*/
public function add_class( $class_name ) {
- if ( $this->is_closing_tag ) {
+ if (
+ self::STATE_MATCHED_TAG !== $this->parser_state ||
+ $this->is_closing_tag
+ ) {
return false;
}
- if ( null !== $this->tag_name_starts_at ) {
- $this->classname_updates[ $class_name ] = self::ADD_CLASS;
- }
+ $this->classname_updates[ $class_name ] = self::ADD_CLASS;
return true;
}
@@ -2274,7 +2535,10 @@ public function add_class( $class_name ) {
* @return bool Whether the class was set to be removed.
*/
public function remove_class( $class_name ) {
- if ( $this->is_closing_tag ) {
+ if (
+ self::STATE_MATCHED_TAG !== $this->parser_state ||
+ $this->is_closing_tag
+ ) {
return false;
}
@@ -2480,4 +2744,57 @@ private function matches() {
return true;
}
+
+ /**
+ * Parser Ready State
+ *
+ * Indicates that the parser is ready to run and waiting for a state transition.
+ * It may not have started yet, or it may have just finished parsing a token and
+ * is ready to find the next one.
+ *
+ * @since 6.5.0
+ *
+ * @access private
+ */
+ const STATE_READY = 'STATE_READY';
+
+ /**
+ * Parser Complete State
+ *
+ * Indicates that the parser has reached the end of the document and there is
+ * nothing left to scan. It finished parsing the last token completely.
+ *
+ * @since 6.5.0
+ *
+ * @access private
+ */
+ const STATE_COMPLETE = 'STATE_COMPLETE';
+
+ /**
+ * Parser Incomplete State
+ *
+ * Indicates that the parser has reached the end of the document before finishing
+ * a token. It started parsing a token but there is a possibility that the input
+ * HTML document was truncated in the middle of a token.
+ *
+ * The parser is reset at the start of the incomplete token and has paused. There
+ * is nothing more than can be scanned unless provided a more complete document.
+ *
+ * @since 6.5.0
+ *
+ * @access private
+ */
+ const STATE_INCOMPLETE = 'STATE_INCOMPLETE';
+
+ /**
+ * Parser Matched Tag State
+ *
+ * Indicates that the parser has found an HTML tag and it's possible to get
+ * the tag name and read or modify its attributes (if it's not a closing tag).
+ *
+ * @since 6.5.0
+ *
+ * @access private
+ */
+ const STATE_MATCHED_TAG = 'STATE_MATCHED_TAG';
}
diff --git a/lib/load.php b/lib/load.php
index 8988a05820425..6314d89cc5c80 100644
--- a/lib/load.php
+++ b/lib/load.php
@@ -79,7 +79,10 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-6.5/html-api/class-gutenberg-html-attribute-token-6-5.php';
require __DIR__ . '/compat/wordpress-6.5/html-api/class-gutenberg-html-span-6-5.php';
require __DIR__ . '/compat/wordpress-6.5/html-api/class-gutenberg-html-text-replacement-6-5.php';
+require __DIR__ . '/compat/wordpress-6.5/html-api/class-gutenberg-html-open-elements-6-5.php';
+require __DIR__ . '/compat/wordpress-6.5/html-api/class-gutenberg-html-processor-state-6-5.php';
require __DIR__ . '/compat/wordpress-6.5/html-api/class-gutenberg-html-tag-processor-6-5.php';
+require __DIR__ . '/compat/wordpress-6.5/html-api/class-gutenberg-html-processor-6-5.php';
/*
* The HTML Processor appeared after WordPress 6.3. If Gutenberg is running on a version of
diff --git a/phpcs.xml.dist b/phpcs.xml.dist
index 21f3fcb8baee1..1eee805dc8087 100644
--- a/phpcs.xml.dist
+++ b/phpcs.xml.dist
@@ -58,6 +58,11 @@
./vendor/*
./test/php/gutenberg-coding-standards/*
+
+ ./lib/compat/wordpress-*/html-api/*.php
+
/phpunit/*