class class@anonymous {}

In this article

Methods

NameDescription
class@anonymous::get_spanGets the span for the current token.
class@anonymous::insert_afterInserts text after the current token.
class@anonymous::insert_beforeInserts text before the current token.
class@anonymous::removeRemoves the current token.
class@anonymous::replace_rich_textReplace the rich text content between a tag opener and matching closer.

Source

$processor = new class( $buffer ) extends WP_HTML_Tag_Processor {
	/**
	 * Gets the span for the current token.
	 *
	 * @return WP_HTML_Span Current token span.
	 */
	private function get_span(): WP_HTML_Span {
		// Note: This call will never fail according to the usage of this class, given it is always called after ::next_tag() is true.
		$this->set_bookmark( 'here' );
		return $this->bookmarks['here'];
	}

	/**
	 * Inserts text before the current token.
	 *
	 * @param string $text Text to insert.
	 */
	public function insert_before( string $text ) {
		$this->lexical_updates[] = new WP_HTML_Text_Replacement( $this->get_span()->start, 0, $text );
	}

	/**
	 * Inserts text after the current token.
	 *
	 * @param string $text Text to insert.
	 */
	public function insert_after( string $text ) {
		$span = $this->get_span();

		$this->lexical_updates[] = new WP_HTML_Text_Replacement( $span->start + $span->length, 0, $text );
	}

	/**
	 * Removes the current token.
	 */
	public function remove() {
		$span = $this->get_span();

		$this->lexical_updates[] = new WP_HTML_Text_Replacement( $span->start, $span->length, '' );
	}
};

User Contributed Notes

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