WP_HTML_Open_Elements::walk_up( WP_HTML_Token|null $above_this_node = null )

In this article

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

Description

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().

Parameters

$above_this_nodeWP_HTML_Token|nulloptional
Start traversing above this node, if provided and if the node exists.

Default:null

Source

public function walk_up( ?WP_HTML_Token $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;
	}
}

Changelog

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

User Contributed Notes

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