Skip to content

Commit

Permalink
Normalize PI-Node lookalike behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
dmsnell committed Jan 23, 2024
1 parent d62099b commit 54d644f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
17 changes: 13 additions & 4 deletions src/wp-includes/html-api/class-wp-html-tag-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1818,7 +1818,6 @@ private function parse_next_tag() {
if ( 0 < $pi_target_length ) {
$pi_target_length += strspn( $comment_text, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:_-.', $pi_target_length );

$this->parser_state = self::STATE_COMMENT;
$this->comment_type = self::COMMENT_AS_PI_NODE_LOOKALIKE;
$this->tag_name_starts_at = $this->token_starts_at + 2;
$this->tag_name_length = $pi_target_length;
Expand Down Expand Up @@ -2545,13 +2544,24 @@ public function get_attribute_names_with_prefix( $prefix ) {
* @return string|null Name of currently matched tag in input HTML, or `null` if none found.
*/
public function get_tag() {
if ( self::STATE_MATCHED_TAG !== $this->parser_state ) {
if ( null === $this->tag_name_starts_at ) {
return null;
}

$tag_name = substr( $this->html, $this->tag_name_starts_at, $this->tag_name_length );

return strtoupper( $tag_name );
if ( self::STATE_MATCHED_TAG === $this->parser_state ) {
return strtoupper( $tag_name );
}

if (
self::STATE_COMMENT === $this->parser_state &&
self::COMMENT_AS_PI_NODE_LOOKALIKE === $this->get_comment_type()
) {
return $tag_name;
}

return null;
}

/**
Expand Down Expand Up @@ -3397,7 +3407,6 @@ private function matches() {
*
* <!-->
* <!--->
* <!---->
*
* @since 6.5.0
*/
Expand Down
27 changes: 17 additions & 10 deletions tests/phpunit/tests/html-api/wpHtmlTagProcessor-token-scanning.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,15 @@ public function test_basic_assertion_cdata_section() {
$processor->next_token();

$this->assertSame(
'#cdata-section',
'#comment',
$processor->get_token_name(),
"Should have found CDATA section name but found {$processor->get_token_name()} instead."
"Should have found comment token but found {$processor->get_token_name()} instead."
);

$this->assertSame(
WP_HTML_Processor::COMMENT_AS_CDATA_LOOKALIKE,
$processor->get_comment_type(),
'Should have detected a CDATA-like invalid comment.'
);

$this->assertNull(
Expand Down Expand Up @@ -405,20 +411,21 @@ public function test_basic_assertion_processing_instruction() {
$processor->next_token();

$this->assertSame(
'#processing-instruction',
$processor->get_token_type(),
"Should have found PI node but found {$processor->get_token_type()} instead."
'#comment',
$processor->get_token_name(),
"Should have found comment token but found {$processor->get_token_name()} instead."
);

$this->assertSame(
'wp-bit',
$processor->get_token_name(),
"Should have found PI target as name but found {$processor->get_token_name()} instead."
WP_HTML_Processor::COMMENT_AS_PI_NODE_LOOKALIKE,
$processor->get_comment_type(),
'Should have detected a Processing Instruction-like invalid comment.'
);

$this->assertNull(
$this->assertSame(
'wp-bit',
$processor->get_tag(),
'Should not have been able to query tag name on non-element token.'
"Should have found PI target as tag name but found {$processor->get_tag()} instead."
);

$this->assertNull(
Expand Down

0 comments on commit 54d644f

Please sign in to comment.