WP_HTML_Tag_Processor::get_updated_html(): string

In this article

Returns the string representation of the HTML Tag Processor.

Return

string The processed HTML.

Source

public function get_updated_html() {
	$requires_no_updating = 0 === count( $this->classname_updates ) && 0 === count( $this->lexical_updates );

	/*
	 * When there is nothing more to update and nothing has already been
	 * updated, return the original document and avoid a string copy.
	 */
	if ( $requires_no_updating ) {
		return $this->html;
	}

	/*
	 * Keep track of the position right before the current tag. This will
	 * be necessary for reparsing the current tag after updating the HTML.
	 */
	$before_current_tag = $this->tag_name_starts_at - 1;

	/*
	 * 1. Apply the enqueued edits and update all the pointers to reflect those changes.
	 */
	$this->class_name_updates_to_attributes_updates();
	$before_current_tag += $this->apply_attributes_updates( $before_current_tag );

	/*
	 * 2. Rewind to before the current tag and reparse to get updated attributes.
	 *
	 * At this point the internal cursor points to the end of the tag name.
	 * Rewind before the tag name starts so that it's as if the cursor didn't
	 * move; a call to `next_tag()` will reparse the recently-updated attributes
	 * and additional calls to modify the attributes will apply at this same
	 * location, but in order to avoid issues with subclasses that might add
	 * behaviors to `next_tag()`, the internal methods should be called here
	 * instead.
	 *
	 * It's important to note that in this specific place there will be no change
	 * because the processor was already at a tag when this was called and it's
	 * rewinding only to the beginning of this very tag before reprocessing it
	 * and its attributes.
	 *
	 * <p>Previous HTML<em>More HTML</em></p>
	 *                 ↑  │ back up by the length of the tag name plus the opening <
	 *                 └←─┘ back up by strlen("em") + 1 ==> 3
	 */
	$this->bytes_already_parsed = $before_current_tag;
	$this->parse_next_tag();
	// Reparse the attributes.
	while ( $this->parse_next_attribute() ) {
		continue;
	}

	$tag_ends_at                = strpos( $this->html, '>', $this->bytes_already_parsed );
	$this->tag_ends_at          = $tag_ends_at;
	$this->bytes_already_parsed = $tag_ends_at;

	return $this->html;
}

Changelog

VersionDescription
6.4.0No longer calls subclass method next_tag() after updating HTML.
6.2.1Shifts the internal cursor corresponding to the applied updates.
6.2.0Introduced.

User Contributed Notes

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