Skip to content

Commit db2f6fe

Browse files
committed
Interactivity API: Allow missing state negation on server
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> }}} Props jonsurrell, luisherranz. Fixes #62374. git-svn-id: https://develop.svn.wordpress.org/trunk@59398 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 6936c5b commit db2f6fe

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

Diff for: src/wp-includes/interactivity-api/class-wp-interactivity-api.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,8 @@ private function evaluate( $directive_value ) {
584584
} elseif ( is_object( $current ) && isset( $current->$path_segment ) ) {
585585
$current = $current->$path_segment;
586586
} else {
587-
return null;
587+
$current = null;
588+
break;
588589
}
589590

590591
if ( $current instanceof Closure ) {

Diff for: tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php

+32
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,38 @@ public function test_evaluate_value_negation() {
10791079
$this->assertFalse( $result );
10801080
}
10811081

1082+
/**
1083+
* Tests that the `evaluate` method operates correctly when used with the
1084+
* negation operator (!) with non-existent paths.
1085+
*
1086+
* @ticket 62374
1087+
*
1088+
* @covers ::evaluate
1089+
*/
1090+
public function test_evaluate_value_negation_non_existent_path() {
1091+
$this->interactivity->state( 'myPlugin', array() );
1092+
$this->interactivity->state( 'otherPlugin', array() );
1093+
$this->set_internal_context_stack(
1094+
array(
1095+
'myPlugin' => array(),
1096+
'otherPlugin' => array(),
1097+
)
1098+
);
1099+
$this->set_internal_namespace_stack( 'myPlugin' );
1100+
1101+
$result = $this->evaluate( '!state.missing' );
1102+
$this->assertTrue( $result );
1103+
1104+
$result = $this->evaluate( '!context.missing' );
1105+
$this->assertTrue( $result );
1106+
1107+
$result = $this->evaluate( 'otherPlugin::!state.deeply.nested.missing' );
1108+
$this->assertTrue( $result );
1109+
1110+
$result = $this->evaluate( 'otherPlugin::!context.deeply.nested.missing' );
1111+
$this->assertTrue( $result );
1112+
}
1113+
10821114
/**
10831115
* Tests the `evaluate` method with non-existent paths.
10841116
*

0 commit comments

Comments
 (0)