Skip to content

Commit

Permalink
Fix the order in which ensure_required_markup() is called (#1041)
Browse files Browse the repository at this point in the history
* Fix the order in which ensure_required_markup() is called

* Check  is defined and add test case to check sanitizing of  non-AMP themes

* Fix test case

* Skip printing paths-scope because it causes push builds to be truncated (and fail) for unknown reason

See xwp/wp-dev-lib#270
  • Loading branch information
amedina authored Mar 28, 2018
1 parent b2e24ce commit d53af32
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 18 deletions.
1 change: 1 addition & 0 deletions .dev-lib
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ PATH_EXCLUDES_PATTERN=includes/lib/
DEFAULT_BASE_BRANCH=develop
ASSETS_DIR=wp-assets
PROJECT_SLUG=amp
SKIP_ECHO_PATHS_SCOPE=1

function after_wp_install {
echo "Installing REST API..."
Expand Down
2 changes: 1 addition & 1 deletion dev-lib
Submodule dev-lib updated 1 files
+1 −1 travis.script.sh
34 changes: 17 additions & 17 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -885,13 +885,6 @@ public static function print_amp_styles() {
* @param DOMDocument $dom Doc.
*/
public static function ensure_required_markup( DOMDocument $dom ) {
$xpath = new DOMXPath( $dom );

// First ensure the mandatory amp attribute is present on the html element, as otherwise it will be stripped entirely.
if ( ! $dom->documentElement->hasAttribute( 'amp' ) && ! $dom->documentElement->hasAttribute( '⚡️' ) ) {
$dom->documentElement->setAttribute( 'amp', '' );
}

$head = $dom->getElementsByTagName( 'head' )->item( 0 );
if ( ! $head ) {
$head = $dom->createElement( 'head' );
Expand Down Expand Up @@ -953,15 +946,6 @@ public static function ensure_required_markup( DOMDocument $dom ) {
) );
$head->appendChild( $rel_canonical );
}

// Make sure scripts from the body get moved to the head.
$scripts = array();
foreach ( $xpath->query( '//body//script[ @custom-element or @custom-template ]' ) as $script ) {
$scripts[] = $script;
}
foreach ( $scripts as $script ) {
$head->appendChild( $script );
}
}

/**
Expand Down Expand Up @@ -1098,10 +1082,26 @@ public static function prepare_response( $response, $args = array() ) {
}
$dom = AMP_DOM_Utils::get_dom( $response );

self::ensure_required_markup( $dom );
$xpath = new DOMXPath( $dom );

$head = $dom->getElementsByTagName( 'head' )->item( 0 );

if ( isset( $head ) ) {
// Make sure scripts from the body get moved to the head.
foreach ( $xpath->query( '//body//script[ @custom-element or @custom-template ]' ) as $script ) {
$head->appendChild( $script );
}
}

// Ensure the mandatory amp attribute is present on the html element, as otherwise it will be stripped entirely.
if ( ! $dom->documentElement->hasAttribute( 'amp' ) && ! $dom->documentElement->hasAttribute( '⚡️' ) ) {
$dom->documentElement->setAttribute( 'amp', '' );
}

$assets = AMP_Content_Sanitizer::sanitize_document( $dom, self::$sanitizer_classes, $args );

self::ensure_required_markup( $dom );

// @todo If 'utf-8' is not the blog charset, then we'll need to do some character encoding conversation or "entityification".
if ( 'utf-8' !== strtolower( get_bloginfo( 'charset' ) ) ) {
/* translators: %s is the charset of the current site */
Expand Down
39 changes: 39 additions & 0 deletions tests/test-class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,45 @@ public function test_prepare_response() {
$this->assertInstanceOf( 'DOMAttr', $removed_nodes['onclick'] );
}

/**
* Test validate_non_amp_theme.
*
* @global WP_Widget_Factory $wp_widget_factory
* @global WP_Scripts $wp_scripts
* @covers AMP_Theme_Support::prepare_response()
*/
public function test_validate_non_amp_theme() {
add_theme_support( 'amp' );
AMP_Theme_Support::init();
AMP_Theme_Support::finish_init();

ob_start();
?>
<!DOCTYPE html>
<html lang="en-US" class="no-js">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<?php wp_head(); ?>
</head>
<body>
<?php wp_print_scripts( 'amp-mathml' ); ?>
</body>
</html>
<?php
$original_html = trim( ob_get_clean() );
$sanitized_html = AMP_Theme_Support::prepare_response( $original_html );

// Invalid viewport meta tag is not present.
$this->assertNotContains( '<meta name="viewport" content="width=device-width">', $sanitized_html );

// Correct viewport meta tag was added.
$this->assertContains( '<meta name="viewport" content="width=device-width,minimum-scale=1">', $sanitized_html );

// MathML script was added.
$this->assertContains( '<script type="text/javascript" src="https://cdn.ampproject.org/v0/amp-mathml-latest.js" async custom-element="amp-mathml"></script>', $sanitized_html ); // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
}

/**
* Test prepare_response for bad/non-HTML.
*
Expand Down

0 comments on commit d53af32

Please sign in to comment.