Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect <meta charset> tags #12759

Merged
merged 5 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions includes/AMP/Output_Buffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
namespace Google\Web_Stories\AMP;

use Google\Web_Stories\Context;
use Google\Web_Stories\Exception\SanitizationException;
use Google\Web_Stories\Infrastructure\Conditional;
use Google\Web_Stories\Service_Base;
use Google\Web_Stories_Dependencies\AmpProject\Dom\Document;
Expand Down Expand Up @@ -221,7 +222,7 @@ public function prepare_response( string $response ): string {
$dom = Document::fromHtml( $response );

if ( ! $dom instanceof Document ) {
return $this->render_error_page( \Google\Web_Stories\Exception\SanitizationException::from_document_parse_error() );
return $this->render_error_page( SanitizationException::from_document_parse_error() );
}

$this->sanitization->sanitize_document( $dom );
Expand All @@ -237,10 +238,12 @@ public function prepare_response( string $response ): string {
*
* @param Throwable $throwable Exception or (as of PHP7) Error.
* @return string Error page.
*
* @todo Improve error message.
*/
private function render_error_page( Throwable $throwable ): string {
return esc_html__( 'There was an error generating the web story, probably because of a server misconfiguration. Try contacting your hosting provider or open a new support request.', 'web-stories' );
return esc_html__( 'There was an error generating the web story, probably because of a server misconfiguration. Try contacting your hosting provider or open a new support request.', 'web-stories' ) .
"\n" .
"\n" .
// translators: 1: error message. 2: location.
sprintf( esc_html__( 'Error message: %1$s (%2$s)', 'web-stories' ), $throwable->getMessage(), $throwable->getFile() . ':' . $throwable->getLine() );
}
}
24 changes: 16 additions & 8 deletions includes/Renderer/Story/HTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
namespace Google\Web_Stories\Renderer\Story;

use Google\Web_Stories\Model\Story;
use Google\Web_Stories_Dependencies\AmpProject\Dom\Document;

/**
* Class HTML
Expand All @@ -42,13 +41,6 @@ class HTML {
*/
protected Story $story;

/**
* Document instance.
*
* @var Document Document instance.
*/
protected Document $document;

/**
* HTML constructor.
*
Expand All @@ -69,6 +61,7 @@ public function __construct( Story $story ) {
*/
public function render(): string {
$markup = $this->story->get_markup();
$markup = $this->fix_incorrect_charset( $markup );
$markup = $this->fix_malformed_script_link_tags( $markup );
$markup = $this->replace_html_head( $markup );
$markup = wp_replace_insecure_home_url( $markup );
Expand All @@ -78,6 +71,21 @@ public function render(): string {
return $markup;
}

/**
* Fix incorrect <meta charset> tags.
*
* React/JSX outputs the charset attribute name as "charSet",
* but libdom and the AMP toolbox only recognize lowercase "charset"
*
* @since 1.28.0
*
* @param string $content Story markup.
* @return string Filtered content
*/
public function fix_incorrect_charset( string $content ): string {
return (string) preg_replace( '/<meta charSet="utf-8"\s?\/>/i', '<meta charset="utf-8"/>', $content );
}

/**
* Fix malformed <a> tags in the <head>.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/phpunit/integration/tests/Renderer/Story/HTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,20 @@ public function test_print_social_share_filter(): void {
$this->assertSame( $source, $actual );
}

/**
* @covers ::fix_incorrect_charset
*/
public function test_fix_incorrect_charset(): void {
$source = '<html><head><meta charSet="utf-8" /></head><body><amp-story></amp-story></body></html>';

$story = new Story();
$renderer = new \Google\Web_Stories\Renderer\Story\HTML( $story );

$actual = $this->call_private_method( $renderer, 'fix_incorrect_charset', [ $source ] );

$this->assertStringContainsString( '<meta charset="utf-8"/>', $actual );
}

/**
* @covers ::fix_malformed_script_link_tags
*/
Expand Down