Skip to content

Commit

Permalink
Fix span-of-dashes comment modifiable text
Browse files Browse the repository at this point in the history
  • Loading branch information
dmsnell committed Jan 15, 2024
1 parent 2481fcb commit 2523382
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 27 deletions.
9 changes: 7 additions & 2 deletions src/wp-includes/html-api/class-wp-html-tag-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1603,8 +1603,13 @@ private function parse_next_tag() {
*/
$this->parser_state = self::STATE_COMMENT;
$this->token_length = $closer_at + $span_of_dashes + 1 - $this->token_starts_at;
$this->text_starts_at = $this->token_starts_at + 4;
$this->text_length = max( 0, $closer_at - $this->text_starts_at );

// Only provide modifiable text if the token is long enough to contain it.
if ( $span_of_dashes >= 2 ) {
$this->text_starts_at = $this->token_starts_at + 4;
$this->text_length = $span_of_dashes - 2;
}

$this->bytes_already_parsed = $closer_at + $span_of_dashes + 1;
return true;
}
Expand Down
32 changes: 7 additions & 25 deletions tests/phpunit/tests/html-api/wpHtmlTagProcessor-token-scanning.php
Original file line number Diff line number Diff line change
Expand Up @@ -531,11 +531,13 @@ public function test_basic_assertion_common_comments( $html, $text ) {
*/
public function data_common_comments() {
return array(
'Shortest comment' => array( '<!-->', '' ),
'Short comment' => array( '<!--->', '' ),
'PI node without target' => array( '<? missing?>', ' missing?' ),
'Invalid PI node' => array( '<?/missing/>', '/missing/' ),
'Invalid ! directive' => array( '<!something else>', 'something else' ),
'Shortest comment' => array( '<!-->', '' ),
'Short comment' => array( '<!--->', '' ),
'Short comment w/o text' => array( '<!---->', '' ),
'Short comment with text' => array( '<!----->', '-' ),
'PI node without target' => array( '<? missing?>', ' missing?' ),
'Invalid PI node' => array( '<?/missing/>', '/missing/' ),
'Invalid ! directive' => array( '<!something else>', 'something else' ),
);
}

Expand Down Expand Up @@ -581,26 +583,6 @@ public function test_basic_assertion_html_comment() {
);
}

/**
* Ensures that <!-----> HTML comment is properly parsed.
*
* @ticket 60170
*
* @since 6.5.0
*
* @covers WP_HTML_Tag_Processor::next_token
*/
public function test_html_comment_single_dash() {
$processor = WP_HTML_Processor::create_fragment( '<!----->' );
$processor->next_token();

$this->assertSame(
'-',
$processor->get_modifiable_text(),
'Found incorrect modifiable text.'
);
}

/**
* Ensures that normative DOCTYPE elements are properly parsed.
*
Expand Down

0 comments on commit 2523382

Please sign in to comment.