WP_HTML_Processor::step_in_head(): bool

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.

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

Description

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

See also

Return

bool Whether an element was found.

Source

	 */
	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 );
}

/**
 * Parses next element in the 'before head' insertion mode.
 *
 * This internal function performs the 'before head' insertion mode
 * logic for the generalized WP_HTML_Processor::step() function.
 *
 * @since 6.7.0
 * @ignore
 *
 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input.
 *
 * @see https://html.spec.whatwg.org/#the-before-head-insertion-mode
 * @see WP_HTML_Processor::step
 *
 * @return bool Whether an element was found.
 */
private function step_before_head(): 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 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_head_anything_else;
			break;

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

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

		/*
		 * > A start tag whose tag name is "html"
		 */
		case '+HTML':
			return $this->step_in_body();

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

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

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

	/*
	 * > Anything else
	 *
	 * > Insert an HTML element for a "head" start tag token with no attributes.
	 */
	before_head_anything_else:
	$this->state->head_element   = $this->insert_virtual_node( 'HEAD' );
	$this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_HEAD;
	return $this->step( self::REPROCESS_CURRENT_NODE );
}

/**
 * Parses next element in the 'in head' insertion mode.
 *
 * This internal function performs the 'in head' insertion mode
 * logic for the generalized WP_HTML_Processor::step() function.
 *
 * @since 6.7.0
 * @ignore
 *
 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input.
 *
 * @see https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inhead
 * @see WP_HTML_Processor::step
 *
 * @return bool Whether an element was found.
 */
private function step_in_head(): 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 ) {
		case '#text':
			/*
			 * > 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
			 */
			if ( parent::TEXT_IS_WHITESPACE === $this->text_node_classification ) {
				// Insert the character.
				$this->insert_html_element( $this->state->current_token );
				return true;
			}

			goto in_head_anything_else;
			break;

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

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

		/*
		 * > A start tag whose tag name is "html"
		 */
		case '+HTML':
			return $this->step_in_body();

		/*
		 * > A start tag whose tag name is one of: "base", "basefont", "bgsound", "link"
		 */
		case '+BASE':
		case '+BASEFONT':
		case '+BGSOUND':
		case '+LINK':
			$this->insert_html_element( $this->state->current_token );
			return true;

		/*
		 * > A start tag whose tag name is "meta"
		 */
		case '+META':
			$this->insert_html_element( $this->state->current_token );

			// All following conditions depend on "tentative" encoding confidence.
			if ( 'tentative' !== $this->state->encoding_confidence ) {
				return true;
			}

			/*
			 * > If the active speculative HTML parser is null, then:
			 * >   - If the element has a charset attribute, and getting an encoding from
			 * >     its value results in an encoding, and the confidence is currently
			 * >     tentative, then change the encoding to the resulting encoding.
			 */
			$charset = $this->get_attribute( 'charset' );
			if ( is_string( $charset ) ) {
				$this->bail( 'Cannot yet process META tags with charset to determine encoding.' );
			}

			/*
			 * >   - Otherwise, if the element has an http-equiv attribute whose value is
			 * >     an ASCII case-insensitive match for the string "Content-Type", and
			 * >     the element has a content attribute, and applying the algorithm for

Changelog

VersionDescription
6.7.0Introduced.

User Contributed Notes

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