From 5f1cc864df7c5cefc2ac461a4e478732c9b05fca Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Fri, 19 Apr 2024 16:19:09 +0200 Subject: [PATCH 01/24] Add debug notice for SSR --- .../interactivity-api/class-wp-interactivity-api.php | 8 ++++++++ 1 file changed, 8 insertions(+) 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 ac9a48982a5c5..4690189ec1e04 100644 --- a/src/wp-includes/interactivity-api/class-wp-interactivity-api.php +++ b/src/wp-includes/interactivity-api/class-wp-interactivity-api.php @@ -272,6 +272,8 @@ public function process_directives( string $html ): string { * it returns null if the HTML contains unbalanced tags. * * @since 6.5.0 + * @since 6.6.0 The function now adds a warning when the HTML contains unbalanced + * tags orSVG/Math with directives. * * @param string $html The HTML content to process. * @param array $context_stack The reference to the array used to keep track of contexts during processing. @@ -295,6 +297,10 @@ private function process_directives_args( string $html, array &$context_stack, a * We still process the rest of the HTML. */ if ( 'SVG' === $tag_name || 'MATH' === $tag_name ) { + if ( $p->get_attribute_names_with_prefix( 'data-wp-' ) ) { + $message = __( 'SVG or MATH tags are currently incompatible with the directives processing. Please refrain from adding directives to them.' ); + _doing_it_wrong( __METHOD__, $message, '6.6' ); + } $p->skip_to_tag_closer(); continue; } @@ -310,6 +316,8 @@ private function process_directives_args( string $html, array &$context_stack, a * stops processing it. */ $unbalanced = true; + $message = __( 'Due to an unbalanced tag in the processed HTML, the Server-Side Rendering directives processing will not function correctly.' ); + _doing_it_wrong( __METHOD__, $message, '6.6' ); break; } else { // Remove the last tag from the stack. From ef7721d4f3b288460d8cf0a68334e8ef21548fa0 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Mon, 22 Apr 2024 12:52:33 +0200 Subject: [PATCH 02/24] Add tagname to warning --- .../interactivity-api/class-wp-interactivity-api.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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 4690189ec1e04..1586edf36297d 100644 --- a/src/wp-includes/interactivity-api/class-wp-interactivity-api.php +++ b/src/wp-includes/interactivity-api/class-wp-interactivity-api.php @@ -298,7 +298,8 @@ private function process_directives_args( string $html, array &$context_stack, a */ if ( 'SVG' === $tag_name || 'MATH' === $tag_name ) { if ( $p->get_attribute_names_with_prefix( 'data-wp-' ) ) { - $message = __( 'SVG or MATH tags are currently incompatible with the directives processing. Please refrain from adding directives to them.' ); + /* translators: 1: SVG or MATH HTML tag. */ + $message = sprintf( '%1s tags are currently incompatible with the directives processing. Please refrain from adding directives to them.', $tag_name ); _doing_it_wrong( __METHOD__, $message, '6.6' ); } $p->skip_to_tag_closer(); @@ -316,7 +317,8 @@ private function process_directives_args( string $html, array &$context_stack, a * stops processing it. */ $unbalanced = true; - $message = __( 'Due to an unbalanced tag in the processed HTML, the Server-Side Rendering directives processing will not function correctly.' ); + /* translators: 1: Tag that caused the error, could by any HTML tag. */ + $message = sprintf( 'Due to an unbalanced %1s tag in the processed HTML, the Server-Side Rendering directives processing will not function correctly.', $tag_name ); _doing_it_wrong( __METHOD__, $message, '6.6' ); break; } else { @@ -390,7 +392,6 @@ private function process_directives_args( string $html, array &$context_stack, a } } } - /* * It returns null if the HTML is unbalanced because unbalanced HTML is * not safe to process. In that case, the Interactivity API runtime will From 12423e6cb49964b15d163e77f3bad10ed4e850c2 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Mon, 22 Apr 2024 16:55:10 +0200 Subject: [PATCH 03/24] Update tests checking the notice --- .../interactivity-api/wpInteractivityAPI.php | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php index 3c2050cab38ed..6e6c4f4e7079a 100644 --- a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php +++ b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php @@ -19,12 +19,32 @@ class Tests_Interactivity_API_WpInteractivityAPI extends WP_UnitTestCase { */ protected $interactivity; + /** + * Array of expected `_doing_it_wrong()` calls. + * + * @var string[] + */ + protected $expected_doing_it_wrong; + /** * Set up. */ public function set_up() { parent::set_up(); - $this->interactivity = new WP_Interactivity_API(); + $this->interactivity = new WP_Interactivity_API(); + $this->expected_doing_it_wrong = array(); + } + + /** + * Declares an expected `_doing_it_wrong()` call from within a test. + * + * @since 4.2.0 + * + * @param string $doing_it_wrong Name of the function, method, or class that appears in + * the first argument of the source `_doing_it_wrong()` call. + */ + public function setExpectedIncorrectUsage( $doing_it_wrong ) { + $this->expected_doing_it_wrong[] = $doing_it_wrong; } public function charset_iso_8859_1() { @@ -705,7 +725,8 @@ public function test_process_directives_changes_html_if_contains_svgs() { '; $processed_html = $this->interactivity->process_directives( $html ); - $p = new WP_HTML_Tag_Processor( $processed_html ); + $this->setExpectedIncorrectUsage( 'WP_Interactivity_API::process_directives_args' ); + $p = new WP_HTML_Tag_Processor( $processed_html ); $p->next_tag( 'svg' ); $this->assertNull( $p->get_attribute( 'width' ) ); $p->next_tag( 'div' ); @@ -770,7 +791,8 @@ public function test_process_directives_change_html_if_contains_math() { '; $processed_html = $this->interactivity->process_directives( $html ); - $p = new WP_HTML_Tag_Processor( $processed_html ); + $this->setExpectedIncorrectUsage( 'WP_Interactivity_API::process_directives_args' ); + $p = new WP_HTML_Tag_Processor( $processed_html ); $p->next_tag( 'math' ); $this->assertNull( $p->get_attribute( 'id' ) ); $p->next_tag( 'div' ); @@ -803,7 +825,8 @@ public function test_process_directives_does_not_change_inner_html_in_math() { '; $processed_html = $this->interactivity->process_directives( $html ); - $p = new WP_HTML_Tag_Processor( $processed_html ); + $this->setExpectedIncorrectUsage( 'WP_Interactivity_API::process_directives_args' ); + $p = new WP_HTML_Tag_Processor( $processed_html ); $p->next_tag( 'div' ); $this->assertNull( $p->get_attribute( 'id' ) ); } From 82a1df17736412c30b523a8ebc156cacf0c28b29 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Mon, 22 Apr 2024 17:09:50 +0200 Subject: [PATCH 04/24] Fix whitespace and rephrase the notice message --- .../interactivity-api/class-wp-interactivity-api.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 1586edf36297d..4a895e77db339 100644 --- a/src/wp-includes/interactivity-api/class-wp-interactivity-api.php +++ b/src/wp-includes/interactivity-api/class-wp-interactivity-api.php @@ -273,7 +273,7 @@ public function process_directives( string $html ): string { * * @since 6.5.0 * @since 6.6.0 The function now adds a warning when the HTML contains unbalanced - * tags orSVG/Math with directives. + * tags or SVG/Math with directives. * * @param string $html The HTML content to process. * @param array $context_stack The reference to the array used to keep track of contexts during processing. @@ -318,7 +318,7 @@ private function process_directives_args( string $html, array &$context_stack, a */ $unbalanced = true; /* translators: 1: Tag that caused the error, could by any HTML tag. */ - $message = sprintf( 'Due to an unbalanced %1s tag in the processed HTML, the Server-Side Rendering directives processing will not function correctly.', $tag_name ); + $message = sprintf( 'Due to an unbalanced %1s tag in the processed HTML, the directives will not be server side processed, JS runtime still work, but there will be a layout shift.', $tag_name ); _doing_it_wrong( __METHOD__, $message, '6.6' ); break; } else { From 9a172ec2685bd61db62f291e0adcd25ea7b29b7d Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Tue, 23 Apr 2024 13:21:12 +0200 Subject: [PATCH 05/24] Fix internal directives inside svg or mathml --- .../interactivity-api/class-wp-interactivity-api.php | 3 ++- tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) 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 4a895e77db339..21eb343df4627 100644 --- a/src/wp-includes/interactivity-api/class-wp-interactivity-api.php +++ b/src/wp-includes/interactivity-api/class-wp-interactivity-api.php @@ -297,7 +297,7 @@ private function process_directives_args( string $html, array &$context_stack, a * We still process the rest of the HTML. */ if ( 'SVG' === $tag_name || 'MATH' === $tag_name ) { - if ( $p->get_attribute_names_with_prefix( 'data-wp-' ) ) { + if ( $p->get_attribute_names_with_prefix( 'data-wp-' ) || str_contains( $p->get_updated_html(), 'data-wp-' ) ) { /* translators: 1: SVG or MATH HTML tag. */ $message = sprintf( '%1s tags are currently incompatible with the directives processing. Please refrain from adding directives to them.', $tag_name ); _doing_it_wrong( __METHOD__, $message, '6.6' ); @@ -392,6 +392,7 @@ private function process_directives_args( string $html, array &$context_stack, a } } } + /* * It returns null if the HTML is unbalanced because unbalanced HTML is * not safe to process. In that case, the Interactivity API runtime will diff --git a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php index 6e6c4f4e7079a..93f93d84f76a8 100644 --- a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php +++ b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php @@ -759,7 +759,8 @@ public function test_process_directives_does_not_change_inner_html_in_svgs() { '; $processed_html = $this->interactivity->process_directives( $html ); - $p = new WP_HTML_Tag_Processor( $processed_html ); + $this->setExpectedIncorrectUsage( 'WP_Interactivity_API::process_directives_args' ); + $p = new WP_HTML_Tag_Processor( $processed_html ); $p->next_tag( 'div' ); $this->assertNull( $p->get_attribute( 'id' ) ); } From 8abb04065f5386a2c04b7d9ea21a328f5ce23513 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 25 Apr 2024 11:34:10 +0200 Subject: [PATCH 06/24] Refactor tests --- .../interactivity-api/wpInteractivityAPI.php | 39 +++++-------------- 1 file changed, 10 insertions(+), 29 deletions(-) diff --git a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php index 93f93d84f76a8..8915b8acc906c 100644 --- a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php +++ b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php @@ -19,32 +19,12 @@ class Tests_Interactivity_API_WpInteractivityAPI extends WP_UnitTestCase { */ protected $interactivity; - /** - * Array of expected `_doing_it_wrong()` calls. - * - * @var string[] - */ - protected $expected_doing_it_wrong; - /** * Set up. */ public function set_up() { parent::set_up(); - $this->interactivity = new WP_Interactivity_API(); - $this->expected_doing_it_wrong = array(); - } - - /** - * Declares an expected `_doing_it_wrong()` call from within a test. - * - * @since 4.2.0 - * - * @param string $doing_it_wrong Name of the function, method, or class that appears in - * the first argument of the source `_doing_it_wrong()` call. - */ - public function setExpectedIncorrectUsage( $doing_it_wrong ) { - $this->expected_doing_it_wrong[] = $doing_it_wrong; + $this->interactivity = new WP_Interactivity_API(); } public function charset_iso_8859_1() { @@ -670,6 +650,7 @@ public function test_process_directives_process_the_directives_in_the_correct_or * @dataProvider data_html_with_unbalanced_tags * * @param string $html HTML containing unbalanced tags and also a directive. + * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args */ public function test_process_directives_doesnt_change_html_if_contains_unbalanced_tags( $html ) { $this->interactivity->state( 'myPlugin', array( 'id' => 'some-id' ) ); @@ -705,6 +686,7 @@ public static function data_html_with_unbalanced_tags() { * @ticket 60517 * * @covers ::process_directives + * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args */ public function test_process_directives_changes_html_if_contains_svgs() { $this->interactivity->state( @@ -725,8 +707,7 @@ public function test_process_directives_changes_html_if_contains_svgs() { '; $processed_html = $this->interactivity->process_directives( $html ); - $this->setExpectedIncorrectUsage( 'WP_Interactivity_API::process_directives_args' ); - $p = new WP_HTML_Tag_Processor( $processed_html ); + $p = new WP_HTML_Tag_Processor( $processed_html ); $p->next_tag( 'svg' ); $this->assertNull( $p->get_attribute( 'width' ) ); $p->next_tag( 'div' ); @@ -742,6 +723,7 @@ public function test_process_directives_changes_html_if_contains_svgs() { * @ticket 60517 * * @covers ::process_directives + * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args */ public function test_process_directives_does_not_change_inner_html_in_svgs() { $this->interactivity->state( @@ -759,8 +741,7 @@ public function test_process_directives_does_not_change_inner_html_in_svgs() { '; $processed_html = $this->interactivity->process_directives( $html ); - $this->setExpectedIncorrectUsage( 'WP_Interactivity_API::process_directives_args' ); - $p = new WP_HTML_Tag_Processor( $processed_html ); + $p = new WP_HTML_Tag_Processor( $processed_html ); $p->next_tag( 'div' ); $this->assertNull( $p->get_attribute( 'id' ) ); } @@ -772,6 +753,7 @@ public function test_process_directives_does_not_change_inner_html_in_svgs() { * @ticket 60517 * * @covers ::process_directives + * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args */ public function test_process_directives_change_html_if_contains_math() { $this->interactivity->state( @@ -792,8 +774,7 @@ public function test_process_directives_change_html_if_contains_math() { '; $processed_html = $this->interactivity->process_directives( $html ); - $this->setExpectedIncorrectUsage( 'WP_Interactivity_API::process_directives_args' ); - $p = new WP_HTML_Tag_Processor( $processed_html ); + $p = new WP_HTML_Tag_Processor( $processed_html ); $p->next_tag( 'math' ); $this->assertNull( $p->get_attribute( 'id' ) ); $p->next_tag( 'div' ); @@ -806,6 +787,7 @@ public function test_process_directives_change_html_if_contains_math() { * * @ticket 60517 * + * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args * @covers ::process_directives */ public function test_process_directives_does_not_change_inner_html_in_math() { @@ -826,8 +808,7 @@ public function test_process_directives_does_not_change_inner_html_in_math() { '; $processed_html = $this->interactivity->process_directives( $html ); - $this->setExpectedIncorrectUsage( 'WP_Interactivity_API::process_directives_args' ); - $p = new WP_HTML_Tag_Processor( $processed_html ); + $p = new WP_HTML_Tag_Processor( $processed_html ); $p->next_tag( 'div' ); $this->assertNull( $p->get_attribute( 'id' ) ); } From 129ee82812964e7f128704d2594ce4ea69403aa0 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 25 Apr 2024 12:05:43 +0200 Subject: [PATCH 07/24] Update test to include previous directives triggering the notice --- .../tests/interactivity-api/wpInteractivityAPI.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php index 8915b8acc906c..23000d7082802 100644 --- a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php +++ b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php @@ -686,7 +686,6 @@ public static function data_html_with_unbalanced_tags() { * @ticket 60517 * * @covers ::process_directives - * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args */ public function test_process_directives_changes_html_if_contains_svgs() { $this->interactivity->state( @@ -698,22 +697,22 @@ public function test_process_directives_changes_html_if_contains_svgs() { ); $html = '
- +
+ Red Circle
-
'; $processed_html = $this->interactivity->process_directives( $html ); $p = new WP_HTML_Tag_Processor( $processed_html ); + $p->next_tag( 'div' ); + $this->assertEquals( '100', $p->get_attribute( 'id' ) ); $p->next_tag( 'svg' ); $this->assertNull( $p->get_attribute( 'width' ) ); $p->next_tag( 'div' ); $this->assertEquals( 'some-id', $p->get_attribute( 'id' ) ); - $p->next_tag( 'div' ); - $this->assertEquals( '100', $p->get_attribute( 'id' ) ); } /** From eff4f6ccce23548168267f8310bdce889ff6397b Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 25 Apr 2024 15:14:25 +0200 Subject: [PATCH 08/24] Do not check tags inside svgs or math for the notice --- .../interactivity-api/class-wp-interactivity-api.php | 10 +++++----- .../tests/interactivity-api/wpInteractivityAPI.php | 1 - 2 files changed, 5 insertions(+), 6 deletions(-) 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 21eb343df4627..bcba49e50d08b 100644 --- a/src/wp-includes/interactivity-api/class-wp-interactivity-api.php +++ b/src/wp-includes/interactivity-api/class-wp-interactivity-api.php @@ -297,9 +297,9 @@ private function process_directives_args( string $html, array &$context_stack, a * We still process the rest of the HTML. */ if ( 'SVG' === $tag_name || 'MATH' === $tag_name ) { - if ( $p->get_attribute_names_with_prefix( 'data-wp-' ) || str_contains( $p->get_updated_html(), 'data-wp-' ) ) { - /* translators: 1: SVG or MATH HTML tag. */ - $message = sprintf( '%1s tags are currently incompatible with the directives processing. Please refrain from adding directives to them.', $tag_name ); + if ( $p->get_attribute_names_with_prefix( 'data-wp-' ) ) { + /* translators: %s: SVG or MATH HTML tag. */ + $message = sprintf( '%s tags are currently incompatible with the directives processing. Please refrain from adding directives to them.', $tag_name ); _doing_it_wrong( __METHOD__, $message, '6.6' ); } $p->skip_to_tag_closer(); @@ -317,8 +317,8 @@ private function process_directives_args( string $html, array &$context_stack, a * stops processing it. */ $unbalanced = true; - /* translators: 1: Tag that caused the error, could by any HTML tag. */ - $message = sprintf( 'Due to an unbalanced %1s tag in the processed HTML, the directives will not be server side processed, JS runtime still work, but there will be a layout shift.', $tag_name ); + /* translators: %s: Tag that caused the error, could by any HTML tag. */ + $message = sprintf( 'Due to an unbalanced %s tag in the processed HTML, the directives will not be server side processed, JS runtime still work, but there will be a layout shift.', $tag_name ); _doing_it_wrong( __METHOD__, $message, '6.6' ); break; } else { diff --git a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php index 23000d7082802..16d7c0b9d6423 100644 --- a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php +++ b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php @@ -722,7 +722,6 @@ public function test_process_directives_changes_html_if_contains_svgs() { * @ticket 60517 * * @covers ::process_directives - * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args */ public function test_process_directives_does_not_change_inner_html_in_svgs() { $this->interactivity->state( From a4474866c7798f6e8c18c55d4693a8e90ccbc577 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 25 Apr 2024 15:25:00 +0200 Subject: [PATCH 09/24] Forgot translation functions --- .../interactivity-api/class-wp-interactivity-api.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 bcba49e50d08b..545e3a29a5827 100644 --- a/src/wp-includes/interactivity-api/class-wp-interactivity-api.php +++ b/src/wp-includes/interactivity-api/class-wp-interactivity-api.php @@ -299,7 +299,7 @@ private function process_directives_args( string $html, array &$context_stack, a if ( 'SVG' === $tag_name || 'MATH' === $tag_name ) { if ( $p->get_attribute_names_with_prefix( 'data-wp-' ) ) { /* translators: %s: SVG or MATH HTML tag. */ - $message = sprintf( '%s tags are currently incompatible with the directives processing. Please refrain from adding directives to them.', $tag_name ); + $message = sprintf( __( '%s tags are currently incompatible with the directives processing. Please refrain from adding directives to them.' ), $tag_name ); _doing_it_wrong( __METHOD__, $message, '6.6' ); } $p->skip_to_tag_closer(); @@ -318,7 +318,7 @@ private function process_directives_args( string $html, array &$context_stack, a */ $unbalanced = true; /* translators: %s: Tag that caused the error, could by any HTML tag. */ - $message = sprintf( 'Due to an unbalanced %s tag in the processed HTML, the directives will not be server side processed, JS runtime still work, but there will be a layout shift.', $tag_name ); + $message = sprintf( __( 'Due to an unbalanced %s tag in the processed HTML, the directives will not be server side processed, JS runtime still work, but there will be a layout shift.' ), $tag_name ); _doing_it_wrong( __METHOD__, $message, '6.6' ); break; } else { From df899ddea1796f6aee1453a8757f1ef0e87a81ac Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 25 Apr 2024 17:20:05 +0200 Subject: [PATCH 10/24] Move warning to the correct place, update tests --- .../class-wp-interactivity-api.php | 16 ++++++++++------ .../interactivity-api/wpInteractivityAPI.php | 4 +++- 2 files changed, 13 insertions(+), 7 deletions(-) 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 545e3a29a5827..f33ac9d098509 100644 --- a/src/wp-includes/interactivity-api/class-wp-interactivity-api.php +++ b/src/wp-includes/interactivity-api/class-wp-interactivity-api.php @@ -317,9 +317,6 @@ private function process_directives_args( string $html, array &$context_stack, a * stops processing it. */ $unbalanced = true; - /* translators: %s: Tag that caused the error, could by any HTML tag. */ - $message = sprintf( __( 'Due to an unbalanced %s tag in the processed HTML, the directives will not be server side processed, JS runtime still work, but there will be a layout shift.' ), $tag_name ); - _doing_it_wrong( __METHOD__, $message, '6.6' ); break; } else { // Remove the last tag from the stack. @@ -392,13 +389,20 @@ private function process_directives_args( string $html, array &$context_stack, a } } } - /* * It returns null if the HTML is unbalanced because unbalanced HTML is * not safe to process. In that case, the Interactivity API runtime will - * update the HTML on the client side during the hydration. + * update the HTML on the client side during the hydration. It will also + * display a notice to the developer to inform them about the issue. */ - return $unbalanced || 0 < count( $tag_stack ) ? null : $p->get_updated_html(); + if ( $unbalanced || 0 < count( $tag_stack ) ) { + /* translators: %s: Tag that caused the error, could by any HTML tag. */ + $message = sprintf( __( 'Due to an unbalanced %s tag in the processed HTML, the directives will not be server side processed, JS runtime still work, but there will be a layout shift.' ), $tag_name ); + _doing_it_wrong( __METHOD__, $message, '6.6' ); + return null; + } + + return $p->get_updated_html(); } /** diff --git a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php index 16d7c0b9d6423..3c4be91544930 100644 --- a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php +++ b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php @@ -649,8 +649,10 @@ public function test_process_directives_process_the_directives_in_the_correct_or * * @dataProvider data_html_with_unbalanced_tags * - * @param string $html HTML containing unbalanced tags and also a directive. * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args + * + * @param string $html HTML containing unbalanced tags and also a directive. + * */ public function test_process_directives_doesnt_change_html_if_contains_unbalanced_tags( $html ) { $this->interactivity->state( 'myPlugin', array( 'id' => 'some-id' ) ); From f62474507d0dcfcebf73791a79419be182960db5 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 25 Apr 2024 18:37:23 +0200 Subject: [PATCH 11/24] Update tests --- .../tests/interactivity-api/wpInteractivityAPI-wp-each.php | 4 ++++ .../tests/interactivity-api/wpInteractivityAPI-wp-text.php | 4 ++++ .../wpInteractivityAPIDirectivesProcessor.php | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-each.php b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-each.php index 42edd9f1b3838..fa166c1799d33 100644 --- a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-each.php +++ b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-each.php @@ -580,6 +580,8 @@ public function test_wp_each_nested_template_tags_using_previous_item_as_list() * @ticket 60356 * * @covers ::process_directives + * + * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args */ public function test_wp_each_unbalanced_tags() { $original = '' . @@ -598,6 +600,8 @@ public function test_wp_each_unbalanced_tags() { * @ticket 60356 * * @covers ::process_directives + * + * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args */ public function test_wp_each_unbalanced_tags_in_nested_template_tags() { $this->interactivity->state( 'myPlugin', array( 'list2' => array( 3, 4 ) ) ); diff --git a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-text.php b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-text.php index 2fa9363ac93f4..792d692e1dccc 100644 --- a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-text.php +++ b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-text.php @@ -117,6 +117,8 @@ public function test_wp_text_sets_inner_content_with_nested_tags() { * @ticket 60356 * * @covers ::process_directives + * + * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args */ public function test_wp_text_sets_inner_content_even_with_unbalanced_but_different_tags_inside_content() { $html = '
Text
'; @@ -131,6 +133,8 @@ public function test_wp_text_sets_inner_content_even_with_unbalanced_but_differe * @ticket 60356 * * @covers ::process_directives + * + * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args */ public function test_wp_text_fails_with_unbalanced_and_same_tags_inside_content() { $html = '
Text
'; diff --git a/tests/phpunit/tests/interactivity-api/wpInteractivityAPIDirectivesProcessor.php b/tests/phpunit/tests/interactivity-api/wpInteractivityAPIDirectivesProcessor.php index 5812c061666c8..de87f0a71413f 100644 --- a/tests/phpunit/tests/interactivity-api/wpInteractivityAPIDirectivesProcessor.php +++ b/tests/phpunit/tests/interactivity-api/wpInteractivityAPIDirectivesProcessor.php @@ -110,6 +110,8 @@ public function test_get_content_between_balanced_template_tags_no_tags() { * @ticket 60356 * * @covers ::get_content_between_balanced_template_tags + * + * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args */ public function test_get_content_between_balanced_template_tags_with_unbalanced_tags() { $content = '