Skip to content

Commit

Permalink
Ensure HTML fragments get proper encoding for DOM parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Aug 22, 2019
1 parent d685ba5 commit e6260da
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -2065,12 +2065,21 @@ public static function prepare_response( $response, $args = [] ) {
* See <https://www.w3.org/International/questions/qa-html-encoding-declarations>.
*/
if ( ! preg_match( '#<meta[^>]+charset=#i', substr( $response, 0, 1024 ) ) ) {
$meta_charset = sprintf( '<meta charset="%s">', esc_attr( get_bloginfo( 'charset' ) ) );

$response = preg_replace(
'/(<head[^>]*>)/i',
'$1' . sprintf( '<meta charset="%s">', esc_attr( get_bloginfo( 'charset' ) ) ),
'$1' . $meta_charset,
$response,
1
1,
$count
);

// Ensure charset is added for document fragments.
// @todo Should not the absense of a <head> indicate that this is not intended to be an AMP page in the first place? Similar to checking for JSON response?
if ( 0 === $count ) {
$response = $meta_charset . $response;
}
}

$dom = AMP_DOM_Utils::get_dom( $response );
Expand Down

0 comments on commit e6260da

Please sign in to comment.