Skip to content

Commit c4bc65d

Browse files
committed
add unit test and fix process directives
1 parent 19f2afb commit c4bc65d

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

src/wp-includes/interactivity-api/class-wp-interactivity-api.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,11 @@ private function _process_directives( string $html ) {
440440
}
441441
} else {
442442
$each_child_attrs = $p->get_attribute_names_with_prefix( 'data-wp-each-child' );
443+
if ( null === $each_child_attrs ) {
444+
continue;
445+
}
443446

444-
if ( null !== $each_child_attrs && 0 !== count( $each_child_attrs ) ) {
447+
if ( 0 !== count( $each_child_attrs ) ) {
445448
/*
446449
* If the tag has a `data-wp-each-child` directive, jump to its closer
447450
* tag because those tags have already been processed.

tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -830,35 +830,32 @@ public static function data_html_with_unbalanced_tags() {
830830
'SPAN opener inside' => array( '<div data-wp-bind--id="myPlugin::state.id"><span>Inner content</div>' ),
831831
'SPAN closer after' => array( '<div data-wp-bind--id="myPlugin::state.id">Inner content</div></span>' ),
832832
'SPAN overlapping' => array( '<div data-wp-bind--id="myPlugin::state.id"><span>Inner content</div></span>' ),
833-
'BR self-closing' => array( '<div data-wp-bind--id="myPlugin::state.id">Content<br></br></div>' ),
834833
);
835834
}
836835

837836
/**
838-
* Tests that the `process_directives` handles self-closing tags with invalid
839-
* closing tags without causing fatal errors.
837+
* Tests that the `process_directives` handles self-closing BR tags without
838+
* causing fatal errors and processes directives correctly.
840839
*
840+
* @ticket 63891
841841
* @covers ::process_directives
842-
*
843-
* @expectedIncorrectUsage WP_Interactivity_API::_process_directives
844842
*/
845-
public function test_process_directives_handles_self_closing_tags_with_invalid_closers() {
843+
public function test_process_directives_handles_br_self_closing_tags_with_invalid_closers() {
846844
$this->interactivity->state(
847845
'myPlugin',
848846
array(
849847
'id' => 'some-id',
850848
),
851849
);
852850

853-
$html = '<div data-wp-bind--id="myPlugin::state.id">Content<br></br></div>';
851+
$html = '</br><div data-wp-bind--id="myPlugin::state.id">Content</div>';
854852

855853
$processed_html = $this->interactivity->process_directives( $html );
856854

857-
$this->assertSame( $html, $processed_html );
858-
859855
$p = new WP_HTML_Tag_Processor( $processed_html );
860856
$p->next_tag( 'div' );
861-
$this->assertNull( $p->get_attribute( 'id' ) );
857+
858+
$this->assertSame( 'some-id', $p->get_attribute( 'id' ) );
862859
}
863860

864861
/**

0 commit comments

Comments
 (0)