This repository was archived by the owner on Jul 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
SSR: Add wp-html and wp-text attribute directive processors
#170
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| <?php | ||
| /** | ||
| * data-wp-html tag directive test. | ||
| */ | ||
|
|
||
| require_once __DIR__ . '/../../../src/directives/attributes/wp-html.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 data-wp-html tag directive. | ||
| * | ||
| * @group directives | ||
| * @covers process_wp_html | ||
| */ | ||
| class Tests_Directives_WpHtml extends WP_UnitTestCase { | ||
| public function test_directive_sets_inner_html_based_on_attribute_value_and_retains_html() { | ||
| $markup = '<div data-wp-html="context.myblock.someHtml"></div>'; | ||
|
|
||
| $tags = new WP_Directive_Processor( $markup ); | ||
| $tags->next_tag(); | ||
|
|
||
| $context_before = new WP_Directive_Context( array( 'myblock' => array( 'someHtml' => 'Lorem <em>ipsum</em> dolor sit.' ) ) ); | ||
| $context = clone $context_before; | ||
| process_wp_html( $tags, $context ); | ||
|
|
||
| $expected_markup = '<div data-wp-html="context.myblock.someHtml">Lorem <em>ipsum</em> dolor sit.</div>'; | ||
| $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 = '<div data-wp-html="context.myblock.someHtml">Lorem ipsum dolor sit.</div>'; | ||
|
|
||
| $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 = '<div data-wp-html="context.myblock.someHtml">Honi soit qui mal y pense.</div>'; | ||
| $this->assertSame( $expected_markup, $tags->get_updated_html() ); | ||
| $this->assertSame( $context_before->get_context(), $context->get_context(), 'data-wp-html directive changed context' ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| <?php | ||
| /** | ||
| * data-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 data-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_and_escapes_html() { | ||
| $markup = '<div data-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' => 'The HTML tag <br> produces a line break.' ) ) ); | ||
| $context = clone $context_before; | ||
| process_wp_text( $tags, $context ); | ||
|
|
||
| $expected_markup = '<div data-wp-text="context.myblock.someText">The HTML tag <br> produces a line break.</div>'; | ||
| $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 = '<div data-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 data-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(), 'data-wp-text directive changed context' ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?php | ||
|
|
||
| require_once __DIR__ . '/../utils.php'; | ||
|
|
||
| function process_wp_html( $tags, $context ) { | ||
| if ( $tags->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 ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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( 'data-wp-text' ); | ||
| if ( null === $value ) { | ||
| return; | ||
| } | ||
|
|
||
| $text = evaluate( $value, $context->get_context() ); | ||
| $tags->set_inner_html( esc_html( $text ) ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.