-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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: Remove all duplicate copies of an attribute when removing #4317
HTML API: Remove all duplicate copies of an attribute when removing #4317
Conversation
The logic LGTM @dmsnell, thank you! I left a few nitpicks about formatting, but that's it. |
a123d29
to
84cbdde
Compare
84cbdde
to
f91eece
Compare
d493967
to
16555d0
Compare
16555d0
to
ba10849
Compare
* While `$this->duplicated_attributes` could always be stored as an `array()`, | ||
* which would simplify the logic here, storing a `null` and only allocating | ||
* an array when encountering duplicates avoids needless allocations in the | ||
* normative case of parsing tags with no duplicate attributes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I'm reading this correctly, but allocating one empty array (per tag processor) doesn't seem terribly expensive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. doesn't seem that it's any clearer though to be more expensive than less does it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe ever so slightly clearer, but fine to keep as-is 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
at some point I'll try and make a reasonable benchmark. until then I'll leave it at avoiding allocation. I'm afraid of allocation
@@ -1067,7 +1061,7 @@ public function test_remove_first_when_duplicated_attribute() { | |||
$p->remove_attribute( 'id' ); | |||
|
|||
$this->assertSame( | |||
'<div id="ignored-id"><span id="second">Text</span></div>', | |||
'<div ><span id="second">Text</span></div>', | |||
$p->get_updated_html(), | |||
'First attribute (when duplicates exist) was not removed' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'First attribute (when duplicates exist) was not removed' | |
'First attribute (and duplicates) were not removed' |
maybe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I rewrote the test to try and be more specific that this one test is solely about removing the first instance of the attribute, as when updating attributes it's the first that matters. Previously this test simply locked-in the behavior that the first was the one to go.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good. Left a few minor notes (mostly non-blocking).
Planning to land this (with updated @since
PHPDocs) later tonight, so we have it ready for both 6.4 and 6.3.2.
…tes exist. (WordPress#4317) When encountering an HTML tag with duplicate copies of an attribute the tag processor ignores the duplicate values, according to the specification. However, when removing an attribute it must remove all copies of that attribute lest one of the duplicates becomes the primary and it appears as if no attributes were removed. In this patch we're adding tests that will be used to ensure that all attribute copies are removed from a tag when one is requested to be removed. Co-authored-by: Bernie Reiter <[email protected]>
ba10849
to
eecadb6
Compare
…tes exist. (WordPress#4317) When encountering an HTML tag with duplicate copies of an attribute the tag processor ignores the duplicate values, according to the specification. However, when removing an attribute it must remove all copies of that attribute lest one of the duplicates becomes the primary and it appears as if no attributes were removed. In this patch we're adding tests that will be used to ensure that all attribute copies are removed from a tag when one is requested to be removed. Co-authored-by: Bernie Reiter <[email protected]>
eecadb6
to
3399f68
Compare
…tes exist. (WordPress#4317) When encountering an HTML tag with duplicate copies of an attribute the tag processor ignores the duplicate values, according to the specification. However, when removing an attribute it must remove all copies of that attribute lest one of the duplicates becomes the primary and it appears as if no attributes were removed. In this patch we're adding tests that will be used to ensure that all attribute copies are removed from a tag when one is requested to be removed. Co-authored-by: Bernie Reiter <[email protected]>
3399f68
to
2098d0c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Committed to Core |
Committed to Core's |
Description
Trac ticket #58119
When encountering an HTML tag with duplicate copies of an attribute the tag processor ignores the duplicate values, according to the specification. However, when removing an attribute it must remove all copies of that attribute lest one of the duplicates becomes the primary and it appears as if no attributes were removed.
In this patch we're adding tests that will be used to ensure that all attribute copies are removed from a tag when one is request to be removed.
Before
After
Previously we have been overlooking duplicate attributes since they don't have an impact on what parses into the DOM. However, as one unit test affirmed (asserting the presence of the bug in the tag processor) when removing an attribute where duplicates exist this meant we ended up changing the value of an attribute instead of removing it.
In this patch we're tracking the text spans of the parsed duplicate attributes so that if we attempt to remove them then we'll have the appropriate information necessary to do so. When an attribute isn't removed we'll simply forget about the tracked duplicates. This involves some overhead for normal operation when in fact there are duplicate attributes on a tag, but that overhead is minimal in the form of integer pairs of indices for each duplicated attribute.
cc: @adamziel @ockham