WP_HTML_Processor::step_before_html(): 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/#the-before-html-insertion-mode instead.

Parses next element in the ‘before html’ insertion mode.

Description

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

See also

Return

bool Whether an element was found.

Source

private function step_before_html(): bool {
	$token_name = $this->get_token_name();
	$token_type = $this->get_token_type();
	$is_closer  = parent::is_tag_closer();
	$op_sigil   = '#tag' === $token_type ? ( $is_closer ? '-' : '+' ) : '';
	$op         = "{$op_sigil}{$token_name}";

	switch ( $op ) {
		/*
		 * > A DOCTYPE token
		 */
		case 'html':
			// Parse error: ignore the token.
			return $this->step();

		/*
		 * > A comment token
		 */
		case '#comment':
		case '#funky-comment':
		case '#presumptuous-tag':
			$this->insert_html_element( $this->state->current_token );
			return true;

		/*
		 * > A character token that is one of U+0009 CHARACTER TABULATION,
		 * > U+000A LINE FEED (LF), U+000C FORM FEED (FF),
		 * > U+000D CARRIAGE RETURN (CR), or U+0020 SPACE
		 *
		 * Parse error: ignore the token.
		 */
		case '#text':
			if ( parent::TEXT_IS_WHITESPACE === $this->text_node_classification ) {
				return $this->step();
			}
			goto before_html_anything_else;
			break;

		/*
		 * > A start tag whose tag name is "html"
		 */
		case '+HTML':
			$this->insert_html_element( $this->state->current_token );
			$this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_BEFORE_HEAD;
			return true;

		/*
		 * > An end tag whose tag name is one of: "head", "body", "html", "br"
		 *
		 * Closing BR tags are always reported by the Tag Processor as opening tags.
		 */
		case '-HEAD':
		case '-BODY':
		case '-HTML':
			/*
			 * > Act as described in the "anything else" entry below.
			 */
			goto before_html_anything_else;
			break;
	}

	/*
	 * > Any other end tag
	 */
	if ( $is_closer ) {
		// Parse error: ignore the token.
		return $this->step();
	}

	/*
	 * > Anything else.
	 *
	 * > Create an html element whose node document is the Document object.
	 * > Append it to the Document object. Put this element in the stack of open elements.
	 * > Switch the insertion mode to "before head", then reprocess the token.
	 */
	before_html_anything_else:
	$this->insert_virtual_node( 'HTML' );
	$this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_BEFORE_HEAD;
	return $this->step( self::REPROCESS_CURRENT_NODE );
}

Changelog

VersionDescription
6.7.0Introduced.

User Contributed Notes

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