From 5508ec9847e4372bb44915d0abdfd6e1e40cdc6b Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 2 Mar 2023 11:15:59 +0100 Subject: [PATCH] SSR: Remove `wp-context` tag directive (#165) * Remove wp-context tag directive * Rename process_wp_context_attribute to process_wp_context * Remove logic for tag directives from wp_process_directives * Fix unit test --- phpunit/directives/attributes/wp-context.php | 6 +- phpunit/directives/tags/wp-context.php | 62 ---------------- phpunit/directives/wp-process-directives.php | 8 +-- src/directives/attributes/wp-context.php | 2 +- src/directives/tags/wp-context.php | 19 ----- src/directives/wp-process-directives.php | 75 +++++++++----------- wp-directives.php | 10 +-- 7 files changed, 46 insertions(+), 136 deletions(-) delete mode 100644 phpunit/directives/tags/wp-context.php delete mode 100644 src/directives/tags/wp-context.php diff --git a/phpunit/directives/attributes/wp-context.php b/phpunit/directives/attributes/wp-context.php index 5af625e1..a8b736cf 100644 --- a/phpunit/directives/attributes/wp-context.php +++ b/phpunit/directives/attributes/wp-context.php @@ -13,7 +13,7 @@ * Tests for the wp-context attribute directive. * * @group directives - * @covers process_wp_context_attribute + * @covers process_wp_context */ class Tests_Directives_Attributes_WpContext extends WP_UnitTestCase { public function test_directive_merges_context_correctly_upon_wp_context_attribute_on_opening_tag() { @@ -28,7 +28,7 @@ public function test_directive_merges_context_correctly_upon_wp_context_attribut $tags = new WP_HTML_Tag_Processor( $markup ); $tags->next_tag(); - process_wp_context_attribute( $tags, $context ); + process_wp_context( $tags, $context ); $this->assertSame( array( @@ -52,7 +52,7 @@ public function test_directive_resets_context_correctly_upon_closing_tag() { $tags = new WP_HTML_Tag_Processor( $markup ); $tags->next_tag( array( 'tag_closers' => 'visit' ) ); - process_wp_context_tag( $tags, $context ); + process_wp_context( $tags, $context ); $this->assertSame( array( 'my-key' => 'original-value' ), diff --git a/phpunit/directives/tags/wp-context.php b/phpunit/directives/tags/wp-context.php deleted file mode 100644 index 92a9abe7..00000000 --- a/phpunit/directives/tags/wp-context.php +++ /dev/null @@ -1,62 +0,0 @@ - array( 'open' => false ), - 'otherblock' => array( 'somekey' => 'somevalue' ), - ) - ); - - $markup = ''; - $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() - ); - } - - public function test_directive_resets_context_correctly_upon_closing_wp_context_tag() { - $context = new WP_Directive_Context( - array( 'my-key' => 'original-value' ) - ); - - $context->set_context( - array( 'my-key' => 'new-value' ) - ); - - $markup = ''; - $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() - ); - } -} diff --git a/phpunit/directives/wp-process-directives.php b/phpunit/directives/wp-process-directives.php index 695b008c..1115decd 100644 --- a/phpunit/directives/wp-process-directives.php +++ b/phpunit/directives/wp-process-directives.php @@ -39,13 +39,13 @@ function( $p ) { ) ); - $attribute_directives = array( + $directives = array( 'foo-test' => array( $test_helper, 'process_foo_test' ), ); $markup = '
Example:
This is a test>
Here is a nested div
'; $tags = new WP_HTML_Tag_Processor( $markup ); - wp_process_directives( $tags, 'foo-', array(), $attribute_directives ); + wp_process_directives( $tags, 'foo-', $directives ); } public function test_directives_with_colon_processed_correctly() { @@ -53,12 +53,12 @@ public function test_directives_with_colon_processed_correctly() { $test_helper->expects( $this->atLeastOnce() ) ->method( 'process_foo_test' ); - $attribute_directives = array( + $directives = array( 'foo-test' => array( $test_helper, 'process_foo_test' ), ); $markup = '
'; $tags = new WP_HTML_Tag_Processor( $markup ); - wp_process_directives( $tags, 'foo-', array(), $attribute_directives ); + wp_process_directives( $tags, 'foo-', $directives ); } } diff --git a/src/directives/attributes/wp-context.php b/src/directives/attributes/wp-context.php index 8ca8a4f4..c8b61594 100644 --- a/src/directives/attributes/wp-context.php +++ b/src/directives/attributes/wp-context.php @@ -1,6 +1,6 @@ is_tag_closer() ) { $context->rewind_context(); return; diff --git a/src/directives/tags/wp-context.php b/src/directives/tags/wp-context.php deleted file mode 100644 index bf2b2a30..00000000 --- a/src/directives/tags/wp-context.php +++ /dev/null @@ -1,19 +0,0 @@ -is_tag_closer() ) { - $context->rewind_context(); - return; - } - - $value = $tags->get_attribute( 'data' ); - if ( null === $value ) { - // No wp-context directive. - return; - } - - $new_context = json_decode( $value, true ); - // TODO: Error handling. - - $context->set_context( $new_context ); -} diff --git a/src/directives/wp-process-directives.php b/src/directives/wp-process-directives.php index fe03ebfb..c465d6ba 100644 --- a/src/directives/wp-process-directives.php +++ b/src/directives/wp-process-directives.php @@ -2,59 +2,54 @@ require_once __DIR__ . '/class-wp-directive-context.php'; -function wp_process_directives( $tags, $prefix, $tag_directives, $attribute_directives ) { +function wp_process_directives( $tags, $prefix, $directives ) { $context = new WP_Directive_Context; $tag_stack = array(); while ( $tags->next_tag( array( 'tag_closers' => 'visit' ) ) ) { $tag_name = strtolower( $tags->get_tag() ); - if ( array_key_exists( $tag_name, $tag_directives ) ) { - call_user_func( $tag_directives[ $tag_name ], $tags, $context ); - } else { - // Components can't have directives (unless we change our mind about this). - // Is this a tag that closes the latest opening tag? - if ( $tags->is_tag_closer() ) { - if ( 0 === count( $tag_stack ) ) { - continue; - } + // Is this a tag that closes the latest opening tag? + if ( $tags->is_tag_closer() ) { + if ( 0 === count( $tag_stack ) ) { + continue; + } - list( $latest_opening_tag_name, $attributes ) = end( $tag_stack ); - if ( $latest_opening_tag_name === $tag_name ) { - array_pop( $tag_stack ); + list( $latest_opening_tag_name, $attributes ) = end( $tag_stack ); + if ( $latest_opening_tag_name === $tag_name ) { + array_pop( $tag_stack ); - // If the matching opening tag didn't have any attribute directives, - // we move on. - if ( 0 === count( $attributes ) ) { - continue; - } + // If the matching opening tag didn't have any attribute directives, + // we move on. + if ( 0 === count( $attributes ) ) { + continue; } - } else { - // Helper that removes the part after the colon before looking - // for the directive processor inside `$attribute_directives`. - $get_directive_type = function ( $attr ) { - return strtok( $attr, ':' ); - }; + } + } else { + // Helper that removes the part after the colon before looking + // for the directive processor inside `$attribute_directives`. + $get_directive_type = function ( $attr ) { + return strtok( $attr, ':' ); + }; - $attributes = $tags->get_attribute_names_with_prefix( $prefix ); - $attributes = array_map( $get_directive_type, $attributes ); - $attributes = array_intersect( $attributes, array_keys( $attribute_directives ) ); + $attributes = $tags->get_attribute_names_with_prefix( $prefix ); + $attributes = array_map( $get_directive_type, $attributes ); + $attributes = array_intersect( $attributes, array_keys( $directives ) ); - // If this is an open tag, and if it either has attribute directives, - // or if we're inside a tag that does, take note of this tag and its attribute - // directives so we can call its directive processor once we encounter the - // matching closing tag. - if ( - ! is_html_void_element( $tags->get_tag() ) && - ( 0 !== count( $attributes ) || 0 !== count( $tag_stack ) ) - ) { - $tag_stack[] = array( $tag_name, $attributes ); - } + // If this is an open tag, and if it either has attribute directives, + // or if we're inside a tag that does, take note of this tag and its attribute + // directives so we can call its directive processor once we encounter the + // matching closing tag. + if ( + ! is_html_void_element( $tags->get_tag() ) && + ( 0 !== count( $attributes ) || 0 !== count( $tag_stack ) ) + ) { + $tag_stack[] = array( $tag_name, $attributes ); } + } - foreach ( $attributes as $attribute ) { - call_user_func( $attribute_directives[ $attribute ], $tags, $context ); - } + foreach ( $attributes as $attribute ) { + call_user_func( $directives[ $attribute ], $tags, $context ); } } diff --git a/wp-directives.php b/wp-directives.php index 44529caf..a3a856ba 100644 --- a/wp-directives.php +++ b/wp-directives.php @@ -212,19 +212,15 @@ function bhe_inner_blocks( $parsed_block, $source_block, $parent_block ) { function process_directives_in_block( $block_content ) { // TODO: Add some directive/components registration mechanism. - $tag_directives = array( - 'wp-context' => 'process_wp_context_tag', - ); - - $attribute_directives = array( - 'wp-context' => 'process_wp_context_attribute', + $directives = array( + 'wp-context' => 'process_wp_context', 'wp-bind' => 'process_wp_bind', 'wp-class' => 'process_wp_class', 'wp-style' => 'process_wp_style', ); $tags = new WP_HTML_Tag_Processor( $block_content ); - $tags = wp_process_directives( $tags, 'wp-', $tag_directives, $attribute_directives ); + $tags = wp_process_directives( $tags, 'wp-', $directives ); return $tags->get_updated_html(); } add_filter(