Skip to content

Commit

Permalink
Remove leading whitespace from class when remove_class has modified
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed Jul 15, 2024
1 parent 7d48ce7 commit caa9a98
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/wp-includes/html-api/class-wp-html-tag-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
16 changes: 8 additions & 8 deletions tests/phpunit/tests/html-api/wpHtmlTagProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1376,12 +1376,12 @@ public function test_remove_class_removes_a_single_class_from_the_class_attribut
$processor->remove_class( 'main' );

$this->assertSame(
'<div class=" with-border" id="first"><span class="not-main bold with-border" id="second">Text</span></div>',
'<div class="with-border" id="first"><span class="not-main bold with-border" id="second">Text</span></div>',
$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()"
);
Expand Down Expand Up @@ -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(
'<div class=" main with-border foo-class" id="first"><span class="not-main bold with-border" id="second">Text</span></div>',
'<div class="main with-border foo-class" id="first"><span class="not-main bold with-border" id="second">Text</span></div>',
$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()"
);
Expand All @@ -1490,12 +1490,12 @@ public function test_remove_class_preserves_whitespaces_when_there_is_a_class_at
$processor->remove_class( 'with-border' );

$this->assertSame(
'<div class=" main" id="first"><span class="not-main bold with-border" id="second">Text</span></div>',
'<div class="main" id="first"><span class="not-main bold with-border" id="second">Text</span></div>',
$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()"
);
Expand Down Expand Up @@ -1696,8 +1696,8 @@ public function test_advanced_use_case() {
$expected_output = <<<HTML
<div data-details="{ &quot;key&quot;: &quot;value&quot; }" selected class="merge-message is-processed" checked>
<div class="select-menu d-inline-block">
<div checked class=" MixedCaseHTML position-relative button-group Another-Mixed-Case" />
<div checked class=" MixedCaseHTML position-relative button-group Another-Mixed-Case">
<div checked class="MixedCaseHTML position-relative button-group Another-Mixed-Case" />
<div checked class="MixedCaseHTML position-relative button-group Another-Mixed-Case">
<button type="button" class="merge-box-button btn-group-merge rounded-left-2 btn BtnGroup-item js-details-target hx_create-pr-button" aria-expanded="false" data-details-container=".js-merge-pr" disabled="">
Merge pull request
</button>
Expand Down

0 comments on commit caa9a98

Please sign in to comment.