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

Conversation

dmsnell
Copy link
Contributor

@dmsnell dmsnell commented Apr 17, 2023

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

Trac ticket: #58146-trac

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.

In this patch we're adding a test to catch the situation, and then fixing
the bug by using the comparable name to key the attribute updates instead
of the case-sensitive name.

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.

In this patch we're adding a test to catch the situation.
@adamziel
Copy link
Contributor

Great fix and great catch!

@dmsnell dmsnell marked this pull request as ready for review April 17, 2023 23:31
@@ -1908,7 +1909,7 @@ 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(
$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 😆

@dmsnell dmsnell force-pushed the html-api/fix-setting-attribute-multiple-times branch from c21f8fa to 72ab357 Compare April 18, 2023 13:30
Copy link
Contributor

@ockham ockham left a comment

Choose a reason for hiding this comment

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

Thank you! :shipit:

@ockham
Copy link
Contributor

ockham commented Apr 19, 2023

Committed to Core trunk in r55659.

@ockham ockham closed this Apr 19, 2023
@dmsnell dmsnell deleted the html-api/fix-setting-attribute-multiple-times branch April 19, 2023 11:41
@ockham
Copy link
Contributor

ockham commented Apr 20, 2023

Backported to Core's 6.2 branch in r55662.

dmsnell added a commit to WordPress/gutenberg that referenced this pull request Apr 20, 2023
 - Support a few extra invalid comment syntaxes. WordPress/wordpress-develop#4256
 - Invalidate bookmarks which have been eliminated by enqueued changes. WordPress/wordpress-develop#4116
 - Expose whether the currently-matched tag has the self-closing flag. WordPress/wordpress-develop#4266
 - Avoid double-writing an attribute value if given case-variations of the name. WordPress/wordpress-develop#4337
dmsnell added a commit to WordPress/gutenberg that referenced this pull request Apr 21, 2023
 - Support a few extra invalid comment syntaxes. WordPress/wordpress-develop#4256
 - Invalidate bookmarks which have been eliminated by enqueued changes. WordPress/wordpress-develop#4116
 - Expose whether the currently-matched tag has the self-closing flag. WordPress/wordpress-develop#4266
 - Avoid double-writing an attribute value if given case-variations of the name. WordPress/wordpress-develop#4337
 - Linting updates from WordPress/wordpress-develop#4360.
dmsnell added a commit to WordPress/gutenberg that referenced this pull request Apr 24, 2023
 - Support a few extra invalid comment syntaxes. WordPress/wordpress-develop#4256
 - Invalidate bookmarks which have been eliminated by enqueued changes. WordPress/wordpress-develop#4116
 - Expose whether the currently-matched tag has the self-closing flag. WordPress/wordpress-develop#4266
 - Avoid double-writing an attribute value if given case-variations of the name. WordPress/wordpress-develop#4337
 - Linting updates from WordPress/wordpress-develop#4360.
 - Avoid losing previous updates in certain cases when seeking earlier in a document. WordPress/wordpress-develop#4345
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants