diff --git a/phpunit/directives/attributes/wp-html.php b/phpunit/directives/attributes/wp-html.php new file mode 100644 index 00000000..6f33e04d --- /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(), 'data-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(), 'data-wp-html directive changed context' ); + } +} diff --git a/phpunit/directives/attributes/wp-text.php b/phpunit/directives/attributes/wp-text.php new file mode 100644 index 00000000..1675b9ee --- /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' => 'The HTML tag
produces a line break.' ) ) ); + $context = clone $context_before; + process_wp_text( $tags, $context ); + + $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(), 'data-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(), 'data-wp-text 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..56bda8b4 --- /dev/null +++ b/src/directives/attributes/wp-html.php @@ -0,0 +1,17 @@ +is_tag_closer() ) { + return; + } + + $value = $tags->get_attribute( 'data-wp-html' ); + if ( null === $value ) { + return; + } + + $text = evaluate( $value, $context->get_context() ); + $tags->set_inner_html( $text ); +} diff --git a/src/directives/attributes/wp-text.php b/src/directives/attributes/wp-text.php new file mode 100644 index 00000000..b2ecf6dc --- /dev/null +++ b/src/directives/attributes/wp-text.php @@ -0,0 +1,17 @@ +is_tag_closer() ) { + return; + } + + $value = $tags->get_attribute( 'data-wp-text' ); + if ( null === $value ) { + return; + } + + $text = evaluate( $value, $context->get_context() ); + $tags->set_inner_html( esc_html( $text ) ); +} diff --git a/wp-directives.php b/wp-directives.php index edd2b071..41e89d86 100644 --- a/wp-directives.php +++ b/wp-directives.php @@ -44,7 +44,9 @@ 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'; function wp_directives_loader() { // Load the Admin page. @@ -216,7 +218,9 @@ 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', ); $tags = new WP_HTML_Tag_Processor( $block_content );