diff --git a/src/wp-includes/interactivity-api/class-wp-interactivity-api.php b/src/wp-includes/interactivity-api/class-wp-interactivity-api.php index 9e5b1be1fa5ed..e4817f3bfe65b 100644 --- a/src/wp-includes/interactivity-api/class-wp-interactivity-api.php +++ b/src/wp-includes/interactivity-api/class-wp-interactivity-api.php @@ -576,7 +576,10 @@ private function data_wp_bind_processor( WP_Interactivity_API_Directives_Process $attribute_value = $p->get_attribute( $attribute_name ); $result = $this->evaluate( $attribute_value, end( $namespace_stack ), end( $context_stack ) ); - if ( null !== $result && ( false !== $result || '-' === $bound_attribute[4] ) ) { + if ( null !== $result && ( + false !== $result || + ( strlen( $bound_attribute ) > 5 && '-' === $bound_attribute[4] ) + ) ) { /* * If the result of the evaluation is a boolean and the attribute is * `aria-` or `data-, convert it to a string "true" or "false". It @@ -584,7 +587,10 @@ private function data_wp_bind_processor( WP_Interactivity_API_Directives_Process * replicate what Preact will later do in the client: * https://github.com/preactjs/preact/blob/ea49f7a0f9d1ff2c98c0bdd66aa0cbc583055246/src/diff/props.js#L131C24-L136 */ - if ( is_bool( $result ) && '-' === $bound_attribute[4] ) { + if ( + is_bool( $result ) && + ( strlen( $bound_attribute ) > 5 && '-' === $bound_attribute[4] ) + ) { $result = $result ? 'true' : 'false'; } $p->set_attribute( $bound_attribute, $result ); diff --git a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-bind.php b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-bind.php index 2c530e4faf817..325eea59616f2 100644 --- a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-bind.php +++ b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-bind.php @@ -33,6 +33,8 @@ public function set_up() { 'null' => null, 'trueString' => 'true', 'falseString' => 'false', + 'trueValue' => true, + 'falseValue' => false, ) ); } @@ -60,7 +62,7 @@ private function process_directives( $html ) { public function test_wp_bind_sets_attribute() { $html = '
Text
'; list($p) = $this->process_directives( $html ); - $this->assertEquals( 'some-id', $p->get_attribute( 'id' ) ); + $this->assertSame( 'some-id', $p->get_attribute( 'id' ) ); } /** @@ -73,7 +75,7 @@ public function test_wp_bind_sets_attribute() { public function test_wp_bind_replaces_attribute() { $html = '
Text
'; list($p) = $this->process_directives( $html ); - $this->assertEquals( 'some-id', $p->get_attribute( 'id' ) ); + $this->assertSame( 'some-id', $p->get_attribute( 'id' ) ); } /** @@ -86,7 +88,7 @@ public function test_wp_bind_replaces_attribute() { public function test_wp_bind_sets_number_value() { $html = ''; list($p) = $this->process_directives( $html ); - $this->assertEquals( '100', $p->get_attribute( 'width' ) ); + $this->assertSame( '100', $p->get_attribute( 'width' ) ); } /** @@ -99,8 +101,8 @@ public function test_wp_bind_sets_number_value() { public function test_wp_bind_sets_true_string() { $html = '
Text
'; list($p, $new_html) = $this->process_directives( $html ); - $this->assertEquals( 'true', $p->get_attribute( 'id' ) ); - $this->assertEquals( '
Text
', $new_html ); + $this->assertSame( 'true', $p->get_attribute( 'id' ) ); + $this->assertSame( '
Text
', $new_html ); } /** @@ -113,8 +115,8 @@ public function test_wp_bind_sets_true_string() { public function test_wp_bind_sets_false_string() { $html = '
Text
'; list($p, $new_html) = $this->process_directives( $html ); - $this->assertEquals( 'false', $p->get_attribute( 'id' ) ); - $this->assertEquals( '
Text
', $new_html ); + $this->assertSame( 'false', $p->get_attribute( 'id' ) ); + $this->assertSame( '
Text
', $new_html ); } /** @@ -127,7 +129,7 @@ public function test_wp_bind_sets_false_string() { public function test_wp_bind_ignores_empty_bound_attribute() { $html = '
Text
'; $new_html = $this->interactivity->process_directives( $html ); - $this->assertEquals( $html, $new_html ); + $this->assertSame( $html, $new_html ); } /** @@ -141,7 +143,7 @@ public function test_wp_bind_ignores_empty_bound_attribute() { public function test_wp_bind_doesnt_do_anything_on_non_existent_references() { $html = '
Text
'; $new_html = $this->interactivity->process_directives( $html ); - $this->assertEquals( $html, $new_html ); + $this->assertSame( $html, $new_html ); } /** @@ -154,7 +156,7 @@ public function test_wp_bind_doesnt_do_anything_on_non_existent_references() { public function test_wp_bind_ignores_empty_value() { $html = '
Text
'; $new_html = $this->interactivity->process_directives( $html ); - $this->assertEquals( $html, $new_html ); + $this->assertSame( $html, $new_html ); } /** @@ -167,7 +169,7 @@ public function test_wp_bind_ignores_empty_value() { public function test_wp_bind_ignores_without_value() { $html = '
Text
'; $new_html = $this->interactivity->process_directives( $html ); - $this->assertEquals( $html, $new_html ); + $this->assertSame( $html, $new_html ); } /** @@ -181,7 +183,7 @@ public function test_wp_bind_ignores_without_value() { public function test_wp_bind_works_with_multiple_same_directives() { $html = '
Text
'; list($p) = $this->process_directives( $html ); - $this->assertEquals( 'some-id', $p->get_attribute( 'id' ) ); + $this->assertSame( 'some-id', $p->get_attribute( 'id' ) ); } /** @@ -195,8 +197,8 @@ public function test_wp_bind_works_with_multiple_same_directives() { public function test_wp_bind_works_with_multiple_different_directives() { $html = ''; list($p) = $this->process_directives( $html ); - $this->assertEquals( 'some-id', $p->get_attribute( 'id' ) ); - $this->assertEquals( '100', $p->get_attribute( 'width' ) ); + $this->assertSame( 'some-id', $p->get_attribute( 'id' ) ); + $this->assertSame( '100', $p->get_attribute( 'width' ) ); } /** @@ -210,7 +212,7 @@ public function test_wp_bind_adds_boolean_attribute_if_true() { $html = '
Text
'; list($p, $new_html) = $this->process_directives( $html ); $this->assertTrue( $p->get_attribute( 'hidden' ) ); - $this->assertEquals( '', $new_html ); + $this->assertSame( '', $new_html ); } /** @@ -225,7 +227,7 @@ public function test_wp_bind_replaces_existing_attribute_if_true() { $html = ''; list($p, $new_html) = $this->process_directives( $html ); $this->assertTrue( $p->get_attribute( 'hidden' ) ); - $this->assertEquals( '', $new_html ); + $this->assertSame( '', $new_html ); } /** @@ -240,12 +242,12 @@ public function test_wp_bind_doesnt_add_boolean_attribute_if_false_or_null() { $html = '
Text
'; list($p, $new_html) = $this->process_directives( $html ); $this->assertNull( $p->get_attribute( 'hidden' ) ); - $this->assertEquals( $html, $new_html ); + $this->assertSame( $html, $new_html ); $html = '
Text
'; list($p, $new_html) = $this->process_directives( $html ); $this->assertNull( $p->get_attribute( 'hidden' ) ); - $this->assertEquals( $html, $new_html ); + $this->assertSame( $html, $new_html ); } /** @@ -277,13 +279,13 @@ public function test_wp_bind_removes_boolean_attribute_if_false_or_null() { public function test_wp_bind_adds_value_if_true_in_aria_or_data_attributes() { $html = '
Text
'; list($p, $new_html) = $this->process_directives( $html ); - $this->assertEquals( 'true', $p->get_attribute( 'aria-hidden' ) ); - $this->assertEquals( '', $new_html ); + $this->assertSame( 'true', $p->get_attribute( 'aria-hidden' ) ); + $this->assertSame( '', $new_html ); $html = '
Text
'; list($p, $new_html) = $this->process_directives( $html ); - $this->assertEquals( 'true', $p->get_attribute( 'data-is-closed' ) ); - $this->assertEquals( '
Text
', $new_html ); + $this->assertSame( 'true', $p->get_attribute( 'data-is-closed' ) ); + $this->assertSame( '
Text
', $new_html ); } /** @@ -297,15 +299,15 @@ public function test_wp_bind_adds_value_if_true_in_aria_or_data_attributes() { public function test_wp_bind_replaces_value_if_true_in_aria_or_data_attributes() { $html = '
Text
'; list($p, $new_html) = $this->process_directives( $html ); - $this->assertEquals( 'true', $p->get_attribute( 'aria-hidden' ) ); - $this->assertEquals( '', $new_html ); + $this->assertSame( 'true', $p->get_attribute( 'aria-hidden' ) ); + $this->assertSame( '', $new_html ); $html = '
Text
'; $new_html = $this->interactivity->process_directives( $html ); $p = new WP_HTML_Tag_Processor( $new_html ); $p->next_tag(); - $this->assertEquals( 'true', $p->get_attribute( 'data-is-closed' ) ); - $this->assertEquals( '
Text
', $new_html ); + $this->assertSame( 'true', $p->get_attribute( 'data-is-closed' ) ); + $this->assertSame( '
Text
', $new_html ); } /** @@ -319,13 +321,13 @@ public function test_wp_bind_replaces_value_if_true_in_aria_or_data_attributes() public function test_wp_bind_adds_value_if_false_in_aria_or_data_attributes() { $html = '
Text
'; list($p, $new_html) = $this->process_directives( $html ); - $this->assertEquals( 'false', $p->get_attribute( 'aria-hidden' ) ); - $this->assertEquals( '
Text
', $new_html ); + $this->assertSame( 'false', $p->get_attribute( 'aria-hidden' ) ); + $this->assertSame( '
Text
', $new_html ); $html = '
Text
'; list($p, $new_html) = $this->process_directives( $html ); - $this->assertEquals( 'false', $p->get_attribute( 'data-is-closed' ) ); - $this->assertEquals( '
Text
', $new_html ); + $this->assertSame( 'false', $p->get_attribute( 'data-is-closed' ) ); + $this->assertSame( '
Text
', $new_html ); } /** @@ -339,13 +341,13 @@ public function test_wp_bind_adds_value_if_false_in_aria_or_data_attributes() { public function test_wp_bind_replaces_value_if_false_in_aria_or_data_attributes() { $html = ''; list($p, $new_html) = $this->process_directives( $html ); - $this->assertEquals( 'false', $p->get_attribute( 'aria-hidden' ) ); - $this->assertEquals( '
Text
', $new_html ); + $this->assertSame( 'false', $p->get_attribute( 'aria-hidden' ) ); + $this->assertSame( '
Text
', $new_html ); $html = '
Text
'; list($p, $new_html) = $this->process_directives( $html ); - $this->assertEquals( 'false', $p->get_attribute( 'data-is-closed' ) ); - $this->assertEquals( '
Text
', $new_html ); + $this->assertSame( 'false', $p->get_attribute( 'data-is-closed' ) ); + $this->assertSame( '
Text
', $new_html ); } /** @@ -375,8 +377,21 @@ public function test_wp_bind_removes_value_if_null_in_aria_or_data_attributes() public function test_wp_bind_handles_nested_bindings() { $html = '
'; list($p) = $this->process_directives( $html ); - $this->assertEquals( 'some-id', $p->get_attribute( 'id' ) ); + $this->assertSame( 'some-id', $p->get_attribute( 'id' ) ); $p->next_tag(); - $this->assertEquals( '100', $p->get_attribute( 'width' ) ); + $this->assertSame( '100', $p->get_attribute( 'width' ) ); + } + + /** + * Tests handling bindings to boolean values. + * + * @ticket 60758 + * + * @covers ::process_directives + */ + public function test_wp_bind_handles_true_value() { + $html = '
'; + list($p) = $this->process_directives( $html ); + $this->assertSame( true, $p->get_attribute( 'id' ) ); } }