Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions src/wp-includes/html-api/class-wp-html-tag-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2843,15 +2843,44 @@ public function get_attribute_names_with_prefix( $prefix ): ?array {
return null;
}

/*
* For the `class` attribute, ensure that enqueued class changes from
* `add_class` and `remove_class` are flushed into attribute updates.
*/
$this->class_name_updates_to_attributes_updates();

$comparable = strtolower( $prefix );

$matches = array();
// Use associative array to efficiently track unique attribute names
$result = array();

// First, collect attributes from the parsed HTML.
foreach ( array_keys( $this->attributes ) as $attr_name ) {
if ( str_starts_with( $attr_name, $comparable ) ) {
$matches[] = $attr_name;
$result[ $attr_name ] = true;
}
}
return $matches;

// Then, apply any enqueued attribute updates.
foreach ( $this->lexical_updates as $name => $update ) {
// Skip numeric keys (used for other types of updates).
if ( is_int( $name ) ) {
continue;
}

// Process only if the name matches the prefix.
if ( str_starts_with( $name, $comparable ) ) {
// If the update text is empty, the attribute is being removed.
if ( '' === $update->text ) {
unset( $result[ $name ] );
} else {
// Attribute is being added or modified.
$result[ $name ] = true;
}
}
}

return array_keys( $result );
}

/**
Expand Down
Loading