diff --git a/src/wp-includes/html-api/class-wp-html-tag-processor.php b/src/wp-includes/html-api/class-wp-html-tag-processor.php
index 4e4a8e1c5a..9a6b7d8f21 100644
--- a/src/wp-includes/html-api/class-wp-html-tag-processor.php
+++ b/src/wp-includes/html-api/class-wp-html-tag-processor.php
@@ -1716,7 +1716,36 @@ final class WP_HTML_Tag_Processor {
 	 * @return string[] List of attribute names.
 	 */
 	public function get_attribute_names_with_prefix( $prefix ) {
-		return array_keys( $this->attributes );
+		$attribute_names = array();
+
+		// Include parsed attributes.
+		foreach ( $this->attributes as $name => $_value ) {
+			if ( str_starts_with( $name, $prefix ) ) {
+				$attribute_names[ $name ] = true;
+			}
+		}
+
+		// Include enqueued attribute updates.
+		foreach ( $this->updated_attributes as $name => $value ) {
+			if ( null === $value ) {
+				unset( $attribute_names[ $name ] );
+				continue;
+			}
+
+			if ( str_starts_with( $name, $prefix ) ) {
+				$attribute_names[ $name ] = true;
+			}
+		}
+
+		return array_keys( $attribute_names );
 	}
 }
