Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Html api: Fix setting attribute multiple times #4337

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/wp-includes/html-api/class-wp-html-tag-processor.php
Expand Up @@ -1815,6 +1815,7 @@ public function is_tag_closer() {
* For string attributes, the value is escaped using the `esc_attr` function.
*
* @since 6.2.0
* @since 6.2.1 Fix: Only create a single update for multiple calls with case-variant attribute names.
*
* @param string $name The attribute name to target.
* @param string|bool $value The new attribute value.
Expand Down Expand Up @@ -1907,8 +1908,8 @@ public function set_attribute( $name, $value ) {
*
* Result: <div id="new"/>
*/
$existing_attribute = $this->attributes[ $comparable_name ];
$this->lexical_updates[ $name ] = new WP_HTML_Text_Replacement(
$existing_attribute = $this->attributes[ $comparable_name ];
$this->lexical_updates[ $comparable_name ] = new WP_HTML_Text_Replacement(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation of = signs seems no longer aligned with respect to the previous line. Kinda surprised our WPCS CI job didn't flag this 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just pushed out c21f8fa which adjusts the alignment and I'm curious. so far, when things look wrong but they pass the linter it's because the linter prefers the less-legible format 😆

$existing_attribute->start,
$existing_attribute->end,
$updated_attribute
Expand Down
18 changes: 18 additions & 0 deletions tests/phpunit/tests/html-api/wpHtmlTagProcessor.php
Expand Up @@ -962,6 +962,24 @@ public function test_set_attribute_with_an_existing_attribute_name_updates_its_v
);
}

/**
* Ensures that when setting an attribute multiple times that only
* one update flushes out into the updated HTML.
*
* @ticket 58146
*
* @covers WP_HTML_Tag_Processor::set_attribute
*/
public function test_set_attribute_with_case_variants_updates_only_the_original_first_copy() {
$p = new WP_HTML_Tag_Processor( '<div data-enabled="5">' );
$p->next_tag();
$p->set_attribute( 'DATA-ENABLED', 'canary' );
$p->set_attribute( 'Data-Enabled', 'canary' );
$p->set_attribute( 'dATa-EnABled', 'canary' );

$this->assertSame( '<div data-enabled="canary">', strtolower( $p->get_updated_html() ) );
}

/**
* @ticket 56299
*
Expand Down