From 4e4b9574b7dba9db6433ccd65bd506bda7f6f6cc Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 20 Dec 2022 17:50:10 +0100 Subject: [PATCH 01/15] WP_HTML_Tag_Processor: Implement get_attribute_names() --- .../html/class-wp-html-tag-processor.php | 26 +++++++++++++++++++ phpunit/html/wp-html-tag-processor-test.php | 12 +++++++++ 2 files changed, 38 insertions(+) diff --git a/lib/experimental/html/class-wp-html-tag-processor.php b/lib/experimental/html/class-wp-html-tag-processor.php index 0dfa57f30f2aa..7f8286db76654 100644 --- a/lib/experimental/html/class-wp-html-tag-processor.php +++ b/lib/experimental/html/class-wp-html-tag-processor.php @@ -1412,6 +1412,32 @@ public function get_attribute( $name ) { return html_entity_decode( $raw_value ); } + /** + * Returns the names of all attributes in the currently-opened tag. + * + * Example: + * + * $p = new WP_HTML_Tag_Processor( '
Test
' ); + * $p->next_tag( [ 'class_name' => 'test' ] ) === true; + * $p->get_attribute_names() === array( 'enabled', 'class', 'data-test-id' ); + * + * $p->next_tag( [] ) === false; + * $p->get_attribute_names() === null; + *
+ * + * @since 6.2.0 + * + * @param string $prefix Prefix of attributes whose value is requested. + * @return array|null List of attribute names, or `null` if not at a tag. + */ + function get_attribute_names() { + if ( null === $this->tag_name_starts_at ) { + return null; + } + + return array_keys( $this->attributes ); + } + /** * 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 e6b3d4d8071af..fafd02cc7b15b 100644 --- a/phpunit/html/wp-html-tag-processor-test.php +++ b/phpunit/html/wp-html-tag-processor-test.php @@ -151,6 +151,18 @@ 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_attribute_names + */ + public function test_get_attribute_names() { + $p = new WP_HTML_Tag_Processor( '
Test
' ); + $p->next_tag(); + $this->assertSame( + array( 'enabled', 'class', 'data-test-id' ), + $p->get_attribute_names() + ); + } + /** * @ticket 56299 * From 157301039737f0e1d784ac257f94f00613048db5 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 22 Dec 2022 11:53:46 +0100 Subject: [PATCH 02/15] Remove superfluous parameter comment --- lib/experimental/html/class-wp-html-tag-processor.php | 1 - 1 file changed, 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 7f8286db76654..22ff19a5e0f16 100644 --- a/lib/experimental/html/class-wp-html-tag-processor.php +++ b/lib/experimental/html/class-wp-html-tag-processor.php @@ -1427,7 +1427,6 @@ public function get_attribute( $name ) { * * @since 6.2.0 * - * @param string $prefix Prefix of attributes whose value is requested. * @return array|null List of attribute names, or `null` if not at a tag. */ function get_attribute_names() { From 8be4ac9844c350f91cc7486ba4edff4820d6b5b6 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 2 Jan 2023 14:45:02 +0100 Subject: [PATCH 03/15] Move test down --- phpunit/html/wp-html-tag-processor-test.php | 24 ++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/phpunit/html/wp-html-tag-processor-test.php b/phpunit/html/wp-html-tag-processor-test.php index fafd02cc7b15b..cd59df546ab16 100644 --- a/phpunit/html/wp-html-tag-processor-test.php +++ b/phpunit/html/wp-html-tag-processor-test.php @@ -151,18 +151,6 @@ 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_attribute_names - */ - public function test_get_attribute_names() { - $p = new WP_HTML_Tag_Processor( '
Test
' ); - $p->next_tag(); - $this->assertSame( - array( 'enabled', 'class', 'data-test-id' ), - $p->get_attribute_names() - ); - } - /** * @ticket 56299 * @@ -207,6 +195,18 @@ public function test_set_attribute_is_case_insensitive() { $this->assertEquals( '
Test
', $p->get_updated_html(), 'A case-insensitive set_attribute call did not update the existing attribute.' ); } + /** + * @covers WP_HTML_Tag_Processor::get_attribute_names + */ + public function test_get_attribute_names() { + $p = new WP_HTML_Tag_Processor( '
Test
' ); + $p->next_tag(); + $this->assertSame( + array( 'enabled', 'class', 'data-test-id' ), + $p->get_attribute_names() + ); + } + /** * @ticket 56299 * From 8c51859ddc4eadb1ae483cd692f0b41f966247c6 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 2 Jan 2023 15:09:12 +0100 Subject: [PATCH 04/15] Add test for get_attribute() when in closing tag --- phpunit/html/wp-html-tag-processor-test.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/phpunit/html/wp-html-tag-processor-test.php b/phpunit/html/wp-html-tag-processor-test.php index cd59df546ab16..6962ed8ddf211 100644 --- a/phpunit/html/wp-html-tag-processor-test.php +++ b/phpunit/html/wp-html-tag-processor-test.php @@ -74,6 +74,19 @@ public function test_get_attribute_returns_null_when_not_in_open_tag() { $this->assertNull( $p->get_attribute( 'class' ), 'Accessing an attribute of a non-existing tag did not return null' ); } + /** + * @ticket 56299 + * + * @covers next_tag + * @covers get_attribute + */ + public function test_get_attribute_returns_null_when_in_closing_tag() { + $p = new WP_HTML_Tag_Processor( '
Test
' ); + $this->assertTrue( $p->next_tag( 'div' ), 'Querying an existing tag did not return true' ); + $this->assertTrue( $p->next_tag( array( 'tag_closers' => 'visit' ) ), 'Querying an existing closing tag did not return true' ); + $this->assertNull( $p->get_attribute( 'class' ), 'Accessing an attribute of a closing tag did not return null' ); + } + /** * @ticket 56299 * From ca06c6483a8a9399f401f5bf5a35bc4c463e2382 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 2 Jan 2023 15:20:44 +0100 Subject: [PATCH 05/15] Add more get_attribute_names tests() --- phpunit/html/wp-html-tag-processor-test.php | 50 ++++++++++++++++++++- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/phpunit/html/wp-html-tag-processor-test.php b/phpunit/html/wp-html-tag-processor-test.php index 6962ed8ddf211..eddfc68fbe845 100644 --- a/phpunit/html/wp-html-tag-processor-test.php +++ b/phpunit/html/wp-html-tag-processor-test.php @@ -209,9 +209,55 @@ public function test_set_attribute_is_case_insensitive() { } /** - * @covers WP_HTML_Tag_Processor::get_attribute_names + * @ticket 56299 + * + * @covers get_attribute_names + */ + public function test_get_attribute_names_returns_null_before_finding_tags() { + $p = new WP_HTML_Tag_Processor( '
Test
' ); + $this->assertNull( $p->get_attribute_names() ); + } + + /** + * @ticket 56299 + * + * @covers get_attribute + */ + public function test_get_attribute_names_returns_null_when_not_in_open_tag() { + $p = new WP_HTML_Tag_Processor( '
Test
' ); + $p->next_tag( 'p' ); + $this->assertNull( $p->get_attribute_names(), 'Accessing attributes of a non-existing tag did not return null' ); + } + + /** + * @ticket 56299 + * + * @covers get_attribute_names + */ + public function test_get_attribute_names_returns_null_when_in_closing_tag() { + $p = new WP_HTML_Tag_Processor( '
Test
' ); + $p->next_tag( 'div' ); + $p->next_tag( array( 'tag_closers' => 'visit' ) ); + $this->assertNull( $p->get_attribute_names(), 'Accessing attributes of a closing tag did not return null' ); + } + + /** + * @ticket 56299 + * + * @covers get_attribute_names + */ + public function test_get_attribute_names_returns_empty_array_when_no_attributes_present() { + $p = new WP_HTML_Tag_Processor( '
Test
' ); + $p->next_tag( 'div' ); + $this->assertSame( array(), $p->get_attribute_names(), 'Accessing the attributes on a tag without any did not return an empty array' ); + } + + /** + * @ticket 56299 + * + * @covers get_attribute_names */ - public function test_get_attribute_names() { + public function test_get_attribute_names_returns_attribute_names() { $p = new WP_HTML_Tag_Processor( '
Test
' ); $p->next_tag(); $this->assertSame( From 2dbf838a66fdb6a5e67194b2ca277932c5bbd508 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 2 Jan 2023 15:20:53 +0100 Subject: [PATCH 06/15] Fix implementation --- 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 22ff19a5e0f16..e50cb3d43f76e 100644 --- a/lib/experimental/html/class-wp-html-tag-processor.php +++ b/lib/experimental/html/class-wp-html-tag-processor.php @@ -1430,7 +1430,7 @@ public function get_attribute( $name ) { * @return array|null List of attribute names, or `null` if not at a tag. */ function get_attribute_names() { - if ( null === $this->tag_name_starts_at ) { + if ( $this->is_closing_tag || null === $this->tag_name_starts_at ) { return null; } From 5b248c08e6228422f4aefd6bceab5c724c6841f7 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 2 Jan 2023 15:58:57 +0100 Subject: [PATCH 07/15] Verify that get_attribute_names finds attribute added by set_attribute --- phpunit/html/wp-html-tag-processor-test.php | 23 +++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/phpunit/html/wp-html-tag-processor-test.php b/phpunit/html/wp-html-tag-processor-test.php index eddfc68fbe845..56b7f312971cf 100644 --- a/phpunit/html/wp-html-tag-processor-test.php +++ b/phpunit/html/wp-html-tag-processor-test.php @@ -266,6 +266,29 @@ public function test_get_attribute_names_returns_attribute_names() { ); } + /** + * @ticket 56299 + * + * @covers set_attribute + * @covers get_updated_html + * @covers get_attribute_names + */ + public function test_get_attribute_names_returns_attribute_added_by_set_attribute() { + $p = new WP_HTML_Tag_Processor( '
Test
' ); + $p->next_tag(); + $p->set_attribute( 'test-attribute', 'test-value' ); + $this->assertSame( + '
Test
', + $p->get_updated_html(), + "Updated HTML doesn't include attribute added via set_attribute" + ); + $this->assertSame( + array( 'test-attribute', 'class' ), + $p->get_attribute_names(), + "Accessing attribute names doesn't find attribute added via set_attribute" + ); + } + /** * @ticket 56299 * From 5f3e9ff2e526adca74e50df25cb0d2a519a11265 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 2 Jan 2023 16:31:56 +0100 Subject: [PATCH 08/15] Replace get_attribute_names with get_attribute_names_with_prefix --- .../html/class-wp-html-tag-processor.php | 20 ++++--- phpunit/html/wp-html-tag-processor-test.php | 54 +++++++++---------- 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/lib/experimental/html/class-wp-html-tag-processor.php b/lib/experimental/html/class-wp-html-tag-processor.php index e50cb3d43f76e..c6ecaef5155ca 100644 --- a/lib/experimental/html/class-wp-html-tag-processor.php +++ b/lib/experimental/html/class-wp-html-tag-processor.php @@ -1413,28 +1413,36 @@ public function get_attribute( $name ) { } /** - * Returns the names of all attributes in the currently-opened tag. + * Returns the names of all attributes matching a given prefix in the currently-opened tag. * * Example: * - * $p = new WP_HTML_Tag_Processor( '
Test
' ); + * $p = new WP_HTML_Tag_Processor( '
Test
' ); * $p->next_tag( [ 'class_name' => 'test' ] ) === true; - * $p->get_attribute_names() === array( 'enabled', 'class', 'data-test-id' ); + * $p->get_attribute_names_with_prefix() === array( 'data-enabled', 'data-test-id' ); * * $p->next_tag( [] ) === false; - * $p->get_attribute_names() === null; + * $p->get_attribute_names_with_prefix() === null; *
* * @since 6.2.0 * * @return array|null List of attribute names, or `null` if not at a tag. */ - function get_attribute_names() { + function get_attribute_names_with_prefix( $prefix ) { if ( $this->is_closing_tag || null === $this->tag_name_starts_at ) { return null; } - return array_keys( $this->attributes ); + $comparable = strtolower( $prefix ); + + $matches = array_filter( + array_keys( $this->attributes ), + function( $attr ) use ( $comparable ) { + return str_starts_with( $attr, $comparable ); + } + ); + return array_values( $matches ); } /** diff --git a/phpunit/html/wp-html-tag-processor-test.php b/phpunit/html/wp-html-tag-processor-test.php index 56b7f312971cf..6ae8d85ecc5c9 100644 --- a/phpunit/html/wp-html-tag-processor-test.php +++ b/phpunit/html/wp-html-tag-processor-test.php @@ -211,58 +211,58 @@ public function test_set_attribute_is_case_insensitive() { /** * @ticket 56299 * - * @covers get_attribute_names + * @covers get_attribute_names_with_prefix */ - public function test_get_attribute_names_returns_null_before_finding_tags() { - $p = new WP_HTML_Tag_Processor( '
Test
' ); - $this->assertNull( $p->get_attribute_names() ); + public function test_get_attribute_names_with_prefix_returns_null_before_finding_tags() { + $p = new WP_HTML_Tag_Processor( '
Test
' ); + $this->assertNull( $p->get_attribute_names_with_prefix( 'data-' ) ); } /** * @ticket 56299 * - * @covers get_attribute + * @covers get_attribute_names_with_prefix */ - public function test_get_attribute_names_returns_null_when_not_in_open_tag() { - $p = new WP_HTML_Tag_Processor( '
Test
' ); + public function test_get_attribute_names_with_prefix_returns_null_when_not_in_open_tag() { + $p = new WP_HTML_Tag_Processor( '
Test
' ); $p->next_tag( 'p' ); - $this->assertNull( $p->get_attribute_names(), 'Accessing attributes of a non-existing tag did not return null' ); + $this->assertNull( $p->get_attribute_names_with_prefix( 'data-' ), 'Accessing attributes of a non-existing tag did not return null' ); } /** * @ticket 56299 * - * @covers get_attribute_names + * @covers get_attribute_names_with_prefix */ - public function test_get_attribute_names_returns_null_when_in_closing_tag() { - $p = new WP_HTML_Tag_Processor( '
Test
' ); + public function test_get_attribute_names_with_prefix_returns_null_when_in_closing_tag() { + $p = new WP_HTML_Tag_Processor( '
Test
' ); $p->next_tag( 'div' ); $p->next_tag( array( 'tag_closers' => 'visit' ) ); - $this->assertNull( $p->get_attribute_names(), 'Accessing attributes of a closing tag did not return null' ); + $this->assertNull( $p->get_attribute_names_with_prefix( 'data-' ), 'Accessing attributes of a closing tag did not return null' ); } /** * @ticket 56299 * - * @covers get_attribute_names + * @covers get_attribute_names_with_prefix */ - public function test_get_attribute_names_returns_empty_array_when_no_attributes_present() { + public function test_get_attribute_names_with_prefix_returns_empty_array_when_no_attributes_present() { $p = new WP_HTML_Tag_Processor( '
Test
' ); $p->next_tag( 'div' ); - $this->assertSame( array(), $p->get_attribute_names(), 'Accessing the attributes on a tag without any did not return an empty array' ); + $this->assertSame( array(), $p->get_attribute_names_with_prefix( 'data-' ), 'Accessing the attributes on a tag without any did not return an empty array' ); } /** * @ticket 56299 * - * @covers get_attribute_names + * @covers get_attribute_names_with_prefix */ - public function test_get_attribute_names_returns_attribute_names() { - $p = new WP_HTML_Tag_Processor( '
Test
' ); + public function test_get_attribute_names_with_prefix_returns_attribute_names() { + $p = new WP_HTML_Tag_Processor( '
Test
' ); $p->next_tag(); $this->assertSame( - array( 'enabled', 'class', 'data-test-id' ), - $p->get_attribute_names() + array( 'data-enabled', 'data-test-id' ), + $p->get_attribute_names_with_prefix( 'data-' ) ); } @@ -271,20 +271,20 @@ public function test_get_attribute_names_returns_attribute_names() { * * @covers set_attribute * @covers get_updated_html - * @covers get_attribute_names + * @covers get_attribute_names_with_prefix */ - public function test_get_attribute_names_returns_attribute_added_by_set_attribute() { - $p = new WP_HTML_Tag_Processor( '
Test
' ); + public function test_get_attribute_names_with_prefix_returns_attribute_added_by_set_attribute() { + $p = new WP_HTML_Tag_Processor( '
Test
' ); $p->next_tag(); - $p->set_attribute( 'test-attribute', 'test-value' ); + $p->set_attribute( 'data-test-id', '14' ); $this->assertSame( - '
Test
', + '
Test
', $p->get_updated_html(), "Updated HTML doesn't include attribute added via set_attribute" ); $this->assertSame( - array( 'test-attribute', 'class' ), - $p->get_attribute_names(), + array( 'data-test-id', 'data-foo' ), + $p->get_attribute_names_with_prefix( 'data-' ), "Accessing attribute names doesn't find attribute added via set_attribute" ); } From 0c614987457b5000a78791d0a5810acf7ec86b23 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 2 Jan 2023 16:44:05 +0100 Subject: [PATCH 09/15] Check for case-insensitive matching --- 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 6ae8d85ecc5c9..6750410f26d43 100644 --- a/phpunit/html/wp-html-tag-processor-test.php +++ b/phpunit/html/wp-html-tag-processor-test.php @@ -257,8 +257,8 @@ public function test_get_attribute_names_with_prefix_returns_empty_array_when_no * * @covers get_attribute_names_with_prefix */ - public function test_get_attribute_names_with_prefix_returns_attribute_names() { - $p = new WP_HTML_Tag_Processor( '
Test
' ); + public function test_get_attribute_names_with_prefix_returns_matching_attribute_names_in_lowercase() { + $p = new WP_HTML_Tag_Processor( '
Test
' ); $p->next_tag(); $this->assertSame( array( 'data-enabled', 'data-test-id' ), From a9d8ec3720ad0ff427aca5470ee963fc24a3eb05 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 3 Jan 2023 11:23:39 +0100 Subject: [PATCH 10/15] Document prefix param --- lib/experimental/html/class-wp-html-tag-processor.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/experimental/html/class-wp-html-tag-processor.php b/lib/experimental/html/class-wp-html-tag-processor.php index c6ecaef5155ca..dfec49f770e14 100644 --- a/lib/experimental/html/class-wp-html-tag-processor.php +++ b/lib/experimental/html/class-wp-html-tag-processor.php @@ -1427,6 +1427,7 @@ public function get_attribute( $name ) { * * @since 6.2.0 * + * @param string $prefix Prefix of requested attribute names. * @return array|null List of attribute names, or `null` if not at a tag. */ function get_attribute_names_with_prefix( $prefix ) { From 8a8c1e2bd9a595ae0a144fe96b2189ac0a910b0d Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 5 Jan 2023 16:51:09 +0100 Subject: [PATCH 11/15] Forgot to include arg in examples --- lib/experimental/html/class-wp-html-tag-processor.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/experimental/html/class-wp-html-tag-processor.php b/lib/experimental/html/class-wp-html-tag-processor.php index dfec49f770e14..1599b8230f1bd 100644 --- a/lib/experimental/html/class-wp-html-tag-processor.php +++ b/lib/experimental/html/class-wp-html-tag-processor.php @@ -1419,10 +1419,10 @@ public function get_attribute( $name ) { * * $p = new WP_HTML_Tag_Processor( '
Test
' ); * $p->next_tag( [ 'class_name' => 'test' ] ) === true; - * $p->get_attribute_names_with_prefix() === array( 'data-enabled', 'data-test-id' ); + * $p->get_attribute_names_with_prefix( 'data-' ) === array( 'data-enabled', 'data-test-id' ); * * $p->next_tag( [] ) === false; - * $p->get_attribute_names_with_prefix() === null; + * $p->get_attribute_names_with_prefix( 'data-' ) === null; *
* * @since 6.2.0 From 263cfa13c1bddec3149d6eda9f6957ba90a1ed05 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 5 Jan 2023 17:00:47 +0100 Subject: [PATCH 12/15] Polish PHPDoc --- .../html/class-wp-html-tag-processor.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/experimental/html/class-wp-html-tag-processor.php b/lib/experimental/html/class-wp-html-tag-processor.php index 1599b8230f1bd..d56556c98f2e7 100644 --- a/lib/experimental/html/class-wp-html-tag-processor.php +++ b/lib/experimental/html/class-wp-html-tag-processor.php @@ -1413,11 +1413,20 @@ public function get_attribute( $name ) { } /** - * Returns the names of all attributes matching a given prefix in the currently-opened tag. + * Returns the (lowercase) names of all attributes matching a given prefix in the currently-opened tag. + * + * Note that matching is case-insensitive. This is in accordance with the spec: + * + * > There must never be two or more attributes on + * > the same start tag whose names are an ASCII + * > case-insensitive match for each other. + * - HTML 5 spec + * + * @see https://html.spec.whatwg.org/multipage/syntax.html#attributes-2:ascii-case-insensitive * * Example: * - * $p = new WP_HTML_Tag_Processor( '
Test
' ); + * $p = new WP_HTML_Tag_Processor( '
Test
' ); * $p->next_tag( [ 'class_name' => 'test' ] ) === true; * $p->get_attribute_names_with_prefix( 'data-' ) === array( 'data-enabled', 'data-test-id' ); * From a4b10719aa84eedd4504f36cf2c7b21f7de8cc19 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 5 Jan 2023 17:25:58 +0100 Subject: [PATCH 13/15] Optimize --- lib/experimental/html/class-wp-html-tag-processor.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/experimental/html/class-wp-html-tag-processor.php b/lib/experimental/html/class-wp-html-tag-processor.php index d56556c98f2e7..2c12ce6ac2ea5 100644 --- a/lib/experimental/html/class-wp-html-tag-processor.php +++ b/lib/experimental/html/class-wp-html-tag-processor.php @@ -1447,12 +1447,13 @@ function get_attribute_names_with_prefix( $prefix ) { $comparable = strtolower( $prefix ); $matches = array_filter( - array_keys( $this->attributes ), + $this->attributes, function( $attr ) use ( $comparable ) { return str_starts_with( $attr, $comparable ); - } + }, + ARRAY_FILTER_USE_KEY ); - return array_values( $matches ); + return array_keys( $matches ); } /** From 37c94c3c6d02e065718d8cf46592629a9c3552ad Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 9 Jan 2023 11:02:20 +0100 Subject: [PATCH 14/15] Use foreach --- .../html/class-wp-html-tag-processor.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/experimental/html/class-wp-html-tag-processor.php b/lib/experimental/html/class-wp-html-tag-processor.php index 2c12ce6ac2ea5..85d14277fd584 100644 --- a/lib/experimental/html/class-wp-html-tag-processor.php +++ b/lib/experimental/html/class-wp-html-tag-processor.php @@ -1446,14 +1446,13 @@ function get_attribute_names_with_prefix( $prefix ) { $comparable = strtolower( $prefix ); - $matches = array_filter( - $this->attributes, - function( $attr ) use ( $comparable ) { - return str_starts_with( $attr, $comparable ); - }, - ARRAY_FILTER_USE_KEY - ); - return array_keys( $matches ); + $matches = array(); + foreach ( array_keys( $this->attributes ) as $attr_name ) { + if ( str_starts_with( $attr_name, $comparable ) ) { + $matches[] = $attr_name; + } + } + return $matches; } /** From ca460275a1c883069d0e09a7fb35695b00b9f1ec Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 10 Jan 2023 16:08:58 +0100 Subject: [PATCH 15/15] Remove parens around '(lowercase)' --- 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 85d14277fd584..9539f0d626e9d 100644 --- a/lib/experimental/html/class-wp-html-tag-processor.php +++ b/lib/experimental/html/class-wp-html-tag-processor.php @@ -1413,7 +1413,7 @@ public function get_attribute( $name ) { } /** - * Returns the (lowercase) names of all attributes matching a given prefix in the currently-opened tag. + * Returns the lowercase names of all attributes matching a given prefix in the currently-opened tag. * * Note that matching is case-insensitive. This is in accordance with the spec: *