diff --git a/phpunit/directives/attributes/wp-show.php b/phpunit/directives/attributes/wp-show.php new file mode 100644 index 00000000..a2cc3e73 --- /dev/null +++ b/phpunit/directives/attributes/wp-show.php @@ -0,0 +1,176 @@ +I should be shown!'; + $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(), 'data-wp-show directive changed context' ); + } + + public function test_directive_wraps_content_in_template_if_when_is_false() { + $markup = '
I should not be shown!
'; + + $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 = ''; + + $this->assertSame( $updated_markup, $tags->get_updated_html() ); + $this->assertSame( $context_before->get_context(), $context->get_context(), 'data-wp-show directive changed context' ); + } + + public function test_directive_does_not_wrap_template_in_template() { + $markup = ''; + + $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 ); + + $this->assertSame( $markup, $tags->get_updated_html() ); + $this->assertSame( $context_before->get_context(), $context->get_context(), 'data-wp-show directive changed context' ); + } + + public function test_directive_wraps_content_preceded_by_other_html_in_template_if_when_is_false() { + $markup = '

Some text

I should not be shown!
'; + + $tags = new WP_Directive_Processor( $markup ); + $tags->next_tag( 'div' ); + + $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 = '

Some text

'; + + $this->assertSame( $updated_markup, $tags->get_updated_html() ); + $this->assertSame( $context_before->get_context(), $context->get_context(), 'data-wp-show directive changed context' ); + } + + public function test_directive_wraps_void_tag_in_template_if_when_is_false() { + $markup = ''; + $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 = ''; + + $this->assertSame( $updated_markup, $tags->get_updated_html() ); + $this->assertSame( $context_before->get_context(), $context->get_context(), 'data-wp-show directive changed context' ); + } + + public function test_nested_directives_within_wp_show_with_truthy_value() { + $markup = << + I should be shown! +
I should be shown!
+
I should not be shown!
+ +END; + + $context_before = new WP_Directive_Context( + array( + 'myBlock' => array( 'open' => true ), + 'myOtherBlock' => array( 'open' => true ), + ) + ); + $context = clone $context_before; + + $tags = new WP_Directive_Processor( $markup ); + + while ( $tags->next_tag( array( 'tag_closers' => 'visit' ) ) ) { + process_wp_show( $tags, $context ); + } + + $updated_markup = << + I should be shown! +
I should be shown!
+ + +END; + $this->assertSame( $updated_markup, $tags->get_updated_html() ); + $this->assertSame( $context_before->get_context(), $context->get_context(), 'data-wp-show directive changed context' ); + } + + public function test_nested_directives_within_wp_show_with_falsy_value() { + $markup = << + I should not be shown! +
I should be shown!
+
I should not be shown!
+ +END; + + $context_before = new WP_Directive_Context( + array( + 'myBlock' => array( 'open' => false ), + 'myOtherBlock' => array( 'open' => true ), + ) + ); + $context = clone $context_before; + + $tags = new WP_Directive_Processor( $markup ); + + while ( $tags->next_tag( array( 'tag_closers' => 'visit' ) ) ) { + process_wp_show( $tags, $context ); + } + + $updated_markup = <<
+ I should not be shown! +
I should be shown!
+ +
+END; + $this->assertSame( $updated_markup, $tags->get_updated_html() ); + $this->assertSame( $context_before->get_context(), $context->get_context(), 'data-wp-show directive changed context' ); + } +} diff --git a/phpunit/directives/wp-directive-processor.php b/phpunit/directives/wp-directive-processor.php index e24e247c..4f01a64f 100644 --- a/phpunit/directives/wp-directive-processor.php +++ b/phpunit/directives/wp-directive-processor.php @@ -125,4 +125,43 @@ 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_balanced_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() ); + $this->assertSame( 'SECTION', $tags->get_tag() ); + $this->assertFalse( $tags->is_tag_closer() ); + } + + public function test_wrap_in_tag_wraps_void_tag_correctly() { + $tags = new WP_Directive_Processor( self::HTML ); + + $tags->next_tag( 'img' ); + $tags->wrap_in_tag( 'TEMPLATE' ); + $this->assertSame( '
outside
inside
', $tags->get_updated_html() ); + $this->assertSame( 'IMG', $tags->get_tag() ); + } + + public function test_wrap_in_tag_followed_by_two_subsequent_calls_of_get_updated_html_works_correctly() { + $tags = new WP_Directive_Processor( self::HTML ); + + $tags->next_tag( 'img' ); + $tags->wrap_in_tag( 'TEMPLATE' ); + $tags->get_updated_html(); + $this->assertSame( '
outside
inside
', $tags->get_updated_html() ); + $this->assertSame( 'IMG', $tags->get_tag() ); + } + + public function test_wrap_in_tag_fails_if_passed_void_tag() { + $tags = new WP_Directive_Processor( self::HTML ); + + $tags->next_tag( 'section' ); + + $this->assertFalse( $tags->wrap_in_tag( 'IMG' ) ); + $this->assertSame( self::HTML, $tags->get_updated_html() ); + $this->assertSame( 'SECTION', $tags->get_tag() ); + } } diff --git a/src/directives/attributes/wp-show.php b/src/directives/attributes/wp-show.php new file mode 100644 index 00000000..46649f80 --- /dev/null +++ b/src/directives/attributes/wp-show.php @@ -0,0 +1,41 @@ +is_tag_closer() ) { + return; + } + + $value = $tags->get_attribute( 'data-wp-show' ); + if ( null === $value ) { + return; + } + + $show = evaluate( $value, $context->get_context() ); + if ( $show ) { + return; + } + + if ( 'TEMPLATE' === $tags->get_tag() ) { + return; // Don't wrap a `