Skip to content

Commit 19f2afb

Browse files
committed
fix null checking and add test
1 parent 4d0212d commit 19f2afb

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,11 +440,8 @@ 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 ( ! is_countable( $each_child_attrs ) ) {
444-
continue;
445-
}
446443

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

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,9 +830,37 @@ 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>' ),
833834
);
834835
}
835836

837+
/**
838+
* Tests that the `process_directives` handles self-closing tags with invalid
839+
* closing tags without causing fatal errors.
840+
*
841+
* @covers ::process_directives
842+
*
843+
* @expectedIncorrectUsage WP_Interactivity_API::_process_directives
844+
*/
845+
public function test_process_directives_handles_self_closing_tags_with_invalid_closers() {
846+
$this->interactivity->state(
847+
'myPlugin',
848+
array(
849+
'id' => 'some-id',
850+
),
851+
);
852+
853+
$html = '<div data-wp-bind--id="myPlugin::state.id">Content<br></br></div>';
854+
855+
$processed_html = $this->interactivity->process_directives( $html );
856+
857+
$this->assertSame( $html, $processed_html );
858+
859+
$p = new WP_HTML_Tag_Processor( $processed_html );
860+
$p->next_tag( 'div' );
861+
$this->assertNull( $p->get_attribute( 'id' ) );
862+
}
863+
836864
/**
837865
* Tests that the `process_directives` process the HTML outside a SVG tag.
838866
*

0 commit comments

Comments
 (0)