Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
9022e45
Add `data-wp-bind` unit test
SantosGuillamot Oct 15, 2025
f42becb
Add `data-wp-class` unit test
SantosGuillamot Oct 15, 2025
a23799c
Add `data-wp-context` unit test
SantosGuillamot Oct 15, 2025
502b075
Add `data-wp-each` unit test
SantosGuillamot Oct 15, 2025
7f2d6e8
Add `data-wp-style` unit tests
SantosGuillamot Oct 15, 2025
f29e52b
Add `data-wp-text` unit tests
SantosGuillamot Oct 15, 2025
123d3d3
Add `parse_directive_name` function
SantosGuillamot Oct 15, 2025
0955b18
Add `test_parse_directive_name` tests
SantosGuillamot Oct 15, 2025
5c56ac9
Replace `extract_prefix_and_suffix` uses
SantosGuillamot Oct 15, 2025
a56cf9d
Ignore unique IDs in `data-wp-bind`
SantosGuillamot Oct 15, 2025
ff91fd6
Ignore unique IDs in `data-wp-style`
SantosGuillamot Oct 15, 2025
f6bcab9
Support in `data-wp-text`
SantosGuillamot Oct 15, 2025
5a7285f
Add logic for `data-wp-each`
SantosGuillamot Oct 15, 2025
0ef2347
Remove regex
SantosGuillamot Oct 15, 2025
3ea876c
Order `data-wp-context`
SantosGuillamot Oct 15, 2025
ae3d74c
Fix `data-wp-class`
SantosGuillamot Oct 16, 2025
1a0f4a9
Change `data-wp-text` class name
SantosGuillamot Oct 16, 2025
60eebfb
Fix invalid directives
SantosGuillamot Oct 16, 2025
c98f8c1
Add `get_directive_entries`
SantosGuillamot Oct 16, 2025
7bb8259
Adapt `evaluate` and directives
SantosGuillamot Oct 16, 2025
dc9e561
Sort entries and adapt `data-wp-context`
SantosGuillamot Oct 16, 2025
f6604dd
Adapt alphabetical order tests
SantosGuillamot Oct 16, 2025
a795490
Adapt `evaluate` tests
SantosGuillamot Oct 16, 2025
af01fea
Add tests for `get_directive_entries`
SantosGuillamot Oct 16, 2025
202f3e3
Change order in `data-wp-context`
SantosGuillamot Oct 16, 2025
a0ce7fb
Update `context_stack` only once
SantosGuillamot Oct 16, 2025
5f55d32
Change `$entry` variable name.
SantosGuillamot Oct 16, 2025
f90496d
Pass whole `entry` to `_doing_it_wrong`
SantosGuillamot Oct 16, 2025
b94c480
Update `evaluate` description
SantosGuillamot Oct 16, 2025
587b37c
Update ticket number
SantosGuillamot Oct 16, 2025
62196b0
Update `data-wp-text` directive
SantosGuillamot Oct 16, 2025
ff7644b
Update `data-wp-text` check
SantosGuillamot Oct 16, 2025
87e5d49
Fix error message
SantosGuillamot Oct 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
277 changes: 178 additions & 99 deletions src/wp-includes/interactivity-api/class-wp-interactivity-api.php

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,22 @@ public function test_wp_bind_handles_true_value() {
list($p) = $this->process_directives( $html );
$this->assertSame( true, $p->get_attribute( 'id' ) );
}

/**
* Tests ignores unique IDs in bind directive.
*
* @ticket 64106
*
* @covers ::process_directives
*/
public function test_wp_bind_ignores_unique_ids() {
$html = '<div data-wp-bind--id="myPlugin::state.trueValue"></div>';
list($p) = $this->process_directives( $html );
$this->assertSame( true, $p->get_attribute( 'id' ) );

$html = '<div data-wp-bind--id---unique-id="myPlugin::state.trueValue"></div>';
list($p) = $this->process_directives( $html );
$this->assertNull( $p->get_attribute( 'id' ) );
$this->assertNull( $p->get_attribute( 'id---unique-id' ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function test_wp_class_sets_multiple_class_names() {
data-wp-class--other-class="myPlugin::state.true"
>Text</div>';
list($p) = $this->process_directives( $html );
$this->assertSame( 'some-class other-class', $p->get_attribute( 'class' ) );
$this->assertSame( 'other-class some-class', $p->get_attribute( 'class' ) );
}

/**
Expand Down Expand Up @@ -328,4 +328,25 @@ public function test_wp_class_sets_class_name_on_falsy_values() {
list($p) = $this->process_directives( $html );
$this->assertNull( $p->get_attribute( 'class' ) );
}

/**
* Tests that classes with several dashes can be used.
*
* @ticket 64106
*
* @covers ::process_directives
*/
public function test_wp_class_can_use_several_dashes() {
$html = '<div data-wp-class--main-bg--color="myPlugin::state.true">Text</div>';
list($p) = $this->process_directives( $html );
$this->assertSame( 'main-bg--color', $p->get_attribute( 'class' ) );

$html = '<div data-wp-class--main-bg---color="myPlugin::state.true">Text</div>';
list($p) = $this->process_directives( $html );
$this->assertSame( 'main-bg---color', $p->get_attribute( 'class' ) );

$html = '<div data-wp-class--main-bg----color="myPlugin::state.true">Text</div>';
list($p) = $this->process_directives( $html );
$this->assertSame( 'main-bg----color', $p->get_attribute( 'class' ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -522,4 +522,45 @@ public function test_wp_context_directive_replaces_old_context_after_closing_tag
$p->next_tag( array( 'class_name' => 'test' ) );
$this->assertSame( 'some-id-1', $p->get_attribute( 'id' ) );
}

/**
* Tests supports multiple context directives in the same element.
*
* @ticket 64106
*
* @covers ::process_directives
*/
public function test_wp_context_supports_multiple_directives_in_the_same_element() {
$html = '
<div
data-wp-interactive="directive-context/multiple"
data-wp-context=\'{ "prop": "parent", "parent": true }\'
>
<div
data-wp-context---id2=\'other-namespace::{ "prop": true }\'
data-wp-context=\'{ "prop": "default", "default": true }\'
data-wp-context---id1=\'{ "prop": "id1", "id1": true }\'
>
<span
class="test"
data-wp-bind--data-test-prop="context.prop"
data-wp-bind--data-test-parent="context.parent"
data-wp-bind--data-test-default="context.default"
data-wp-bind--data-test-id1="context.id1"
data-wp-bind--data-test-other="other-namespace::context.prop"
></span>
</div>
</div>
';
list($p) = $this->process_directives( $html );
$this->assertSame( 'id1', $p->get_attribute( 'data-test-prop' ) );
foreach ( array( 'parent', 'default', 'id1', 'other' ) as $attribute ) {
$attr_name = "data-test-$attribute";
$this->assertSame(
'true',
$p->get_attribute( $attr_name ),
"Failed asserting that $attr_name equals 'true'"
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -684,4 +684,36 @@ public function test_wp_each_doesnt_process_with_manual_server_directive_process
$new = $this->interactivity->process_directives( $original );
$this->assertSame( $expected, $new );
}

/**
* Tests it doesn't support multiple directives.
*
* @ticket 64106
*
* @covers ::process_directives
*/
public function test_wp_each_doesnt_support_multiple_directives() {
$original = '' .
'<div data-wp-interactive="directive-each">' .
'<template data-wp-each="myPlugin::state.list" data-wp-each--item="myPlugin::state.list">' .
'<span data-wp-text="myPlugin::context.item"></span>' .
'</template>' .
'<template data-wp-each---unique-id="myPlugin::state.list">' .
'<span data-wp-text="myPlugin::context.item"></span>' .
'</template>' .
'<div data-wp-bind--id="myPlugin::state.after">Text</div>' .
'</div>';
$expected = '' .
'<div data-wp-interactive="directive-each">' .
'<template data-wp-each="myPlugin::state.list" data-wp-each--item="myPlugin::state.list">' .
'<span data-wp-text="myPlugin::context.item"></span>' .
'</template>' .
'<template data-wp-each---unique-id="myPlugin::state.list">' .
'<span data-wp-text="myPlugin::context.item"></span>' .
'</template>' .
'<div id="after-wp-each" data-wp-bind--id="myPlugin::state.after">Text</div>' .
'</div>';
$new = $this->interactivity->process_directives( $original );
$this->assertSame( $expected, $new );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public function test_wp_style_sets_multiple_style_properties() {
data-wp-style--background="myPlugin::state.green"
>Text</div>';
list($p) = $this->process_directives( $html );
$this->assertSame( 'color:green;background:green;', $p->get_attribute( 'style' ) );
$this->assertSame( 'background:green;color:green;', $p->get_attribute( 'style' ) );
}

/**
Expand Down Expand Up @@ -448,4 +448,30 @@ public function test_wp_style_doesnt_add_style_property_on_falsy_values() {
list($p) = $this->process_directives( $html );
$this->assertNull( $p->get_attribute( 'style' ) );
}

/**
* Tests it can use CSS variables.
*
* @ticket 64106
*
* @covers ::process_directives
*/
public function test_wp_style_can_use_CSS_variables() {
$html = '<div data-wp-style----text-color="myPlugin::state.green">Text</div>';
list($p) = $this->process_directives( $html );
$this->assertSame( '--text-color:green;', $p->get_attribute( 'style' ) );
}

/**
* Tests it ignores unique IDs.
*
* @ticket 64106
*
* @covers ::process_directives
*/
public function test_wp_style_ignores_unique_ids() {
$html = '<div data-wp-style--color---unique-id="myPlugin::state.green">Text</div>';
list($p) = $this->process_directives( $html );
$this->assertNull( $p->get_attribute( 'style' ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @group interactivity-api
*/
class Tests_Interactivity_API_WpInteractivityAPIWPText extends WP_UnitTestCase {
class Tests_WP_Interactivity_API_WP_Text extends WP_UnitTestCase {
/**
* Instance of WP_Interactivity_API.
*
Expand Down Expand Up @@ -154,4 +154,35 @@ public function test_wp_text_cant_set_inner_html_in_the_content() {
$new_html = $this->interactivity->process_directives( $html );
$this->assertSame( '<div data-wp-text="myPlugin::state.text">&lt;span&gt;Updated&lt;/span&gt;</div>', $new_html );
}

/**
* Tests it ignores suffixes and unique-ids.
*
* @ticket 64106
*
* @covers ::process_directives
*/
public function test_wp_text_ignores_suffixes_and_unique_ids() {
$html = '<span data-wp-text--suffix="myPlugin::state.text">Text</span>';
$new_html = $this->interactivity->process_directives( $html );
$this->assertSame( $html, $new_html );

$html = '<span data-wp-text---unique-id="myPlugin::state.text">Text</span>';
$new_html = $this->interactivity->process_directives( $html );
$this->assertSame( $html, $new_html );
}

/**
* Tests first `data-wp-text` works even when suffixes and unique-ids are included.
*
* @ticket 64106
*
* @covers ::process_directives
*/
public function test_wp_text_works_even_when_suffixes_and_unique_ids_are_included() {
$original = '<span data-wp-text--suffix="myPlugin::state.text" data-wp-text---unique-id="myPlugin::state.text" data-wp-text="myPlugin::state.text">Text</span>';
$expected = '<span data-wp-text--suffix="myPlugin::state.text" data-wp-text---unique-id="myPlugin::state.text" data-wp-text="myPlugin::state.text">Updated</span>';
$new_html = $this->interactivity->process_directives( $original );
$this->assertSame( $expected, $new_html );
}
}
Loading
Loading