WP_HTML_Tag_Processor::remove_attribute()publicWP 6.2.0

Remove an attribute from the currently-matched tag.

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

Хуков нет.

Возвращает

true|false. Whether an attribute was removed.

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

$WP_HTML_Tag_Processor = new WP_HTML_Tag_Processor();
$WP_HTML_Tag_Processor->remove_attribute( $name );
$name(строка) (обязательный)
The attribute name to remove.

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

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

Код WP_HTML_Tag_Processor::remove_attribute() WP 6.3.1

public function remove_attribute( $name ) {
	if ( $this->is_closing_tag ) {
		return false;
	}

	/*
	 * > There must never be two or more attributes on
	 * > the same start tag whose names are an ASCII
	 * > case-insensitive match for each other.
	 *     - HTML 5 spec
	 *
	 * @see https://html.spec.whatwg.org/multipage/syntax.html#attributes-2:ascii-case-insensitive
	 */
	$name = strtolower( $name );

	/*
	 * Any calls to update the `class` attribute directly should wipe out any
	 * enqueued class changes from `add_class` and `remove_class`.
	 */
	if ( 'class' === $name && count( $this->classname_updates ) !== 0 ) {
		$this->classname_updates = array();
	}

	/*
	 * If updating an attribute that didn't exist in the input
	 * document, then remove the enqueued update and move on.
	 *
	 * For example, this might occur when calling `remove_attribute()`
	 * after calling `set_attribute()` for the same attribute
	 * and when that attribute wasn't originally present.
	 */
	if ( ! isset( $this->attributes[ $name ] ) ) {
		if ( isset( $this->lexical_updates[ $name ] ) ) {
			unset( $this->lexical_updates[ $name ] );
		}
		return false;
	}

	/*
	 * Removes an existing tag attribute.
	 *
	 * Example – remove the attribute id from <div id="main"/>:
	 *    <div id="initial_id"/>
	 *         ^-------------^
	 *         start         end
	 *    replacement: ``
	 *
	 *    Result: <div />
	 */
	$this->lexical_updates[ $name ] = new WP_HTML_Text_Replacement(
		$this->attributes[ $name ]->start,
		$this->attributes[ $name ]->end,
		''
	);

	return true;
}