WP_HTML_Processor::reset_insertion_mode_appropriately()

In this article

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

Runs the reset the insertion mode appropriately algorithm.

Description

See also

Source

		return false;
	}

	$last_entry = $this->state->active_formatting_elements->current_node();
	if (

		/*
		 * > If the last (most recently added) entry in the list of active formatting elements is a marker;
		 * > stop this algorithm.
		 */
		'marker' === $last_entry->node_name ||

		/*
		 * > If the last (most recently added) entry in the list of active formatting elements is an
		 * > element that is in the stack of open elements, then there is nothing to reconstruct;
		 * > stop this algorithm.
		 */
		$this->state->stack_of_open_elements->contains_node( $last_entry )
	) {
		return false;
	}

	$this->bail( 'Cannot reconstruct active formatting elements when advancing and rewinding is required.' );
}

/**
 * Runs the reset the insertion mode appropriately algorithm.
 *
 * @since 6.7.0
 * @ignore
 *
 * @see https://html.spec.whatwg.org/multipage/parsing.html#reset-the-insertion-mode-appropriately
 */
private function reset_insertion_mode_appropriately(): void {
	// Set the first node.
	$first_node = null;
	foreach ( $this->state->stack_of_open_elements->walk_down() as $first_node ) {
		break;
	}

	/*
	 * > 1. Let _last_ be false.
	 */
	$last = false;
	foreach ( $this->state->stack_of_open_elements->walk_up() as $node ) {
		/*
		 * > 2. Let _node_ be the last node in the stack of open elements.
		 * > 3. _Loop_: If _node_ is the first node in the stack of open elements, then set _last_
		 * >            to true, and, if the parser was created as part of the HTML fragment parsing
		 * >            algorithm (fragment case), set node to the context element passed to
		 * >            that algorithm.
		 * > …
		 */
		if ( $node === $first_node ) {
			$last = true;
			if ( isset( $this->context_node ) ) {
				$node = $this->context_node;
			}
		}

		// All of the following rules are for matching HTML elements.
		if ( 'html' !== $node->namespace ) {
			continue;
		}

		switch ( $node->node_name ) {
			/*
			 * > 4. If _node_ is a `td` or `th` element and _last_ is false, then switch the
			 * >    insertion mode to "in cell" and return.
			 */
			case 'TD':
			case 'TH':
				if ( ! $last ) {
					$this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_CELL;
					return;
				}
				break;

			/*
			* > 5. If _node_ is a `tr` element, then switch the insertion mode to "in row"
			* >    and return.
			*/
			case 'TR':
				$this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_ROW;
				return;

			/*
			 * > 6. If _node_ is a `tbody`, `thead`, or `tfoot` element, then switch the
			 * >    insertion mode to "in table body" and return.
			 */
			case 'TBODY':
			case 'THEAD':
			case 'TFOOT':
				$this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE_BODY;
				return;

			/*
			 * > 7. If _node_ is a `caption` element, then switch the insertion mode to
			 * >    "in caption" and return.
			 */
			case 'CAPTION':
				$this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_CAPTION;
				return;

			/*
			 * > 8. If _node_ is a `colgroup` element, then switch the insertion mode to
			 * >    "in column group" and return.
			 */
			case 'COLGROUP':
				$this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_COLUMN_GROUP;
				return;

			/*
			 * > 9. If _node_ is a `table` element, then switch the insertion mode to
			 * >    "in table" and return.
			 */
			case 'TABLE':
				$this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE;
				return;

			/*
			 * > 10. If _node_ is a `template` element, then switch the insertion mode to the
			 * >     current template insertion mode and return.
			 */
			case 'TEMPLATE':
				$this->state->insertion_mode = end( $this->state->stack_of_template_insertion_modes );
				return;

			/*
			 * > 11. If _node_ is a `head` element and _last_ is false, then switch the
			 * >     insertion mode to "in head" and return.
			 */
			case 'HEAD':
				if ( ! $last ) {
					$this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_HEAD;
					return;
				}
				break;

			/*
			 * > 12. If _node_ is a `body` element, then switch the insertion mode to "in body"
			 * >     and return.
			 */
			case 'BODY':
				$this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_BODY;
				return;

			/*
			 * > 13. If _node_ is a `frameset` element, then switch the insertion mode to
			 * >     "in frameset" and return. (fragment case)
			 */
			case 'FRAMESET':
				$this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_FRAMESET;
				return;

			/*
			 * > 14. If _node_ is an `html` element, run these substeps:
			 * >     1. If the head element pointer is null, switch the insertion mode to
			 * >        "before head" and return. (fragment case)
			 * >     2. Otherwise, the head element pointer is not null, switch the insertion
			 * >        mode to "after head" and return.
			 */
			case 'HTML':
				$this->state->insertion_mode = isset( $this->state->head_element )
					? WP_HTML_Processor_State::INSERTION_MODE_AFTER_HEAD
					: WP_HTML_Processor_State::INSERTION_MODE_BEFORE_HEAD;
				return;
		}
	}

	/*
	 * > 15. If _last_ is true, then switch the insertion mode to "in body"
	 * >     and return. (fragment case)
	 *
	 * This is only reachable if `$last` is true, as per the fragment parsing case.
	 */
	$this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_BODY;
}

/**
 * Runs the adoption agency algorithm.
 *
 * @since 6.4.0
 * @ignore

Changelog

VersionDescription
6.7.0Introduced.

User Contributed Notes

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