WP_HTML_Processor::step( string $node_to_process = self::PROCESS_NEXT_NODE ): bool

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

Description

See also

Parameters

$node_to_processstringoptional
Whether to parse the next node or reprocess the current node.

Default:self::PROCESS_NEXT_NODE

Return

bool Whether a tag was matched.

Source

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 (
			$top_node && (
				// Void elements.
				self::is_void( $top_node->node_name ) ||
				// Comments, text nodes, and other atomic tokens.
				'#' === $top_node->node_name[0] ||
				// Doctype declarations.
				'html' === $top_node->node_name
			)
		) {
			$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;
	}
}

Changelog

VersionDescription
6.4.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.