WP_HTML_Processor::get_breadcrumbs(): string[]|null

In this article

Computes the HTML breadcrumbs for the currently-matched node, if matched.

Description

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.

Return

string[]|null Array of tag names representing path to matched node, if matched, otherwise NULL.

Source

 *       until there are events or until there are no more
 *       tokens works in the meantime and isn't obviously wrong.
 */
if ( empty( $this->element_queue ) && $this->step() ) {
	return $this->next_visitable_token();
}

// Process the next event on the queue.
$this->current_element = array_shift( $this->element_queue );
if ( ! isset( $this->current_element ) ) {
	// There are no tokens left, so close all remaining open elements.
	while ( $this->state->stack_of_open_elements->pop() ) {
		continue;
	}

	return empty( $this->element_queue ) ? false : $this->next_visitable_token();
}

$is_pop = WP_HTML_Stack_Event::POP === $this->current_element->operation;

/*
 * The root node only exists in the fragment parser, and closing it
 * indicates that the parse is complete. Stop before popping it from
 * the breadcrumbs.
 */
if ( 'root-node' === $this->current_element->token->bookmark_name ) {
	return $this->next_visitable_token();
}

// Adjust the breadcrumbs for this event.
if ( $is_pop ) {
	array_pop( $this->breadcrumbs );
} else {
	$this->breadcrumbs[] = $this->current_element->token->node_name;
}

// Avoid sending close events for elements which don't expect a closing.
if ( $is_pop && ! $this->expects_closer( $this->current_element->token ) ) {
	return $this->next_visitable_token();
}

return true;

Changelog

VersionDescription
6.4.0Introduced.

User Contributed Notes

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