Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Commit

Permalink
Add wp-text directive processor
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Mar 7, 2023
1 parent bbc790f commit fb1420a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
49 changes: 49 additions & 0 deletions phpunit/directives/attributes/wp-text.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* wp-text tag directive test.
*/

require_once __DIR__ . '/../../../src/directives/attributes/wp-text.php';

require_once __DIR__ . '/../../../src/directives/class-wp-directive-context.php';
require_once __DIR__ . '/../../../src/directives/class-wp-directive-processor.php';

require_once __DIR__ . '/../../../src/directives/wp-html.php';

/**
* Tests for the wp-text tag directive.
*
* @group directives
* @covers process_wp_text
*/
class Tests_Directives_WpText extends WP_UnitTestCase {
public function test_directive_sets_inner_html_based_on_attribute_value() {
$markup = '<div wp-text="context.myblock.someText"></div>';

$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 = '<div wp-text="context.myblock.someText">Lorem ipsum dolor sit.</div>';
$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 = '<div wp-text="context.myblock.someText">Lorem ipsum dolor sit.</div>';

$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 = '<div wp-text="context.myblock.someText">Honi soit qui mal y pense.</div>';
$this->assertSame( $expected_markup, $tags->get_updated_html() );
$this->assertSame( $context_before->get_context(), $context->get_context(), 'wp-text directive changed context' );
}
}
17 changes: 17 additions & 0 deletions src/directives/attributes/wp-text.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

require_once __DIR__ . '/../utils.php';

function process_wp_text( $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 );
}
2 changes: 2 additions & 0 deletions wp-directives.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function () {
require_once __DIR__ . '/src/directives/attributes/wp-bind.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.
Expand Down Expand Up @@ -216,6 +217,7 @@ function process_directives_in_block( $block_content ) {
'wp-bind' => 'process_wp_bind',
'wp-class' => 'process_wp_class',
'wp-style' => 'process_wp_style',
'wp-text' => 'process_wp_text',
);

$tags = new WP_HTML_Tag_Processor( $block_content );
Expand Down

0 comments on commit fb1420a

Please sign in to comment.