Skip to content

Commit

Permalink
Interactivity API: Allow missing state negation on server
Browse files Browse the repository at this point in the history
Aligns on the behavior of the negation operator with directives to missing paths in client and in server.

With a directive like the following:
{{{
<div data-wp-bind--hidden="!state.missing.property">
	This should be hidden by the <code>hidden</code> attribute.
</div>
}}}
Both server and client will return with this fix:
{{{
<div data-wp-bind--hidden="!state.missing.property" hidden="">
	This should be hidden by the <code>hidden</code> attribute.
</div>
}}}

Reviewed by cbravobernal.
Merges [59398] to the 6.7 branch.

Props jonsurrell, luisherranz.
Fixes #62374.


git-svn-id: https://develop.svn.wordpress.org/branches/6.7@59404 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
cbravobernal committed Nov 14, 2024
1 parent 0ac2f66 commit 4126580
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,8 @@ private function evaluate( $directive_value ) {
} elseif ( is_object( $current ) && isset( $current->$path_segment ) ) {
$current = $current->$path_segment;
} else {
return null;
$current = null;
break;
}

if ( $current instanceof Closure ) {
Expand Down
32 changes: 32 additions & 0 deletions tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,38 @@ public function test_evaluate_value_negation() {
$this->assertFalse( $result );
}

/**
* Tests that the `evaluate` method operates correctly when used with the
* negation operator (!) with non-existent paths.
*
* @ticket 62374
*
* @covers ::evaluate
*/
public function test_evaluate_value_negation_non_existent_path() {
$this->interactivity->state( 'myPlugin', array() );
$this->interactivity->state( 'otherPlugin', array() );
$this->set_internal_context_stack(
array(
'myPlugin' => array(),
'otherPlugin' => array(),
)
);
$this->set_internal_namespace_stack( 'myPlugin' );

$result = $this->evaluate( '!state.missing' );
$this->assertTrue( $result );

$result = $this->evaluate( '!context.missing' );
$this->assertTrue( $result );

$result = $this->evaluate( 'otherPlugin::!state.deeply.nested.missing' );
$this->assertTrue( $result );

$result = $this->evaluate( 'otherPlugin::!context.deeply.nested.missing' );
$this->assertTrue( $result );
}

/**
* Tests the `evaluate` method with non-existent paths.
*
Expand Down

0 comments on commit 4126580

Please sign in to comment.