From 446ca57da79cf0556b1e9cfb66d7208a026b3a34 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 2 Mar 2023 11:20:31 +0100 Subject: [PATCH 01/38] SSR: Add wp-show attribute directive processor --- phpunit/directives/attributes/wp-show.php | 64 +++++++++++++++++++++++ src/directives/attributes/wp-show.php | 27 ++++++++++ wp-directives.php | 2 + 3 files changed, 93 insertions(+) create mode 100644 phpunit/directives/attributes/wp-show.php create mode 100644 src/directives/attributes/wp-show.php diff --git a/phpunit/directives/attributes/wp-show.php b/phpunit/directives/attributes/wp-show.php new file mode 100644 index 00000000..3a550212 --- /dev/null +++ b/phpunit/directives/attributes/wp-show.php @@ -0,0 +1,64 @@ + + I should be shown! + +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 = << + I should be shown! + +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; + $this->assertSame( $updated_markup, $tags->get_updated_html() ); + $this->assertSame( $context_before->get_context(), $context->get_context(), 'wp-show directive changed context' ); + } +} diff --git a/src/directives/attributes/wp-show.php b/src/directives/attributes/wp-show.php new file mode 100644 index 00000000..89ed6126 --- /dev/null +++ b/src/directives/attributes/wp-show.php @@ -0,0 +1,27 @@ +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, '' ); + } + } + $tags->seek( $end ); + $tags->release_bookmark( $start ); + $tags->release_bookmark( $end ); +} diff --git a/wp-directives.php b/wp-directives.php index 9ca9fdac..16599a14 100644 --- a/wp-directives.php +++ b/wp-directives.php @@ -47,6 +47,7 @@ function () { require_once __DIR__ . '/src/directives/attributes/wp-context.php'; require_once __DIR__ . '/src/directives/attributes/wp-class.php'; require_once __DIR__ . '/src/directives/attributes/wp-html.php'; +require_once __DIR__ . '/src/directives/attributes/wp-show.php'; require_once __DIR__ . '/src/directives/attributes/wp-style.php'; require_once __DIR__ . '/src/directives/attributes/wp-text.php'; @@ -261,6 +262,7 @@ function process_directives_in_block( $block_content ) { 'data-wp-bind' => 'process_wp_bind', 'data-wp-class' => 'process_wp_class', 'data-wp-html' => 'process_wp_html', + 'data-wp-show' => 'process_wp_show', 'data-wp-style' => 'process_wp_style', 'data-wp-text' => 'process_wp_text', ); From 8a66bacf624cf3ec6c7e83fb318bfa1b3bd1fd57 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 9 Mar 2023 14:56:04 +0100 Subject: [PATCH 02/38] Simplify implementation of wp-show --- src/directives/attributes/wp-show.php | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/directives/attributes/wp-show.php b/src/directives/attributes/wp-show.php index 89ed6126..f9a93f84 100644 --- a/src/directives/attributes/wp-show.php +++ b/src/directives/attributes/wp-show.php @@ -3,25 +3,18 @@ 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' ); + if ( $tags->is_tag_closer() ) { 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 ( null === $value ) { + return; + } - if ( ! $show ) { - $content = $tags->get_content_inside_bookmarks( $start, $end ); - $tags->set_content_inside_bookmarks( $start, $end, '' ); - } + $show = evaluate( $value, $context->get_context() ); + if ( ! $show ) { + $content = $tags->get_inner_html(); + $tags->set_inner_html( '' ); } - $tags->seek( $end ); - $tags->release_bookmark( $start ); - $tags->release_bookmark( $end ); } From bc4adf3175fa9284e1e60494a6294f047fc8e41a Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 15 Mar 2023 17:12:36 +0100 Subject: [PATCH 03/38] Implement basic wrap_in_tag method() --- phpunit/directives/wp-directive-processor.php | 8 ++++++ .../class-wp-directive-processor.php | 26 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/phpunit/directives/wp-directive-processor.php b/phpunit/directives/wp-directive-processor.php index e24e247c..9c0a3526 100644 --- a/phpunit/directives/wp-directive-processor.php +++ b/phpunit/directives/wp-directive-processor.php @@ -125,4 +125,12 @@ public function test_set_inner_html_invalidates_bookmarks_that_point_to_replaced $successful_seek = $tags->seek( 'replaced' ); $this->assertFalse( $successful_seek ); } + + public function test_wrap_in_tag_wraps_tag_correctly() { + $tags = new WP_Directive_Processor( self::HTML ); + + $tags->next_tag( 'section' ); + $tags->wrap_in_tag( 'TEMPLATE' ); + $this->assertSame( '
outside
', $tags->get_updated_html() ); + } } diff --git a/src/directives/class-wp-directive-processor.php b/src/directives/class-wp-directive-processor.php index b6a2e046..408c40c1 100644 --- a/src/directives/class-wp-directive-processor.php +++ b/src/directives/class-wp-directive-processor.php @@ -131,6 +131,32 @@ public function get_balanced_tag_bookmarks() { return array( $start_name, $end_name ); } + public function wrap_in_tag( $tag ) { + $tag = strtolower( $tag ); // TODO: Allow passing in tags with attributes, e.g.