WP_HTML_Processor::next_visitable_token(): bool

In this article

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

Ensures internal accounting is maintained for HTML semantic rules while the underlying Tag Processor class is seeking to a bookmark.

Description

This doesn’t currently have a way to represent non-tags and doesn’t process semantic rules for text nodes. For access to the raw tokens consider using WP_HTML_Tag_Processor instead.

Note that this method may call itself recursively. This is why it is not implemented as WP_HTML_Processor::next_token(), which instead calls this method similarly to how WP_HTML_Tag_Processor::next_token() calls the WP_HTML_Tag_Processor::base_class_next_token() method.

Return

bool

Source

private function next_visitable_token(): bool {
	$this->current_element = null;

	if ( isset( $this->last_error ) ) {
		return false;
	}

	/*
	 * Prime the events if there are none.
	 *
	 * @todo In some cases, probably related to the adoption agency
	 *       algorithm, this call to step() doesn't create any new
	 *       events. Calling it again creates them. Figure out why
	 *       this is and if it's inherent or if it's a bug. Looping
	 *       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.7.2Introduced.

User Contributed Notes

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