Make WordPress Core

Changeset 58740


Ignore:
Timestamp:
07/17/2024 12:10:52 AM (18 months ago)
Author:
dmsnell
Message:

HTML API: Remove leading whitespace after removing class names.

In part of a larger review of CSS semantics and behaviors, this patch
takes the opportunity to remove leading whitespace in an updated class
attribute after the first class in the attribute has been removed.

Previously, if the first class name had been removed, the whitespace
that formerly followed it would remain in the class attribute. This
stood in contrast to removing other class names, which removed their
associated whitespace.

There should be no semantic or functional changes in this patch, only
a slightly-large diff for modified HTML documents that looks prettier
when removing the first class name in a class attribute.

Developed in https://github.com/WordPress/wordpress-develop/pull/6933
Discussed in https://core.trac.wordpress.org/ticket/61531

Props dmsnell, jonsurrell.
See #61531.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/html-api/class-wp-html-tag-processor.php

    r58613 r58740  
    22192219             * in the output HTML but produce a noisier change.
    22202220             */
    2221             $class .= substr( $existing_class, $ws_at, $ws_length );
     2221            if ( '' !== $class ) {
     2222                $class .= substr( $existing_class, $ws_at, $ws_length );
     2223            }
    22222224            $class .= $name;
    22232225        }
  • trunk/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php

    r58233 r58740  
    13771377
    13781378        $this->assertSame(
    1379             '<div class=" with-border" id="first"><span class="not-main bold with-border" id="second">Text</span></div>',
     1379            '<div class="with-border" id="first"><span class="not-main bold with-border" id="second">Text</span></div>',
    13801380            $processor->get_updated_html(),
    13811381            'Updated HTML does not reflect class name removed from existing class attribute via remove_class()'
    13821382        );
    13831383        $this->assertSame(
    1384             ' with-border',
     1384            'with-border',
    13851385            $processor->get_attribute( 'class' ),
    13861386            "get_attribute( 'class' ) does not reflect class name removed from existing class attribute via remove_class()"
     
    14671467
    14681468        $this->assertSame(
    1469             '<div class="   main   with-border foo-class" id="first"><span class="not-main bold with-border" id="second">Text</span></div>',
     1469            '<div class="main   with-border foo-class" id="first"><span class="not-main bold with-border" id="second">Text</span></div>',
    14701470            $processor->get_updated_html(),
    14711471            'Updated HTML does not reflect existing excessive whitespace after adding class name via add_class()'
    14721472        );
    14731473        $this->assertSame(
    1474             '   main   with-border foo-class',
     1474            'main   with-border foo-class',
    14751475            $processor->get_attribute( 'class' ),
    14761476            "get_attribute( 'class' ) does not reflect existing excessive whitespace after adding class name via add_class()"
     
    14911491
    14921492        $this->assertSame(
    1493             '<div class="   main" id="first"><span class="not-main bold with-border" id="second">Text</span></div>',
     1493            '<div class="main" id="first"><span class="not-main bold with-border" id="second">Text</span></div>',
    14941494            $processor->get_updated_html(),
    14951495            'Updated HTML does not reflect existing excessive whitespace after removing class name via remove_class()'
    14961496        );
    14971497        $this->assertSame(
    1498             '   main',
     1498            'main',
    14991499            $processor->get_attribute( 'class' ),
    15001500            "get_attribute( 'class' ) does not reflect existing excessive whitespace after removing class name via removing_class()"
     
    16971697<div data-details="{ &quot;key&quot;: &quot;value&quot; }" selected class="merge-message is-processed" checked>
    16981698    <div class="select-menu d-inline-block">
    1699         <div checked class=" MixedCaseHTML position-relative button-group Another-Mixed-Case" />
    1700         <div checked class=" MixedCaseHTML position-relative button-group Another-Mixed-Case">
     1699        <div checked class="MixedCaseHTML position-relative button-group Another-Mixed-Case" />
     1700        <div checked class="MixedCaseHTML position-relative button-group Another-Mixed-Case">
    17011701            <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="">
    17021702              Merge pull request
Note: See TracChangeset for help on using the changeset viewer.