WP_HTML_Tag_Processor::remove_attribute
Remove an attribute from the currently-matched tag.
Method of the class: WP_HTML_Tag_Processor{}
No Hooks.
Returns
true|false. Whether an attribute was removed.
Usage
$WP_HTML_Tag_Processor = new WP_HTML_Tag_Processor(); $WP_HTML_Tag_Processor->remove_attribute( $name ): bool;
- $name(string) (required)
- The attribute name to remove.
Changelog
| Since 6.2.0 | Introduced. |
WP_HTML_Tag_Processor::remove_attribute() WP HTML Tag Processor::remove attribute code WP 6.9.1
public function remove_attribute( $name ): bool {
if (
self::STATE_MATCHED_TAG !== $this->parser_state ||
$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 ]->length,
''
);
// Removes any duplicated attributes if they were also present.
foreach ( $this->duplicate_attributes[ $name ] ?? array() as $attribute_token ) {
$this->lexical_updates[] = new WP_HTML_Text_Replacement(
$attribute_token->start,
$attribute_token->length,
''
);
}
return true;
}