Skip to content

Commit 03171e1

Browse files
committed
Interactivity API: Fatal error processing incorrect closed tags.
Fix for fatal errors caused by incorrect closing tags in HTML, such as `</br>`. In these cases, the `get_attribute_names_with_prefix` method of the `WP_HTML_Tag_Processor` returns `null`, and the Interactivity API was not handling this situation correctly. Props hugosolar, jonsurrell, darerodz. Fixes #63891. git-svn-id: https://develop.svn.wordpress.org/trunk@61197 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 4b4e950 commit 03171e1

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,12 @@ private function _process_directives( string $html ) {
520520
array_pop( $tag_stack );
521521
}
522522
} else {
523-
if ( 0 !== count( $p->get_attribute_names_with_prefix( 'data-wp-each-child' ) ) ) {
523+
$each_child_attrs = $p->get_attribute_names_with_prefix( 'data-wp-each-child' );
524+
if ( null === $each_child_attrs ) {
525+
continue;
526+
}
527+
528+
if ( 0 !== count( $each_child_attrs ) ) {
524529
/*
525530
* If the tag has a `data-wp-each-child` directive, jump to its closer
526531
* tag because those tags have already been processed.

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,31 @@ public static function data_html_with_unbalanced_tags() {
12561256
);
12571257
}
12581258

1259+
/**
1260+
* Tests that the `process_directives` handles self-closing BR tags without
1261+
* causing fatal errors and processes directives correctly.
1262+
*
1263+
* @ticket 63891
1264+
* @covers ::process_directives
1265+
*/
1266+
public function test_process_directives_handles_br_self_closing_tags_with_invalid_closers() {
1267+
$this->interactivity->state(
1268+
'myPlugin',
1269+
array(
1270+
'id' => 'some-id',
1271+
)
1272+
);
1273+
1274+
$html = '</br><div data-wp-bind--id="myPlugin::state.id">Content</div>';
1275+
1276+
$processed_html = $this->interactivity->process_directives( $html );
1277+
1278+
$p = new WP_HTML_Tag_Processor( $processed_html );
1279+
$p->next_tag( 'div' );
1280+
1281+
$this->assertSame( 'some-id', $p->get_attribute( 'id' ) );
1282+
}
1283+
12591284
/**
12601285
* Tests that the `process_directives` process the HTML outside a SVG tag.
12611286
*

0 commit comments

Comments
 (0)