From 3c914a73fe577f928278a68ffa3a6cf289e1068b Mon Sep 17 00:00:00 2001 From: Mario Santos <34552881+SantosGuillamot@users.noreply.github.com> Date: Fri, 6 Nov 2026 16:36:12 +0100 Subject: [PATCH 01/12] Interactivity API: Add a forward-only content replacement to the directives processor. `WP_Interactivity_API_Directives_Processor::skip_to_balanced_tag_closer()` moves to the matching closer tag of the current opener tag and optionally replaces the content between them. It neither applies the pending updates nor seeks back to the opener tag, so a processor can replace the content of any number of elements. See #66246. --- ...interactivity-api-directives-processor.php | 46 ++++++++++ .../wpInteractivityAPIDirectivesProcessor.php | 85 +++++++++++++++++++ 2 files changed, 131 insertions(+) diff --git a/src/wp-includes/interactivity-api/class-wp-interactivity-api-directives-processor.php b/src/wp-includes/interactivity-api/class-wp-interactivity-api-directives-processor.php index bac24fba19526..e0d509d72c4e3 100644 --- a/src/wp-includes/interactivity-api/class-wp-interactivity-api-directives-processor.php +++ b/src/wp-includes/interactivity-api/class-wp-interactivity-api-directives-processor.php @@ -105,6 +105,52 @@ public function set_raw_content_between_balanced_tags( string $new_content ): bo return true; } + /** + * Moves to the matching closer tag of the current opener tag, skipping the + * content between them, and optionally replaces that content. + * + * The matching closer tag is found as in + * {@see WP_Interactivity_API_Directives_Processor::next_balanced_tag_closer_tag()}. + * The new content is written as given. Unlike + * {@see WP_Interactivity_API_Directives_Processor::set_content_between_balanced_tags()}, + * it neither applies the pending updates nor seeks back to the opener tag, + * so it can be called any number of times on the same processor. + * + * @since 7.2.0 + * + * @access private + * + * @param string|null $new_content Optional. The markup to replace the content between the matching tags + * with, or null to keep the content. Default null. + * @return bool Whether the matching closer tag was found. When it is not found, the cursor is at the end + * of the HTML and nothing is replaced. + */ + public function skip_to_balanced_tag_closer( ?string $new_content = null ): bool { + if ( null === $new_content ) { + return $this->next_balanced_tag_closer_tag(); + } + + $bookmarks = $this->get_balanced_tag_bookmarks(); + if ( ! $bookmarks ) { + return false; + } + list( $opener_tag, $closer_tag ) = $bookmarks; + + $after_opener_tag = $this->bookmarks[ $opener_tag ]->start + $this->bookmarks[ $opener_tag ]->length; + $before_closer_tag = $this->bookmarks[ $closer_tag ]->start; + + $this->release_bookmark( $opener_tag ); + $this->release_bookmark( $closer_tag ); + + $this->lexical_updates[] = new WP_HTML_Text_Replacement( + $after_opener_tag, + $before_closer_tag - $after_opener_tag, + $new_content + ); + + return true; + } + /** * Appends content after the closing tag of a template tag. * diff --git a/tests/phpunit/tests/interactivity-api/wpInteractivityAPIDirectivesProcessor.php b/tests/phpunit/tests/interactivity-api/wpInteractivityAPIDirectivesProcessor.php index f6c3beb79b620..b15a22f3abf28 100644 --- a/tests/phpunit/tests/interactivity-api/wpInteractivityAPIDirectivesProcessor.php +++ b/tests/phpunit/tests/interactivity-api/wpInteractivityAPIDirectivesProcessor.php @@ -389,6 +389,91 @@ public function test_set_content_between_balanced_tags_with_unbalanced_tags() { $this->assertSame( '
New text
', $p->get_updated_html() ); } + /** + * Tests that the `skip_to_balanced_tag_closer` method replaces exactly the + * content between the opener tag and its matching closer tag, writing it as + * given, and leaves the cursor on that closer tag without scanning the new + * content. + * + * @ticket 66246 + * + * @covers ::skip_to_balanced_tag_closer + */ + public function test_skip_to_balanced_tag_closer_replaces_the_content() { + $p = new WP_Interactivity_API_Directives_Processor( '
Nested

Text

After
' ); + $p->next_tag(); + + $this->assertTrue( $p->skip_to_balanced_tag_closer( 'New
' ) ); + $this->assertSame( 'DIV', $p->get_tag() ); + $this->assertTrue( $p->is_tag_closer() ); + + $p->next_tag(); + $this->assertSame( 'after', $p->get_attribute( 'id' ), 'Expected the next tag to be the one after the closer tag.' ); + $this->assertSame( '
New
After
', $p->get_updated_html() ); + } + + /** + * Tests that the `skip_to_balanced_tag_closer` method keeps the content when + * it receives no new content. + * + * @ticket 66246 + * + * @covers ::skip_to_balanced_tag_closer + */ + public function test_skip_to_balanced_tag_closer_keeps_the_content_without_new_content() { + $content = '
Text
After'; + $p = new WP_Interactivity_API_Directives_Processor( $content ); + $p->next_tag(); + + $this->assertTrue( $p->skip_to_balanced_tag_closer() ); + $this->assertSame( 'DIV', $p->get_tag() ); + $this->assertTrue( $p->is_tag_closer() ); + $this->assertSame( $content, $p->get_updated_html() ); + } + + /** + * Tests that the `skip_to_balanced_tag_closer` method reports a missing + * closer tag and replaces nothing. + * + * @ticket 66246 + * + * @covers ::skip_to_balanced_tag_closer + */ + public function test_skip_to_balanced_tag_closer_without_a_matching_closer_tag() { + $content = '
Missing closing div
'; + $p = new WP_Interactivity_API_Directives_Processor( $content ); + $p->next_tag(); + + $this->assertFalse( $p->skip_to_balanced_tag_closer( 'New text' ) ); + $this->assertFalse( $p->next_tag(), 'Expected the cursor to be at the end of the HTML.' ); + $this->assertSame( $content, $p->get_updated_html() ); + } + + /** + * Tests that the `skip_to_balanced_tag_closer` method replaces the content + * of every element of a long document in one processor: more often than the + * HTML API allows seeking, while the processor applies its queue of updates + * on its own, and after an attribute update on each opener tag. + * + * @ticket 66246 + * + * @covers ::skip_to_balanced_tag_closer + */ + public function test_skip_to_balanced_tag_closer_replaces_every_content_of_a_long_document() { + $count = 2 * WP_HTML_Tag_Processor::MAX_SEEK_OPS; + $p = new WP_Interactivity_API_Directives_Processor( str_repeat( '
Old text
', $count ) ); + $expected = ''; + + for ( $i = 0; $i < $count; $i++ ) { + $this->assertTrue( $p->next_tag( 'div' ) ); + $p->set_attribute( 'data-index', (string) $i ); + $this->assertTrue( $p->skip_to_balanced_tag_closer( "{$i}" ) ); + $expected .= "
{$i}
"; + } + + $this->assertSame( $expected, $p->get_updated_html() ); + } + /** * Tests the `has_and_visits_its_closer_tag` method. * From 4e8d8729671da3310a0500bcdf3e5cedb1920a95 Mon Sep 17 00:00:00 2001 From: Mario Santos <34552881+SantosGuillamot@users.noreply.github.com> Date: Fri, 6 Nov 2026 16:36:12 +0100 Subject: [PATCH 02/12] Interactivity API: Render `data-wp-html` on the server as the client hydrates it. The server now follows the client's rules for `data-wp-html`, so hydration keeps what the server rendered: - A host whose reference resolves to `null` or to nothing is emptied without a notice. That covers missing paths, throwing derived-state closures, references with a `__proto__`, `constructor` or `prototype` path segment or namespace, empty values, and values that name no property. The reference is read raw and never decoded as JSON. - A host whose reference resolves to any other value that is not a token is emptied with a notice. A `!` applies after the reserved-name check, as in the client. - The content of an element that carries a `data-wp-html` entry without a suffix is never processed, so the directives in it do not run and the derived-state closures referenced only there are not listed. An element with only unique-ID entries keeps its content as written. - `data-wp-html` is ignored, with a notice, when the element also carries `data-wp-text`, `data-wp-each` or `data-wp-router-region` in any form, on any element. Directives the client drops for a reserved name part or namespace do not count, and a dropped `data-wp-html` leaves the element as if it had none. - A `script`, `style`, `textarea`, `title` or other raw-text host is emptied for a value that is not a token, and keeps its text with a notice for a token. The content of each host is consumed in one forward pass, without seeking back to its opener tag, so a call renders every host however many it holds, and processing time grows with the size of the page alone. The rewinding `WP_Interactivity_API_Directives_Processor::set_raw_content_between_balanced_tags()` is removed. See #66246. --- ...interactivity-api-directives-processor.php | 21 +- .../class-wp-interactivity-api.php | 265 ++++++++--- .../wpInteractivityAPI-wp-html.php | 433 +++++++++++++----- 3 files changed, 545 insertions(+), 174 deletions(-) diff --git a/src/wp-includes/interactivity-api/class-wp-interactivity-api-directives-processor.php b/src/wp-includes/interactivity-api/class-wp-interactivity-api-directives-processor.php index e0d509d72c4e3..33bbbe1662391 100644 --- a/src/wp-includes/interactivity-api/class-wp-interactivity-api-directives-processor.php +++ b/src/wp-includes/interactivity-api/class-wp-interactivity-api-directives-processor.php @@ -71,25 +71,6 @@ public function get_content_between_balanced_template_tags() { * @return bool Whether the content was successfully replaced. */ public function set_content_between_balanced_tags( string $new_content ): bool { - return $this->set_raw_content_between_balanced_tags( esc_html( $new_content ) ); - } - - /** - * Sets the content between two balanced tags, writing it as given. - * - * Unlike {@see WP_Interactivity_API_Directives_Processor::set_content_between_balanced_tags()}, - * it does not escape the content. Like it, it replaces the content between - * the current opener tag and its matching closer tag, and it rewinds to the - * opener tag. - * - * @since 7.2.0 - * - * @access private - * - * @param string $new_content The markup to replace the content between the matching tags. It is written as is. - * @return bool Whether the content was successfully replaced. - */ - public function set_raw_content_between_balanced_tags( string $new_content ): bool { $positions = $this->get_after_opener_tag_and_before_closer_tag_positions( true ); if ( ! $positions ) { return false; @@ -99,7 +80,7 @@ public function set_raw_content_between_balanced_tags( string $new_content ): bo $this->lexical_updates[] = new WP_HTML_Text_Replacement( $after_opener_tag, $before_closer_tag - $after_opener_tag, - $new_content + esc_html( $new_content ) ); return true; diff --git a/src/wp-includes/interactivity-api/class-wp-interactivity-api.php b/src/wp-includes/interactivity-api/class-wp-interactivity-api.php index d8f11e2d05fe3..79bf1062773d9 100644 --- a/src/wp-includes/interactivity-api/class-wp-interactivity-api.php +++ b/src/wp-includes/interactivity-api/class-wp-interactivity-api.php @@ -52,6 +52,15 @@ final class WP_Interactivity_API { 'data-wp-each' => 'data_wp_each_processor', ); + /** + * Matches a directive value that names its namespace, as in + * `myPlugin::state.foo`, capturing the namespace and the rest of the value. + * + * @since 7.2.0 + * @var string + */ + private const NAMESPACED_VALUE_REGEX = '/^([\w\/-]+)::(.+)$/D'; + /** * Holds the initial state of the different Interactivity API stores. * @@ -652,7 +661,15 @@ private function _process_directives( string $html ) { 'attributes' => $element_attrs, ); + $is_content_consumed = false; + foreach ( $modes as $mode => $should_run ) { + // An element whose content was consumed is completed as at its closer tag. + if ( 'exit' === $mode && $is_content_consumed ) { + array_pop( $tag_stack ); + $should_run = true; + } + if ( ! $should_run ) { continue; } @@ -671,6 +688,16 @@ private function _process_directives( string $html ) { : array( $this, self::$directive_processors[ $directive_prefix ] ); call_user_func_array( $func, array( $p, $mode, &$tag_stack ) ); + + /* + * A processor that consumes the content of the element it enters + * leaves the cursor on the element's closer tag, where no other + * processor enters. `data-wp-each` completes its `template` itself. + */ + if ( 'enter' === $mode && $p->is_tag_closer() ) { + $is_content_consumed = 'data-wp-each' !== $directive_prefix; + break; + } } } @@ -1518,18 +1545,25 @@ private function data_wp_text_processor( WP_Interactivity_API_Directives_Process /** * Processes the `data-wp-html` directive. * - * It sets the content of the current HTML element to the HTML of the token - * that its associated reference resolves to. The HTML is written as given and - * the directives inside it are not processed. See - * {@see wp_interactivity_as_dangerous_html()} for the risks of marking HTML - * as trusted. - * - * The element is left as it would be without the directive, and a notice - * reports the reason, when the reference does not resolve to a token, the - * element cannot hold content, or the element also carries `data-wp-text` or, - * for a `template`, `data-wp-each`. A reference that resolves to `null` - * declines without a notice. Only the entry without a suffix or a unique ID - * is processed. + * It renders the element as the client hydrates it. The client never + * processes the content of an element that carries a `data-wp-html` entry + * without a suffix, so no directive inside that content is processed here + * either. The entry without a unique ID then decides the content: the HTML + * of the token its reference resolves to, written as given, or nothing for + * any other value. See {@see wp_interactivity_as_dangerous_html()} for the + * risks of marking HTML as trusted. An element with only unique-ID entries + * keeps its content as written. + * + * A value other than a token or `null` raises a notice. A reference that + * resolves to nothing does not. + * + * The element is processed as if it had no `data-wp-html` directive when + * the client ignores the directive: when the element also carries + * `data-wp-text`, `data-wp-each` or `data-wp-router-region`, which raises a + * notice, or when its entries all have a suffix or are dropped by the + * client. A void element is left as it is, with a notice. An element whose + * content is text, such as `textarea`, keeps that text for a token, with a + * notice, and is emptied for any other value. * * @since 7.2.0 * @@ -1541,22 +1575,40 @@ private function data_wp_html_processor( WP_Interactivity_API_Directives_Process return; } - // Get the first valid `data-wp-html` entry without suffix or unique ID. - $valid_entry = array_find( - $this->get_directive_entries( $p, 'html' ), - fn( $entry ) => null === $entry['suffix'] && null === $entry['unique_id'] && ! empty( $entry['value'] ) - ); + $entries = $this->get_client_directives( $p, 'html' ); + if ( empty( $entries ) ) { + return; + } + + foreach ( array( 'text', 'each', 'router-region' ) as $conflicting_prefix ) { + if ( ! empty( $this->get_client_directives( $p, $conflicting_prefix ) ) ) { + _doing_it_wrong( + __METHOD__, + sprintf( + /* translators: %s: The directive combined with data-wp-html, such as data-wp-text. */ + __( 'The "data-wp-html" directive cannot be combined with the "%s" directive on the same element. The "data-wp-html" directive is ignored.' ), + esc_html( 'data-wp-' . $conflicting_prefix ) + ), + '7.2.0' + ); + return; + } + } - if ( null === $valid_entry ) { + $entries = array_filter( $entries, fn( $entry ) => null === $entry['suffix'] ); + if ( empty( $entries ) ) { return; } - /* - * The structural reasons decline without evaluating the reference, so - * their notice does not depend on the value and no derived state runs - * for a result that would be discarded. - */ - if ( ! $p->has_and_visits_its_closer_tag() ) { + $host_entry = array_find( $entries, fn( $entry ) => null === $entry['unique_id'] ); + if ( null === $host_entry ) { + if ( $p->has_and_visits_its_closer_tag() ) { + $p->skip_to_balanced_tag_closer(); + } + return; + } + + if ( WP_HTML_Processor::is_void( $p->get_tag() ) ) { _doing_it_wrong( __METHOD__, sprintf( @@ -1569,57 +1621,168 @@ private function data_wp_html_processor( WP_Interactivity_API_Directives_Process return; } - $combined_directive = null; - if ( array_find( $this->get_directive_entries( $p, 'text' ), fn( $entry ) => null === $entry['suffix'] && null === $entry['unique_id'] ) ) { - $combined_directive = 'data-wp-text'; - } elseif ( 'TEMPLATE' === $p->get_tag() && ! empty( $this->get_directive_entries( $p, 'each' ) ) ) { - $combined_directive = 'data-wp-each'; - } - if ( null !== $combined_directive ) { + $result = $this->resolve_html_reference( $host_entry['value'] ); + $html = WP_Interactivity_Dangerous_HTML::get_html( $result ); + + if ( null === $html && null !== $result ) { _doing_it_wrong( __METHOD__, - sprintf( - /* translators: %s: The directive combined with data-wp-html, such as data-wp-text. */ - __( 'The "data-wp-html" directive cannot be combined with the "%s" directive on the same element. The "data-wp-html" directive is ignored.' ), - esc_html( $combined_directive ) - ), + __( 'The "data-wp-html" directive requires a reference that resolves to the value returned by wp_interactivity_as_dangerous_html(). Other values, such as strings of HTML, are not rendered and the content of the element is removed.' ), '7.2.0' ); - return; } - /* - * An entry value that is not a string is a JSON literal written in the - * markup, which no store property can be named by, so it is not evaluated. - */ - $result = is_string( $valid_entry['value'] ) ? $this->evaluate( $valid_entry ) : $valid_entry['value']; - - // Unresolved references and `null` values keep the element as it is, as they do in the client. - if ( null === $result ) { - return; - } + if ( ! $p->has_and_visits_its_closer_tag() ) { + if ( null === $html ) { + $p->set_modifiable_text( '' ); + return; + } - $html = WP_Interactivity_Dangerous_HTML::get_html( $result ); - if ( null === $html ) { _doing_it_wrong( __METHOD__, - __( 'The "data-wp-html" directive requires a reference that resolves to the value returned by wp_interactivity_as_dangerous_html(). Other values, such as strings of HTML, are not rendered and the element is left as it is.' ), + sprintf( + /* translators: %s: The HTML tag name of the element, in lowercase. */ + __( 'The "data-wp-html" directive cannot render HTML in a <%s> element because its content is text. The content of the element is left as it is.' ), + esc_html( strtolower( $p->get_tag() ) ) + ), '7.2.0' ); return; } + if ( null === $html ) { + $p->skip_to_balanced_tag_closer( '' ); + return; + } + /* * The HTML is held back and a placeholder comment takes its place, so no * later step of the call scans it. A comment is a single token for the * processors, and the marker in it occurs nowhere in the input. */ $placeholder = ''; - if ( $p->set_raw_content_between_balanced_tags( $placeholder ) ) { + if ( $p->skip_to_balanced_tag_closer( $placeholder ) ) { $this->html_placeholders[ $placeholder ] = $html; } } + /** + * Gets the directives with the given prefix on the current element that the + * client keeps. + * + * The client drops a directive when a reserved word (`__proto__`, + * `constructor` or `prototype`) is any `--`-separated part of its suffix or + * its unique ID, ignoring case and leading and trailing dashes, or when its + * value names a reserved word as its namespace. + * + * @since 7.2.0 + * + * @param WP_Interactivity_API_Directives_Processor $p The directives processor instance. + * @param string $prefix The directive prefix, such as `html`. + * @return array[] The directives, in the order of the element's attributes, with their raw values. + * @phpstan-return list + */ + private function get_client_directives( WP_Interactivity_API_Directives_Processor $p, string $prefix ): array { + $directives = array(); + foreach ( $p->get_attribute_names_with_prefix( 'data-wp-' . $prefix ) ?? array() as $attribute_name ) { + $parsed_directive = $this->parse_directive_name( $attribute_name ); + if ( null === $parsed_directive || $prefix !== $parsed_directive['prefix'] ) { + continue; + } + + $name_parts = null === $parsed_directive['suffix'] ? array() : explode( '--', $parsed_directive['suffix'] ); + if ( null !== $parsed_directive['unique_id'] ) { + $name_parts[] = $parsed_directive['unique_id']; + } + foreach ( $name_parts as $name_part ) { + if ( self::is_reserved_word( strtolower( trim( $name_part, '-' ) ) ) ) { + continue 2; + } + } + + $value = $p->get_attribute( $attribute_name ); + if ( is_string( $value ) && 1 === preg_match( self::NAMESPACED_VALUE_REGEX, $value, $matches ) && self::is_reserved_word( $matches[1] ) ) { + continue; + } + + $directives[] = array( + 'suffix' => $parsed_directive['suffix'], + 'unique_id' => $parsed_directive['unique_id'], + 'value' => $value, + ); + } + return $directives; + } + + /** + * Resolves the reference of a `data-wp-html` directive as the client does. + * + * The reference is the raw attribute value, never decoded as JSON: an + * optional `::`, an optional `!` and a dot-separated path. It + * resolves to `null` without being evaluated when it is empty, or when its + * namespace or a segment of its path is a reserved word (`__proto__`, + * `constructor` or `prototype`). The `!` applies after that, so + * `!state.x.constructor` resolves to `true`. + * + * @since 7.2.0 + * + * @param string|true $reference The attribute value. It is `true` when the attribute has no value. + * @return mixed The value the reference resolves to, or `null` if it resolves to nothing. + */ + private function resolve_html_reference( $reference ) { + if ( ! is_string( $reference ) ) { + return null; + } + + if ( 1 === preg_match( self::NAMESPACED_VALUE_REGEX, $reference, $matches ) ) { + list( , $namespace, $path ) = $matches; + } else { + $namespace = array_last( $this->namespace_stack ?? array() ); + $namespace = is_string( $namespace ) ? $namespace : null; + $path = $reference; + } + + $should_negate_value = false; + if ( str_starts_with( $path, '!' ) ) { + $path = substr( $path, 1 ); + $should_negate_value = '' !== $path; + } + + if ( + '' === $path || + ( null !== $namespace && self::is_reserved_word( $namespace ) ) || + null !== array_find( explode( '.', $path ), fn( $segment ) => self::is_reserved_word( $segment ) ) + ) { + $value = null; + } else { + $value = $this->evaluate( + array( + 'namespace' => $namespace, + 'value' => $path, + ) + ); + } + + return $should_negate_value ? ! $value : $value; + } + + /** + * Checks whether a name is one the client never resolves or keeps as a + * directive name part, because it would reach the prototype of an object. + * + * @since 7.2.0 + * + * @param string $name The name. + * @return bool Whether the name is `__proto__`, `constructor` or `prototype`. + */ + private static function is_reserved_word( string $name ): bool { + return '__proto__' === $name || 'constructor' === $name || 'prototype' === $name; + } + /** * Returns the CSS styles for animating the top loading bar in the router. * diff --git a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-html.php b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-html.php index ac6dc6625b67b..3b07340e9cd56 100644 --- a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-html.php +++ b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-html.php @@ -28,12 +28,13 @@ class Tests_WP_Interactivity_API_WP_Html extends WP_UnitTestCase { const MARKER_HTML = 'marker-7f3a'; /** - * Children with directives, for a host that declines to keep and process. + * Children with directives, for an element whose content is processed or + * replaced. */ const CHILDREN = 'xy'; /** - * The children of a declining host once processed. + * The children once processed. */ const PROCESSED_CHILDREN = 'processedy'; @@ -353,9 +354,9 @@ public function test_host_resolves_in_its_own_context() { } /** - * Tests that a value that is not a token keeps the host's content, with the - * directives in it processed, and raises exactly one notice, which names the - * function that creates tokens. + * Tests that a value that is not a token empties the host without processing + * its content, and raises exactly one notice, which names the function that + * creates tokens. * * @ticket 66246 * @@ -364,18 +365,16 @@ public function test_host_resolves_in_its_own_context() { * * @dataProvider data_values_that_are_not_tokens * - * @param mixed $value The value of `state.html`. + * @param string $attributes The attributes of the host. + * @param array $state The state of the `test` namespace. */ - public function test_value_that_is_not_a_token_declines_with_one_notice( $value ) { + public function test_value_that_is_not_a_token_empties_the_host_with_one_notice( string $attributes, array $state ) { $this->setExpectedIncorrectUsage( self::NOTICE_SOURCE ); - $this->set_state( array( 'html' => $value ) ); + $this->set_state( $state ); - $output = wp_interactivity_process_directives( $this->page( '
' . self::CHILDREN . '
' ) ); + $output = wp_interactivity_process_directives( $this->page( '
' . self::CHILDREN . '
' ) ); - $this->assertSame( - $this->processed_page( '
' . self::PROCESSED_CHILDREN . '
' ), - $output - ); + $this->assertSame( $this->processed_page( '
' ), $output ); $this->assertCount( 1, $this->html_notice_messages(), 'Expected exactly one data-wp-html notice.' ); $this->assertCount( 1, $this->notices, 'Expected no other notice.' ); $this->assertStringContainsString( 'wp_interactivity_as_dangerous_html()', $this->html_notice_messages()[0] ); @@ -384,51 +383,61 @@ public function test_value_that_is_not_a_token_declines_with_one_notice( $value /** * Data provider. * - * @return array Data provider. + * @return array Data provider. */ public static function data_values_that_are_not_tokens(): array { + $token = wp_interactivity_as_dangerous_html( '

Hi

' ); + return array( - 'string of HTML' => array( '

Hi

' ), - 'empty string' => array( '' ), - 'integer' => array( 1 ), - 'boolean' => array( true ), - 'false' => array( false ), - 'array' => array( array( '

Hi

' ) ), - 'array with an html key' => array( array( 'html' => '

Hi

' ) ), - 'stdClass' => array( new stdClass() ), - 'json_decode object' => array( json_decode( '{"html":"

Hi

"}' ) ), - 'other object' => array( new ArrayObject( array( '

Hi

' ) ) ), - 'json_decode of a token' => array( json_decode( wp_json_encode( new WP_Interactivity_Dangerous_HTML( '

Hi

' ) ) ) ), - 'marker decoded as an array' => array( json_decode( '{"wpServerHtml":true}', true ) ), - 'closure returning a string' => array( - static function () { - return '

Hi

'; - }, + 'string of HTML' => array( 'data-wp-html="state.html"', array( 'html' => '

Hi

' ) ), + 'integer' => array( 'data-wp-html="state.html"', array( 'html' => 1 ) ), + 'false' => array( 'data-wp-html="state.html"', array( 'html' => false ) ), + 'list' => array( 'data-wp-html="state.html"', array( 'html' => array( '

Hi

' ) ) ), + 'associative array' => array( 'data-wp-html="state.html"', array( 'html' => array( 'html' => '

Hi

' ) ) ), + 'object' => array( 'data-wp-html="state.html"', array( 'html' => new stdClass() ) ), + 'marker imitated by an array' => array( 'data-wp-html="state.html"', array( 'html' => array( 'wpServerHtml' => true ) ) ), + 'marker imitated by an object' => array( + 'data-wp-html="state.html"', + array( + 'html' => new class() implements JsonSerializable { + public function jsonSerialize(): array { + return array( 'wpServerHtml' => true ); + } + }, + ), ), + 'negated token' => array( 'data-wp-html="!state.html"', array( 'html' => $token ) ), + 'negated reserved path segment' => array( 'data-wp-html="test::!state.obj.constructor"', array( 'obj' => array( 'constructor' => $token ) ) ), + 'string in context' => array( 'data-wp-context=\'{"h":"

Hi

"}\' data-wp-html="context.h"', array() ), ); } /** - * Tests that a `null` value and an unresolved reference keep the host's - * content, with the directives in it processed, without a notice. + * Tests that a reference that resolves to `null` or to nothing empties the + * host without processing its content, and without a notice. * * @ticket 66246 * * @covers ::process_directives * - * @dataProvider data_null_references + * @dataProvider data_references_that_resolve_to_nothing * - * @param string $reference The reference. + * @param string $attributes The attributes of the host. */ - public function test_null_and_unresolved_references_decline_without_notice( string $reference ) { - $this->set_state( array( 'html' => null ) ); + public function test_reference_that_resolves_to_nothing_empties_the_host_without_notice( string $attributes ) { + $token = $this->token(); + $this->set_state( + array( + 'html' => $token, + 'nul' => null, + 'constructor' => $token, + 'obj' => array( 'prototype' => $token ), + ) + ); - $output = wp_interactivity_process_directives( $this->page( '
' . self::CHILDREN . '
' ) ); + $output = wp_interactivity_process_directives( $this->page( '
' . self::CHILDREN . '
' ) ); - $this->assertSame( - $this->processed_page( '
' . self::PROCESSED_CHILDREN . '
' ), - $output - ); + $this->assertSame( $this->processed_page( '
' ), $output ); $this->assertSame( array(), $this->notices, 'Expected no notice.' ); } @@ -437,19 +446,73 @@ public function test_null_and_unresolved_references_decline_without_notice( stri * * @return array Data provider. */ - public static function data_null_references(): array { + public static function data_references_that_resolve_to_nothing(): array { return array( - 'null in state' => array( 'state.html' ), - 'unresolved reference' => array( 'state.missing' ), - 'unknown namespace' => array( 'other::state.html' ), + 'null in state' => array( 'data-wp-html="state.nul"' ), + 'missing path' => array( 'data-wp-html="state.missing"' ), + 'reserved path segment' => array( 'data-wp-html="state.constructor"' ), + 'nested reserved path segment' => array( 'data-wp-html="state.obj.prototype"' ), + 'quoted reference' => array( 'data-wp-html=\'"state.html"\'' ), + 'null literal' => array( 'data-wp-html="null"' ), + 'JSON object' => array( 'data-wp-html=\'{"a":1}\'' ), + 'empty value' => array( 'data-wp-html=""' ), + 'no value' => array( 'data-wp-html' ), + 'reserved namespace in effect' => array( 'data-wp-interactive="constructor" data-wp-html="state.html"' ), + ); + } + + /** + * Tests that a reference whose evaluation fails empties the host with only + * the notice of the evaluation: a derived-state closure that throws, and a + * host processed apart from the `data-wp-interactive` element that gives it + * its namespace. + * + * @ticket 66246 + * + * @covers ::process_directives + * + * @dataProvider data_references_whose_evaluation_fails + * + * @param string $input The HTML to process. + * @param string $expected The expected output. + */ + public function test_failed_evaluation_empties_the_host_without_a_data_wp_html_notice( string $input, string $expected ) { + $this->setExpectedIncorrectUsage( 'WP_Interactivity_API::evaluate' ); + $this->set_state( + array( + 'html' => $this->token(), + 'throws' => static function () { + throw new Exception( 'Failed.' ); + }, + ) ); + + $this->assertSame( $expected, wp_interactivity_process_directives( $input ) ); + $this->assertSame( array( 'WP_Interactivity_API::evaluate' ), array_column( $this->notices, 'source' ), 'Expected only the notice of the evaluation.' ); } /** - * Tests that a host that cannot hold content, or that carries a directive - * `data-wp-html` cannot be combined with, declines with one notice naming - * the reason, without resolving its reference, and that its other - * directives still run. + * Data provider. + * + * @return array Data provider. + */ + public static function data_references_whose_evaluation_fails(): array { + return array( + 'throwing closure' => array( + '
' . self::CHILDREN . '
', + '
', + ), + 'no namespace' => array( + '
' . self::CHILDREN . '
', + '
', + ), + ); + } + + /** + * Tests that a directive the client cannot combine with `data-wp-html` + * makes the element render as if it had no `data-wp-html` directive, with + * one notice naming that directive, and without resolving the reference. * * A reference that was resolved through a derived-state closure would be * listed in the client data, so an empty list shows it was not. @@ -458,13 +521,13 @@ public static function data_null_references(): array { * * @covers ::process_directives * - * @dataProvider data_structurally_declining_hosts + * @dataProvider data_directives_that_conflict * * @param string $host The host markup. * @param string $expected The expected host markup. - * @param string $reason Text the notice contains to name the reason. + * @param string $name The name of the conflicting directive. */ - public function test_structural_decline_names_its_reason_without_resolving_the_reference( string $host, string $expected, string $reason ) { + public function test_conflicting_directive_ignores_data_wp_html_with_one_notice( string $host, string $expected, string $name ) { $this->setExpectedIncorrectUsage( self::NOTICE_SOURCE ); $token = $this->token(); $this->set_state( @@ -480,7 +543,7 @@ public function test_structural_decline_names_its_reason_without_resolving_the_r $this->assertSame( $this->processed_page( $expected ), $output ); $this->assertCount( 1, $this->html_notice_messages(), 'Expected exactly one data-wp-html notice.' ); - $this->assertStringContainsString( $reason, $this->html_notice_messages()[0], 'Expected the notice to name the reason.' ); + $this->assertStringContainsString( '"' . $name . '"', $this->html_notice_messages()[0], 'Expected the notice to name the directive.' ); $this->assertSame( array(), $this->announced_paths(), 'Expected the reference not to be resolved.' ); } @@ -489,75 +552,214 @@ public function test_structural_decline_names_its_reason_without_resolving_the_r * * @return array Data provider. */ - public static function data_structurally_declining_hosts(): array { + public static function data_directives_that_conflict(): array { return array( - 'void element' => array( - '', - '', - '', - ), - 'element with raw content' => array( - '', - '', - 'B' ), + 'html' => static function () use ( $token ) { + return $token; + }, + ) + ); + if ( null !== $notice ) { + $this->setExpectedIncorrectUsage( self::NOTICE_SOURCE ); + } + + $output = wp_interactivity_process_directives( $this->page( $host ) ); + + $this->assertSame( $this->processed_page( $expected ), $output ); + if ( null === $notice ) { + $this->assertSame( array(), $this->notices, 'Expected no notice.' ); + } else { + $this->assertCount( 1, $this->notices, 'Expected exactly one notice.' ); + $this->assertStringContainsString( $notice, $this->html_notice_messages()[0] ); + } + $this->assertSame( array(), $this->announced_paths(), 'Expected the void element not to resolve its reference.' ); } /** * Data provider. * - * @return array Data provider. + * @return array Data provider. */ - public static function data_authored_literals(): array { + public static function data_hosts_that_cannot_hold_html(): array { return array( - 'JSON object' => array( '{"a":"b"}' ), - 'namespaced integer' => array( 'test::42' ), + 'textarea with null' => array( + '', + '', + null, + ), + 'script with a string' => array( + '', + '', + 'wp_interactivity_as_dangerous_html()', + ), + 'textarea with a token' => array( + '', + '', + 'cannot render HTML in a B' ), - 'html' => static function () use ( $token ) { + 'html' => static function () use ( $token, &$calls ) { + ++$calls; return $token; }, ) ); - if ( null !== $notice ) { - $this->setExpectedIncorrectUsage( self::NOTICE_SOURCE ); - } - $output = wp_interactivity_process_directives( $this->page( $host ) ); $this->assertSame( $this->processed_page( $expected ), $output ); - if ( null === $notice ) { - $this->assertSame( array(), $this->notices, 'Expected no notice.' ); - } else { - $this->assertCount( 1, $this->notices, 'Expected exactly one notice.' ); - $this->assertStringContainsString( $notice, $this->html_notice_messages()[0] ); - } - $this->assertSame( array(), $this->announced_paths(), 'Expected the void element not to resolve its reference.' ); + $this->assertCount( 1, $this->notices, 'Expected exactly one notice.' ); + $this->assertCount( 1, $this->html_notice_messages(), 'Expected a data-wp-html notice.' ); + $this->assertStringContainsString( $notice, $this->html_notice_messages()[0] ); + $this->assertSame( 0, $calls, 'Expected the reference not to be evaluated.' ); + $this->assertSame( array(), $this->announced_paths(), 'Expected no closure to be listed.' ); } /** * Data provider. * - * @return array Data provider. + * @return array Data provider. */ public static function data_hosts_that_cannot_hold_html(): array { - return array( - 'textarea with null' => array( - '', - '', - null, - ), - 'script with a string' => array( - '', - '', - 'wp_interactivity_as_dangerous_html()', - ), - 'textarea with a token' => array( - '', - '', - 'cannot render HTML in a