WP_HTML_Processor::step_in_head()privateWP 6.7.0

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.

Method of the class: WP_HTML_Processor{}

No Hooks.

Return

true|false. Whether an element was found.

Usage

// private - for code of main (parent) class only
$result = $this->step_in_head(): bool;

Notes

Changelog

Since 6.7.0 Introduced.

WP_HTML_Processor::step_in_head() code WP 6.7.1

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
		 */
		case '#comment':
		case '#funky-comment':
		case '#presumptuous-tag':
			$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 );

			/*
			 * > 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 ) && 'tentative' === $this->state->encoding_confidence ) {
				$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
			 * >     extracting a character encoding from a meta element to that attribute's
			 * >     value returns an encoding, and the confidence is currently tentative,
			 * >     then change the encoding to the extracted encoding.
			 */
			$http_equiv = $this->get_attribute( 'http-equiv' );
			$content    = $this->get_attribute( 'content' );
			if (
				is_string( $http_equiv ) &&
				is_string( $content ) &&
				0 === strcasecmp( $http_equiv, 'Content-Type' ) &&
				'tentative' === $this->state->encoding_confidence
			) {
				$this->bail( 'Cannot yet process META tags with http-equiv Content-Type to determine encoding.' );
			}

			return true;

		/*
		 * > A start tag whose tag name is "title"
		 */
		case '+TITLE':
			$this->insert_html_element( $this->state->current_token );
			return true;

		/*
		 * > A start tag whose tag name is "noscript", if the scripting flag is enabled
		 * > A start tag whose tag name is one of: "noframes", "style"
		 *
		 * The scripting flag is never enabled in this parser.
		 */
		case '+NOFRAMES':
		case '+STYLE':
			$this->insert_html_element( $this->state->current_token );
			return true;

		/*
		 * > A start tag whose tag name is "noscript", if the scripting flag is disabled
		 */
		case '+NOSCRIPT':
			$this->insert_html_element( $this->state->current_token );
			$this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_HEAD_NOSCRIPT;
			return true;

		/*
		 * > A start tag whose tag name is "script"
		 *
		 * @todo Could the adjusted insertion location be anything other than the current location?
		 */
		case '+SCRIPT':
			$this->insert_html_element( $this->state->current_token );
			return true;

		/*
		 * > An end tag whose tag name is "head"
		 */
		case '-HEAD':
			$this->state->stack_of_open_elements->pop();
			$this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_AFTER_HEAD;
			return true;

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

		/*
		 * > A start tag whose tag name is "template"
		 *
		 * @todo Could the adjusted insertion location be anything other than the current location?
		 */
		case '+TEMPLATE':
			$this->state->active_formatting_elements->insert_marker();
			$this->state->frameset_ok = false;

			$this->state->insertion_mode                      = WP_HTML_Processor_State::INSERTION_MODE_IN_TEMPLATE;
			$this->state->stack_of_template_insertion_modes[] = WP_HTML_Processor_State::INSERTION_MODE_IN_TEMPLATE;

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

		/*
		 * > An end tag whose tag name is "template"
		 */
		case '-TEMPLATE':
			if ( ! $this->state->stack_of_open_elements->contains( 'TEMPLATE' ) ) {
				// @todo Indicate a parse error once it's possible.
				return $this->step();
			}

			$this->generate_implied_end_tags_thoroughly();
			if ( ! $this->state->stack_of_open_elements->current_node_is( 'TEMPLATE' ) ) {
				// @todo Indicate a parse error once it's possible.
			}

			$this->state->stack_of_open_elements->pop_until( 'TEMPLATE' );
			$this->state->active_formatting_elements->clear_up_to_last_marker();
			array_pop( $this->state->stack_of_template_insertion_modes );
			$this->reset_insertion_mode_appropriately();
			return true;
	}

	/*
	 * > A start tag whose tag name is "head"
	 * > Any other end tag
	 */
	if ( '+HEAD' === $op || $is_closer ) {
		// Parse error: ignore the token.
		return $this->step();
	}

	/*
	 * > Anything else
	 */
	in_head_anything_else:
	$this->state->stack_of_open_elements->pop();
	$this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_AFTER_HEAD;
	return $this->step( self::REPROCESS_CURRENT_NODE );
}