Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
WIP: HTML API: Expose self-closing flag in Tag Processor
  • Loading branch information
dmsnell committed Mar 7, 2023
commit 84e35efa24b291faf72cb92872c7eca0a1e939dd
8 changes: 8 additions & 0 deletions src/wp-includes/html-api/class-wp-html-tag-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1743,6 +1743,14 @@ public function get_tag() {
return strtoupper( $tag_name );
}

public function has_self_closing_flag() {
if ( ! $this->tag_name_starts_at ) {
return false;
}

return '/' === $this->html[ $this->tag_ends_at - 1 ];
}

/**
* Indicates if the current tag token is a tag closer.
*
Expand Down
32 changes: 32 additions & 0 deletions tests/phpunit/tests/html-api/wpHtmlTagProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,38 @@ public function test_get_tag_returns_open_tag_name() {
$this->assertSame( 'DIV', $p->get_tag(), 'Accessing an existing tag name did not return "div"' );
}

/**
* @ticket NEEDS TICKET
*
* @covers WP_HTML_Tag_Processor::has_self_closing_flag()
*
* @dataProvider data_has_self_closing_flag
*
* @param string $html input HTML whose first tag might contain the self-closing flag `/`.
* @param bool $flag_is_set whether the input HTML's first tag contains the self-closing flag.
*/
public function test_has_self_closing_flag_matches_input_html( $html, $flag_is_set ) {
$p = new WP_HTML_Tag_Processor( $html );
$p->next_tag( array( 'tag_closers' => 'visit' ) );

if ( $flag_is_set ) {
$this->assertTrue( $p->has_self_closing_flag(), 'Did not find the self-closing tag when it was present.' );
} else {
$this->assertFalse( $p->has_self_closing_flag(), 'Found the self-closing tag when it was absent.' );
}
}

public function data_has_self_closing_flag() {
return array(
array( '<div />', true ),
array( '<div>', false ),
array( '<img />', true ),
array( '<img>', false ),
array( '</textarea>', false ),
array( '</textarea />', true ),
);
}

/**
* @ticket 56299
*
Expand Down