diff --git a/src/wp-includes/html-api/class-wp-html-active-formatting-elements.php b/src/wp-includes/html-api/class-wp-html-active-formatting-elements.php
index a2c5d4f72452d..fb05a6e937499 100644
--- a/src/wp-includes/html-api/class-wp-html-active-formatting-elements.php
+++ b/src/wp-includes/html-api/class-wp-html-active-formatting-elements.php
@@ -144,11 +144,7 @@ public function push( AFE_Element|AFE_Marker $afe ): void {
*/
$count = 0;
foreach ( $this->walk_up_until_marker() as $item ) {
- if (
- $item->namespace === $afe->namespace &&
- $item->tag_name === $afe->tag_name &&
- $item->attributes === $afe->attributes
- ) {
+ if ( $item->is_equivalent( $afe ) ) {
if ( ++$count >= 3 ) {
return;
}
@@ -294,6 +290,23 @@ class AFE_Element {
/** @var WP_HTML_Token */
public $token;
+ public function is_equivalent( self $afe ): bool {
+ if (
+ $this->namespace !== $afe->namespace ||
+ $this->tag_name !== $afe->tag_name ||
+ count( $this->attributes ) !== count( $afe->attributes )
+ ) {
+ return false;
+ }
+
+ foreach ( $this->attributes as $name => $value ) {
+ if ( ! array_key_exists( $name, $afe->attributes ) || $value !== $afe->attributes[ $name ] ) {
+ return false;
+ }
+ }
+ return true;
+ }
+
public function __construct( string $tag_namespace, string $tag_name, array $attributes, WP_HTML_Token $token ) {
$this->namespace = $tag_namespace;
$this->tag_name = $tag_name;
diff --git a/src/wp-includes/html-api/class-wp-html-processor.php b/src/wp-includes/html-api/class-wp-html-processor.php
index f9e801572a16c..cfcb809abdb93 100644
--- a/src/wp-includes/html-api/class-wp-html-processor.php
+++ b/src/wp-includes/html-api/class-wp-html-processor.php
@@ -6227,7 +6227,6 @@ private function push_active_formatting_element( WP_HTML_Token $token ) {
foreach ( $proc->get_attribute_names_with_prefix( '' ) as $name ) {
$attributes[ $name ] = $proc->get_attribute( $name );
}
- sort( $attributes );
$afe = new AFE_Element(
$token->namespace,
$token->node_name,