WP_HTML_Tag_Processor::skip_script_data(): 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 in other core functions. It is listed here for completeness.

Skips contents of script tags.

Return

bool Whether the script tag was closed before the end of the document.

Source


/**
 * Skips contents of generic rawtext elements.
 *
 * @since 6.3.2
 *
 * @see https://html.spec.whatwg.org/#generic-raw-text-element-parsing-algorithm
 *
 * @param string $tag_name The uppercase tag name which will close the RAWTEXT region.
 * @return bool Whether an end to the RAWTEXT region was found before the end of the document.
 */
private function skip_rawtext( string $tag_name ): bool {
	/*
	 * These two functions distinguish themselves on whether character references are
	 * decoded, and since functionality to read the inner markup isn't supported, it's
	 * not necessary to implement these two functions separately.
	 */
	return $this->skip_rcdata( $tag_name );
}

/**
 * Skips contents of RCDATA elements, namely title and textarea tags.
 *
 * @since 6.2.0
 *
 * @see https://html.spec.whatwg.org/multipage/parsing.html#rcdata-state
 *
 * @param string $tag_name The uppercase tag name which will close the RCDATA region.
 * @return bool Whether an end to the RCDATA region was found before the end of the document.
 */
private function skip_rcdata( string $tag_name ): bool {
	$html       = $this->html;
	$doc_length = strlen( $html );
	$tag_length = strlen( $tag_name );

	$at = $this->bytes_already_parsed;

	while ( false !== $at && $at < $doc_length ) {
		$at                       = strpos( $this->html, '</', $at );
		$this->tag_name_starts_at = $at;

		// Fail if there is no possible tag closer.
		if ( false === $at || ( $at + $tag_length ) >= $doc_length ) {
			return false;
		}

		$at += 2;

		/*
		 * Find a case-insensitive match to the tag name.
		 *
		 * Because tag names are limited to US-ASCII there is no
		 * need to perform any kind of Unicode normalization when
		 * comparing; any character which could be impacted by such
		 * normalization could not be part of a tag name.
		 */
		for ( $i = 0; $i < $tag_length; $i++ ) {
			$tag_char  = $tag_name[ $i ];
			$html_char = $html[ $at + $i ];

			if ( $html_char !== $tag_char && strtoupper( $html_char ) !== $tag_char ) {
				$at += $i;
				continue 2;
			}
		}

		$at                        += $tag_length;
		$this->bytes_already_parsed = $at;

		if ( $at >= strlen( $html ) ) {
			return false;
		}

		/*
		 * Ensure that the tag name terminates to avoid matching on
		 * substrings of a longer tag name. For example, the sequence
		 * "</textarearug" should not match for "</textarea" even
		 * though "textarea" is found within the text.
		 */
		$c = $html[ $at ];
		if ( ' ' !== $c && "\t" !== $c && "\r" !== $c && "\n" !== $c && '/' !== $c && '>' !== $c ) {
			continue;
		}

		while ( $this->parse_next_attribute() ) {
			continue;
		}

		$at = $this->bytes_already_parsed;
		if ( $at >= strlen( $this->html ) ) {
			return false;
		}

		if ( '>' === $html[ $at ] ) {
			$this->bytes_already_parsed = $at + 1;
			return true;
		}

		if ( $at + 1 >= strlen( $this->html ) ) {
			return false;
		}

		if ( '/' === $html[ $at ] && '>' === $html[ $at + 1 ] ) {
			$this->bytes_already_parsed = $at + 2;
			return true;
		}
	}

	return false;
}

/**
 * Skips contents of script tags.
 *
 * @since 6.2.0
 *
 * @return bool Whether the script tag was closed before the end of the document.
 */
private function skip_script_data(): bool {
	$state      = 'unescaped';
	$html       = $this->html;
	$doc_length = strlen( $html );
	$at         = $this->bytes_already_parsed;

	while ( false !== $at && $at < $doc_length ) {
		$at += strcspn( $html, '-<', $at );

		/*
		 * For all script states a "-->"  transitions
		 * back into the normal unescaped script mode,
		 * even if that's the current state.
		 */
		if (

Changelog

VersionDescription
6.2.0Introduced.

User Contributed Notes

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