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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have strong grounds for this claim? It wouldn’t surprise me if this were less efficient than a basic string array and in_array(), or as is done from time to time by building a string of the names separated by spaces and doing string search.

I don’t know that it matters too much here, but if we are going to leave some performance claims in the code we should have some evidence substantiating the choice.

$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