WP_HTML_Tag_Processor::next_tag()publicWP 6.2.0

Finds the next tag matching the $query.

Метод класса: WP_HTML_Tag_Processor{}

Хуков нет.

Возвращает

true|false. Whether a tag was matched.

Использование

$WP_HTML_Tag_Processor = new WP_HTML_Tag_Processor();
$WP_HTML_Tag_Processor->next_tag( $query );
$query(массив|строка|null)

Which tag name to find, having which class, etc.

По умолчанию: to find any tag

  • tag_name(строка|null)
    Which tag to find, or null for "any tag."

  • match_offset(int|null)
    Find the Nth tag matching all search criteria.
    1 for "first" tag, 3 for "third," etc.
    По умолчанию: first tag

  • class_name(строка|null)
    Tag must contain this whole class name to match.

  • tag_closers(строка|null)
    "visit" or "skip": whether to stop on tag closers, e.g. </div>.

Список изменений

С версии 6.2.0 Введена.

Код WP_HTML_Tag_Processor::next_tag() WP 6.3.1

public function next_tag( $query = null ) {
	$this->parse_query( $query );
	$already_found = 0;

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

		// Find the next tag if it exists.
		if ( false === $this->parse_next_tag() ) {
			$this->bytes_already_parsed = strlen( $this->html );

			return false;
		}

		// Parse all of its attributes.
		while ( $this->parse_next_attribute() ) {
			continue;
		}

		// Ensure that the tag closes before the end of the document.
		if ( $this->bytes_already_parsed >= strlen( $this->html ) ) {
			return false;
		}

		$tag_ends_at = strpos( $this->html, '>', $this->bytes_already_parsed );
		if ( false === $tag_ends_at ) {
			return false;
		}
		$this->tag_ends_at          = $tag_ends_at;
		$this->bytes_already_parsed = $tag_ends_at;

		// Finally, check if the parsed tag and its attributes match the search query.
		if ( $this->matches() ) {
			++$already_found;
		}

		/*
		 * For non-DATA sections which might contain text that looks like HTML tags but
		 * isn't, scan with the appropriate alternative mode. Looking at the first letter
		 * of the tag name as a pre-check avoids a string allocation when it's not needed.
		 */
		$t = $this->html[ $this->tag_name_starts_at ];
		if ( ! $this->is_closing_tag && ( 's' === $t || 'S' === $t || 't' === $t || 'T' === $t ) ) {
			$tag_name = $this->get_tag();

			if ( 'SCRIPT' === $tag_name && ! $this->skip_script_data() ) {
				$this->bytes_already_parsed = strlen( $this->html );
				return false;
			} elseif (
				( 'TEXTAREA' === $tag_name || 'TITLE' === $tag_name ) &&
				! $this->skip_rcdata( $tag_name )
			) {
				$this->bytes_already_parsed = strlen( $this->html );
				return false;
			}
		}
	} while ( $already_found < $this->sought_match_offset );

	return true;
}