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 a388af1ef79c3..46fc192083ec6 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 @@ -2218,7 +2218,9 @@ private function class_name_updates_to_attributes_updates() { * whitespace to a single space, which might appear cleaner * in the output HTML but produce a noisier change. */ - $class .= substr( $existing_class, $ws_at, $ws_length ); + if ( '' !== $class ) { + $class .= substr( $existing_class, $ws_at, $ws_length ); + } $class .= $name; } diff --git a/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php b/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php index b9f696bcfed5c..dd8a849d0b20f 100644 --- a/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php +++ b/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php @@ -1376,12 +1376,12 @@ public function test_remove_class_removes_a_single_class_from_the_class_attribut $processor->remove_class( 'main' ); $this->assertSame( - '
Text
', + '
Text
', $processor->get_updated_html(), 'Updated HTML does not reflect class name removed from existing class attribute via remove_class()' ); $this->assertSame( - ' with-border', + 'with-border', $processor->get_attribute( 'class' ), "get_attribute( 'class' ) does not reflect class name removed from existing class attribute via remove_class()" ); @@ -1466,12 +1466,12 @@ public function test_add_class_when_there_is_a_class_attribute_with_excessive_wh $processor->add_class( 'foo-class' ); $this->assertSame( - '
Text
', + '
Text
', $processor->get_updated_html(), 'Updated HTML does not reflect existing excessive whitespace after adding class name via add_class()' ); $this->assertSame( - ' main with-border foo-class', + 'main with-border foo-class', $processor->get_attribute( 'class' ), "get_attribute( 'class' ) does not reflect existing excessive whitespace after adding class name via add_class()" ); @@ -1490,12 +1490,12 @@ public function test_remove_class_preserves_whitespaces_when_there_is_a_class_at $processor->remove_class( 'with-border' ); $this->assertSame( - '
Text
', + '
Text
', $processor->get_updated_html(), 'Updated HTML does not reflect existing excessive whitespace after removing class name via remove_class()' ); $this->assertSame( - ' main', + 'main', $processor->get_attribute( 'class' ), "get_attribute( 'class' ) does not reflect existing excessive whitespace after removing class name via removing_class()" ); @@ -1696,8 +1696,8 @@ public function test_advanced_use_case() { $expected_output = <<
-
-
+
+