Skip to content

Commit

Permalink
Fix server processing of an empty data-wp-context directive (#55482)
Browse files Browse the repository at this point in the history
* Add failing test

* Fix the test

* Fix WPCS

* Add a test for an empty directive
  • Loading branch information
luisherranz authored and noahtallen committed Oct 25, 2023
1 parent 3a5a1a7 commit fe54c03
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 10 deletions.
15 changes: 5 additions & 10 deletions lib/experimental/interactivity-api/directives/wp-context.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@ function gutenberg_interactivity_process_wp_context( $tags, $context ) {
}

$value = $tags->get_attribute( 'data-wp-context' );
if ( null === $value ) {
// No data-wp-context directive.
return;
}

$new_context = json_decode( $value, true );
if ( null === $new_context ) {
// If the JSON is not valid, we still add an empty array to the stack.
$new_context = array();
}
$new_context = json_decode(
is_string( $value ) && ! empty( $value ) ? $value : '{}',
true
);

$context->set_context( $new_context );
$context->set_context( $new_context ?? array() );
}
102 changes: 102 additions & 0 deletions phpunit/experimental/interactivity-api/directives/wp-context-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,106 @@ public function test_directive_keeps_working_after_malformed_context_objects() {
$context->get_context()
);
}

public function test_directive_keeps_working_with_a_directive_without_value() {
$context = new WP_Directive_Context();

$markup = '
<div data-wp-context=\'{ "my-key": "some-value" }\'>
<div data-wp-context>
</div>
</div>
';
$tags = new WP_HTML_Tag_Processor( $markup );

// Parent div.
$tags->next_tag( array( 'tag_closers' => 'visit' ) );
gutenberg_interactivity_process_wp_context( $tags, $context );

$this->assertSame(
array( 'my-key' => 'some-value' ),
$context->get_context()
);

// Children div.
$tags->next_tag( array( 'tag_closers' => 'visit' ) );
gutenberg_interactivity_process_wp_context( $tags, $context );

// Still the same context.
$this->assertSame(
array( 'my-key' => 'some-value' ),
$context->get_context()
);

// Closing children div.
$tags->next_tag( array( 'tag_closers' => 'visit' ) );
gutenberg_interactivity_process_wp_context( $tags, $context );

// Still the same context.
$this->assertSame(
array( 'my-key' => 'some-value' ),
$context->get_context()
);

// Closing parent div.
$tags->next_tag( array( 'tag_closers' => 'visit' ) );
gutenberg_interactivity_process_wp_context( $tags, $context );

// Now the context is empty.
$this->assertSame(
array(),
$context->get_context()
);
}

public function test_directive_keeps_working_with_an_empty_directive() {
$context = new WP_Directive_Context();

$markup = '
<div data-wp-context=\'{ "my-key": "some-value" }\'>
<div data-wp-context="">
</div>
</div>
';
$tags = new WP_HTML_Tag_Processor( $markup );

// Parent div.
$tags->next_tag( array( 'tag_closers' => 'visit' ) );
gutenberg_interactivity_process_wp_context( $tags, $context );

$this->assertSame(
array( 'my-key' => 'some-value' ),
$context->get_context()
);

// Children div.
$tags->next_tag( array( 'tag_closers' => 'visit' ) );
gutenberg_interactivity_process_wp_context( $tags, $context );

// Still the same context.
$this->assertSame(
array( 'my-key' => 'some-value' ),
$context->get_context()
);

// Closing children div.
$tags->next_tag( array( 'tag_closers' => 'visit' ) );
gutenberg_interactivity_process_wp_context( $tags, $context );

// Still the same context.
$this->assertSame(
array( 'my-key' => 'some-value' ),
$context->get_context()
);

// Closing parent div.
$tags->next_tag( array( 'tag_closers' => 'visit' ) );
gutenberg_interactivity_process_wp_context( $tags, $context );

// Now the context is empty.
$this->assertSame(
array(),
$context->get_context()
);
}
}

0 comments on commit fe54c03

Please sign in to comment.