Skip to content
This repository was archived by the owner on Jul 28, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions phpunit/directives/attributes/wp-context.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* wp-context attribute directive test.
*/

// require_once __DIR__ . '/../../../src/directives/attributes/wp-context.php'; // TODO
require_once __DIR__ . '/../../../src/directives/attributes/wp-context.php';

require_once __DIR__ . '/../../../src/directives/class-wp-directive-context.php';

Expand All @@ -17,8 +17,6 @@
*/
class Tests_Directives_Attributes_WpContext extends WP_UnitTestCase {
public function test_directive_merges_context_correctly_upon_wp_context_attribute_on_opening_tag() {
$this->markTestSkipped( 'Need to implement the wp-context attribute directive processor first.' );

$context = new WP_Directive_Context(
array(
'myblock' => array( 'open' => false ),
Expand All @@ -40,4 +38,25 @@ public function test_directive_merges_context_correctly_upon_wp_context_attribut
$context->get_context()
);
}

public function test_directive_resets_context_correctly_upon_closing_tag() {
$context = new WP_Directive_Context(
array( 'my-key' => 'original-value' )
);

$context->set_context(
array( 'my-key' => 'new-value' )
);

$markup = '</div>';
$tags = new WP_HTML_Tag_Processor( $markup );
$tags->next_tag( array( 'tag_closers' => 'visit' ) );

process_wp_context_tag( $tags, $context );

$this->assertSame(
array( 'my-key' => 'original-value' ),
$context->get_context()
);
}
}
25 changes: 0 additions & 25 deletions phpunit/directives/tags/wp-context.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,4 @@ public function test_directive_resets_context_correctly_upon_closing_wp_context_
$context->get_context()
);
}

public function test_directive_merges_context_correctly_upon_wp_context_attribute_on_opening_tag() {
$this->markTestSkipped( 'Need to implement the wp-context attribute directive processor first.' );

$context = new WP_Directive_Context(
array(
'myblock' => array( 'open' => false ),
'otherblock' => array( 'somekey' => 'somevalue' ),
)
);

$markup = '<div wp-context=\'{ "myblock": { "open": true } }\'>';
$tags = new WP_HTML_Tag_Processor( $markup );
$tags->next_tag();

process_wp_context_tag( $tags, $context );

$this->assertSame(
array(
'myblock' => array( 'open' => true ),
'otherblock' => array( 'somekey' => 'somevalue' ),
),
$context->get_context()
);
}
}
19 changes: 19 additions & 0 deletions src/directives/attributes/wp-context.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

function process_wp_context_attribute( $tags, $context ) {
if ( $tags->is_tag_closer() ) {
$context->rewind_context();
return;
}

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

$new_context = json_decode( $value, true );
// TODO: Error handling.

$context->set_context( $new_context );
}