WP_HTML_Processor::next_tag( array|string|null $query = null ): bool

In this article

Finds the next tag matching the $query.

Parameters

$queryarray|string|nulloptional
Which tag name to find, having which class, etc. Default is to find any tag.
  • tag_name string|null
    Which tag to find, or null for "any tag."
  • tag_closers string
    'visit' to pause at tag closers, 'skip' or unset to only visit openers.
  • match_offset int|null
    Find the Nth tag matching all search criteria.
    1 for "first" tag, 3 for "third," etc.
    Defaults to first tag.
  • class_name string|null
    Tag must contain this whole class name to match.
  • breadcrumbs string[]
    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', '*' ).

Default:null

Return

bool Whether a tag was matched.

Source

 *
 * @throws WP_HTML_Unsupported_Exception Halts execution of the parser.
 *
 * @since 6.7.0
 *
 * @param string $message Explains support is missing in order to parse the current node.
 */
private function bail( string $message ) {
	$here  = $this->bookmarks[ $this->state->current_token->bookmark_name ];
	$token = substr( $this->html, $here->start, $here->length );

	$open_elements = array();
	foreach ( $this->state->stack_of_open_elements->stack as $item ) {
		$open_elements[] = $item->node_name;
	}

	$active_formats = array();
	foreach ( $this->state->active_formatting_elements->walk_down() as $item ) {
		$active_formats[] = $item->node_name;
	}

	$this->last_error = self::ERROR_UNSUPPORTED;

	$this->unsupported_exception = new WP_HTML_Unsupported_Exception(
		$message,
		$this->state->current_token->node_name,
		$here->start,
		$token,
		$open_elements,
		$active_formats
	);

	throw $this->unsupported_exception;
}

/**
 * Returns the last error, if any.
 *
 * Various situations lead to parsing failure but this class will
 * return `false` in all those cases. To determine why something
 * failed it's possible to request the last error. This can be
 * helpful to know to distinguish whether a given tag couldn't
 * be found or if content in the document caused the processor
 * to give up and abort processing.
 *
 * Example
 *
 *     $processor = WP_HTML_Processor::create_fragment( '<template><strong><button><em><p><em>' );
 *     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(): ?string {
	return $this->last_error;
}

/**
 * Returns context for why the parser aborted due to unsupported HTML, if it did.
 *
 * This is meant for debugging purposes, not for production use.
 *
 * @since 6.7.0
 *
 * @see self::$unsupported_exception
 *

Changelog

VersionDescription
6.6.0Visits all tokens, including virtual ones.
6.4.0Introduced.

User Contributed Notes

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