WP_HTML_Processor::step()publicWP 6.4.0

Steps through the HTML document and stop at the next tag, if any.

Method of the class: WP_HTML_Processor{}

No Hooks.

Return

true|false. Whether a tag was matched.

Usage

$WP_HTML_Processor = new WP_HTML_Processor();
$WP_HTML_Processor->step( $node_to_process );
$node_to_process(string)
Whether to parse the next node or reprocess the current node.
Default: self::PROCESS_NEXT_NODE

Notes

  • See: self::PROCESS_NEXT_NODE
  • See: self::REPROCESS_CURRENT_NODE

Changelog

Since 6.4.0 Introduced.

WP_HTML_Processor::step() code WP 6.6.2

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::REPROCESS_CURRENT_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 ( isset( $top_node ) && ! static::expects_closer( $top_node ) ) {
			$this->state->stack_of_open_elements->pop();
		}
	}

	if ( self::PROCESS_NEXT_NODE === $node_to_process ) {
		parent::next_token();
	}

	// Finish stepping when there are no more tokens in the document.
	if (
		WP_HTML_Tag_Processor::STATE_INCOMPLETE_INPUT === $this->parser_state ||
		WP_HTML_Tag_Processor::STATE_COMPLETE === $this->parser_state
	) {
		return false;
	}

	$this->state->current_token = new WP_HTML_Token(
		$this->bookmark_token(),
		$this->get_token_name(),
		$this->has_self_closing_flag(),
		$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;
	}
}