From 7a166e669c307ec7bc7bc9b22bba9a16939c43f8 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 1 Mar 2023 18:06:36 +0100 Subject: [PATCH 01/14] SSR: Add basic wp-each test coverage --- phpunit/directives/attributes/wp-each.php | 108 ++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 phpunit/directives/attributes/wp-each.php diff --git a/phpunit/directives/attributes/wp-each.php b/phpunit/directives/attributes/wp-each.php new file mode 100644 index 00000000..c4666e58 --- /dev/null +++ b/phpunit/directives/attributes/wp-each.php @@ -0,0 +1,108 @@ + array( + array( + 'id' => 123, + 'label' => 'foo', + ), + array( + 'id' => 456, + 'label' => 'bar', + ), + array( + 'id' => 789, + 'label' => 'foobar', + ), + ), + ) + ); + + $markup = << + + + + + + + + + + + + + +EOF; + $tags = new WP_HTML_Tag_Processor( $markup ); + $tags->next_tag(); // table + $tags->next_tag(); // tbody + + process_wp_each( $tags, $context ); + + $updated_markup = << + + + 123 + + foo + + + + + + + + + + + + + + + + + +
456 + bar + + +
+ + + + + + + + + +
789 + foobar + + +
+EOF; + + $this->assertSame( $updated_markup, $tags->get_updated_html() ); + } +} From 520ff9215564b1fa469b16f5b19eab0f28953277 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 1 Mar 2023 19:04:11 +0100 Subject: [PATCH 02/14] Add pseudo-code-ish wp-each implementation --- src/directives/attributes/wp-each.php | 40 +++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/directives/attributes/wp-each.php diff --git a/src/directives/attributes/wp-each.php b/src/directives/attributes/wp-each.php new file mode 100644 index 00000000..a11ce5bd --- /dev/null +++ b/src/directives/attributes/wp-each.php @@ -0,0 +1,40 @@ +is_tag_closer() ) { + return; + } + + $prefixed_attributes = $tags->get_attribute_names_with_prefix( 'wp-each:' ); + if ( 0 === count( $prefixed_attributes ) ) { + return; + } + $attribute_name = $prefixed_attributes[0]; + + list( , $iterator_name ) = explode( ':', $attribute_name ); + + $value = $tags->get_attribute( $attribute_name ); + if ( null === $value ) { + // No wp-each directive. + return; + } + + $loop_array = evaluate( $value, $context->get_context() ); + // TODO: Error handling. + + $loop_inner_html = ''; + foreach ( $loop_array as $iteration_item ) { + $context->set_context( array( 'item' => $iteration_item ) ); + + $inner_html = $tags->get_inner_html(); + $inner_tags = new WP_Directive_Processor( $inner_html ); + $inner_tags = wp_process_directives( $inner_tags, $context ); + $loop_inner_html .= $inner_tags->get_inner_html(); + + $context->rewind_context(); + } + $tags->set_inner_html( $loop_inner_html ); + $tags->find_balanced_tag(); +} From 6b02c3c39ab98138d66a902359f78105dbcdf0f3 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 2 Mar 2023 11:25:26 +0100 Subject: [PATCH 03/14] Fix import path --- src/directives/attributes/wp-each.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/directives/attributes/wp-each.php b/src/directives/attributes/wp-each.php index a11ce5bd..2fbac63a 100644 --- a/src/directives/attributes/wp-each.php +++ b/src/directives/attributes/wp-each.php @@ -1,6 +1,6 @@ is_tag_closer() ) { From 49778d0165ea137b5e2275ba725f1fa5faed8b53 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 2 Mar 2023 11:40:06 +0100 Subject: [PATCH 04/14] Use Directive Processor in unit test --- phpunit/directives/attributes/wp-each.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpunit/directives/attributes/wp-each.php b/phpunit/directives/attributes/wp-each.php index c4666e58..4c7903f0 100644 --- a/phpunit/directives/attributes/wp-each.php +++ b/phpunit/directives/attributes/wp-each.php @@ -7,7 +7,7 @@ require_once __DIR__ . '/../../../src/directives/class-wp-directive-context.php'; -require_once __DIR__ . '/../../../src/directives/wp-html.php'; +require_once __DIR__ . '/../../../src/class-wp-directive-processor.php'; /** * Tests for the wp-context attribute directive. @@ -52,7 +52,7 @@ public function test_directive_expands_correctly() { EOF; - $tags = new WP_HTML_Tag_Processor( $markup ); + $tags = new WP_Directive_Processor( $markup ); $tags->next_tag(); // table $tags->next_tag(); // tbody From e643bccef016623de8ef8d55d0fdf6097a7b0238 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 9 Mar 2023 21:59:38 +0100 Subject: [PATCH 05/14] Fix import paths --- phpunit/directives/attributes/wp-each.php | 2 +- src/directives/attributes/wp-each.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/phpunit/directives/attributes/wp-each.php b/phpunit/directives/attributes/wp-each.php index 4c7903f0..36954589 100644 --- a/phpunit/directives/attributes/wp-each.php +++ b/phpunit/directives/attributes/wp-each.php @@ -7,7 +7,7 @@ require_once __DIR__ . '/../../../src/directives/class-wp-directive-context.php'; -require_once __DIR__ . '/../../../src/class-wp-directive-processor.php'; +require_once __DIR__ . '/../../../src/directives/class-wp-directive-processor.php'; /** * Tests for the wp-context attribute directive. diff --git a/src/directives/attributes/wp-each.php b/src/directives/attributes/wp-each.php index 2fbac63a..a11ce5bd 100644 --- a/src/directives/attributes/wp-each.php +++ b/src/directives/attributes/wp-each.php @@ -1,6 +1,6 @@ is_tag_closer() ) { From f9dfe9d081ff89f12f2ffc75815bbee7efa2fe89 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 13 Mar 2023 15:33:02 +0100 Subject: [PATCH 06/14] s/find_balanced_tag/next_balanced_closer/g --- src/directives/attributes/wp-each.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/directives/attributes/wp-each.php b/src/directives/attributes/wp-each.php index a11ce5bd..e14c6b3f 100644 --- a/src/directives/attributes/wp-each.php +++ b/src/directives/attributes/wp-each.php @@ -36,5 +36,5 @@ function process_wp_each( $tags, $context ) { $context->rewind_context(); } $tags->set_inner_html( $loop_inner_html ); - $tags->find_balanced_tag(); + $tags->next_balanced_closer(); } From f88648752ff313b1e3e434eb02d545cb748eff47 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 13 Mar 2023 15:33:33 +0100 Subject: [PATCH 07/14] Pass directives array to directive to enable recursion --- phpunit/directives/attributes/wp-each.php | 18 +++++++++++++++++- src/directives/attributes/wp-each.php | 4 ++-- src/directives/wp-process-directives.php | 2 +- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/phpunit/directives/attributes/wp-each.php b/phpunit/directives/attributes/wp-each.php index 36954589..9f563287 100644 --- a/phpunit/directives/attributes/wp-each.php +++ b/phpunit/directives/attributes/wp-each.php @@ -17,6 +17,22 @@ */ class Tests_Directives_Attributes_WpEach extends WP_UnitTestCase { public function test_directive_expands_correctly() { + $directives = array( + 'wp-text' => function( $tags, $context ) { + if ( $tags->is_tag_closer() ) { + return; + } + + $value = $tags->get_attribute( 'wp-text' ); + if ( null === $value ) { + return; + } + + $text = evaluate( $value, $context->get_context() ); + $tags->set_inner_html( $text ); + }, + ); + $context = new WP_Directive_Context( array( 'data' => array( @@ -56,7 +72,7 @@ public function test_directive_expands_correctly() { $tags->next_tag(); // table $tags->next_tag(); // tbody - process_wp_each( $tags, $context ); + process_wp_each( $tags, $context, $directives ); $updated_markup = << diff --git a/src/directives/attributes/wp-each.php b/src/directives/attributes/wp-each.php index e14c6b3f..9e5bf32e 100644 --- a/src/directives/attributes/wp-each.php +++ b/src/directives/attributes/wp-each.php @@ -2,7 +2,7 @@ require_once __DIR__ . '/../class-wp-directive-processor.php'; -function process_wp_each( $tags, $context ) { +function process_wp_each( $tags, $context, $directives ) { if ( $tags->is_tag_closer() ) { return; } @@ -30,7 +30,7 @@ function process_wp_each( $tags, $context ) { $inner_html = $tags->get_inner_html(); $inner_tags = new WP_Directive_Processor( $inner_html ); - $inner_tags = wp_process_directives( $inner_tags, $context ); + $inner_tags = wp_process_directives( $inner_tags, 'wp-', $directives ); $loop_inner_html .= $inner_tags->get_inner_html(); $context->rewind_context(); diff --git a/src/directives/wp-process-directives.php b/src/directives/wp-process-directives.php index c8746e8e..05661971 100644 --- a/src/directives/wp-process-directives.php +++ b/src/directives/wp-process-directives.php @@ -50,7 +50,7 @@ function wp_process_directives( $tags, $prefix, $directives ) { } foreach ( $attributes as $attribute ) { - call_user_func( $directives[ $attribute ], $tags, $context ); + call_user_func( $directives[ $attribute ], $tags, $context, $directives ); } } From 39d505464b04f42ff721317c57db11bb6f75e4c2 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 13 Mar 2023 15:52:24 +0100 Subject: [PATCH 08/14] Pass context --- src/directives/attributes/wp-each.php | 3 +-- src/directives/wp-process-directives.php | 6 ++++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/directives/attributes/wp-each.php b/src/directives/attributes/wp-each.php index 9e5bf32e..b0b0472f 100644 --- a/src/directives/attributes/wp-each.php +++ b/src/directives/attributes/wp-each.php @@ -30,9 +30,8 @@ function process_wp_each( $tags, $context, $directives ) { $inner_html = $tags->get_inner_html(); $inner_tags = new WP_Directive_Processor( $inner_html ); - $inner_tags = wp_process_directives( $inner_tags, 'wp-', $directives ); + $inner_tags = wp_process_directives( $inner_tags, 'wp-', $directives, $context ); $loop_inner_html .= $inner_tags->get_inner_html(); - $context->rewind_context(); } $tags->set_inner_html( $loop_inner_html ); diff --git a/src/directives/wp-process-directives.php b/src/directives/wp-process-directives.php index 05661971..c024f978 100644 --- a/src/directives/wp-process-directives.php +++ b/src/directives/wp-process-directives.php @@ -3,8 +3,10 @@ require_once __DIR__ . '/class-wp-directive-context.php'; require_once __DIR__ . '/class-wp-directive-processor.php'; -function wp_process_directives( $tags, $prefix, $directives ) { - $context = new WP_Directive_Context; +function wp_process_directives( $tags, $prefix, $directives, $context ) { + if ( ! $context ) { + $context = new WP_Directive_Context; + } $tag_stack = array(); while ( $tags->next_tag( array( 'tag_closers' => 'visit' ) ) ) { From bcce63f888fb30d3186cb9a05a97955676c14c45 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 13 Mar 2023 15:52:50 +0100 Subject: [PATCH 09/14] Get updated rather than inner HTML --- src/directives/attributes/wp-each.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/directives/attributes/wp-each.php b/src/directives/attributes/wp-each.php index b0b0472f..2dfe397b 100644 --- a/src/directives/attributes/wp-each.php +++ b/src/directives/attributes/wp-each.php @@ -31,7 +31,7 @@ function process_wp_each( $tags, $context, $directives ) { $inner_html = $tags->get_inner_html(); $inner_tags = new WP_Directive_Processor( $inner_html ); $inner_tags = wp_process_directives( $inner_tags, 'wp-', $directives, $context ); - $loop_inner_html .= $inner_tags->get_inner_html(); + $loop_inner_html .= $inner_tags->get_updated_html(); $context->rewind_context(); } $tags->set_inner_html( $loop_inner_html ); From 3ccc3fb8c2e3efe1466baf4146c1e21dd162189e Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 13 Mar 2023 15:56:27 +0100 Subject: [PATCH 10/14] Fix expected value --- phpunit/directives/attributes/wp-each.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/phpunit/directives/attributes/wp-each.php b/phpunit/directives/attributes/wp-each.php index 9f563287..b93c9467 100644 --- a/phpunit/directives/attributes/wp-each.php +++ b/phpunit/directives/attributes/wp-each.php @@ -87,10 +87,7 @@ public function test_directive_expands_correctly() { - - - - + - -
456 @@ -101,10 +98,7 @@ public function test_directive_expands_correctly() {
- - + - - + + + - - + + + - - + + - - + +
789 From fefb87497eb94226e731c04dceeb32acd9ded265 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 13 Mar 2023 17:44:24 +0100 Subject: [PATCH 11/14] Default context to null --- src/directives/wp-process-directives.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/directives/wp-process-directives.php b/src/directives/wp-process-directives.php index c024f978..3f7a5588 100644 --- a/src/directives/wp-process-directives.php +++ b/src/directives/wp-process-directives.php @@ -3,7 +3,7 @@ require_once __DIR__ . '/class-wp-directive-context.php'; require_once __DIR__ . '/class-wp-directive-processor.php'; -function wp_process_directives( $tags, $prefix, $directives, $context ) { +function wp_process_directives( $tags, $prefix, $directives, $context = null ) { if ( ! $context ) { $context = new WP_Directive_Context; } From 70afd836d694e0a4c2328f25637549854d1f8bd0 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 13 Mar 2023 17:53:24 +0100 Subject: [PATCH 12/14] Fix test failure (needs tweaking) --- phpunit/directives/attributes/wp-each.php | 1 + 1 file changed, 1 insertion(+) diff --git a/phpunit/directives/attributes/wp-each.php b/phpunit/directives/attributes/wp-each.php index b93c9467..6ab6ac66 100644 --- a/phpunit/directives/attributes/wp-each.php +++ b/phpunit/directives/attributes/wp-each.php @@ -30,6 +30,7 @@ public function test_directive_expands_correctly() { $text = evaluate( $value, $context->get_context() ); $tags->set_inner_html( $text ); + $tags->get_updated_html(); // FIXME: We shouldn't need to do this here. }, ); From 9713a02f05e9bec3d7612f777ea29166f74c28d7 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 20 Mar 2023 14:12:06 +0100 Subject: [PATCH 13/14] Add data- prefix --- phpunit/directives/attributes/wp-each.php | 36 +++++++++++------------ src/directives/attributes/wp-each.php | 4 +-- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/phpunit/directives/attributes/wp-each.php b/phpunit/directives/attributes/wp-each.php index 6ab6ac66..8ba2a84a 100644 --- a/phpunit/directives/attributes/wp-each.php +++ b/phpunit/directives/attributes/wp-each.php @@ -1,6 +1,6 @@ function( $tags, $context ) { + 'data-wp-text' => function( $tags, $context ) { if ( $tags->is_tag_closer() ) { return; } - $value = $tags->get_attribute( 'wp-text' ); + $value = $tags->get_attribute( 'data-wp-text' ); if ( null === $value ) { return; } @@ -55,11 +55,11 @@ public function test_directive_expands_correctly() { $markup = << -
- + @@ -77,11 +77,11 @@ public function test_directive_expands_correctly() { $updated_markup = << -
123
123 - foo + foo @@ -89,10 +89,10 @@ public function test_directive_expands_correctly() {
456
456 - bar + bar @@ -100,10 +100,10 @@ public function test_directive_expands_correctly() {
789
789 - foobar + foobar diff --git a/src/directives/attributes/wp-each.php b/src/directives/attributes/wp-each.php index 2dfe397b..a65610bf 100644 --- a/src/directives/attributes/wp-each.php +++ b/src/directives/attributes/wp-each.php @@ -7,7 +7,7 @@ function process_wp_each( $tags, $context, $directives ) { return; } - $prefixed_attributes = $tags->get_attribute_names_with_prefix( 'wp-each:' ); + $prefixed_attributes = $tags->get_attribute_names_with_prefix( 'data-wp-each:' ); if ( 0 === count( $prefixed_attributes ) ) { return; } @@ -30,7 +30,7 @@ function process_wp_each( $tags, $context, $directives ) { $inner_html = $tags->get_inner_html(); $inner_tags = new WP_Directive_Processor( $inner_html ); - $inner_tags = wp_process_directives( $inner_tags, 'wp-', $directives, $context ); + $inner_tags = wp_process_directives( $inner_tags, 'data-wp-', $directives, $context ); $loop_inner_html .= $inner_tags->get_updated_html(); $context->rewind_context(); } From d800ef5cabd4e65a8ac5f65f4f30f82b012a7054 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 20 Mar 2023 14:12:42 +0100 Subject: [PATCH 14/14] Add to wp-directives.php --- wp-directives.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/wp-directives.php b/wp-directives.php index d54a17ee..04bc3921 100644 --- a/wp-directives.php +++ b/wp-directives.php @@ -44,6 +44,7 @@ function () { require_once __DIR__ . '/src/directives/attributes/wp-bind.php'; 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-each.php'; require_once __DIR__ . '/src/directives/attributes/wp-html.php'; require_once __DIR__ . '/src/directives/attributes/wp-style.php'; require_once __DIR__ . '/src/directives/attributes/wp-text.php'; @@ -218,6 +219,7 @@ function process_directives_in_block( $block_content ) { 'data-wp-context' => 'process_wp_context', 'data-wp-bind' => 'process_wp_bind', 'data-wp-class' => 'process_wp_class', + 'data-wp-each' => 'process_wp_each', 'data-wp-html' => 'process_wp_html', 'data-wp-style' => 'process_wp_style', 'data-wp-text' => 'process_wp_text',