Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 7 additions & 3 deletions src/wp-includes/html-api/class-wp-html-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1760,14 +1760,19 @@ private function step_in_head(): bool {
case '+META':
$this->insert_html_element( $this->state->current_token );

// All following conditions depend on "tentative" encoding confidence.
if ( 'tentative' !== $this->state->encoding_confidence ) {
return true;
}

/*
* > If the active speculative HTML parser is null, then:
* > - If the element has a charset attribute, and getting an encoding from
* > its value results in an encoding, and the confidence is currently
* > tentative, then change the encoding to the resulting encoding.
*/
$charset = $this->get_attribute( 'charset' );
if ( is_string( $charset ) && 'tentative' === $this->state->encoding_confidence ) {
if ( is_string( $charset ) ) {
$this->bail( 'Cannot yet process META tags with charset to determine encoding.' );
}

Expand All @@ -1784,8 +1789,7 @@ private function step_in_head(): bool {
if (
is_string( $http_equiv ) &&
is_string( $content ) &&
0 === strcasecmp( $http_equiv, 'Content-Type' ) &&
'tentative' === $this->state->encoding_confidence
0 === strcasecmp( $http_equiv, 'Content-Type' )
) {
$this->bail( 'Cannot yet process META tags with http-equiv Content-Type to determine encoding.' );
}
Expand Down
77 changes: 77 additions & 0 deletions tests/phpunit/tests/html-api/wpHtmlProcessorMetaTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Unit tests covering WP_HTML_Processor META tag handling.
*
* @package WordPress
* @subpackage HTML-API
*
* @since 6.9
*
* @group html-api
*
* @coversDefaultClass WP_HTML_Processor
*/
class Tests_HtmlApi_WpHtmlProcessorMetaTag extends WP_UnitTestCase {
/**
* Data provider.
*/
public static function data_supported_meta_tags(): array {
return array(
'No attributes' => array( '<meta>' ),
'Unrelated attributes' => array( '<meta not-charset="OK">' ),
'Boolean charset' => array( '<meta charset>' ),
'HTTP Equiv: accept' => array( '<meta http-equiv="accept" content="">' ),
'HTTP Equiv: content-type, no content' => array( '<meta http-equiv="content-type">' ),
'Boolean HTTP Equiv' => array( '<meta http-equiv content="">' ),
);
}

/**
* Ensures that META tags correctly handle encoding confidence.
*
* @ticket 63738
*
* @dataProvider data_supported_meta_tags
*/
public function test_supported_meta_tag( string $html ) {
$html = '<!DOCTYPE html>' . $html;
$processor = new class($html) extends WP_HTML_Processor {
public function __construct( $html ) {
parent::__construct( $html, parent::CONSTRUCTOR_UNLOCK_CODE );
}
};

$this->assertTrue( $processor->next_tag( 'META' ) );
}

/**
* Data provider.
*/
public function data_unsupported_meta_tags(): array {
return array(
'With charset' => array( '<meta charset="utf8">', 'Cannot yet process META tags with charset to determine encoding.' ),
'With CHARSET' => array( '<meta CHARSET="utf8">', 'Cannot yet process META tags with charset to determine encoding.' ),
'With http-equiv' => array( '<meta http-equiv="content-type" content="">', 'Cannot yet process META tags with http-equiv Content-Type to determine encoding.' ),
'With http-equiv and content' => array( '<meta http-equiv="Content-Type" content="UTF-8">', 'Cannot yet process META tags with http-equiv Content-Type to determine encoding.' ),
);
}

/**
* Ensures that unsupported encoding META tags bail.
*
* @ticket 63738
*
* @dataProvider data_unsupported_meta_tags
*/
public function test_unsupported_meta_tags( string $html, string $unsupported_message ) {
$html = '<!DOCTYPE html>' . $html;
$processor = new class($html) extends WP_HTML_Processor {
public function __construct( $html ) {
parent::__construct( $html, parent::CONSTRUCTOR_UNLOCK_CODE );
}
};

$this->assertFalse( $processor->next_tag( 'META' ) );
$this->assertInstanceOf( WP_HTML_Unsupported_Exception::class, $processor->get_unsupported_exception() );
}
}
Loading