WP_HTML_Processor::step_in_body(): bool

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. Use https://html.spec.whatwg.org/#parsing-main-inbody instead.

Parses next element in the ‘in body’ insertion mode.

Description

This internal function performs the ‘in body’ insertion mode logic for the generalized WP_HTML_Processor::step() function.

See also

Return

bool Whether an element was found.

Source

private function step_in_body() {
	$tag_name = $this->get_tag();
	$op_sigil = $this->is_tag_closer() ? '-' : '+';
	$op       = "{$op_sigil}{$tag_name}";

	switch ( $op ) {
		/*
		 * > A start tag whose tag name is "button"
		 */
		case '+BUTTON':
			if ( $this->state->stack_of_open_elements->has_element_in_scope( 'BUTTON' ) ) {
				// @TODO: Indicate a parse error once it's possible. This error does not impact the logic here.
				$this->generate_implied_end_tags();
				$this->state->stack_of_open_elements->pop_until( 'BUTTON' );
			}

			$this->reconstruct_active_formatting_elements();
			$this->insert_html_element( $this->state->current_token );
			$this->state->frameset_ok = false;

			return true;

		/*
		 * > A start tag whose tag name is one of: "address", "article", "aside",
		 * > "blockquote", "center", "details", "dialog", "dir", "div", "dl",
		 * > "fieldset", "figcaption", "figure", "footer", "header", "hgroup",
		 * > "main", "menu", "nav", "ol", "p", "search", "section", "summary", "ul"
		 */
		case '+BLOCKQUOTE':
		case '+DIV':
		case '+FIGCAPTION':
		case '+FIGURE':
		case '+P':
			if ( $this->state->stack_of_open_elements->has_p_in_button_scope() ) {
				$this->close_a_p_element();
			}

			$this->insert_html_element( $this->state->current_token );
			return true;

		/*
		 * > An end tag whose tag name is one of: "address", "article", "aside", "blockquote",
		 * > "button", "center", "details", "dialog", "dir", "div", "dl", "fieldset",
		 * > "figcaption", "figure", "footer", "header", "hgroup", "listing", "main",
		 * > "menu", "nav", "ol", "pre", "search", "section", "summary", "ul"
		 */
		case '-BLOCKQUOTE':
		case '-BUTTON':
		case '-DIV':
		case '-FIGCAPTION':
		case '-FIGURE':
			if ( ! $this->state->stack_of_open_elements->has_element_in_scope( $tag_name ) ) {
				// @TODO: Report parse error.
				// Ignore the token.
				return $this->step();
			}

			$this->generate_implied_end_tags();
			if ( $this->state->stack_of_open_elements->current_node()->node_name !== $tag_name ) {
				// @TODO: Record parse error: this error doesn't impact parsing.
			}
			$this->state->stack_of_open_elements->pop_until( $tag_name );
			return true;

		/*
		 * > An end tag whose tag name is "p"
		 */
		case '-P':
			if ( ! $this->state->stack_of_open_elements->has_p_in_button_scope() ) {
				$this->insert_html_element( $this->state->current_token );
			}

			$this->close_a_p_element();
			return true;

		// > A start tag whose tag name is "a"
		case '+A':
			foreach ( $this->state->active_formatting_elements->walk_up() as $item ) {
				switch ( $item->node_name ) {
					case 'marker':
						break;

					case 'A':
						$this->run_adoption_agency_algorithm();
						$this->state->active_formatting_elements->remove_node( $item );
						$this->state->stack_of_open_elements->remove_node( $item );
						break;
				}
			}

			$this->reconstruct_active_formatting_elements();
			$this->insert_html_element( $this->state->current_token );
			$this->state->active_formatting_elements->push( $this->state->current_token );
			return true;

		/*
		 * > A start tag whose tag name is one of: "b", "big", "code", "em", "font", "i",
		 * > "s", "small", "strike", "strong", "tt", "u"
		 */
		case '+B':
		case '+BIG':
		case '+CODE':
		case '+EM':
		case '+FONT':
		case '+I':
		case '+S':
		case '+SMALL':
		case '+STRIKE':
		case '+STRONG':
		case '+TT':
		case '+U':
			$this->reconstruct_active_formatting_elements();
			$this->insert_html_element( $this->state->current_token );
			$this->state->active_formatting_elements->push( $this->state->current_token );
			return true;

		/*
		 * > An end tag whose tag name is one of: "a", "b", "big", "code", "em", "font", "i",
		 * > "nobr", "s", "small", "strike", "strong", "tt", "u"
		 */
		case '-A':
		case '-B':
		case '-BIG':
		case '-CODE':
		case '-EM':
		case '-FONT':
		case '-I':
		case '-S':
		case '-SMALL':
		case '-STRIKE':
		case '-STRONG':
		case '-TT':
		case '-U':
			$this->run_adoption_agency_algorithm();
			return true;

		/*
		 * > A start tag whose tag name is one of: "area", "br", "embed", "img", "keygen", "wbr"
		 */
		case '+IMG':
			$this->reconstruct_active_formatting_elements();
			$this->insert_html_element( $this->state->current_token );
			return true;

		/*
		 * > Any other start tag
		 */
		case '+SPAN':
			$this->reconstruct_active_formatting_elements();
			$this->insert_html_element( $this->state->current_token );
			return true;

		/*
		 * Any other end tag
		 */
		case '-SPAN':
			foreach ( $this->state->stack_of_open_elements->walk_up() as $item ) {
				// > If node is an HTML element with the same tag name as the token, then:
				if ( $item->node_name === $tag_name ) {
					$this->generate_implied_end_tags( $tag_name );

					// > If node is not the current node, then this is a parse error.

					$this->state->stack_of_open_elements->pop_until( $tag_name );
					return true;
				}

				// > Otherwise, if node is in the special category, then this is a parse error; ignore the token, and return.
				if ( self::is_special( $item->node_name ) ) {
					return $this->step();
				}
			}
			// Execution should not reach here; if it does then something went wrong.
			return false;

		default:
			$this->last_error = self::ERROR_UNSUPPORTED;
			throw new WP_HTML_Unsupported_Exception( "Cannot process {$tag_name} element." );
	}
}

Changelog

VersionDescription
6.4.0Introduced.

User Contributed Notes

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