From a216cb6d0cb3b33494fe531559f96883588386a9 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 20 Dec 2022 11:48:18 +0100 Subject: [PATCH 1/3] WP_HTML_Tag_Processor: Add get_attributes_by_prefix() method --- .../html/class-wp-html-tag-processor.php | 39 +++++++++++++++++++ phpunit/html/wp-html-tag-processor-test.php | 15 +++++++ 2 files changed, 54 insertions(+) diff --git a/lib/experimental/html/class-wp-html-tag-processor.php b/lib/experimental/html/class-wp-html-tag-processor.php index 0dfa57f30f2aab..dd0cdeafb211d1 100644 --- a/lib/experimental/html/class-wp-html-tag-processor.php +++ b/lib/experimental/html/class-wp-html-tag-processor.php @@ -1412,6 +1412,45 @@ public function get_attribute( $name ) { return html_entity_decode( $raw_value ); } + /** + * Returns parsed attributes matching a given prefix in the currently-opened tag. + * + * Example: + * + * $p = new WP_HTML_Tag_Processor( '
Test
' ); + * $p->next_tag( [ 'class_name' => 'test' ] ) === true; + * $p->get_attributes_by_prefix( 'data-' ) === array( 'data-enabled' => true, 'data-test-id' => '14' ); + * + * $p->next_tag( [] ) === false; + * $p->get_attributes_by_prefix( 'data' ) === null; + *
+ * + * @since 6.2.0 + * + * @param string $prefix Prefix of attributes whose value is requested. + * @return array|null Associative array of attribute names and values, or `null` if not at a tag. + * Boolean attributes map to `true`. + */ + function get_attributes_by_prefix( $prefix ) { + if ( null === $this->tag_name_starts_at ) { + return null; + } + + $comparable = strtolower( $prefix ); + $matches = array_filter( + array_keys( $this->attributes ), + function( $attr ) use ( $comparable ) { + return str_starts_with( $attr, $comparable ); + } + ); + + $results = array(); + foreach ( $matches as $attr ) { + $results[ $attr ] = $this->get_attribute( $attr ); + } + return $results; + } + /** * Returns the lowercase name of the currently-opened tag. * diff --git a/phpunit/html/wp-html-tag-processor-test.php b/phpunit/html/wp-html-tag-processor-test.php index e6b3d4d8071af5..263df4f6695fe5 100644 --- a/phpunit/html/wp-html-tag-processor-test.php +++ b/phpunit/html/wp-html-tag-processor-test.php @@ -151,6 +151,21 @@ public function test_attributes_parser_treats_slash_as_attribute_separator() { $this->assertSame( 'test', $p->get_attribute( 'e' ), 'Accessing an existing e="test" did not return "test"' ); } + /** + * @covers WP_HTML_Tag_Processor::get_attributes_by_prefix + */ + public function test_get_attributes_by_prefix_returns_set_of_prefixed_attributes() { + $p = new WP_HTML_Tag_Processor( '
Test
' ); + $p->next_tag(); + $this->assertSame( + array( + 'data-enabled' => true, + 'data-test-id' => '14', + ), + $p->get_attributes_by_prefix( 'data-' ) + ); + } + /** * @ticket 56299 * From b7cac441cbe23ce3e6a0ca81bd05ebf670bdd158 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 20 Dec 2022 11:58:52 +0100 Subject: [PATCH 2/3] Clarify comment --- lib/experimental/html/class-wp-html-tag-processor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/html/class-wp-html-tag-processor.php b/lib/experimental/html/class-wp-html-tag-processor.php index dd0cdeafb211d1..c8bc95d9c6f464 100644 --- a/lib/experimental/html/class-wp-html-tag-processor.php +++ b/lib/experimental/html/class-wp-html-tag-processor.php @@ -1428,7 +1428,7 @@ public function get_attribute( $name ) { * @since 6.2.0 * * @param string $prefix Prefix of attributes whose value is requested. - * @return array|null Associative array of attribute names and values, or `null` if not at a tag. + * @return array|null Associative array of matching attribute names and values, or `null` if not at a tag. * Boolean attributes map to `true`. */ function get_attributes_by_prefix( $prefix ) { From 0962176ed6af12301f39568e89e702237d8ded99 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 2 Jan 2023 16:16:18 +0100 Subject: [PATCH 3/3] Check that get_attributes_by_prefix is case-insensitive --- phpunit/html/wp-html-tag-processor-test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpunit/html/wp-html-tag-processor-test.php b/phpunit/html/wp-html-tag-processor-test.php index 263df4f6695fe5..6ad4f6872ae3b5 100644 --- a/phpunit/html/wp-html-tag-processor-test.php +++ b/phpunit/html/wp-html-tag-processor-test.php @@ -154,8 +154,8 @@ public function test_attributes_parser_treats_slash_as_attribute_separator() { /** * @covers WP_HTML_Tag_Processor::get_attributes_by_prefix */ - public function test_get_attributes_by_prefix_returns_set_of_prefixed_attributes() { - $p = new WP_HTML_Tag_Processor( '
Test
' ); + public function test_get_attributes_by_prefix_returns_set_of_matching_attributes_in_lowercase() { + $p = new WP_HTML_Tag_Processor( '
Test
' ); $p->next_tag(); $this->assertSame( array(