Skip to content

Commit 59fa804

Browse files
committed
HTML API: Simplify META tag processing.
META tag processing can be simplified in most cases. Add an early check and return to avoid additional processing. Developed in WordPress#9231. Props jonsurrell, dmsnell. See #63738. git-svn-id: https://develop.svn.wordpress.org/trunk@60502 602fd350-edb4-49c9-b593-d223f7449a82
1 parent fe8e074 commit 59fa804

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-3
lines changed

src/wp-includes/html-api/class-wp-html-processor.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,14 +1760,19 @@ private function step_in_head(): bool {
17601760
case '+META':
17611761
$this->insert_html_element( $this->state->current_token );
17621762

1763+
// All following conditions depend on "tentative" encoding confidence.
1764+
if ( 'tentative' !== $this->state->encoding_confidence ) {
1765+
return true;
1766+
}
1767+
17631768
/*
17641769
* > If the active speculative HTML parser is null, then:
17651770
* > - If the element has a charset attribute, and getting an encoding from
17661771
* > its value results in an encoding, and the confidence is currently
17671772
* > tentative, then change the encoding to the resulting encoding.
17681773
*/
17691774
$charset = $this->get_attribute( 'charset' );
1770-
if ( is_string( $charset ) && 'tentative' === $this->state->encoding_confidence ) {
1775+
if ( is_string( $charset ) ) {
17711776
$this->bail( 'Cannot yet process META tags with charset to determine encoding.' );
17721777
}
17731778

@@ -1784,8 +1789,7 @@ private function step_in_head(): bool {
17841789
if (
17851790
is_string( $http_equiv ) &&
17861791
is_string( $content ) &&
1787-
0 === strcasecmp( $http_equiv, 'Content-Type' ) &&
1788-
'tentative' === $this->state->encoding_confidence
1792+
0 === strcasecmp( $http_equiv, 'Content-Type' )
17891793
) {
17901794
$this->bail( 'Cannot yet process META tags with http-equiv Content-Type to determine encoding.' );
17911795
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Unit tests covering WP_HTML_Processor META tag handling.
4+
*
5+
* @package WordPress
6+
* @subpackage HTML-API
7+
*
8+
* @since 6.9
9+
*
10+
* @group html-api
11+
*
12+
* @coversDefaultClass WP_HTML_Processor
13+
*/
14+
class Tests_HtmlApi_WpHtmlProcessorMetaTag extends WP_UnitTestCase {
15+
/**
16+
* Data provider.
17+
*/
18+
public static function data_supported_meta_tags(): array {
19+
return array(
20+
'No attributes' => array( '<meta>' ),
21+
'Unrelated attributes' => array( '<meta not-charset="OK">' ),
22+
'Boolean charset' => array( '<meta charset>' ),
23+
'HTTP Equiv: accept' => array( '<meta http-equiv="accept" content="">' ),
24+
'HTTP Equiv: content-type, no content' => array( '<meta http-equiv="content-type">' ),
25+
'Boolean HTTP Equiv' => array( '<meta http-equiv content="">' ),
26+
);
27+
}
28+
29+
/**
30+
* Ensures that META tags correctly handle encoding confidence.
31+
*
32+
* @ticket 63738
33+
*
34+
* @dataProvider data_supported_meta_tags
35+
*/
36+
public function test_supported_meta_tag( string $html ) {
37+
$html = '<!DOCTYPE html>' . $html;
38+
$processor = new class($html) extends WP_HTML_Processor {
39+
public function __construct( $html ) {
40+
parent::__construct( $html, parent::CONSTRUCTOR_UNLOCK_CODE );
41+
}
42+
};
43+
44+
$this->assertTrue( $processor->next_tag( 'META' ) );
45+
}
46+
47+
/**
48+
* Data provider.
49+
*/
50+
public function data_unsupported_meta_tags(): array {
51+
return array(
52+
'With charset' => array( '<meta charset="utf8">', 'Cannot yet process META tags with charset to determine encoding.' ),
53+
'With CHARSET' => array( '<meta CHARSET="utf8">', 'Cannot yet process META tags with charset to determine encoding.' ),
54+
'With http-equiv' => array( '<meta http-equiv="content-type" content="">', 'Cannot yet process META tags with http-equiv Content-Type to determine encoding.' ),
55+
'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.' ),
56+
);
57+
}
58+
59+
/**
60+
* Ensures that unsupported encoding META tags bail.
61+
*
62+
* @ticket 63738
63+
*
64+
* @dataProvider data_unsupported_meta_tags
65+
*/
66+
public function test_unsupported_meta_tags( string $html, string $unsupported_message ) {
67+
$html = '<!DOCTYPE html>' . $html;
68+
$processor = new class($html) extends WP_HTML_Processor {
69+
public function __construct( $html ) {
70+
parent::__construct( $html, parent::CONSTRUCTOR_UNLOCK_CODE );
71+
}
72+
};
73+
74+
$this->assertFalse( $processor->next_tag( 'META' ) );
75+
$this->assertInstanceOf( WP_HTML_Unsupported_Exception::class, $processor->get_unsupported_exception() );
76+
}
77+
}

0 commit comments

Comments
 (0)