Skip to content

Commit

Permalink
Limit noscript placeholder replacements to the HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jun 26, 2018
1 parent 7fd0f69 commit 66e04d1
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions includes/utils/class-amp-dom-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,22 @@ public static function get_dom( $document ) {
* When appearing in the head element, a noscript can cause the head to close prematurely
* and the noscript gets moved to the body and anything after it which was in the head.
* See <https://stackoverflow.com/questions/39013102/why-does-noscript-move-into-body-tag-instead-of-head-tag>.
* This is limited to only running in the head element because this is where the problem lies,
* and it is important for the AMP_Script_Sanitizer to be able to access the noscript elements
* in the body otherwise.
*/
$document = preg_replace_callback(
'#<noscript[^>]*>.*?</noscript>#si',
function( $matches ) {
$placeholder = sprintf( '<!--noscript:%s-->', (string) wp_rand() );
AMP_DOM_Utils::$noscript_placeholder_comments[ $placeholder ] = $matches[0];
return $placeholder;
'#^.+?(?=<body#s',
function( $head_match ) {
return preg_replace_callback(
'#<noscript[^>]*>.*?</noscript>#si',
function( $matches ) {
$placeholder = sprintf( '<!--noscript:%s-->', (string) wp_rand() );
AMP_DOM_Utils::$noscript_placeholder_comments[ $placeholder ] = $matches[0];
return $placeholder;
},
$head_match
);
},
$document
);
Expand Down

0 comments on commit 66e04d1

Please sign in to comment.