This repository has been archived by the owner on Jul 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SSR: Add wp-show attribute directive processor
- Loading branch information
Showing
5 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
/** | ||
* wp-show tag directive test. | ||
*/ | ||
|
||
require_once __DIR__ . '/../../../src/directives/attributes/wp-show.php'; | ||
|
||
require_once __DIR__ . '/../../../src/directives/class-wp-directive-context.php'; | ||
require_once __DIR__ . '/../../../src/directives/class-wp-directive-processor.php'; | ||
|
||
require_once __DIR__ . '/../../../src/directives/wp-html.php'; | ||
|
||
/** | ||
* Tests for the wp-show tag directive. | ||
* | ||
* @group directives | ||
* @covers process_wp_show | ||
*/ | ||
class Tests_Directives_WpShow extends WP_UnitTestCase { | ||
public function test_directive_leaves_content_unchanged_if_when_is_true() { | ||
$markup = <<<EOF | ||
<div wp-show="context.myblock.open"> | ||
I should be shown! | ||
</div> | ||
EOF; | ||
$tags = new WP_Directive_Processor( $markup ); | ||
$tags->next_tag(); | ||
|
||
$context_before = new WP_Directive_Context( array( 'myblock' => array( 'open' => true ) ) ); | ||
$context = clone $context_before; | ||
process_wp_show( $tags, $context ); | ||
|
||
$tags->next_tag( array( 'tag_closers' => 'visit' ) ); | ||
process_wp_show( $tags, $context ); | ||
|
||
$this->assertSame( $markup, $tags->get_updated_html() ); | ||
$this->assertSame( $context_before->get_context(), $context->get_context(), 'wp-show directive changed context' ); | ||
} | ||
|
||
public function test_directive_wraps_content_in_template_if_when_is_false() { | ||
$markup = <<<EOF | ||
<div wp-show="context.myblock.open"> | ||
I should be shown! | ||
</div> | ||
EOF; | ||
$tags = new WP_Directive_Processor( $markup ); | ||
$tags->next_tag(); | ||
|
||
$context_before = new WP_Directive_Context( array( 'myblock' => array( 'open' => false ) ) ); | ||
$context = clone $context_before; | ||
process_wp_show( $tags, $context ); | ||
|
||
$tags->next_tag( array( 'tag_closers' => 'visit' ) ); | ||
process_wp_show( $tags, $context ); | ||
|
||
$updated_markup = <<<EOF | ||
<div wp-show="context.myblock.open"><template> | ||
I should be shown! | ||
</template></div> | ||
EOF; | ||
$this->assertSame( $updated_markup, $tags->get_updated_html() ); | ||
$this->assertSame( $context_before->get_context(), $context->get_context(), 'wp-show directive changed context' ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
require_once __DIR__ . '/../utils.php'; | ||
|
||
function process_wp_show( $tags, $context ) { | ||
if ( ! $tags->is_tag_closer() ) { // TODO: Exclude void and self-closing! | ||
set_bookmark_for_directive( $tags, 'wp-show' ); | ||
return; | ||
} | ||
|
||
$end = 'wp-show-closer'; | ||
$tags->set_bookmark( 'wp-show-closer' ); | ||
$start = seek_bookmark_for_directive( $tags, 'wp-show' ); | ||
|
||
$value = $tags->get_attribute( 'wp-show' ); | ||
if ( null !== $value ) { | ||
$show = evaluate( $value, $context->get_context() ); | ||
|
||
if ( ! $show ) { | ||
$content = $tags->get_content_inside_bookmarks( $start, $end ); | ||
$tags->set_content_inside_bookmarks( $start, $end, '<template>' . $content . '</template>' ); | ||
} | ||
} | ||
$tags->seek( $end ); | ||
$tags->release_bookmark( $start ); | ||
$tags->release_bookmark( $end ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
require_once __DIR__ . '/wp-html.php'; | ||
|
||
class WP_Directive_Processor extends WP_HTML_Tag_Processor { | ||
public function get_content_inside_bookmarks( $start_bookmark, $end_bookmark ) { | ||
if ( ! isset( $this->bookmarks[ $start_bookmark ], $this->bookmarks[ $end_bookmark ] ) ) { | ||
return null; | ||
} | ||
|
||
$start = $this->bookmarks[ $start_bookmark ]; | ||
$end = $this->bookmarks[ $end_bookmark ]; | ||
|
||
return substr( $this->get_updated_html(), $start->end + 1, $end->start - $start->end - 2 ); | ||
} | ||
|
||
public function set_content_inside_bookmarks( $start_bookmark, $end_bookmark, $content ) { | ||
if ( ! isset( $this->bookmarks[ $start_bookmark ], $this->bookmarks[ $end_bookmark ] ) ) { | ||
return null; | ||
} | ||
|
||
$start = $this->bookmarks[ $start_bookmark ]; | ||
$end = $this->bookmarks[ $end_bookmark ]; | ||
|
||
$this->lexical_updates[] = new WP_HTML_Text_Replacement( $start->end + 1, $end->start - 1, $content ); | ||
} | ||
|
||
/** | ||
* Checks whether a bookmark with the given name exists. | ||
* | ||
* @param string $bookmark_name Name to identify a bookmark that potentially exists. | ||
* @return bool Whether that bookmark exists. | ||
*/ | ||
public function has_bookmark( $bookmark_name ) { | ||
return array_key_exists( $bookmark_name, $this->bookmarks ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters