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

Fixes #4759 Hide scripts from the buffer #4838

Merged
merged 10 commits into from
Apr 11, 2022
Merged
5 changes: 1 addition & 4 deletions inc/Dependencies/RocketLazyload/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ class Image {
* @return string
*/
public function lazyloadImages( $html, $buffer, $use_native = true ) {
$clean_buffer = preg_replace( '/<script\b(?:[^>]*)>(?:.+)?<\/script>/Umsi', '', $html );
$clean_buffer = preg_replace( '#<noscript>(?:.+)</noscript>#Umsi', '', $clean_buffer );

if ( ! preg_match_all( '#<img(?<atts>\s.+)\s?/?>#iUs', $clean_buffer, $images, PREG_SET_ORDER ) ) {
if ( ! preg_match_all( '#<img(?<atts>\s.+)\s?/?>#iUs', $buffer, $images, PREG_SET_ORDER ) ) {
return $html;
}

Expand Down
43 changes: 2 additions & 41 deletions inc/Engine/CriticalPath/CriticalCSSSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace WP_Rocket\Engine\CriticalPath;

use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Optimization\RegexTrait;
use WP_Rocket\Event_Management\Subscriber_Interface;
use WP_Filesystem_Direct;

Expand All @@ -12,7 +13,7 @@
* @since 3.3
*/
class CriticalCSSSubscriber implements Subscriber_Interface {

use RegexTrait;
/**
* Instance of Critical CSS.
*
Expand Down Expand Up @@ -750,44 +751,4 @@ public function stop_critical_css_generation() {
delete_transient( 'rocket_critical_css_generation_process_running' );
delete_transient( 'rocket_critical_css_generation_process_complete' );
}

/**
* Hides unwanted blocks from the HTML to be parsed.
*
* @param string $html HTML content.
*
* @return string
*/
private function hide_comments( string $html ): string {
$replace = preg_replace( '#<!--\s*noptimize\s*-->.*?<!--\s*/\s*noptimize\s*-->#is', '', $html );

if ( null === $replace ) {
return $html;
}

$replace = preg_replace( '/<!--(.*)-->/Uis', '', $replace );

if ( null === $replace ) {
return $html;
}

return $replace;
}

/**
* Hides <noscript> blocks from the HTML to be parsed.
*
* @param string $html HTML content.
*
* @return string
*/
private function hide_noscripts( string $html ): string {
$replace = preg_replace( '#<noscript[^>]*>.*?<\/noscript\s*>#mis', '', $html );

if ( null === $replace ) {
return $html;
}

return $replace;
}
}
7 changes: 6 additions & 1 deletion inc/Engine/Media/ImageDimensions/ImageDimensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
use WP_Filesystem_Direct;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Admin\Settings\Settings;
use WP_Rocket\Engine\Optimization\RegexTrait;
use WP_Rocket\Logger\Logger;

class ImageDimensions {
use RegexTrait;
/**
* Options_Data instance
*
Expand Down Expand Up @@ -98,7 +100,10 @@ public function specify_image_dimensions( $html ) {
$images_regex = '<\s*picture[^>]*>.*<\s*\/\s*picture\s*>(*SKIP)(*FAIL)|' . $images_regex;
}

preg_match_all( "/{$images_regex}/Uis", $html, $images_match );
$clean_html = $this->hide_scripts( $html );
$clean_html = $this->hide_noscripts( $clean_html );

preg_match_all( "/{$images_regex}/Uis", $clean_html, $images_match );

if ( empty( $images_match ) ) {
Logger::debug( 'Specify Image Dimensions failed because there is no image without dimensions on this page.' );
Expand Down
34 changes: 8 additions & 26 deletions inc/Engine/Media/Lazyload/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use WP_Rocket\Dependencies\RocketLazyload\Image;
use WP_Rocket\Dependencies\RocketLazyload\Iframe;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Optimization\RegexTrait;
use WP_Rocket\Event_Management\Subscriber_Interface;

/**
Expand All @@ -15,6 +16,8 @@
* @since 3.3
*/
class Subscriber implements Subscriber_Interface {
use RegexTrait;

const SCRIPT_VERSION = '17.5';

/**
Expand Down Expand Up @@ -356,8 +359,8 @@ public function lazyload( $html ) {
return $html;
}

$buffer = $this->ignore_scripts( $html );
$buffer = $this->ignore_noscripts( $buffer );
$buffer = $this->hide_scripts( $html );
$buffer = $this->hide_noscripts( $buffer );

if ( $this->can_lazyload_iframes() ) {
$args = [
Expand All @@ -370,6 +373,9 @@ public function lazyload( $html ) {
if ( $this->can_lazyload_images() ) {
if ( ! $this->is_native_images() ) {
$html = $this->image->lazyloadPictures( $html, $buffer );

$buffer = $this->hide_scripts( $html );
$buffer = $this->hide_noscripts( $buffer );
}

$html = $this->image->lazyloadImages( $html, $buffer, $this->is_native_images() );
Expand Down Expand Up @@ -498,18 +504,6 @@ public function add_exclusions( array $exclusions ) : array {
return array_unique( array_merge( $exclusions, $exclude_lazyload ) );
}

/**
* Remove inline scripts from the HTML to parse
*
* @since 3.3
*
* @param string $html HTML content.
* @return string
*/
private function ignore_scripts( $html ) {
return preg_replace( '/<script\b(?:[^>]*)>(?:.+)?<\/script>/Umsi', '', $html );
}

/**
* Checks if we can lazyload images.
*
Expand Down Expand Up @@ -554,18 +548,6 @@ private function can_lazyload_iframes() {
return apply_filters( 'do_rocket_lazyload_iframes', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
}

/**
* Remove noscript tags from the HTML to parse
*
* @since 3.3
*
* @param string $html HTML content.
* @return string
*/
private function ignore_noscripts( $html ) {
return preg_replace( '#<noscript>(?:.+)</noscript>#Umsi', '', $html );
}

/**
* Checks if native lazyload is enabled for images
*
Expand Down
2 changes: 1 addition & 1 deletion inc/Engine/Optimization/AbstractOptimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @author Remy Perona
*/
abstract class AbstractOptimization {

use RegexTrait;
/**
* Plugin options.
*
Expand Down
59 changes: 0 additions & 59 deletions inc/Engine/Optimization/RUCSS/Controller/UsedCSS.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,65 +383,6 @@ private function add_used_css_to_html( string $html, UsedCSS_Row $used_css ): st
return $replace;
}

/**
* Hides <noscript> blocks from the HTML to be parsed.
*
* @param string $html HTML content.
*
* @return string
*/
private function hide_noscripts( string $html ): string {
$replace = preg_replace( '#<noscript[^>]*>.*?<\/noscript\s*>#mis', '', $html );

if ( null === $replace ) {
return $html;
}

return $replace;
}

/**
* Hides unwanted blocks from the HTML to be parsed.
*
* @param string $html HTML content.
*
* @return string
*/
private function hide_comments( string $html ): string {
$replace = preg_replace( '#<!--\s*noptimize\s*-->.*?<!--\s*/\s*noptimize\s*-->#is', '', $html );

if ( null === $replace ) {
return $html;
}

$replace = preg_replace( '/<!--(.*)-->/Uis', '', $replace );

if ( null === $replace ) {
return $html;
}

return $replace;
}

/**
* Hides scripts from the HTML to be parsed when removing CSS from it
*
* @since 3.10.2
*
* @param string $html HTML content.
*
* @return string
*/
private function hide_scripts( string $html ): string {
$replace = preg_replace( '#<script[^>]*>.*?<\/script\s*>#mis', '', $html );

if ( null === $replace ) {
return $html;
}

return $replace;
}

/**
* Return Markup for used_css into the page.
*
Expand Down
51 changes: 48 additions & 3 deletions inc/Engine/Optimization/RegexTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,54 @@ protected function find( string $pattern, string $html, string $modifiers = 'Ums
* @return string
*/
protected function hide_comments( $html ) {
$html = preg_replace( '#<!--\s*noptimize\s*-->.*?<!--\s*/\s*noptimize\s*-->#is', '', $html );
$html = preg_replace( '/<!--(.*)-->/Uis', '', $html );
$replace = preg_replace( '#<!--\s*noptimize\s*-->.*?<!--\s*/\s*noptimize\s*-->#is', '', $html );

return $html;
if ( null === $replace ) {
return $html;
}

$replace = preg_replace( '/<!--(.*)-->/Uis', '', $replace );

if ( null === $replace ) {
return $html;
}

return $replace;
}

/**
* Hides scripts from the HTML to be parsed when removing CSS from it
*
* @since 3.10.2
*
* @param string $html HTML content.
*
* @return string
*/
protected function hide_scripts( $html ) {
$replace = preg_replace( '#<script[^>]*>.*?<\/script\s*>#mis', '', $html );

if ( null === $replace ) {
return $html;
}

return $replace;
}

/**
* Hides <noscript> blocks from the HTML to be parsed.
*
* @param string $html HTML content.
*
* @return string
*/
protected function hide_noscripts( $html ) {
$replace = preg_replace( '#<noscript[^>]*>.*?<\/noscript\s*>#mis', '', $html );

if ( null === $replace ) {
return $html;
}

return $replace;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -279,5 +279,15 @@
],
'expected' => '<!DOCTYPE html><html><body><img width="100" height="150" src="http://example.org/wp-content/themes/viewbox.svg"></body></html>',
],

'testShouldNotAddDimensionsWhenImageInsideAScript' => [
'html' => '<!DOCTYPE html><html><body><script><img src="http://example.org/wp-content/themes/viewbox.svg"></script></body></html>',
'config' => [
'image_dimensions' => true,
'rocket_specify_image_dimensions_filter' => true,
'internal' => true,
],
'expected' => '<!DOCTYPE html><html><body><script><img src="http://example.org/wp-content/themes/viewbox.svg"></script></body></html>',
],
],
];
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ function ( $value ) {
->andReturn( $config['rocket_specify_image_dimensions_for_distant_filter'] );
}
}

$this->assertSame(
$this->format_the_html( $expected ),
$this->format_the_html( $this->image_dimensions->specify_image_dimensions( $input ) )
Expand Down