Skip to content

Commit 3299ff3

Browse files
committed
Interactivity API: Keep host tag names in data-wp-html notices.
1 parent 86a23b8 commit 3299ff3

2 files changed

Lines changed: 75 additions & 3 deletions

File tree

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,7 @@ private static function get_dangerous_html( $value ): ?string {
14741474
*
14751475
* Declines incompatible directives, targets that cannot hold content, and
14761476
* unregistered values with a notice. Null values and ignored entries are silent.
1477+
* Notices identify the host tag as plain text for error-message sanitization.
14771478
*
14781479
* @since 7.2.0
14791480
*
@@ -1490,7 +1491,7 @@ private function data_wp_html_processor( WP_Interactivity_API_Directives_Process
14901491
__METHOD__,
14911492
sprintf(
14921493
/* translators: 1: Directive name, 2: HTML tag name, 3: Directive references. */
1493-
__( 'The %1$s directive on <%2$s> cannot be combined with data-wp-text or with data-wp-each on a template. References: %3$s.' ),
1494+
__( 'The %1$s directive on a %2$s tag cannot be combined with data-wp-text or with data-wp-each on a template. References: %3$s.' ),
14941495
'data-wp-html',
14951496
$p->get_tag(),
14961497
implode( ', ', array_unique( array_filter( array_column( $entries, 'value' ), 'is_string' ) ) )
@@ -1512,7 +1513,7 @@ private function data_wp_html_processor( WP_Interactivity_API_Directives_Process
15121513
__METHOD__,
15131514
sprintf(
15141515
/* translators: 1: Directive name, 2: Directive reference, 3: HTML tag name. */
1515-
__( 'The %1$s directive with reference "%2$s" cannot render because <%3$s> cannot hold content.' ),
1516+
__( 'The %1$s directive with reference "%2$s" cannot render because a %3$s tag cannot hold content.' ),
15161517
'data-wp-html',
15171518
$entry['value'],
15181519
$p->get_tag()
@@ -1530,7 +1531,7 @@ private function data_wp_html_processor( WP_Interactivity_API_Directives_Process
15301531
__METHOD__,
15311532
sprintf(
15321533
/* translators: 1: Directive name, 2: Directive reference, 3: HTML tag name, 4: Trusted HTML function name. */
1533-
__( 'The %1$s directive with reference "%2$s" on <%3$s> resolved to a value that is not a token returned by %4$s.' ),
1534+
__( 'The %1$s directive with reference "%2$s" on a %3$s tag resolved to a value that is not a token returned by %4$s.' ),
15341535
'data-wp-html',
15351536
$entry['value'],
15361537
$p->get_tag(),

‎tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-html.php‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,77 @@ public function test_each_combinations() {
374374
}
375375
}
376376

377+
/**
378+
* Tests delivered decline notices retain the host tag after sanitization.
379+
*
380+
* @dataProvider data_delivered_decline_notices
381+
* @expectedIncorrectUsage WP_Interactivity_API::data_wp_html_processor
382+
* @param string $host Host markup.
383+
* @param string $tag Host tag name.
384+
* @param string $reason Decline reason.
385+
* @param string $reference Directive reference, if present.
386+
*/
387+
public function test_delivered_decline_notices( string $host, string $tag, string $reason, string $reference ) {
388+
$this->assertTrue( WP_DEBUG );
389+
wp_interactivity_state(
390+
'test',
391+
array(
392+
'html' => 'evaluated-value-7f3a',
393+
'list' => array( 'a' ),
394+
)
395+
);
396+
$delivered_notices = array();
397+
remove_filter( 'doing_it_wrong_trigger_error', '__return_false' );
398+
set_error_handler(
399+
/**
400+
* Records the message delivered by PHP's notice handler.
401+
*
402+
* @param int $severity Error severity.
403+
* @param string $message Delivered message.
404+
* @return bool Whether the notice was handled.
405+
*/
406+
static function ( $severity, $message ) use ( &$delivered_notices ) {
407+
$delivered_notices[] = array( $severity, $message );
408+
return true;
409+
},
410+
E_USER_NOTICE
411+
);
412+
try {
413+
wp_interactivity_process_directives( $this->region( $host ) );
414+
} finally {
415+
restore_error_handler();
416+
add_filter( 'doing_it_wrong_trigger_error', '__return_false' );
417+
}
418+
$this->assert_decline_notice( $reason, $tag, $reference );
419+
$this->assertCount( 1, $delivered_notices );
420+
$this->assertSame( E_USER_NOTICE, $delivered_notices[0][0] );
421+
$message = $delivered_notices[0][1];
422+
$this->assertStringContainsString( $tag . ' tag', $message );
423+
$this->assertStringContainsString( $reason, $message );
424+
$this->assertStringContainsString( 'WP_Interactivity_API::data_wp_html_processor', $message );
425+
$this->assertStringContainsString( 'version 7.2.0', $message );
426+
if ( '' !== $reference ) {
427+
$this->assertStringContainsString( $reference, $message );
428+
}
429+
$this->assertStringNotContainsString( 'evaluated-value-7f3a', $message );
430+
}
431+
432+
/**
433+
* Supplies all decline reasons, including allowed and disallowed HTML tags.
434+
*
435+
* @return array[] Host markup, tag, reason, and reference.
436+
*/
437+
public static function data_delivered_decline_notices() {
438+
return array(
439+
'not a token' => array( '<div data-wp-html="state.html">Fallback</div>', 'DIV', 'not a token', 'state.html' ),
440+
'void image' => array( '<img data-wp-html="state.html">', 'IMG', 'cannot hold content', 'state.html' ),
441+
'void break' => array( '<br data-wp-html="state.html">', 'BR', 'cannot hold content', 'state.html' ),
442+
'text combination' => array( '<div data-wp-html="state.html" data-wp-text="state.text">Fallback</div>', 'DIV', 'cannot be combined', 'state.html' ),
443+
'each combination' => array( '<template data-wp-html="state.html" data-wp-each="state.list"><span>x</span></template>', 'TEMPLATE', 'cannot be combined', 'state.html' ),
444+
'empty combination' => array( '<template data-wp-html="" data-wp-each="state.list"><span>x</span></template>', 'TEMPLATE', 'cannot be combined', '' ),
445+
);
446+
}
447+
377448
/**
378449
* Tests the three public notice messages distinguish decline reasons.
379450
*

0 commit comments

Comments
 (0)