diff --git a/src/wp-includes/block-supports/block-style-variations.php b/src/wp-includes/block-supports/block-style-variations.php index 07b33ed36e978..888303f004c10 100644 --- a/src/wp-includes/block-supports/block-style-variations.php +++ b/src/wp-includes/block-supports/block-style-variations.php @@ -21,8 +21,20 @@ function wp_get_block_style_variation_name_from_class( $class_string ): ?array { return null; } - preg_match_all( '/\bis-style-(?!default)(\S+)\b/', $class_string, $matches ); - return $matches[1]; + $prefix = 'is-style-'; + $variations = array(); + foreach ( wp_html_attribute_split_space_separated_tokens( $class_string ) as $class_name ) { + if ( ! str_starts_with( $class_name, $prefix ) ) { + continue; + } + + $variation = substr( $class_name, strlen( $prefix ) ); + if ( '' !== $variation && ! str_starts_with( $variation, 'default' ) ) { + $variations[] = $variation; + } + } + + return $variations; } /** @@ -227,12 +239,18 @@ function wp_render_block_style_variation_class_name( $block_content, $block ) { } /* - * Matches a class prefixed by `is-style`, followed by the + * Finds a class prefixed by `is-style-`, followed by the * variation slug, then `--`, and finally an instance number. */ - preg_match( '/\bis-style-(\S+?--\d+)\b/', $block_class_name, $matches ); + $instance_class_name = null; + foreach ( wp_html_attribute_split_space_separated_tokens( $block_class_name ) as $class_name ) { + if ( preg_match( '/^is-style-.+--\d+$/', $class_name ) ) { + $instance_class_name = $class_name; + break; + } + } - if ( empty( $matches ) ) { + if ( null === $instance_class_name ) { return $block_content; } @@ -244,7 +262,7 @@ function wp_render_block_style_variation_class_name( $block_content, $block ) { * `render_block_data` filter is applied in markup. * See `wp_render_block_style_variation_support_styles`. */ - $tags->add_class( $matches[0] ); + $tags->add_class( $instance_class_name ); } return $tags->get_updated_html(); diff --git a/src/wp-includes/block-supports/custom-css.php b/src/wp-includes/block-supports/custom-css.php index 1c9a6a95d048d..a310ea33351a6 100644 --- a/src/wp-includes/block-supports/custom-css.php +++ b/src/wp-includes/block-supports/custom-css.php @@ -284,14 +284,11 @@ function wp_render_custom_css_class_name( $block_content, $block ) { // Parse out the 'wp-custom-css-*' class name added by wp_render_custom_css_support_styles(). if ( is_string( $class_name_attr ) && str_contains( $class_name_attr, $class_name_prefix ) ) { - $token_delimiter = " \t\f\r\n"; - $class_token = strtok( $class_name_attr, $token_delimiter ); - while ( false !== $class_token ) { + foreach ( wp_html_attribute_split_space_separated_tokens( $class_name_attr ) as $class_token ) { if ( str_starts_with( $class_token, $class_name_prefix ) ) { $matched_class_name = $class_token; break; } - $class_token = strtok( $token_delimiter ); } } diff --git a/src/wp-includes/block-supports/elements.php b/src/wp-includes/block-supports/elements.php index e8076cc1bd3ba..bc081fb468310 100644 --- a/src/wp-includes/block-supports/elements.php +++ b/src/wp-includes/block-supports/elements.php @@ -285,14 +285,11 @@ function wp_render_elements_class_name( $block_content, $block ) { // Parse out the 'wp-elements-*' class name. $matched_class_name = null; - $token_delimiter = " \t\f\r\n"; - $class_token = strtok( $class_name_attr, $token_delimiter ); - while ( false !== $class_token ) { + foreach ( wp_html_attribute_split_space_separated_tokens( $class_name_attr ) as $class_token ) { if ( str_starts_with( $class_token, $class_name_prefix ) ) { $matched_class_name = $class_token; break; } - $class_token = strtok( $token_delimiter ); } if ( null === $matched_class_name ) { return $block_content; diff --git a/src/wp-includes/block-supports/layout.php b/src/wp-includes/block-supports/layout.php index dc3b56c0171c5..c3a51b800d911 100644 --- a/src/wp-includes/block-supports/layout.php +++ b/src/wp-includes/block-supports/layout.php @@ -25,7 +25,7 @@ function wp_get_block_style_variation_name_from_registered_style( string $class_ $prefix = 'is-style-'; $length = strlen( $prefix ); - foreach ( explode( ' ', $class_name ) as $class ) { + foreach ( wp_html_attribute_split_space_separated_tokens( $class_name ) as $class ) { if ( str_starts_with( $class, $prefix ) ) { $variation = substr( $class, $length ); if ( 'default' !== $variation && in_array( $variation, $registered_names, true ) ) { diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 3b9e247fd44fd..3dd65689ff5ff 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -5106,6 +5106,59 @@ function wp_parse_list( $input_list ): array { return $input_list; } +/** + * Generator for a foreach loop to step through values in an unescaped HTML + * attribute whose type is a set of space-separated tokens, such as `class`. + * + * > A set of space-separated tokens is a string containing zero or more + * > words (known as tokens) separated by one or more ASCII whitespace, + * > where words consist of any string of one or more characters, none + * > of which are ASCII whitespace. + * + * Certain HTML attributes, such as `class`, `rel`, and `sizes`, store their + * values using this syntax. Use this function to split such decoded + * attribute values into their tokens. + * + * This is a purely lexical operation on a plain-text string: tokens are + * neither deduplicated nor case-normalized, and no character references are + * decoded. The input must already be a fully decoded attribute value, not + * raw markup, since whitespace produced by a character reference (such as + * ` `) would otherwise be treated differently here than it is by an + * HTML parser. To read the class names of an HTML tag, use + * {@see WP_HTML_Tag_Processor::class_list()} instead. + * + * ```php interactive + * foreach ( wp_html_attribute_split_space_separated_tokens( " wp-block\tis-style-outline \n" ) as $token ) { + * echo "{$token} "; + * } + * // Outputs: "wp-block is-style-outline " + * ``` + * + * @since 7.2.0 + * + * @link https://html.spec.whatwg.org/#attributes-3 + * @link https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#set-of-space-separated-tokens + * + * @param string $decoded_attribute_value Full un-escaped attribute value to split. + * @return Generator Tokens in order of appearance. + */ +function wp_html_attribute_split_space_separated_tokens( string $decoded_attribute_value ): Generator { + $at = strspn( $decoded_attribute_value, " \t\f\r\n" ); + $input_length = strlen( $decoded_attribute_value ); + while ( $at < $input_length ) { + // Find the byte length until the next boundary. + $length = strcspn( $decoded_attribute_value, " \t\f\r\n", $at ); + if ( 0 === $length ) { + return; + } + + yield substr( $decoded_attribute_value, $at, $length ); + + $at += $length; + $at += strspn( $decoded_attribute_value, " \t\f\r\n", $at ); + } +} + /** * Cleans up an array, comma- or space-separated list of IDs. * diff --git a/tests/phpunit/tests/block-supports/block-style-variations.php b/tests/phpunit/tests/block-supports/block-style-variations.php index 3898178099e7e..a5b9c518b2c48 100644 --- a/tests/phpunit/tests/block-supports/block-style-variations.php +++ b/tests/phpunit/tests/block-supports/block-style-variations.php @@ -643,6 +643,63 @@ public function test_block_style_variation_class_name_with_non_string_class_name ); } + /** + * Tests that the variation instance class name is applied only when it is + * a complete class name in the `className` attribute. + * + * @ticket 65466 + * + * @covers ::wp_render_block_style_variation_class_name + * + * @dataProvider data_block_style_variation_class_name + * + * @param string $class_name Value of the `className` block attribute. + * @param string $expected Expected rendered block content. + */ + public function test_block_style_variation_class_name( string $class_name, string $expected ): void { + $block = array( + 'blockName' => 'core/paragraph', + 'attrs' => array( + 'className' => $class_name, + ), + ); + + $this->assertSame( + $expected, + wp_render_block_style_variation_class_name( '

Test

', $block ) + ); + } + + /** + * Data provider. + * + * @return array + */ + public static function data_block_style_variation_class_name(): array { + return array( + 'instance class name' => array( + 'custom is-style-fancy--1', + '

Test

', + ), + 'variation slug containing dashes' => array( + 'is-style-my--fancy--3', + '

Test

', + ), + 'variation without an instance number' => array( + 'is-style-fancy', + '

Test

', + ), + 'prefix inside another class name' => array( + 'custom-is-style-fancy--1', + '

Test

', + ), + 'instance number followed by a suffix' => array( + 'is-style-fancy--1-extra', + '

Test

', + ), + ); + } + /** * Tests to ensure that there are no references to an undefined array key * if `className` is not assigned. diff --git a/tests/phpunit/tests/block-supports/wpGetBlockStyleVariationNameFromClass.php b/tests/phpunit/tests/block-supports/wpGetBlockStyleVariationNameFromClass.php index c5c336f4079b2..d36cb6ed9efc7 100644 --- a/tests/phpunit/tests/block-supports/wpGetBlockStyleVariationNameFromClass.php +++ b/tests/phpunit/tests/block-supports/wpGetBlockStyleVariationNameFromClass.php @@ -36,30 +36,40 @@ public function test_block_style_variation_name_extraction( $class_string, $expe public static function data_block_style_variation_name_extraction() { return array( // @ticket 61312 - 'missing class string' => array( + 'missing class string' => array( 'class_string' => null, 'expected' => null, ), // @ticket 61312 - 'empty class string' => array( + 'empty class string' => array( 'class_string' => '', 'expected' => array(), ), // @ticket 61312 - 'no variation' => array( + 'no variation' => array( 'class_string' => 'is-style no-variation', 'expected' => array(), ), // @ticket 61312 - 'single variation' => array( + 'single variation' => array( 'class_string' => 'custom-class is-style-outline', 'expected' => array( 'outline' ), ), // @ticket 61312 - 'multiple variations' => array( + 'multiple variations' => array( 'class_string' => 'is-style-light custom-class is-style-outline', 'expected' => array( 'light', 'outline' ), ), + // @ticket 65466 + 'prefix inside another class name' => array( + 'class_string' => 'custom-is-style-outline', + 'expected' => array(), + ), + // @ticket 65466 + 'variation ending in a non-word character' => array( + 'class_string' => 'is-style-outline-', + 'expected' => array( 'outline-' ), + ), ); } } diff --git a/tests/phpunit/tests/functions/wpHtmlAttributeSplitSpaceSeparatedTokens.php b/tests/phpunit/tests/functions/wpHtmlAttributeSplitSpaceSeparatedTokens.php new file mode 100644 index 0000000000000..9420cb44ee8aa --- /dev/null +++ b/tests/phpunit/tests/functions/wpHtmlAttributeSplitSpaceSeparatedTokens.php @@ -0,0 +1,85 @@ + $expected Tokens wp_html_attribute_split_space_separated_tokens() should yield. + */ + public function test_wp_html_attribute_split_space_separated_tokens( string $input, array $expected ): void { + $this->assertSame( + $expected, + iterator_to_array( wp_html_attribute_split_space_separated_tokens( $input ) ), + 'Should have yielded the expected tokens.' + ); + } + + /** + * Data provider. + * + * @return array }> + */ + public static function data_wp_html_attribute_split_space_separated_tokens(): array { + return array( + 'empty string' => array( + 'input' => '', + 'expected' => array(), + ), + 'whitespace only' => array( + 'input' => " \t\n\f\r ", + 'expected' => array(), + ), + 'single token' => array( + 'input' => 'foo', + 'expected' => array( 'foo' ), + ), + 'single spaces' => array( + 'input' => 'foo bar baz', + 'expected' => array( 'foo', 'bar', 'baz' ), + ), + 'leading, trailing and repeated space' => array( + 'input' => ' foo bar ', + 'expected' => array( 'foo', 'bar' ), + ), + 'all ASCII whitespace separators' => array( + 'input' => "a\tb\nc\fd\re f", + 'expected' => array( 'a', 'b', 'c', 'd', 'e', 'f' ), + ), + 'vertical tab is not a separator' => array( + 'input' => "a\vb", + 'expected' => array( "a\vb" ), + ), + 'non-breaking space is not a separator' => array( + 'input' => "a\u{00A0}b c", + 'expected' => array( "a\u{00A0}b", 'c' ), + ), + 'duplicates are preserved' => array( + 'input' => 'foo bar foo', + 'expected' => array( 'foo', 'bar', 'foo' ), + ), + 'case is preserved' => array( + 'input' => 'Foo foo', + 'expected' => array( 'Foo', 'foo' ), + ), + 'character references are not decoded' => array( + 'input' => 'a&b c d', + 'expected' => array( 'a&b', 'c d' ), + ), + 'zero is a valid token' => array( + 'input' => '0 1', + 'expected' => array( '0', '1' ), + ), + ); + } +}