Skip to content

Commit

Permalink
WP_HTML_Tag_Processor: Implement get_attribute_names()
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Dec 21, 2022
1 parent db0ff52 commit 8f6e866
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/experimental/html/class-wp-html-tag-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,32 @@ public function get_attribute( $name ) {
return html_entity_decode( $raw_value );
}

/**
* Returns the names of all attributes in the currently-opened tag.
*
* Example:
* <code>
* $p = new WP_HTML_Tag_Processor( '<div enabled class="test" data-test-id="14">Test</div>' );
* $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;
* </code>
*
* @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.
*
Expand Down
12 changes: 12 additions & 0 deletions phpunit/html/wp-html-tag-processor-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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( '<div enabled class="test" data-test-id="14">Test</div>' );
$p->next_tag();
$this->assertSame(
array( 'enabled', 'class', 'data-test-id' ),
$p->get_attribute_names()
);
}

/**
* @ticket 56299
*
Expand Down

1 comment on commit 8f6e866

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3748083959
📝 Reported issues:

Please sign in to comment.