Skip to content

Commit

Permalink
Move warning to the correct place, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cbravobernal committed Apr 25, 2024
1 parent 6b50fa5 commit 5758d38
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/wp-includes/interactivity-api/class-wp-interactivity-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,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.
Expand Down Expand Up @@ -358,13 +355,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();
}

/**
Expand Down
4 changes: 3 additions & 1 deletion tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,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' ) );
Expand Down

0 comments on commit 5758d38

Please sign in to comment.