Make WordPress Core

Changeset 55659


Ignore:
Timestamp:
04/19/2023 09:25:32 AM (2 years ago)
Author:
Bernhard Reiter
Message:

HTML API: Ensure attribute updates happen only once for case variants.

When setting a new value for an attribute multiple times and providing
multiple case variations of the attribute name the Tag Processor has
been appending multiple copies of the attribute into the updated HTML.

This means that only the first attribute set determines the value in
the final output, plus the output will appear wrong.

In this patch we're adding a test to catch the situation and resolving it
by using the appropriate comparable attribute name as a key for storing
the updates as we go. Previously we stored updates to the attribute by
its given $name, but when a new update of the same name with a
case variant was queued, it would not override the previously-enqueued
value as it out to have.

Props dmsnell, zieladam.
Fixes #58146.

Location:
trunk
Files:
2 edited

Legend:

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

    r55619 r55659  
    18161816     *
    18171817     * @since 6.2.0
     1818     * @since 6.2.1 Fix: Only create a single update for multiple calls with case-variant attribute names.
    18181819     *
    18191820     * @param string      $name  The attribute name to target.
     
    19081909             *    Result: <div id="new"/>
    19091910             */
    1910             $existing_attribute             = $this->attributes[ $comparable_name ];
    1911             $this->lexical_updates[ $name ] = new WP_HTML_Text_Replacement(
     1911            $existing_attribute = $this->attributes[ $comparable_name ];
     1912            $this->lexical_updates[ $comparable_name ] = new WP_HTML_Text_Replacement(
    19121913                $existing_attribute->start,
    19131914                $existing_attribute->end,
  • trunk/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php

    r55619 r55659  
    964964
    965965    /**
     966     * Ensures that when setting an attribute multiple times that only
     967     * one update flushes out into the updated HTML.
     968     *
     969     * @ticket 58146
     970     *
     971     * @covers WP_HTML_Tag_Processor::set_attribute
     972     */
     973    public function test_set_attribute_with_case_variants_updates_only_the_original_first_copy() {
     974        $p = new WP_HTML_Tag_Processor( '<div data-enabled="5">' );
     975        $p->next_tag();
     976        $p->set_attribute( 'DATA-ENABLED', 'canary' );
     977        $p->set_attribute( 'Data-Enabled', 'canary' );
     978        $p->set_attribute( 'dATa-EnABled', 'canary' );
     979
     980        $this->assertSame( '<div data-enabled="canary">', strtolower( $p->get_updated_html() ) );
     981    }
     982
     983    /**
    966984     * @ticket 56299
    967985     *
Note: See TracChangeset for help on using the changeset viewer.