Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Commit

Permalink
SSR: Add wp-show attribute directive processor
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Mar 2, 2023
1 parent 5508ec9 commit f40ae17
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 0 deletions.
64 changes: 64 additions & 0 deletions phpunit/directives/attributes/wp-show.php
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' );
}
}
27 changes: 27 additions & 0 deletions src/directives/attributes/wp-show.php
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 );
}
37 changes: 37 additions & 0 deletions src/directives/class-wp-directive-processor.php
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 );
}
}
22 changes: 22 additions & 0 deletions src/directives/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,25 @@ function set_style( $style, $name, $value ) {
}
return implode( ';', $style_assignments );
}

function count_bookmarks_for_directive( $tags, $directive ) {
$i = 0;
while ( $tags->has_bookmark( $directive . '-' . $i ) ) {
++$i;
}
return $i;
}

function set_bookmark_for_directive( $tags, $directive ) {
$i = count_bookmarks_for_directive( $tags, $directive );
$bookmark = $directive . '-' . $i;
$tags->set_bookmark( $bookmark );
return $bookmark;
}

function seek_bookmark_for_directive( $tags, $directive ) {
$i = count_bookmarks_for_directive( $tags, $directive ) - 1;
$bookmark = $directive . '-' . $i;
$tags->seek( $bookmark );
return $bookmark;
}
2 changes: 2 additions & 0 deletions wp-directives.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function () {

require_once __DIR__ . '/src/directives/attributes/wp-bind.php';
require_once __DIR__ . '/src/directives/attributes/wp-class.php';
require_once __DIR__ . '/src/directives/attributes/wp-show.php';
require_once __DIR__ . '/src/directives/attributes/wp-style.php';
require_once __DIR__ . '/src/directives/tags/wp-context.php';

Expand Down Expand Up @@ -216,6 +217,7 @@ function process_directives_in_block( $block_content ) {
'wp-context' => 'process_wp_context',
'wp-bind' => 'process_wp_bind',
'wp-class' => 'process_wp_class',
'wp-show' => 'process_wp_show',
'wp-style' => 'process_wp_style',
);

Expand Down

0 comments on commit f40ae17

Please sign in to comment.