Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/wp-includes/html-api/class-wp-html-tag-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2342,10 +2342,12 @@ private function class_name_updates_to_attributes_updates(): void {
}

if ( false === $existing_class && isset( $this->attributes['class'] ) ) {
$existing_class = substr(
$this->html,
$this->attributes['class']->value_starts_at,
$this->attributes['class']->value_length
$existing_class = WP_HTML_Decoder::decode_attribute(
substr(
$this->html,
$this->attributes['class']->value_starts_at,
$this->attributes['class']->value_length
)
Copy link
Member

Choose a reason for hiding this comment

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

I’ve been staring at this for a bit now and have no idea how we missed it. Surely we didn’t overlook this since 6.2 in the original introduction?

but you know what? we did!

php > $p = new WP_HTML_Tag_Processor( '<div class="&amp; &#x61;bc">' );
php > $p->next_tag();
php > $p->add_class( '&' );
php > $p->add_class( 'abc' );
php > echo $p->get_updated_html();
<div class="&amp; &#x61;bc &amp; abc">
php > $p->remove_class( 'abc' );
php > echo $p->get_updated_html();
<div class="&amp; &#x61;bc &amp;">
php > $p->remove_class( '&' );
php > echo $p->get_updated_html();
<div class="&amp; &#x61;bc &amp;">

now I don’t remember why; maybe it was performance-related or maybe it was a choice to lean on already-existing bugs, but I suppose it’s proper here to fix it regardless.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, there's been a latent issue for a while that was covered up by the behavior of esc_html(). It's become very clear with the change in 6.9 to use more rigorous attribute encoding.

);
}

Expand Down
48 changes: 46 additions & 2 deletions tests/phpunit/tests/html-api/wpHtmlTagProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2887,11 +2887,11 @@ public static function data_updating_attributes_in_malformed_html() {
),
'HTML tag opening inside attribute value' => array(
'input' => '<pre id="<code" class="wp-block-code <code is poetry&gt;"><code>This &lt;is> a &lt;strong is="true">thing.</code></pre><span>test</span>',
'expected' => '<pre foo="bar" id="<code" class="wp-block-code &lt;code is poetry&amp;gt; firstTag"><code class="secondTag">This &lt;is> a &lt;strong is="true">thing.</code></pre><span>test</span>',
'expected' => '<pre foo="bar" id="<code" class="wp-block-code &lt;code is poetry&gt; firstTag"><code class="secondTag">This &lt;is> a &lt;strong is="true">thing.</code></pre><span>test</span>',
),
'HTML tag brackets in attribute values and data markup' => array(
'input' => '<pre id="<code-&gt;-block-&gt;" class="wp-block-code <code is poetry&gt;"><code>This &lt;is> a &lt;strong is="true">thing.</code></pre><span>test</span>',
'expected' => '<pre foo="bar" id="<code-&gt;-block-&gt;" class="wp-block-code &lt;code is poetry&amp;gt; firstTag"><code class="secondTag">This &lt;is> a &lt;strong is="true">thing.</code></pre><span>test</span>',
'expected' => '<pre foo="bar" id="<code-&gt;-block-&gt;" class="wp-block-code &lt;code is poetry&gt; firstTag"><code class="secondTag">This &lt;is> a &lt;strong is="true">thing.</code></pre><span>test</span>',
Comment on lines +2890 to +2894
Copy link
Member Author

@sirreal sirreal Dec 3, 2025

Choose a reason for hiding this comment

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

These tests seem to have been ensuring incorrect behavior.

In both cases updated here an input class like poetry&gt; (decoded value poetry>) was changed to poetry&amp;gt; (decoded value poetry&gt;).

They were updated in #10143. This change restores their original (correct) assertion.

),
'Single and double quotes in attribute value' => array(
'input' => '<p title="Demonstrating how to use single quote (\') and double quote (&quot;)"><span>test</span>',
Expand Down Expand Up @@ -3028,6 +3028,50 @@ public static function data_updating_attributes_in_malformed_html() {
);
}

/**
* @ticket 64340
*/
public function test_class_changes_produce_correct_html() {
$processor = new WP_HTML_Tag_Processor( '<div class="&amp;">' );
$processor->next_tag();

$processor->add_class( '"' );
$processor->get_updated_html();

$processor->add_class( 'OK' );
$processor->get_updated_html();

$this->assertTrue( $processor->has_class( '&' ), 'Missing expected "&" class.' );
$this->assertTrue( $processor->has_class( '"' ), 'Missing expected \'"\' class.' );
$this->assertTrue( $processor->has_class( 'OK' ), 'Missing expected "OK" class.' );

$expected = '<div class="&amp; &quot; OK">';
$this->assertEqualHTML(
$expected,
$processor->get_updated_html(),
'<body>',
'HTML was not correctly updated after adding classes.'
);

$processor->remove_class( '&' );
$processor->get_updated_html();

$processor->remove_class( '"' );
$processor->get_updated_html();

$this->assertFalse( $processor->has_class( '&' ) );
$this->assertFalse( $processor->has_class( '"' ) );
$this->assertTrue( $processor->has_class( 'OK' ) );

$expected = '<div class="OK">';
$this->assertEqualHTML(
$expected,
$processor->get_updated_html(),
'<body>',
'HTML was not correctly updated after removing classes.'
);
}

/**
* @covers WP_HTML_Tag_Processor::next_tag
*/
Expand Down
Loading