[wp-trac] [WordPress Trac] #58119: HTML API: Remove all duplicate copies of an attribute when removing

WordPress Trac noreply at wordpress.org
Wed Apr 12 13:19:44 UTC 2023


#58119: HTML API: Remove all duplicate copies of an attribute when removing
--------------------------+--------------------------------------
 Reporter:  dmsnell       |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  HTML API      |    Version:  6.2
 Severity:  normal        |   Keywords:  has-patch has-unit-tests
  Focuses:                |
--------------------------+--------------------------------------
 [https://github.com/WordPress/wordpress-develop/pull/4317 wordpress
 /wordpress-develop#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 request to be removed.

 **Before**

 {{{#!php
 <?php
 $p = new WP_HTML_Tag_Processor( '<br id=one id="two" id='three' id>' );
 $p->next_tag();
 $p->remove_attribute( 'id' );
 $p->get_updated_html();
 // <br id="two" id='three' id>
 }}}

 **After**

 {{{#!php
 <?php
 $p = new WP_HTML_Tag_Processor( '<br id=one id="two" id='three' id>' );
 $p->next_tag();
 $p->remove_attribute( 'id' );
 $p->get_updated_html();
 // <br>
 }}}

 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.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/58119>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list