WP_HTML_Open_Elements::walk_up()publicWP 6.4.0

Steps through the stack of open elements, starting with the bottom element (added last) and walking upwards to the one added first.

This generator function is designed to be used inside a "foreach" loop.

Example:

$html = '<em><strong><a>We are here';
foreach ( $stack->walk_up() as $node ) {
	echo "{$node->node_name} -> ";
}
> A -> STRONG -> EM ->

To start with the first added element and walk towards the bottom, see WP_HTML_Open_Elements::walk_down().

Method of the class: WP_HTML_Open_Elements{}

No Hooks.

Return

null. Nothing (null).

Usage

$WP_HTML_Open_Elements = new WP_HTML_Open_Elements();
$WP_HTML_Open_Elements->walk_up( $above_this_node );
$above_this_node(?WP_HTML_Token)
Start traversing above this node, if provided and if the node exists.
Default: null

Changelog

Since 6.4.0 Introduced.
Since 6.5.0 Accepts $above_this_node to start traversal above a given node, if it exists.

WP_HTML_Open_Elements::walk_up() code WP 6.6.2

public function walk_up( $above_this_node = null ) {
	$has_found_node = null === $above_this_node;

	for ( $i = count( $this->stack ) - 1; $i >= 0; $i-- ) {
		$node = $this->stack[ $i ];

		if ( ! $has_found_node ) {
			$has_found_node = $node === $above_this_node;
			continue;
		}

		yield $node;
	}
}