WP_HTML_Tag_Processor::parse_next_attribute(): 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 the next attribute.

Return

bool Whether an attribute was found before the end of the document.

Source

			 *
			 * > NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] |
			 *                     [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] |
			 *                     [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] |
			 *                     [#x10000-#xEFFFF]
			 * > NameChar      ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
			 *
			 * @see https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-PITarget
			 */
			if ( $this->token_length >= 5 && '?' === $html[ $closer_at - 1 ] ) {
				$comment_text     = substr( $html, $this->token_starts_at + 2, $this->token_length - 4 );
				$pi_target_length = strspn( $comment_text, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ:_' );

				if ( 0 < $pi_target_length ) {
					$pi_target_length += strspn( $comment_text, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:_-.', $pi_target_length );

					$this->comment_type       = self::COMMENT_AS_PI_NODE_LOOKALIKE;
					$this->tag_name_starts_at = $this->token_starts_at + 2;
					$this->tag_name_length    = $pi_target_length;
					$this->text_starts_at    += $pi_target_length;
					$this->text_length       -= $pi_target_length + 1;
				}
			}

			return true;
		}

		/*
		 * If a non-alpha starts the tag name in a tag closer it's a comment.
		 * Find the first `>`, which closes the comment.
		 *
		 * This parser classifies these particular comments as special "funky comments"
		 * which are made available for further processing.
		 *
		 * See https://html.spec.whatwg.org/#parse-error-invalid-first-character-of-tag-name
		 */
		if ( $this->is_closing_tag ) {
			// No chance of finding a closer.
			if ( $at + 3 > $doc_length ) {
				$this->parser_state = self::STATE_INCOMPLETE_INPUT;

				return false;
			}

			$closer_at = strpos( $html, '>', $at + 2 );
			if ( false === $closer_at ) {
				$this->parser_state = self::STATE_INCOMPLETE_INPUT;

				return false;
			}

			$this->parser_state         = self::STATE_FUNKY_COMMENT;
			$this->token_length         = $closer_at + 1 - $this->token_starts_at;
			$this->text_starts_at       = $this->token_starts_at + 2;
			$this->text_length          = $closer_at - $this->text_starts_at;
			$this->bytes_already_parsed = $closer_at + 1;
			return true;
		}

		++$at;
	}

	/*
	 * This does not imply an incomplete parse; it indicates that there
	 * can be nothing left in the document other than a #text node.
	 */
	$this->parser_state         = self::STATE_TEXT_NODE;
	$this->token_starts_at      = $was_at;
	$this->token_length         = $doc_length - $was_at;
	$this->text_starts_at       = $was_at;
	$this->text_length          = $this->token_length;
	$this->bytes_already_parsed = $doc_length;
	return true;
}

/**
 * Parses the next attribute.
 *
 * @since 6.2.0
 * @ignore
 *
 * @return bool Whether an attribute was found before the end of the document.
 */
private function parse_next_attribute(): bool {
	$doc_length = strlen( $this->html );

	// Skip whitespace and slashes.
	$skipped_length              = strspn( $this->html, " \t\f\r\n/", $this->bytes_already_parsed );
	$this->bytes_already_parsed += $skipped_length;
	if ( $this->bytes_already_parsed >= $doc_length ) {
		$this->parser_state = self::STATE_INCOMPLETE_INPUT;

		return false;
	}

	/**
	 * This block serves two purposes:
	 *
	 * - A fast path for common tag-ending `>`.
	 * - A check for the self-closing flag which must appear as `/>`.
	 *
	 * In a tag like `<g attr=/>`, `/` is the attribute value, not a self-closing
	 * flag. When it appears in this form, the parser has already consumed the
	 * attribute value, `$skipped_length` is 0, and this checks below correctly
	 * identify whether there is a self-closing flag.
	 *
	 * Note: Both start and end tags may have the self-closing flag.
	 */
	if ( '>' === $this->html[ $this->bytes_already_parsed ] ) {
		if ( $skipped_length > 0 && '/' === $this->html[ $this->bytes_already_parsed - 1 ] ) {
			$this->has_self_closing_flag = true;
		}
		return false;
	}

	/*
	 * Treat the equal sign as a part of the attribute
	 * name if it is the first encountered byte.
	 *
	 * @see https://html.spec.whatwg.org/multipage/parsing.html#before-attribute-name-state
	 */
	$name_length = '=' === $this->html[ $this->bytes_already_parsed ]
		? 1 + strcspn( $this->html, "=/> \t\f\r\n", $this->bytes_already_parsed + 1 )
		: strcspn( $this->html, "=/> \t\f\r\n", $this->bytes_already_parsed );

	// No attribute, just tag closer.
	if ( 0 === $name_length || $this->bytes_already_parsed + $name_length >= $doc_length ) {
		return false;
	}

Changelog

VersionDescription
6.2.0Introduced.

User Contributed Notes

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