WP_HTML_Tag_Processor::apply_attributes_updates( int $shift_this_point ): int

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.

Applies attribute updates to HTML document.

Parameters

$shift_this_pointintrequired
Accumulate and return shift for this position.

Return

int How many bytes the given pointer moved in response to the updates.

Source

 * @see WP_HTML_Tag_Processor::$lexical_updates
 * @see WP_HTML_Tag_Processor::$classname_updates
 */
private function class_name_updates_to_attributes_updates(): void {
	if ( count( $this->classname_updates ) === 0 ) {
		return;
	}

	$existing_class = $this->get_enqueued_attribute_value( 'class' );
	if ( null === $existing_class || true === $existing_class ) {
		$existing_class = '';
	}

	if ( false === $existing_class && isset( $this->attributes['class'] ) ) {
		$existing_class = substr(
			$this->html,
			$this->attributes['class']->value_starts_at,
			$this->attributes['class']->value_length
		);
	}

	if ( false === $existing_class ) {
		$existing_class = '';
	}

	/**
	 * Updated "class" attribute value.
	 *
	 * This is incrementally built while scanning through the existing class
	 * attribute, skipping removed classes on the way, and then appending
	 * added classes at the end. Only when finished processing will the
	 * value contain the final new value.

	 * @var string $class
	 */
	$class = '';

	/**
	 * Tracks the cursor position in the existing
	 * class attribute value while parsing.
	 *
	 * @var int $at
	 */
	$at = 0;

	/**
	 * Indicates if there's any need to modify the existing class attribute.
	 *
	 * If a call to `add_class()` and `remove_class()` wouldn't impact
	 * the `class` attribute value then there's no need to rebuild it.
	 * For example, when adding a class that's already present or
	 * removing one that isn't.
	 *
	 * This flag enables a performance optimization when none of the enqueued
	 * class updates would impact the `class` attribute; namely, that the
	 * processor can continue without modifying the input document, as if
	 * none of the `add_class()` or `remove_class()` calls had been made.
	 *
	 * This flag is set upon the first change that requires a string update.
	 *
	 * @var bool $modified
	 */
	$modified = false;

	$seen      = array();
	$to_remove = array();
	$is_quirks = self::QUIRKS_MODE === $this->compat_mode;
	if ( $is_quirks ) {
		foreach ( $this->classname_updates as $updated_name => $action ) {
			if ( self::REMOVE_CLASS === $action ) {
				$to_remove[] = strtolower( $updated_name );
			}
		}
	} else {
		foreach ( $this->classname_updates as $updated_name => $action ) {
			if ( self::REMOVE_CLASS === $action ) {
				$to_remove[] = $updated_name;
			}
		}
	}

	// Remove unwanted classes by only copying the new ones.
	$existing_class_length = strlen( $existing_class );
	while ( $at < $existing_class_length ) {
		// Skip to the first non-whitespace character.
		$ws_at     = $at;
		$ws_length = strspn( $existing_class, " \t\f\r\n", $ws_at );
		$at       += $ws_length;

Changelog

VersionDescription
6.3.0Invalidate any bookmarks whose targets are overwritten.
6.2.1Accumulates shift for internal cursor and passed pointer.
6.2.0Introduced.

User Contributed Notes

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