From 82b76871ffde26bbfe4cafce6d8258a005d9011e Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sun, 29 Apr 2018 13:40:35 -0700 Subject: [PATCH 1/2] Move any content output during shutdown to be injected before closing body tag --- includes/class-amp-theme-support.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index d33e362945e..7bd72f17b41 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -1188,6 +1188,9 @@ public static function prepare_response( $response, $args = array() ) { ); } + // Move anything after , such as Query Monitor output added at shutdown, to be moved before . + $response = preg_replace( '#(.*)(.+)#s', '$2$1', $response ); + $dom = AMP_DOM_Utils::get_dom( $response ); $head = $dom->getElementsByTagName( 'head' )->item( 0 ); From b5948c53b81f9643e0eed1915de835cf4a0f80ba Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 19 Jun 2018 18:03:44 -0700 Subject: [PATCH 2/2] Move trailing content after to be placed before the --- includes/class-amp-theme-support.php | 19 ++++++++++++++++--- tests/test-class-amp-theme-support.php | 9 ++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index 7bd72f17b41..a7c78204a7f 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -1188,12 +1188,25 @@ public static function prepare_response( $response, $args = array() ) { ); } - // Move anything after , such as Query Monitor output added at shutdown, to be moved before . - $response = preg_replace( '#(.*)(.+)#s', '$2$1', $response ); - $dom = AMP_DOM_Utils::get_dom( $response ); $head = $dom->getElementsByTagName( 'head' )->item( 0 ); + // Move anything after , such as Query Monitor output added at shutdown, to be moved before . + $body = $dom->getElementsByTagName( 'body' )->item( 0 ); + if ( $body ) { + while ( $dom->documentElement->nextSibling ) { + // Trailing elements after will get wrapped in additional elements. + if ( 'html' === $dom->documentElement->nextSibling->nodeName ) { + while ( $dom->documentElement->nextSibling->firstChild ) { + $body->appendChild( $dom->documentElement->nextSibling->firstChild ); + } + $dom->removeChild( $dom->documentElement->nextSibling ); + } else { + $body->appendChild( $dom->documentElement->nextSibling ); + } + } + } + // Make sure scripts from the body get moved to the head. if ( isset( $head ) ) { $xpath = new DOMXPath( $dom ); diff --git a/tests/test-class-amp-theme-support.php b/tests/test-class-amp-theme-support.php index 5b09c0b5269..aeffad14281 100644 --- a/tests/test-class-amp-theme-support.php +++ b/tests/test-class-amp-theme-support.php @@ -1102,7 +1102,7 @@ public function test_prepare_response() { - + + @@ -1118,6 +1119,9 @@ public function test_prepare_response() { + +
+ gets moved. + $this->assertRegExp( '#\s*
\s*\s*\s*\s*$#s', $sanitized_html ); + $prepare_response_args = array( 'enable_response_caching' => true, );