diff --git a/.dev-lib b/.dev-lib index 6549fa32c46..663a5ce0043 100644 --- a/.dev-lib +++ b/.dev-lib @@ -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..." diff --git a/dev-lib b/dev-lib index 223f238eb74..f5161dd0bfb 160000 --- a/dev-lib +++ b/dev-lib @@ -1 +1 @@ -Subproject commit 223f238eb74dd2c65892b8fb6c1c06ae04e95234 +Subproject commit f5161dd0bfb184a8be7574b47192741d50e7520a diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index 1cfcc4f74cf..6373d2e6764 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -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' ); @@ -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 ); - } } /** @@ -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 */ diff --git a/tests/test-class-amp-theme-support.php b/tests/test-class-amp-theme-support.php index 0e1bd39df8e..439807a584d 100644 --- a/tests/test-class-amp-theme-support.php +++ b/tests/test-class-amp-theme-support.php @@ -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(); + ?> + + +
+ + + + + + + + + assertNotContains( '', $sanitized_html ); + + // Correct viewport meta tag was added. + $this->assertContains( '', $sanitized_html ); + + // MathML script was added. + $this->assertContains( '', $sanitized_html ); // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript + } + /** * Test prepare_response for bad/non-HTML. *