mirror of
https://github.com/WordPress/WordPress.git
synced 2024-12-22 17:18:32 +01:00
HTML API: Remove all duplicate copies of an attribute when removing.
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. Props dmsnell, zieladam. Fixes #58119. Built from https://develop.svn.wordpress.org/trunk@56684 git-svn-id: http://core.svn.wordpress.org/trunk@56196 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
66c782f6cd
commit
061c32d574
@ -406,6 +406,16 @@ class WP_HTML_Tag_Processor {
|
||||
*/
|
||||
private $attributes = array();
|
||||
|
||||
/**
|
||||
* Tracks spans of duplicate attributes on a given tag, used for removing
|
||||
* all copies of an attribute when calling `remove_attribute()`.
|
||||
*
|
||||
* @since 6.3.2
|
||||
*
|
||||
* @var (WP_HTML_Span[])[]|null
|
||||
*/
|
||||
private $duplicate_attributes = null;
|
||||
|
||||
/**
|
||||
* Which class names to add or remove from a tag.
|
||||
*
|
||||
@ -1286,6 +1296,25 @@ class WP_HTML_Tag_Processor {
|
||||
$attribute_end,
|
||||
! $has_value
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Track the duplicate attributes so if we remove it, all disappear together.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
$duplicate_span = new WP_HTML_Span( $attribute_start, $attribute_end );
|
||||
if ( null === $this->duplicate_attributes ) {
|
||||
$this->duplicate_attributes = array( $comparable_name => array( $duplicate_span ) );
|
||||
} elseif ( ! array_key_exists( $comparable_name, $this->duplicate_attributes ) ) {
|
||||
$this->duplicate_attributes[ $comparable_name ] = array( $duplicate_span );
|
||||
} else {
|
||||
$this->duplicate_attributes[ $comparable_name ][] = $duplicate_span;
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -1307,11 +1336,12 @@ class WP_HTML_Tag_Processor {
|
||||
*/
|
||||
private function after_tag() {
|
||||
$this->get_updated_html();
|
||||
$this->tag_name_starts_at = null;
|
||||
$this->tag_name_length = null;
|
||||
$this->tag_ends_at = null;
|
||||
$this->is_closing_tag = null;
|
||||
$this->attributes = array();
|
||||
$this->tag_name_starts_at = null;
|
||||
$this->tag_name_length = null;
|
||||
$this->tag_ends_at = null;
|
||||
$this->is_closing_tag = null;
|
||||
$this->attributes = array();
|
||||
$this->duplicate_attributes = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2080,6 +2110,17 @@ class WP_HTML_Tag_Processor {
|
||||
''
|
||||
);
|
||||
|
||||
// Removes any duplicated attributes if they were also present.
|
||||
if ( null !== $this->duplicate_attributes && array_key_exists( $name, $this->duplicate_attributes ) ) {
|
||||
foreach ( $this->duplicate_attributes[ $name ] as $attribute_token ) {
|
||||
$this->lexical_updates[] = new WP_HTML_Text_Replacement(
|
||||
$attribute_token->start,
|
||||
$attribute_token->end,
|
||||
''
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -16,7 +16,7 @@
|
||||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '6.4-alpha-56683';
|
||||
$wp_version = '6.4-alpha-56684';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
Loading…
Reference in New Issue
Block a user