From ddb3c06a7f4fa157d2ef9a78420ecefb985ad34a Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 6 Mar 2023 16:45:28 +0100 Subject: [PATCH 1/5] Add wp-text directive processor --- phpunit/directives/attributes/wp-text.php | 49 +++++++++++++++++++++++ src/directives/attributes/wp-text.php | 17 ++++++++ wp-directives.php | 2 + 3 files changed, 68 insertions(+) create mode 100644 phpunit/directives/attributes/wp-text.php create mode 100644 src/directives/attributes/wp-text.php diff --git a/phpunit/directives/attributes/wp-text.php b/phpunit/directives/attributes/wp-text.php new file mode 100644 index 00000000..54d39495 --- /dev/null +++ b/phpunit/directives/attributes/wp-text.php @@ -0,0 +1,49 @@ +'; + + $tags = new WP_Directive_Processor( $markup ); + $tags->next_tag(); + + $context_before = new WP_Directive_Context( array( 'myblock' => array( 'someText' => 'Lorem ipsum dolor sit.' ) ) ); + $context = clone $context_before; + process_wp_text( $tags, $context ); + + $expected_markup = '
Lorem ipsum dolor sit.
'; + $this->assertSame( $expected_markup, $tags->get_updated_html() ); + $this->assertSame( $context_before->get_context(), $context->get_context(), 'wp-text directive changed context' ); + } + + public function test_directive_overwrites_inner_html_based_on_attribute_value() { + $markup = '
Lorem ipsum dolor sit.
'; + + $tags = new WP_Directive_Processor( $markup ); + $tags->next_tag(); + + $context_before = new WP_Directive_Context( array( 'myblock' => array( 'someText' => 'Honi soit qui mal y pense.' ) ) ); + $context = clone $context_before; + process_wp_text( $tags, $context ); + + $expected_markup = '
Honi soit qui mal y pense.
'; + $this->assertSame( $expected_markup, $tags->get_updated_html() ); + $this->assertSame( $context_before->get_context(), $context->get_context(), 'wp-text directive changed context' ); + } +} diff --git a/src/directives/attributes/wp-text.php b/src/directives/attributes/wp-text.php new file mode 100644 index 00000000..5c2f701c --- /dev/null +++ b/src/directives/attributes/wp-text.php @@ -0,0 +1,17 @@ +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 ); +} diff --git a/wp-directives.php b/wp-directives.php index edd2b071..79173c6b 100644 --- a/wp-directives.php +++ b/wp-directives.php @@ -45,6 +45,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-style.php'; +require_once __DIR__ . '/src/directives/attributes/wp-text.php'; function wp_directives_loader() { // Load the Admin page. @@ -217,6 +218,7 @@ function process_directives_in_block( $block_content ) { 'data-wp-bind' => 'process_wp_bind', 'data-wp-class' => 'process_wp_class', 'data-wp-style' => 'process_wp_style', + 'data-wp-text' => 'process_wp_text', ); $tags = new WP_HTML_Tag_Processor( $block_content ); From 4d85afaab8675678ce61772ee740745c09abdaea Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 14 Mar 2023 16:03:47 +0100 Subject: [PATCH 2/5] Add wp-html directive and unit test --- phpunit/directives/attributes/wp-html.php | 49 +++++++++++++++++++++++ src/directives/attributes/wp-html.php | 17 ++++++++ wp-directives.php | 1 + 3 files changed, 67 insertions(+) create mode 100644 phpunit/directives/attributes/wp-html.php create mode 100644 src/directives/attributes/wp-html.php diff --git a/phpunit/directives/attributes/wp-html.php b/phpunit/directives/attributes/wp-html.php new file mode 100644 index 00000000..6962c480 --- /dev/null +++ b/phpunit/directives/attributes/wp-html.php @@ -0,0 +1,49 @@ +'; + + $tags = new WP_Directive_Processor( $markup ); + $tags->next_tag(); + + $context_before = new WP_Directive_Context( array( 'myblock' => array( 'someHtml' => 'Lorem ipsum dolor sit.' ) ) ); + $context = clone $context_before; + process_wp_html( $tags, $context ); + + $expected_markup = '
Lorem ipsum dolor sit.
'; + $this->assertSame( $expected_markup, $tags->get_updated_html() ); + $this->assertSame( $context_before->get_context(), $context->get_context(), 'wp-html directive changed context' ); + } + + public function test_directive_overwrites_inner_html_based_on_attribute_value() { + $markup = '
Lorem ipsum dolor sit.
'; + + $tags = new WP_Directive_Processor( $markup ); + $tags->next_tag(); + + $context_before = new WP_Directive_Context( array( 'myblock' => array( 'someHtml' => 'Honi soit qui mal y pense.' ) ) ); + $context = clone $context_before; + process_wp_html( $tags, $context ); + + $expected_markup = '
Honi soit qui mal y pense.
'; + $this->assertSame( $expected_markup, $tags->get_updated_html() ); + $this->assertSame( $context_before->get_context(), $context->get_context(), 'wp-html directive changed context' ); + } +} diff --git a/src/directives/attributes/wp-html.php b/src/directives/attributes/wp-html.php new file mode 100644 index 00000000..b894dd75 --- /dev/null +++ b/src/directives/attributes/wp-html.php @@ -0,0 +1,17 @@ +is_tag_closer() ) { + return; + } + + $value = $tags->get_attribute( 'wp-html' ); + if ( null === $value ) { + return; + } + + $text = evaluate( $value, $context->get_context() ); + $tags->set_inner_html( $text ); +} diff --git a/wp-directives.php b/wp-directives.php index 79173c6b..cfdfd870 100644 --- a/wp-directives.php +++ b/wp-directives.php @@ -217,6 +217,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-html' => 'process_wp_html', 'data-wp-style' => 'process_wp_style', 'data-wp-text' => 'process_wp_text', ); From dbb38e12a3c9e88b3df399fb84d588c163eeedc8 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 14 Mar 2023 16:06:35 +0100 Subject: [PATCH 3/5] Add escaping to wp-text --- phpunit/directives/attributes/wp-text.php | 6 +++--- src/directives/attributes/wp-text.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/phpunit/directives/attributes/wp-text.php b/phpunit/directives/attributes/wp-text.php index 54d39495..56f5fb75 100644 --- a/phpunit/directives/attributes/wp-text.php +++ b/phpunit/directives/attributes/wp-text.php @@ -17,17 +17,17 @@ * @covers process_wp_text */ class Tests_Directives_WpText extends WP_UnitTestCase { - public function test_directive_sets_inner_html_based_on_attribute_value() { + public function test_directive_sets_inner_html_based_on_attribute_value_and_escapes_html() { $markup = '
'; $tags = new WP_Directive_Processor( $markup ); $tags->next_tag(); - $context_before = new WP_Directive_Context( array( 'myblock' => array( 'someText' => 'Lorem ipsum dolor sit.' ) ) ); + $context_before = new WP_Directive_Context( array( 'myblock' => array( 'someText' => 'The HTML tag
produces a line break.' ) ) ); $context = clone $context_before; process_wp_text( $tags, $context ); - $expected_markup = '
Lorem ipsum dolor sit.
'; + $expected_markup = '
The HTML tag <br> produces a line break.
'; $this->assertSame( $expected_markup, $tags->get_updated_html() ); $this->assertSame( $context_before->get_context(), $context->get_context(), 'wp-text directive changed context' ); } diff --git a/src/directives/attributes/wp-text.php b/src/directives/attributes/wp-text.php index 5c2f701c..30eb3b45 100644 --- a/src/directives/attributes/wp-text.php +++ b/src/directives/attributes/wp-text.php @@ -13,5 +13,5 @@ function process_wp_text( $tags, $context ) { } $text = evaluate( $value, $context->get_context() ); - $tags->set_inner_html( $text ); + $tags->set_inner_html( esc_html( $text ) ); } From e93bdca0432703a1161cb60a1d35a73dab4c6fa4 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 15 Mar 2023 17:21:26 +0100 Subject: [PATCH 4/5] Add wp-html import --- wp-directives.php | 1 + 1 file changed, 1 insertion(+) diff --git a/wp-directives.php b/wp-directives.php index cfdfd870..41e89d86 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-html.php'; require_once __DIR__ . '/src/directives/attributes/wp-style.php'; require_once __DIR__ . '/src/directives/attributes/wp-text.php'; From 544f226ce9658dddffa7b78c9d1091d6d05f8be8 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 20 Mar 2023 13:54:45 +0100 Subject: [PATCH 5/5] Add data- prefix to directives --- phpunit/directives/attributes/wp-html.php | 16 ++++++++-------- phpunit/directives/attributes/wp-text.php | 16 ++++++++-------- src/directives/attributes/wp-html.php | 2 +- src/directives/attributes/wp-text.php | 2 +- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/phpunit/directives/attributes/wp-html.php b/phpunit/directives/attributes/wp-html.php index 6962c480..6f33e04d 100644 --- a/phpunit/directives/attributes/wp-html.php +++ b/phpunit/directives/attributes/wp-html.php @@ -1,6 +1,6 @@ '; + $markup = '
'; $tags = new WP_Directive_Processor( $markup ); $tags->next_tag(); @@ -27,13 +27,13 @@ public function test_directive_sets_inner_html_based_on_attribute_value_and_reta $context = clone $context_before; process_wp_html( $tags, $context ); - $expected_markup = '
Lorem ipsum dolor sit.
'; + $expected_markup = '
Lorem ipsum dolor sit.
'; $this->assertSame( $expected_markup, $tags->get_updated_html() ); - $this->assertSame( $context_before->get_context(), $context->get_context(), 'wp-html directive changed context' ); + $this->assertSame( $context_before->get_context(), $context->get_context(), 'data-wp-html directive changed context' ); } public function test_directive_overwrites_inner_html_based_on_attribute_value() { - $markup = '
Lorem ipsum dolor sit.
'; + $markup = '
Lorem ipsum dolor sit.
'; $tags = new WP_Directive_Processor( $markup ); $tags->next_tag(); @@ -42,8 +42,8 @@ public function test_directive_overwrites_inner_html_based_on_attribute_value() $context = clone $context_before; process_wp_html( $tags, $context ); - $expected_markup = '
Honi soit qui mal y pense.
'; + $expected_markup = '
Honi soit qui mal y pense.
'; $this->assertSame( $expected_markup, $tags->get_updated_html() ); - $this->assertSame( $context_before->get_context(), $context->get_context(), 'wp-html directive changed context' ); + $this->assertSame( $context_before->get_context(), $context->get_context(), 'data-wp-html directive changed context' ); } } diff --git a/phpunit/directives/attributes/wp-text.php b/phpunit/directives/attributes/wp-text.php index 56f5fb75..1675b9ee 100644 --- a/phpunit/directives/attributes/wp-text.php +++ b/phpunit/directives/attributes/wp-text.php @@ -1,6 +1,6 @@ '; + $markup = '
'; $tags = new WP_Directive_Processor( $markup ); $tags->next_tag(); @@ -27,13 +27,13 @@ public function test_directive_sets_inner_html_based_on_attribute_value_and_esca $context = clone $context_before; process_wp_text( $tags, $context ); - $expected_markup = '
The HTML tag <br> produces a line break.
'; + $expected_markup = '
The HTML tag <br> produces a line break.
'; $this->assertSame( $expected_markup, $tags->get_updated_html() ); - $this->assertSame( $context_before->get_context(), $context->get_context(), 'wp-text directive changed context' ); + $this->assertSame( $context_before->get_context(), $context->get_context(), 'data-wp-text directive changed context' ); } public function test_directive_overwrites_inner_html_based_on_attribute_value() { - $markup = '
Lorem ipsum dolor sit.
'; + $markup = '
Lorem ipsum dolor sit.
'; $tags = new WP_Directive_Processor( $markup ); $tags->next_tag(); @@ -42,8 +42,8 @@ public function test_directive_overwrites_inner_html_based_on_attribute_value() $context = clone $context_before; process_wp_text( $tags, $context ); - $expected_markup = '
Honi soit qui mal y pense.
'; + $expected_markup = '
Honi soit qui mal y pense.
'; $this->assertSame( $expected_markup, $tags->get_updated_html() ); - $this->assertSame( $context_before->get_context(), $context->get_context(), 'wp-text directive changed context' ); + $this->assertSame( $context_before->get_context(), $context->get_context(), 'data-wp-text directive changed context' ); } } diff --git a/src/directives/attributes/wp-html.php b/src/directives/attributes/wp-html.php index b894dd75..56bda8b4 100644 --- a/src/directives/attributes/wp-html.php +++ b/src/directives/attributes/wp-html.php @@ -7,7 +7,7 @@ function process_wp_html( $tags, $context ) { return; } - $value = $tags->get_attribute( 'wp-html' ); + $value = $tags->get_attribute( 'data-wp-html' ); if ( null === $value ) { return; } diff --git a/src/directives/attributes/wp-text.php b/src/directives/attributes/wp-text.php index 30eb3b45..b2ecf6dc 100644 --- a/src/directives/attributes/wp-text.php +++ b/src/directives/attributes/wp-text.php @@ -7,7 +7,7 @@ function process_wp_text( $tags, $context ) { return; } - $value = $tags->get_attribute( 'wp-text' ); + $value = $tags->get_attribute( 'data-wp-text' ); if ( null === $value ) { return; }