Skip to content

Commit 9081c19

Browse files
committed
Add supporting test for the malformed br case.
1 parent 3ab520d commit 9081c19

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,34 @@
1212
* @coversDefaultClass WP_Interactivity_API_Directives_Processor
1313
*/
1414
class Tests_Interactivity_API_WpInteractivityAPIDirectivesProcessor extends WP_UnitTestCase {
15+
16+
/**
17+
* Regression: _process_directives should not fatal when encountering malformed markup
18+
* (e.g., a stray </br>) that causes get_attribute_names_with_prefix() to return null.
19+
*
20+
* Before the fix: PHP 8+ threw a TypeError from count(null).
21+
*/
22+
public function test_process_directives_handles_noncountable_each_child_attrs() {
23+
$html = '<div><br></br><span data-wp-bind--text="state.foo">x</span></div>';
24+
25+
$api = new WP_Interactivity_API();
26+
27+
$ref = new ReflectionClass( $api );
28+
$method = $ref->getMethod( '_process_directives' );
29+
$method->setAccessible( true );
30+
31+
// This would throw if there is an error.
32+
$processed = $method->invoke( $api, $html );
33+
34+
$this->assertIsString( $processed );
35+
$this->assertStringContainsString( '<span', $processed );
36+
$this->assertStringContainsString( 'data-wp-bind--text', $processed );
37+
38+
// Assert content still intact despite the malformed </br>.
39+
$this->assertStringContainsString( '<div>', $processed );
40+
}
41+
42+
1543
/**
1644
* Tests the `get_content_between_balanced_template_tags` method on template
1745
* tags.

0 commit comments

Comments
 (0)