From 8517fa51369a62e5a8b466e9c4e4edc33ff129ad Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Mon, 28 Aug 2023 12:19:58 +0200 Subject: [PATCH 01/27] Fixed a bug when having two blocks with the same selector --- .../Media/Lazyload/CSS/Front/Extractor.php | 19 +++++-- .../Lazyload/CSS/Front/RuleFormatter.php | 51 +++++++++++++++---- .../Front/Extractor/CSS/content_with_url.php | 15 +++++- .../Lazyload/CSS/Front/Extractor/extract.php | 19 +++++++ .../CSS/Front/RuleFormatter/format.php | 24 ++++++++- .../Subscriber/integration/CSS/external.css | 6 +++ .../integration/CSS/external.css.json | 2 +- .../integration/CSS/external_lazyloaded.css | 6 +++ .../integration/CSS/lazyload.css.json | 2 +- .../integration/HTML/html_excluded.php | 6 +-- .../integration/HTML/html_output.php | 4 +- .../Lazyload/CSS/Front/Extractor/extract.php | 3 +- 12 files changed, 131 insertions(+), 26 deletions(-) diff --git a/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php b/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php index e6d8e0c8d4..cb6419dfdf 100644 --- a/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php +++ b/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php @@ -77,8 +77,7 @@ function ( $matches ) { $property = trim( $property ); $selector = trim( $selector ); - $urls = $this->extract_urls( $property ); - + $urls = $this->extract_urls( $property ); $block = trim( $match[0] ); foreach ( $this->comments_mapping as $id => $comment ) { $block = str_replace( $id, $comment, $block ); @@ -124,10 +123,12 @@ protected function extract_urls( string $content ): array { $ignored_urls = (array) apply_filters( 'rocket_lazyload_css_ignored_urls', [] ); foreach ( $matches as $match ) { - if ( ! key_exists( 'tag', $match ) || ! key_exists( 'url', $match ) || ! $match['url'] || $this->is_url_ignored( $match['url'], $ignored_urls ) ) { + + $url = $match['url'] ?: ''; + $url = $this->make_url_complete( $url ); + if ( ! key_exists( 'tag', $match ) || ! key_exists( 'url', $match ) || ! $url || $this->is_url_ignored( $url, $ignored_urls ) ) { continue; } - $url = $match['url']; $url = str_replace( '"', '', $url ); $url = str_replace( "'", '', $url ); $url = trim( $url ); @@ -177,4 +178,14 @@ protected function is_url_ignored( string $url, array $ignored_urls ): bool { } return false; } + + protected function make_url_complete( string $url ): string { + $host = wp_parse_url( $url, PHP_URL_HOST ); + + if ( $host ) { + return $url; + } + + return home_url() . '/' . trim( $url, '/' ); + } } diff --git a/inc/Engine/Media/Lazyload/CSS/Front/RuleFormatter.php b/inc/Engine/Media/Lazyload/CSS/Front/RuleFormatter.php index 9c57e03bc5..59feb02297 100644 --- a/inc/Engine/Media/Lazyload/CSS/Front/RuleFormatter.php +++ b/inc/Engine/Media/Lazyload/CSS/Front/RuleFormatter.php @@ -12,7 +12,6 @@ class RuleFormatter { * @return string */ public function format( string $css, array $data ): string { - if ( count( $data ) === 0 ) { return $css; } @@ -20,23 +19,53 @@ public function format( string $css, array $data ): string { $block = ''; $replaced_block = null; + $blocks = []; + foreach ( $data as $datum ) { - if ( ! key_exists( 'selector', $datum ) || ! key_exists( 'original', $datum ) || ! key_exists( 'block', $datum ) || ! key_exists( 'hash', $datum ) ) { - return $css; + $added = false; + foreach ( $blocks as &$block ) { + if ( $block['block'] === $datum['block'] ) { + $block['items'] [] = $datum; + $added = true; + break; + } } - $block = $datum['block']; - $replaced_block = $replaced_block ?: $datum['block']; - $url = $datum['original']; + if ( $added ) { + continue; + } + + $blocks [] = [ + 'items' => [ + $datum, + ], + 'block' => $datum['block'], + ]; + } + + foreach ( $blocks as $block ) { + $replaced_block = null; + foreach ( $block['items'] as $datum ) { + if ( ! key_exists( 'selector', $datum ) || ! key_exists( 'original', $datum ) || ! key_exists( 'block', $datum ) || ! key_exists( 'hash', $datum ) ) { + return $css; + } + + $original_block = $datum['block']; + $replaced_block = $replaced_block ?: $datum['block']; + $url = $datum['original']; + + $hash = $datum['hash']; - $hash = $datum['hash']; + $placeholder = "--wpr-bg-$hash"; + $variable_placeholder = "var($placeholder)"; - $placeholder = "--wpr-bg-$hash"; - $variable_placeholder = "var($placeholder)"; + $replaced_block = str_replace( $url, $variable_placeholder, $replaced_block ); + + } - $replaced_block = str_replace( $url, $variable_placeholder, $replaced_block ); + $css = str_replace( $original_block, $replaced_block, $css ); } - return str_replace( $block, $replaced_block, $css ); + return $css; } } diff --git a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/Extractor/CSS/content_with_url.php b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/Extractor/CSS/content_with_url.php index 02e5d08072..b0da55a4e3 100644 --- a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/Extractor/CSS/content_with_url.php +++ b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/Extractor/CSS/content_with_url.php @@ -107,7 +107,7 @@ background-color: aqua; font-family: Cambria, Cochin, Georgia, Times, "Times New Roman", serif; } - + .exclude-internal-relative-file{ display:none; } @@ -123,3 +123,16 @@ .test{ color:rgb(165, 36, 197); } + + [class^="icon-"], [class*=" icon-"] { + display: inline-block; + width: 14px; + height: 14px; + margin-top: 1px; + *margin-right: .3em; + line-height: 14px; + vertical-align: text-top; + background-image: url("../img/glyphicons-halflings.png"); + background-position: 14px 14px; + background-repeat: no-repeat + } diff --git a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/Extractor/extract.php b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/Extractor/extract.php index 3ed30d4d36..aaf03f2497 100644 --- a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/Extractor/extract.php +++ b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/Extractor/extract.php @@ -128,6 +128,25 @@ 'block' => '#internal-BG-images{background:url(/wp-content/rocket-test-data/images/butterfly.avif) right bottom no-repeat, url(/wp-content/rocket-test-data/images/file_example_TIFF_1MB.tiff) left top repeat;padding:15px;}' ], ], + '[class^="icon-"], [class*=" icon-"]' => [ + [ + 'selector' => '[class^="icon-"], [class*=" icon-"]', + 'url' => "url('../img/glyphicons-halflings.png')", + 'original' => 'url("../img/glyphicons-halflings.png")', + 'block' => '[class^="icon-"], [class*=" icon-"] { + display: inline-block; + width: 14px; + height: 14px; + margin-top: 1px; + *margin-right: .3em; + line-height: 14px; + vertical-align: text-top; + background-image: url("../img/glyphicons-halflings.png"); + background-position: 14px 14px; + background-repeat: no-repeat + }' + ] + ], '.external-css-background-image' => [ [ 'selector' => '.external-css-background-image', diff --git a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/RuleFormatter/format.php b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/RuleFormatter/format.php index d4fcf3b1d1..65311f2dd5 100644 --- a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/RuleFormatter/format.php +++ b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/RuleFormatter/format.php @@ -22,6 +22,11 @@ .internal-css-background{ background: url('/wp-content/rocket-test-data/images/test.png'); } + .internal-css-background{ + width: 100%; + height: 400px; + background-image: linear-gradient(rgba(0, 0, 255, 0.5), rgba(255, 255, 0, 0.5)), url("/wp-content/rocket-test-data/images/paper.jpeg"); + } CSS; @@ -46,6 +51,11 @@ .internal-css-background{ background: var(--wpr-bg-_ida122ad12df2); } + .internal-css-background{ + width: 100%; + height: 400px; + background-image: linear-gradient(rgba(0, 0, 255, 0.5), rgba(255, 255, 0, 0.5)), var(--wpr-bg-_ida122ad12df4); + } CSS; @@ -62,11 +72,21 @@ background: url('/wp-content/rocket-test-data/images/test.png'); }", 'hash' => '_ida122ad12df2', - ] + ], + [ + 'selector' => '.internal-css-background', + 'url' => "url('/wp-content/rocket-test-data/images/test.png')", + 'original' => 'url("/wp-content/rocket-test-data/images/paper.jpeg")', + 'block' => '.internal-css-background{ + width: 100%; + height: 400px; + background-image: linear-gradient(rgba(0, 0, 255, 0.5), rgba(255, 255, 0, 0.5)), url("/wp-content/rocket-test-data/images/paper.jpeg"); + }', + 'hash' => '_ida122ad12df4', + ], ], ], 'expected' => $css_formatted ], - ]; diff --git a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/CSS/external.css b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/CSS/external.css index 80c59a9181..39c57a0ddd 100644 --- a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/CSS/external.css +++ b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/CSS/external.css @@ -1,4 +1,10 @@ .external-css-background-image{width:100%;height:400px;background-image:url("https://new.rocketlabsqa.ovh/wp-content/rocket-test-data/images/paper.jpeg");background-color:#ccc}.external-css-background-images{width:100%;height:400px;background-image:url('./wp-content/rocket-test-data/images/test.png'),url("/wp-content/rocket-test-data/images/paper.jpeg");background-color:#3dd83d}.external-css-background-image-gradient{width:100%;height:400px;background-image:linear-gradient(rgba(0,0,255,.5),rgba(255,255,0,.5)),url(/wp-content/rocket-test-data/images/paper.jpeg)}.external-css-background{background:url('https://upload.wikimedia.org/wikipedia/commons/1/11/Test-Logo.svg') no-repeat #c63dd8} +.external-css-background-image-space{ + width: 100%; + height: 400px; + background-image: url("../rocket-test-data/images/papertest.jpeg"); + background-color: #cccccc; +} .external-css-background-image-space{ width: 100%; height: 400px; diff --git a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/CSS/external.css.json b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/CSS/external.css.json index d48108b1ec..ba1815abc0 100644 --- a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/CSS/external.css.json +++ b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/CSS/external.css.json @@ -1 +1 @@ -[{"selector":".external-css-background","style":":root{--wpr-bg-hash: url('https:\/\/upload.wikimedia.org\/wikipedia\/commons\/1\/11\/Test-Logo.svg');}"},{"selector":".external-css-background-image","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".external-css-background-images","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/styles\/.\/wp-content\/rocket-test-data\/images\/test.png');}"},{"selector":".external-css-background-images","style":":root{--wpr-bg-hash: url('\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".external-css-background-image-gradient","style":":root{--wpr-bg-hash: url('\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".external-css-background-image-space","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/images\/papertest.jpeg');}"}] +[{"selector":".external-css-background","style":":root{--wpr-bg-hash: url('https:\/\/upload.wikimedia.org\/wikipedia\/commons\/1\/11\/Test-Logo.svg');}"},{"selector":".external-css-background-image","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".external-css-background-images","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/styles\/.\/wp-content\/rocket-test-data\/images\/test.png');}"},{"selector":".external-css-background-images","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".external-css-background-image-gradient","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".external-css-background-image-space","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/styles\/..\/rocket-test-data\/images\/papertest.jpeg');}"},{"selector":".external-css-background-image-space","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/images\/papertest.jpeg');}"}] diff --git a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/CSS/external_lazyloaded.css b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/CSS/external_lazyloaded.css index e2093ea420..021bc6739b 100644 --- a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/CSS/external_lazyloaded.css +++ b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/CSS/external_lazyloaded.css @@ -5,6 +5,12 @@ background-image: var(--wpr-bg-hash); background-color: #cccccc; } +.external-css-background-image-space{ + width: 100%; + height: 400px; + background-image: var(--wpr-bg-hash); + background-color: #cccccc; +} .external-css-background-image-svg{ width: 100%; height: 400px; diff --git a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/CSS/lazyload.css.json b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/CSS/lazyload.css.json index 4ab5800035..651991275e 100644 --- a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/CSS/lazyload.css.json +++ b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/CSS/lazyload.css.json @@ -1 +1 @@ -[{"selector":".external-css-background","style":":root{--wpr-bg-hash: url('\/test.png');}"},{"selector":".external-css-backgroundsvg","style":":root{--wpr-bg-hash: url('https:\/\/upload.wikimedia.org\/wikipedia\/commons\/1\/11\/Test-Logo.svg');}"},{"selector":".external-css-background-image","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".external-css-background-images","style":":root{--wpr-bg-hash: url('\/wp-content\/rocket-test-data\/images\/test.png');}"},{"selector":".external-css-background-images","style":":root{--wpr-bg-hash: url('\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".external-css-background-image-gradient","style":":root{--wpr-bg-hash: url('\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"}] +[{"selector":".external-css-background","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/test.png');}"},{"selector":".external-css-backgroundsvg","style":":root{--wpr-bg-hash: url('https:\/\/upload.wikimedia.org\/wikipedia\/commons\/1\/11\/Test-Logo.svg');}"},{"selector":".external-css-background-image","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".external-css-background-images","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/test.png');}"},{"selector":".external-css-background-images","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".external-css-background-image-gradient","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"}] diff --git a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/HTML/html_excluded.php b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/HTML/html_excluded.php index 5a27be9a7f..0f3fcd3e1a 100644 --- a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/HTML/html_excluded.php +++ b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/HTML/html_excluded.php @@ -40,13 +40,13 @@ + :root{--wpr-bg-hash: url('http://example.org/wp-content/rocket-test-data/images/test.png');}:root{--wpr-bg-hash: url('http://example.org/wp-content/rocket-test-data/images/paper.jpeg');}:root{--wpr-bg-hash: url('https://new.rocketlabsqa.ovh/wp-content/rocket-test-data/styles/./wp-content/rocket-test-data/images/test.png');} + const rocket_pairs = [{"selector":".external-css-background","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/test.png');}"},{"selector":".external-css-backgroundsvg","style":":root{--wpr-bg-hash: url('https:\/\/upload.wikimedia.org\/wikipedia\/commons\/1\/11\/Test-Logo.svg');}"},{"selector":".external-css-background-image","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".external-css-background-images","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/test.png');}"},{"selector":".external-css-background-images","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".external-css-background-image-gradient","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".external-css-background","style":":root{--wpr-bg-hash: url('https:\/\/upload.wikimedia.org\/wikipedia\/commons\/1\/11\/Test-Logo.svg');}"},{"selector":".external-css-background-images","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/styles\/.\/wp-content\/rocket-test-data\/images\/test.png');}"},{"selector":".external-css-background-image-space","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/styles\/..\/rocket-test-data\/images\/papertest.jpeg');}"},{"selector":".external-css-background-image-space","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/images\/papertest.jpeg');}"},{"selector":".internal-css-background","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/test.png');}"},{"selector":".internal-css-background404","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/testnotExist.png');}"},{"selector":"#internal-BG-images","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/butterfly.avif');}"},{"selector":"#internal-BG-images","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/file_example_TIFF_1MB.tiff');}"},{"selector":".internal-css-background-image","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".internal-css-background-images","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/images\/test.png');}"},{"selector":".internal-css-background-images","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/ \/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".internal-css-background-image-gradient","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"}];
diff --git a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/HTML/html_output.php b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/HTML/html_output.php index dcc864b681..7dcda46c79 100644 --- a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/HTML/html_output.php +++ b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/HTML/html_output.php @@ -44,10 +44,10 @@ + const rocket_pairs = [{"selector":".external-css-background","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/test.png');}"},{"selector":".external-css-backgroundsvg","style":":root{--wpr-bg-hash: url('https:\/\/upload.wikimedia.org\/wikipedia\/commons\/1\/11\/Test-Logo.svg');}"},{"selector":".external-css-background-image","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".external-css-background-images","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/test.png');}"},{"selector":".external-css-background-images","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".external-css-background-image-gradient","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".external-css-background","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/test_excluded.png');}"},{"selector":".external-css-background","style":":root{--wpr-bg-hash: url('https:\/\/upload.wikimedia.org\/wikipedia\/commons\/1\/11\/Test-Logo.svg');}"},{"selector":".external-css-background-images","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/styles\/.\/wp-content\/rocket-test-data\/images\/test.png');}"},{"selector":".external-css-background-image-space","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/styles\/..\/rocket-test-data\/images\/papertest.jpeg');}"},{"selector":".external-css-background-image-space","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/images\/papertest.jpeg');}"},{"selector":".internal-css-background","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/test.png');}"},{"selector":".internal-css-background404","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/testnotExist.png');}"},{"selector":"#internal-BG-images","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/butterfly.avif');}"},{"selector":"#internal-BG-images","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/file_example_TIFF_1MB.tiff');}"},{"selector":".internal-css-background-image","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".internal-css-background-images","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/images\/test.png');}"},{"selector":".internal-css-background-images","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/ \/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".internal-css-background-image-gradient","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"}];

Background images from internal CSS

diff --git a/tests/Unit/inc/Engine/Media/Lazyload/CSS/Front/Extractor/extract.php b/tests/Unit/inc/Engine/Media/Lazyload/CSS/Front/Extractor/extract.php index 5fd28a0ebe..27b9b9d011 100644 --- a/tests/Unit/inc/Engine/Media/Lazyload/CSS/Front/Extractor/extract.php +++ b/tests/Unit/inc/Engine/Media/Lazyload/CSS/Front/Extractor/extract.php @@ -6,7 +6,7 @@ use WP_Rocket\Tests\Unit\TestCase; - +use Brain\Monkey\Functions; /** * @covers \WP_Rocket\Engine\Media\Lazyload\CSS\Front\Extractor::extract */ @@ -28,6 +28,7 @@ public function set_up() { */ public function testShouldReturnAsExpected( $config, $expected ) { + Functions\when('wp_parse_url')->justReturn('example.org'); $this->assertEqualsCanonicalizing($expected, $this->extractor->extract($config['content'])); } } From df61774388a8a7af1fae65db4136259a17bb3f03 Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Mon, 28 Aug 2023 14:50:16 +0200 Subject: [PATCH 02/27] Apply cdn only on internal URLs --- .../Media/Lazyload/CSS/Front/Extractor.php | 37 +++++++++++++++++-- .../Lazyload/CSS/Front/Extractor/extract.php | 1 + 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php b/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php index cb6419dfdf..ff4cfee486 100644 --- a/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php +++ b/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php @@ -129,10 +129,14 @@ protected function extract_urls( string $content ): array { if ( ! key_exists( 'tag', $match ) || ! key_exists( 'url', $match ) || ! $url || $this->is_url_ignored( $url, $ignored_urls ) ) { continue; } - $url = str_replace( '"', '', $url ); - $url = str_replace( "'", '', $url ); - $url = trim( $url ); - $url = $this->apply_string_filter( 'css_url', $url ); + $url = str_replace( '"', '', $url ); + $url = str_replace( "'", '', $url ); + $url = trim( $url ); + + if ( ! $this->is_url_external( $url ) ) { + $url = $this->apply_string_filter( 'css_url', $url ); + } + $url = "url('$url')"; $original_url = trim( $match['tag'], ' ,' ); $output[] = [ @@ -179,6 +183,13 @@ protected function is_url_ignored( string $url, array $ignored_urls ): bool { return false; } + /** + * Complete the URL if necessary. + * + * @param string $url URL to complete. + * + * @return string + */ protected function make_url_complete( string $url ): string { $host = wp_parse_url( $url, PHP_URL_HOST ); @@ -188,4 +199,22 @@ protected function make_url_complete( string $url ): string { return home_url() . '/' . trim( $url, '/' ); } + + /** + * Check if the URL is external. + * + * @param string $url URL to check. + * + * @return bool + */ + protected function is_url_external( string $url ): bool { + $host = wp_parse_url( $url, PHP_URL_HOST ); + if ( ! $host ) { + return false; + } + + $home_host = wp_parse_url( home_url(), PHP_URL_HOST ); + + return $host != $home_host; + } } diff --git a/tests/Unit/inc/Engine/Media/Lazyload/CSS/Front/Extractor/extract.php b/tests/Unit/inc/Engine/Media/Lazyload/CSS/Front/Extractor/extract.php index 27b9b9d011..267ed7b229 100644 --- a/tests/Unit/inc/Engine/Media/Lazyload/CSS/Front/Extractor/extract.php +++ b/tests/Unit/inc/Engine/Media/Lazyload/CSS/Front/Extractor/extract.php @@ -29,6 +29,7 @@ public function set_up() { public function testShouldReturnAsExpected( $config, $expected ) { Functions\when('wp_parse_url')->justReturn('example.org'); + Functions\when('home_url')->justReturn('http://example.org'); $this->assertEqualsCanonicalizing($expected, $this->extractor->extract($config['content'])); } } From cb9c8ed25fb061f87cc4ae340b5a4501c4349a42 Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Mon, 28 Aug 2023 14:52:28 +0200 Subject: [PATCH 03/27] Fix phpcs --- inc/Engine/Media/Lazyload/CSS/Front/Extractor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php b/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php index ff4cfee486..4f41ad7b8c 100644 --- a/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php +++ b/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php @@ -215,6 +215,6 @@ protected function is_url_external( string $url ): bool { $home_host = wp_parse_url( home_url(), PHP_URL_HOST ); - return $host != $home_host; + return $host !== $home_host; } } From 1aec0db8513808176b098f741957a060e5a40938 Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Mon, 28 Aug 2023 16:06:53 +0200 Subject: [PATCH 04/27] Fixed the bug with spaces --- inc/Engine/Media/Lazyload/CSS/Front/Extractor.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php b/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php index 4f41ad7b8c..efb030241b 100644 --- a/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php +++ b/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php @@ -125,13 +125,13 @@ protected function extract_urls( string $content ): array { foreach ( $matches as $match ) { $url = $match['url'] ?: ''; + $url = str_replace( '"', '', $url ); + $url = str_replace( "'", '', $url ); + $url = trim( $url ); $url = $this->make_url_complete( $url ); if ( ! key_exists( 'tag', $match ) || ! key_exists( 'url', $match ) || ! $url || $this->is_url_ignored( $url, $ignored_urls ) ) { continue; } - $url = str_replace( '"', '', $url ); - $url = str_replace( "'", '', $url ); - $url = trim( $url ); if ( ! $this->is_url_external( $url ) ) { $url = $this->apply_string_filter( 'css_url', $url ); @@ -197,7 +197,7 @@ protected function make_url_complete( string $url ): string { return $url; } - return home_url() . '/' . trim( $url, '/' ); + return home_url() . '/' . trim( $url, '/ ' ); } /** From 0d5591f12aa8134ae14a612d13b1e1bbaa85f00a Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Mon, 28 Aug 2023 16:28:26 +0200 Subject: [PATCH 05/27] Added fix for tests --- .../CSS/Subscriber/integration/HTML/html_excluded.php | 4 ++-- .../Lazyload/CSS/Subscriber/integration/HTML/html_output.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/HTML/html_excluded.php b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/HTML/html_excluded.php index 0f3fcd3e1a..dddaef2027 100644 --- a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/HTML/html_excluded.php +++ b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/HTML/html_excluded.php @@ -43,10 +43,10 @@ :root{--wpr-bg-hash: url('http://example.org/wp-content/rocket-test-data/images/test.png');}:root{--wpr-bg-hash: url('http://example.org/wp-content/rocket-test-data/images/paper.jpeg');}:root{--wpr-bg-hash: url('https://new.rocketlabsqa.ovh/wp-content/rocket-test-data/styles/./wp-content/rocket-test-data/images/test.png');} + const rocket_pairs = [{"selector":".external-css-background","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/test.png');}"},{"selector":".external-css-backgroundsvg","style":":root{--wpr-bg-hash: url('https:\/\/upload.wikimedia.org\/wikipedia\/commons\/1\/11\/Test-Logo.svg');}"},{"selector":".external-css-background-image","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".external-css-background-images","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/test.png');}"},{"selector":".external-css-background-images","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".external-css-background-image-gradient","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".external-css-background","style":":root{--wpr-bg-hash: url('https:\/\/upload.wikimedia.org\/wikipedia\/commons\/1\/11\/Test-Logo.svg');}"},{"selector":".external-css-background-images","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/styles\/.\/wp-content\/rocket-test-data\/images\/test.png');}"},{"selector":".external-css-background-image-space","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/styles\/..\/rocket-test-data\/images\/papertest.jpeg');}"},{"selector":".external-css-background-image-space","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/images\/papertest.jpeg');}"},{"selector":".internal-css-background","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/test.png');}"},{"selector":".internal-css-background404","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/testnotExist.png');}"},{"selector":"#internal-BG-images","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/butterfly.avif');}"},{"selector":"#internal-BG-images","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/file_example_TIFF_1MB.tiff');}"},{"selector":".internal-css-background-image","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".internal-css-background-images","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/images\/test.png');}"},{"selector":".internal-css-background-images","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".internal-css-background-image-gradient","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"}];
diff --git a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/HTML/html_output.php b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/HTML/html_output.php index 7dcda46c79..8a7a3ffeba 100644 --- a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/HTML/html_output.php +++ b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/HTML/html_output.php @@ -44,10 +44,10 @@ + const rocket_pairs = [{"selector":".external-css-background","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/test.png');}"},{"selector":".external-css-backgroundsvg","style":":root{--wpr-bg-hash: url('https:\/\/upload.wikimedia.org\/wikipedia\/commons\/1\/11\/Test-Logo.svg');}"},{"selector":".external-css-background-image","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".external-css-background-images","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/test.png');}"},{"selector":".external-css-background-images","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".external-css-background-image-gradient","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".external-css-background","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/test_excluded.png');}"},{"selector":".external-css-background","style":":root{--wpr-bg-hash: url('https:\/\/upload.wikimedia.org\/wikipedia\/commons\/1\/11\/Test-Logo.svg');}"},{"selector":".external-css-background-images","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/styles\/.\/wp-content\/rocket-test-data\/images\/test.png');}"},{"selector":".external-css-background-image-space","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/styles\/..\/rocket-test-data\/images\/papertest.jpeg');}"},{"selector":".external-css-background-image-space","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/images\/papertest.jpeg');}"},{"selector":".internal-css-background","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/test.png');}"},{"selector":".internal-css-background404","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/testnotExist.png');}"},{"selector":"#internal-BG-images","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/butterfly.avif');}"},{"selector":"#internal-BG-images","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/file_example_TIFF_1MB.tiff');}"},{"selector":".internal-css-background-image","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".internal-css-background-images","style":":root{--wpr-bg-hash: url('https:\/\/new.rocketlabsqa.ovh\/wp-content\/rocket-test-data\/images\/test.png');}"},{"selector":".internal-css-background-images","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"},{"selector":".internal-css-background-image-gradient","style":":root{--wpr-bg-hash: url('http:\/\/example.org\/wp-content\/rocket-test-data\/images\/paper.jpeg');}"}];

Background images from internal CSS

From bcef911a0b0a1feaf7931bb48af5982445527e6e Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Tue, 29 Aug 2023 11:10:11 +0200 Subject: [PATCH 06/27] Added fix for the meta classes --- .../Media/Lazyload/CSS/Front/Extractor.php | 4 +-- .../Lazyload/CSS/Front/MappingFormatter.php | 16 +++++++++ .../CSS/Front/MappingFormatter/format.php | 36 ++++++++++++++++++- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php b/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php index efb030241b..f713dac68f 100644 --- a/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php +++ b/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php @@ -37,9 +37,9 @@ function ( $matches ) { $content ); - $background_regex = '(?[ \-,:\w.\n\r^[*"\'=\]#]+)\s?{[^}]*background\s*:(?[^;}]*)[^}]*}'; + $background_regex = '(?[ \-,:\w.\n\r^>[*"\'=\]#]+)\s?{[^}]*background\s*:(?[^;}]*)[^}]*}'; - $background_image_regex = '(?[ \-,:\w.\n\r^[*"\'=\]#]+)\s?{[^}]*background-image\s*:(?[^;}]*)[^}]*}'; + $background_image_regex = '(?[ \-,:\w.\n\r>^[*"\'=\]#]+)\s?{[^}]*background-image\s*:(?[^;}]*)[^}]*}'; /** * Lazyload background property regex. diff --git a/inc/Engine/Media/Lazyload/CSS/Front/MappingFormatter.php b/inc/Engine/Media/Lazyload/CSS/Front/MappingFormatter.php index cc72d195cf..c44e735f16 100644 --- a/inc/Engine/Media/Lazyload/CSS/Front/MappingFormatter.php +++ b/inc/Engine/Media/Lazyload/CSS/Front/MappingFormatter.php @@ -16,6 +16,7 @@ public function format( array $data ): array { $hash = $datum['hash']; $selector = $datum['selector']; + $selector = $this->remove_pseudo_classes( $selector ); $url = $datum['url']; $placeholder = "--wpr-bg-$hash"; @@ -28,4 +29,19 @@ public function format( array $data ): array { return $formatted_urls; } + + /** + * Remove pseudo classes. + * + * @param string $selector Selector to clean. + * + * @return string + */ + protected function remove_pseudo_classes( string $selector ): string { + $result = preg_replace( '/::[\w\-]+/', '', $selector ); + if ( ! $result ) { + return 'body'; + } + return (string) $result; + } } diff --git a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/MappingFormatter/format.php b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/MappingFormatter/format.php index 2fcc724dce..0c15f995da 100644 --- a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/MappingFormatter/format.php +++ b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/MappingFormatter/format.php @@ -26,5 +26,39 @@ ] ] ], - + 'shouldRemoveMetaElements' => [ + 'config' => [ + 'data' => [ + [ + 'hash' => 'a122ad12df3', + 'selector' => '#first_id::before', + 'url' => 'http://example.org', + ], + [ + 'hash' => 'a122ad12df2', + 'selector' => '#second_id::after, #test', + 'url' => 'http://example.org/test', + ], + [ + 'hash' => 'a122ad12df3', + 'selector' => '::after', + 'url' => 'http://example.org/test', + ] + ], + ], + 'expected' => [ + [ + 'selector' => '#first_id', + 'style' => ':root{--wpr-bg-a122ad12df3: http://example.org;}' + ], + [ + 'selector' => '#second_id, #test', + 'style' => ':root{--wpr-bg-a122ad12df2: http://example.org/test;}' + ], + [ + 'selector' => 'body', + 'style' => ':root{--wpr-bg-a122ad12df3: http://example.org/test;}' + ] + ] + ], ]; From 233f70a40374128a534a441252155df59e55f1a6 Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Tue, 29 Aug 2023 12:14:11 +0200 Subject: [PATCH 07/27] Added a wider selector regex --- inc/Engine/Media/Lazyload/CSS/Front/Extractor.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php b/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php index f713dac68f..b7e0877ca6 100644 --- a/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php +++ b/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php @@ -37,9 +37,9 @@ function ( $matches ) { $content ); - $background_regex = '(?[ \-,:\w.\n\r^>[*"\'=\]#]+)\s?{[^}]*background\s*:(?[^;}]*)[^}]*}'; + $background_regex = '(?[ \-,:\w.()\n\r^>[*"\'=\]#]+)\s?{[^}]*background\s*:(?[^;}]*)[^}]*}'; - $background_image_regex = '(?[ \-,:\w.\n\r>^[*"\'=\]#]+)\s?{[^}]*background-image\s*:(?[^;}]*)[^}]*}'; + $background_image_regex = '(?[ \-,:\w.()\n\r>^[*"\'=\]#]+)\s?{[^}]*background-image\s*:(?[^;}]*)[^}]*}'; /** * Lazyload background property regex. From 4ffe583a5f1cfae427ad7f8023366ff4e29f20b7 Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Tue, 29 Aug 2023 16:02:38 +0200 Subject: [PATCH 08/27] Changed the script to internal --- inc/Engine/Media/Lazyload/CSS/Subscriber.php | 30 +++++++++++++++---- .../Subscriber/addLazyloadScriptExcludeJs.php | 2 +- ...dLazyloadScriptRocketDelayJsExclusions.php | 2 +- .../addLazyloadScriptRocketExcludeDeferJs.php | 2 +- .../CSS/Subscriber/insertLazyloadScript.php | 12 ++++++++ .../CSS/Subscriber/SubscriberTrait.php | 9 +++++- .../CSS/Subscriber/insertLazyloadScript.php | 13 +++++++- wp-rocket.php | 2 ++ 8 files changed, 62 insertions(+), 10 deletions(-) diff --git a/inc/Engine/Media/Lazyload/CSS/Subscriber.php b/inc/Engine/Media/Lazyload/CSS/Subscriber.php index 9856678d2e..42db9c1e06 100644 --- a/inc/Engine/Media/Lazyload/CSS/Subscriber.php +++ b/inc/Engine/Media/Lazyload/CSS/Subscriber.php @@ -93,6 +93,13 @@ class Subscriber implements Subscriber_Interface, LoggerAwareInterface { */ protected $lazyloaded_content_factory; + /** + * WordPress filesystem. + * + * @var WP_Filesystem_Direct + */ + protected $filesystem; + /** * Instantiate class. * @@ -106,8 +113,9 @@ class Subscriber implements Subscriber_Interface, LoggerAwareInterface { * @param ContextInterface $context Context. * @param Options_Data $options WPR Options. * @param LazyloadCSSContentFactory $lazyloaded_content_factory Make LazyloadedContent instance. + * @param WP_Filesystem_Direct|null $filesystem WordPress filesystem. */ - public function __construct( Extractor $extractor, RuleFormatter $rule_formatter, FileResolver $file_resolver, CacheInterface $cache, MappingFormatter $mapping_formatter, TagGenerator $tag_generator, ContentFetcher $fetcher, ContextInterface $context, Options_Data $options, LazyloadCSSContentFactory $lazyloaded_content_factory ) { + public function __construct( Extractor $extractor, RuleFormatter $rule_formatter, FileResolver $file_resolver, CacheInterface $cache, MappingFormatter $mapping_formatter, TagGenerator $tag_generator, ContentFetcher $fetcher, ContextInterface $context, Options_Data $options, LazyloadCSSContentFactory $lazyloaded_content_factory, WP_Filesystem_Direct $filesystem = null ) { $this->extractor = $extractor; $this->cache = $cache; $this->rule_formatter = $rule_formatter; @@ -118,6 +126,7 @@ public function __construct( Extractor $extractor, RuleFormatter $rule_formatter $this->options = $options; $this->fetcher = $fetcher; $this->lazyloaded_content_factory = $lazyloaded_content_factory; + $this->filesystem = $filesystem ?: rocket_direct_filesystem(); } /** @@ -232,8 +241,19 @@ public function insert_lazyload_script() { */ $threshold = (int) apply_filters( 'rocket_lazyload_threshold', 300 ); - wp_enqueue_script( 'rocket_lazyload_css', rocket_get_constant( 'WP_ROCKET_ASSETS_JS_URL' ) . 'lazyload-css.min.js', [], rocket_get_constant( 'WP_ROCKET_VERSION' ), true ); + $script_path = rocket_get_constant( 'WP_ROCKET_ASSETS_JS_PATH' ) . 'lazyload-css.min.js'; + + if ( ! $this->filesystem->exists( $script_path ) ) { + return; + } + + $content = $this->filesystem->get_contents( $script_path ); + + if ( ! $content ) { + return; + } + wp_add_inline_script( 'rocket_lazyload_css', $content, true ); wp_localize_script( 'rocket_lazyload_css', 'rocket_lazyload_css_data', @@ -548,7 +568,7 @@ public function add_lazyload_script_exclude_js( array $js_files ) { return $js_files; } - $js_files [] = 'wp-rocket/assets/js/lazyload-css.min.js'; + $js_files [] = '#rocket_lazyload_css-js'; return $js_files; } @@ -563,7 +583,7 @@ public function add_lazyload_script_rocket_exclude_defer_js( array $exclude_defe return $exclude_defer_js; } - $exclude_defer_js [] = 'wp-rocket/assets/js/lazyload-css.min.js'; + $exclude_defer_js [] = '#rocket_lazyload_css-js'; return $exclude_defer_js; } @@ -578,7 +598,7 @@ public function add_lazyload_script_rocket_delay_js_exclusions( array $js_files return $js_files; } - $js_files [] = 'wp-rocket/assets/js/lazyload-css.min.js'; + $js_files [] = '#rocket_lazyload_css-js'; return $js_files; } diff --git a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/addLazyloadScriptExcludeJs.php b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/addLazyloadScriptExcludeJs.php index bdba0a8d1b..4a26b6db54 100644 --- a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/addLazyloadScriptExcludeJs.php +++ b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/addLazyloadScriptExcludeJs.php @@ -6,7 +6,7 @@ 'enabled' => true, ], 'expected' => [ - 'wp-rocket/assets/js/lazyload-css.min.js', + '#rocket_lazyload_css-js', ] ], 'disabledShouldReturnSame' => [ diff --git a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/addLazyloadScriptRocketDelayJsExclusions.php b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/addLazyloadScriptRocketDelayJsExclusions.php index c977c244f0..420ef6a1da 100644 --- a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/addLazyloadScriptRocketDelayJsExclusions.php +++ b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/addLazyloadScriptRocketDelayJsExclusions.php @@ -6,7 +6,7 @@ 'enabled' => true, ], 'expected' => [ - 'wp-rocket/assets/js/lazyload-css.min.js', + '#rocket_lazyload_css-js', ] ], 'DisabledShouldKeepSame' => [ diff --git a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/addLazyloadScriptRocketExcludeDeferJs.php b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/addLazyloadScriptRocketExcludeDeferJs.php index 139d5ef5f7..301d057c0e 100644 --- a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/addLazyloadScriptRocketExcludeDeferJs.php +++ b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/addLazyloadScriptRocketExcludeDeferJs.php @@ -6,7 +6,7 @@ 'enabled' => true, ], 'expected' => [ - 'wp-rocket/assets/js/lazyload-css.min.js', + '#rocket_lazyload_css-js', ] ], 'disactivatedShouldReturnSame' => [ diff --git a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/insertLazyloadScript.php b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/insertLazyloadScript.php index eb4bf3d965..c8a4f81b93 100644 --- a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/insertLazyloadScript.php +++ b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/insertLazyloadScript.php @@ -5,11 +5,17 @@ 'is_allowed' => true, 'WP_ROCKET_VERSION' => '1.0.0', 'WP_ROCKET_ASSETS_JS_URL' => 'https://example.org/test/', + 'WP_ROCKET_ASSETS_JS_PATH' => '/path/test/', + 'path' => '/path/test/lazyload-css.min.js', 'threshold' => 400, + 'script_data' => 'script_data', + 'exists' => true, ], 'expected' => [ 'url' => 'https://example.org/test/lazyload-css.min.js', + 'path' => '/path/test/lazyload-css.min.js', 'version' => '1.0.0', + 'script_data' => 'script_data', 'data' => [ 'threshold' => 400 ] @@ -20,11 +26,17 @@ 'is_allowed' => false, 'WP_ROCKET_VERSION' => '1.0.0', 'WP_ROCKET_ASSETS_JS_URL' => 'https://example.org/test/', + 'path' => '/path/test/lazyload-css.min.js', + 'WP_ROCKET_ASSETS_JS_PATH' => '/path/test/', 'threshold' => 400, + 'script_data' => 'script_data', + 'exists' => true, ], 'expected' => [ 'url' => 'https://example.org/test/lazyload-css.min.js', + 'path' => '/path/test/lazyload-css.min.js', 'version' => '1.0.0', + 'script_data' => 'script_data', 'data' => [ 'threshold' => 400 ] diff --git a/tests/Unit/inc/Engine/Media/Lazyload/CSS/Subscriber/SubscriberTrait.php b/tests/Unit/inc/Engine/Media/Lazyload/CSS/Subscriber/SubscriberTrait.php index ea0b186224..2dbf0ce31d 100644 --- a/tests/Unit/inc/Engine/Media/Lazyload/CSS/Subscriber/SubscriberTrait.php +++ b/tests/Unit/inc/Engine/Media/Lazyload/CSS/Subscriber/SubscriberTrait.php @@ -2,6 +2,7 @@ namespace Engine\Media\Lazyload\CSS\Subscriber; +use WP_Filesystem_Direct; use WP_Rocket\Admin\Options_Data; use WP_Rocket\Engine\Common\Cache\FilesystemCache; use WP_Rocket\Engine\Common\Context\ContextInterface; @@ -75,6 +76,11 @@ trait SubscriberTrait */ protected $subscriber; + /** + * @var WP_Filesystem_Direct + */ + protected $filesystem; + protected function init_subscriber() { parent::set_up(); $this->extractor = Mockery::mock(Extractor::class); @@ -87,8 +93,9 @@ protected function init_subscriber() { $this->context = Mockery::mock(ContextInterface::class); $this->options = Mockery::mock(Options_Data::class); $this->lazyload_content_factory = new LazyloadCSSContentFactory(); + $this->filesystem = Mockery::mock(WP_Filesystem_Direct::class); - $this->subscriber = new Subscriber($this->extractor, $this->rule_formatter, $this->file_resolver, $this->filesystem_cache, $this->json_formatter, $this->tag_generator, $this->fetcher, $this->context, $this->options, $this->lazyload_content_factory); + $this->subscriber = new Subscriber($this->extractor, $this->rule_formatter, $this->file_resolver, $this->filesystem_cache, $this->json_formatter, $this->tag_generator, $this->fetcher, $this->context, $this->options, $this->lazyload_content_factory, $this->filesystem); $this->set_logger($this->subscriber); } } diff --git a/tests/Unit/inc/Engine/Media/Lazyload/CSS/Subscriber/insertLazyloadScript.php b/tests/Unit/inc/Engine/Media/Lazyload/CSS/Subscriber/insertLazyloadScript.php index 589fc7a53b..e604852fe1 100644 --- a/tests/Unit/inc/Engine/Media/Lazyload/CSS/Subscriber/insertLazyloadScript.php +++ b/tests/Unit/inc/Engine/Media/Lazyload/CSS/Subscriber/insertLazyloadScript.php @@ -31,14 +31,25 @@ public function testShouldDoAsExpected( $config, $expected ) return $config['WP_ROCKET_ASSETS_JS_URL']; } + if('WP_ROCKET_ASSETS_JS_PATH' === $name) { + return $config['WP_ROCKET_ASSETS_JS_PATH']; + } + return null; }); + + + $this->context->expects()->is_allowed()->andReturn($config['is_allowed']); if($config['is_allowed']) { + + $this->filesystem->expects()->exists($expected['path'])->andReturn($config['exists']); + + $this->filesystem->expects()->get_contents($expected['path'])->andReturn($config['script_data']); Filters\expectApplied('rocket_lazyload_threshold')->with(300)->andReturn($config['threshold']); - Functions\expect('wp_enqueue_script')->with('rocket_lazyload_css', $expected['url'], [], $expected['version'], true); + Functions\expect('wp_add_inline_script')->with('rocket_lazyload_css', $expected['script_data'], true); Functions\expect('wp_localize_script')->with('rocket_lazyload_css', 'rocket_lazyload_css_data', $expected['data']); } diff --git a/wp-rocket.php b/wp-rocket.php index 37493a35b0..7c2b5dc491 100755 --- a/wp-rocket.php +++ b/wp-rocket.php @@ -53,7 +53,9 @@ define( 'WP_ROCKET_INC_URL', WP_ROCKET_URL . 'inc/' ); define( 'WP_ROCKET_ADMIN_URL', WP_ROCKET_INC_URL . 'admin/' ); define( 'WP_ROCKET_ASSETS_URL', WP_ROCKET_URL . 'assets/' ); +define( 'WP_ROCKET_ASSETS_PATH', WP_ROCKET_PATH . 'assets/' ); define( 'WP_ROCKET_ASSETS_JS_URL', WP_ROCKET_ASSETS_URL . 'js/' ); +define( 'WP_ROCKET_ASSETS_JS_PATH', WP_ROCKET_ASSETS_PATH . 'js/' ); define( 'WP_ROCKET_ASSETS_CSS_URL', WP_ROCKET_ASSETS_URL . 'css/' ); define( 'WP_ROCKET_ASSETS_IMG_URL', WP_ROCKET_ASSETS_URL . 'img/' ); From 589a15d62d563758d6aa6e6b2aec7a2cdec47774 Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Tue, 29 Aug 2023 16:21:50 +0200 Subject: [PATCH 09/27] Added new regex to fix issue with meta queries --- inc/Engine/Media/Lazyload/CSS/Front/Extractor.php | 4 ++-- .../CSS/Front/Extractor/CSS/content_with_url.php | 6 ++++++ .../Media/Lazyload/CSS/Front/Extractor/extract.php | 10 ++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php b/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php index b7e0877ca6..71ffbd2154 100644 --- a/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php +++ b/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php @@ -37,9 +37,9 @@ function ( $matches ) { $content ); - $background_regex = '(?[ \-,:\w.()\n\r^>[*"\'=\]#]+)\s?{[^}]*background\s*:(?[^;}]*)[^}]*}'; + $background_regex = '(?[ \-,:\w.()\n\r^>[*"\'=\]#]+)\s?{[^{}]*background\s*:(?[^;}]*)[^}]*}'; - $background_image_regex = '(?[ \-,:\w.()\n\r>^[*"\'=\]#]+)\s?{[^}]*background-image\s*:(?[^;}]*)[^}]*}'; + $background_image_regex = '(?[ \-,:\w.()\n\r>^[*"\'=\]#]+)\s?{[^{}]*background-image\s*:(?[^;}]*)[^}]*}'; /** * Lazyload background property regex. diff --git a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/Extractor/CSS/content_with_url.php b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/Extractor/CSS/content_with_url.php index b0da55a4e3..e0b51bbdfe 100644 --- a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/Extractor/CSS/content_with_url.php +++ b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/Extractor/CSS/content_with_url.php @@ -136,3 +136,9 @@ background-position: 14px 14px; background-repeat: no-repeat } + + @media(max-width: 1200px) { + .fl-node-reyn24wd18os > .fl-row-content-wrap { + background-image: url(https://www.villmarksbua.no/wp-content/uploads/2022/03/nordic-pocket-saw-foldbar-tursag-for-tre-og-metall.jpg); + } + } diff --git a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/Extractor/extract.php b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/Extractor/extract.php index aaf03f2497..1e9e8e8805 100644 --- a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/Extractor/extract.php +++ b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/Extractor/extract.php @@ -64,6 +64,16 @@ }' ], ], + '.fl-node-reyn24wd18os > .fl-row-content-wrap' => [ + [ + 'selector' => '.fl-node-reyn24wd18os > .fl-row-content-wrap', + 'url' => "url('https://www.villmarksbua.no/wp-content/uploads/2022/03/nordic-pocket-saw-foldbar-tursag-for-tre-og-metall.jpg')", + 'original' => 'url(https://www.villmarksbua.no/wp-content/uploads/2022/03/nordic-pocket-saw-foldbar-tursag-for-tre-og-metall.jpg)', + 'block' => '.fl-node-reyn24wd18os > .fl-row-content-wrap { + background-image: url(https://www.villmarksbua.no/wp-content/uploads/2022/03/nordic-pocket-saw-foldbar-tursag-for-tre-og-metall.jpg); + }' + ], + ], '.internal-css-background-image-gradient' => [ [ 'selector' => '.internal-css-background-image-gradient', From 79527a7db16a74a86456ab6328c61b4fbd923108 Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Wed, 30 Aug 2023 09:48:37 +0200 Subject: [PATCH 10/27] Added fix inline script --- inc/Engine/Media/Lazyload/CSS/Subscriber.php | 10 ++++++---- .../CSS/Subscriber/addLazyloadScriptExcludeJs.php | 2 +- .../addLazyloadScriptRocketDelayJsExclusions.php | 2 +- .../addLazyloadScriptRocketExcludeDeferJs.php | 2 +- .../Lazyload/CSS/Subscriber/insertLazyloadScript.php | 4 +++- 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/inc/Engine/Media/Lazyload/CSS/Subscriber.php b/inc/Engine/Media/Lazyload/CSS/Subscriber.php index 42db9c1e06..1b1f9d310c 100644 --- a/inc/Engine/Media/Lazyload/CSS/Subscriber.php +++ b/inc/Engine/Media/Lazyload/CSS/Subscriber.php @@ -253,7 +253,9 @@ public function insert_lazyload_script() { return; } - wp_add_inline_script( 'rocket_lazyload_css', $content, true ); + wp_register_script( 'rocket_lazyload_css', '', [], false, true ); + wp_enqueue_script( 'rocket_lazyload_css' ); + wp_add_inline_script( 'rocket_lazyload_css', $content, 'after' ); wp_localize_script( 'rocket_lazyload_css', 'rocket_lazyload_css_data', @@ -568,7 +570,7 @@ public function add_lazyload_script_exclude_js( array $js_files ) { return $js_files; } - $js_files [] = '#rocket_lazyload_css-js'; + $js_files [] = '#rocket_lazyload_css-js-after'; return $js_files; } @@ -583,7 +585,7 @@ public function add_lazyload_script_rocket_exclude_defer_js( array $exclude_defe return $exclude_defer_js; } - $exclude_defer_js [] = '#rocket_lazyload_css-js'; + $exclude_defer_js [] = '#rocket_lazyload_css-js-after'; return $exclude_defer_js; } @@ -598,7 +600,7 @@ public function add_lazyload_script_rocket_delay_js_exclusions( array $js_files return $js_files; } - $js_files [] = '#rocket_lazyload_css-js'; + $js_files [] = '#rocket_lazyload_css-js-after'; return $js_files; } diff --git a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/addLazyloadScriptExcludeJs.php b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/addLazyloadScriptExcludeJs.php index 4a26b6db54..305db45ea0 100644 --- a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/addLazyloadScriptExcludeJs.php +++ b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/addLazyloadScriptExcludeJs.php @@ -6,7 +6,7 @@ 'enabled' => true, ], 'expected' => [ - '#rocket_lazyload_css-js', + '#rocket_lazyload_css-js-after', ] ], 'disabledShouldReturnSame' => [ diff --git a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/addLazyloadScriptRocketDelayJsExclusions.php b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/addLazyloadScriptRocketDelayJsExclusions.php index 420ef6a1da..ba60c0da06 100644 --- a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/addLazyloadScriptRocketDelayJsExclusions.php +++ b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/addLazyloadScriptRocketDelayJsExclusions.php @@ -6,7 +6,7 @@ 'enabled' => true, ], 'expected' => [ - '#rocket_lazyload_css-js', + '#rocket_lazyload_css-js-after', ] ], 'DisabledShouldKeepSame' => [ diff --git a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/addLazyloadScriptRocketExcludeDeferJs.php b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/addLazyloadScriptRocketExcludeDeferJs.php index 301d057c0e..9198d73ea9 100644 --- a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/addLazyloadScriptRocketExcludeDeferJs.php +++ b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/addLazyloadScriptRocketExcludeDeferJs.php @@ -6,7 +6,7 @@ 'enabled' => true, ], 'expected' => [ - '#rocket_lazyload_css-js', + '#rocket_lazyload_css-js-after', ] ], 'disactivatedShouldReturnSame' => [ diff --git a/tests/Unit/inc/Engine/Media/Lazyload/CSS/Subscriber/insertLazyloadScript.php b/tests/Unit/inc/Engine/Media/Lazyload/CSS/Subscriber/insertLazyloadScript.php index e604852fe1..552c52454c 100644 --- a/tests/Unit/inc/Engine/Media/Lazyload/CSS/Subscriber/insertLazyloadScript.php +++ b/tests/Unit/inc/Engine/Media/Lazyload/CSS/Subscriber/insertLazyloadScript.php @@ -49,7 +49,9 @@ public function testShouldDoAsExpected( $config, $expected ) $this->filesystem->expects()->get_contents($expected['path'])->andReturn($config['script_data']); Filters\expectApplied('rocket_lazyload_threshold')->with(300)->andReturn($config['threshold']); - Functions\expect('wp_add_inline_script')->with('rocket_lazyload_css', $expected['script_data'], true); + Functions\expect('wp_register_script')->with('rocket_lazyload_css', '', [], false, true); + Functions\expect('wp_enqueue_script')->with('rocket_lazyload_css'); + Functions\expect('wp_add_inline_script')->with('rocket_lazyload_css', $expected['script_data'], 'after'); Functions\expect('wp_localize_script')->with('rocket_lazyload_css', 'rocket_lazyload_css_data', $expected['data']); } From e1decaf435078b9ec96104ad272d2c8dddabbc6a Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Wed, 30 Aug 2023 15:25:10 +0200 Subject: [PATCH 11/27] Added new script to handle dynamic elements --- assets/js/lazyload-css.js | 63 ++++++++++++++++++++++++++--------- assets/js/lazyload-css.min.js | 1 - 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/assets/js/lazyload-css.js b/assets/js/lazyload-css.js index f4002d2240..83ac194f10 100644 --- a/assets/js/lazyload-css.js +++ b/assets/js/lazyload-css.js @@ -1,4 +1,4 @@ -function rocket_css_lazyload() { +function rocket_css_lazyload_launch() { const pairs = rocket_pairs || []; @@ -6,7 +6,7 @@ function rocket_css_lazyload() { const threshold = rocket_lazyload_css_data.threshold || 300; - const observer = new IntersectionObserver(entries => { + const observer = rocket_lazyload_css_observer = new IntersectionObserver(entries => { entries.forEach(entry => { if (entry.isIntersecting) { const pairs = rocket_pairs.filter(s => entry.target.matches(s.selector)); @@ -16,7 +16,7 @@ function rocket_css_lazyload() { pair.elements.forEach(el => { el.setAttribute('data-rocket-lazy-bg', 'loaded'); // Stop observing the target element - observer.unobserve(el); + rocket_lazyload_css_observer.unobserve(el); }); } }) @@ -26,18 +26,51 @@ function rocket_css_lazyload() { rootMargin: threshold + 'px' }); - pairs.forEach(pair => { - try { - const elements = document.querySelectorAll(pair.selector); - elements.forEach(el => { - observer.observe(el); - // Save el in the pair object (create a new empty array if it doesn't exist) - (pair.elements ||= []).push(el); - }); - } catch (error) { - console.error(error); + function lazyload() { + + + pairs.forEach(pair => { + try { + const elements = document.querySelectorAll(pair.selector); + elements.forEach(el => { + observer.observe(el); + // Save el in the pair object (create a new empty array if it doesn't exist) + (pair.elements ||= []).push(el); + }); + } catch (error) { + console.error(error); + } + }); + } + + lazyload(); + + const observe_DOM = (function(){ + const MutationObserver = window.MutationObserver || window.WebKitMutationObserver; + + return function( obj, callback ){ + if( !obj || obj.nodeType !== 1 ) return; + + if( MutationObserver ){ + // define a new observer + const mutationObserver = new MutationObserver(callback); + + // have the observer observe for changes in children + mutationObserver.observe( obj, { childList:true, subtree:true }) + return mutationObserver + } + + // browser support fallback + else if( window.addEventListener ){ + obj.addEventListener('DOMNodeInserted', callback, false) + obj.addEventListener('DOMNodeRemoved', callback, false) + } + + obj.addEventListener('DOMSubtreeModified', callback, false) } - }); + })() + + observe_DOM(document.querySelector('body'), lazyload) } -rocket_css_lazyload(); \ No newline at end of file +rocket_css_lazyload_launch(); \ No newline at end of file diff --git a/assets/js/lazyload-css.min.js b/assets/js/lazyload-css.min.js index 32d7069025..82945003b8 100644 --- a/assets/js/lazyload-css.min.js +++ b/assets/js/lazyload-css.min.js @@ -1,2 +1 @@ !function o(n,c,s){function i(r,e){if(!c[r]){if(!n[r]){var t="function"==typeof require&&require;if(!e&&t)return t(r,!0);if(a)return a(r,!0);throw(t=new Error("Cannot find module '"+r+"'")).code="MODULE_NOT_FOUND",t}t=c[r]={exports:{}},n[r][0].call(t.exports,function(e){return i(n[r][1][e]||e)},t,t.exports,o,n,c,s)}return c[r].exports}for(var a="function"==typeof require&&require,e=0;e{e.forEach(r=>{if(r.isIntersecting){const e=rocket_pairs.filter(e=>r.target.matches(e.selector));e.map(e=>{e&&(t.innerHTML+=e.style,e.elements.forEach(e=>{e.setAttribute("data-rocket-lazy-bg","loaded"),o.unobserve(e)}))})}})},{rootMargin:r+"px"});e.forEach(r=>{try{const e=document.querySelectorAll(r.selector);e.forEach(e=>{o.observe(e),(r.elements||(r.elements=[])).push(e)})}catch(e){console.error(e)}})}()},{}]},{},[1]); -//# sourceMappingURL=lazyload-css.min.js.map From ddb9af203f4dab190c6008c91bf9d9559da780fa Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Wed, 30 Aug 2023 16:03:01 +0200 Subject: [PATCH 12/27] Added working version from the script --- assets/js/lazyload-css.js | 8 +++-- assets/js/lazyload-css.min.js | 1 - src/js/global/lazyload-css.js | 63 +++++++++++++++++++++++++++-------- 3 files changed, 54 insertions(+), 18 deletions(-) delete mode 100644 assets/js/lazyload-css.min.js diff --git a/assets/js/lazyload-css.js b/assets/js/lazyload-css.js index 83ac194f10..6b8c6df5c4 100644 --- a/assets/js/lazyload-css.js +++ b/assets/js/lazyload-css.js @@ -65,12 +65,14 @@ function rocket_css_lazyload_launch() { obj.addEventListener('DOMNodeInserted', callback, false) obj.addEventListener('DOMNodeRemoved', callback, false) } - - obj.addEventListener('DOMSubtreeModified', callback, false) } })() - observe_DOM(document.querySelector('body'), lazyload) + const body = document.querySelector('body'); + + observe_DOM(body, lazyload) + body.addEventListener('DOMSubtreeModified', lazyload, false) + } rocket_css_lazyload_launch(); \ No newline at end of file diff --git a/assets/js/lazyload-css.min.js b/assets/js/lazyload-css.min.js deleted file mode 100644 index 82945003b8..0000000000 --- a/assets/js/lazyload-css.min.js +++ /dev/null @@ -1 +0,0 @@ -!function o(n,c,s){function i(r,e){if(!c[r]){if(!n[r]){var t="function"==typeof require&&require;if(!e&&t)return t(r,!0);if(a)return a(r,!0);throw(t=new Error("Cannot find module '"+r+"'")).code="MODULE_NOT_FOUND",t}t=c[r]={exports:{}},n[r][0].call(t.exports,function(e){return i(n[r][1][e]||e)},t,t.exports,o,n,c,s)}return c[r].exports}for(var a="function"==typeof require&&require,e=0;e{e.forEach(r=>{if(r.isIntersecting){const e=rocket_pairs.filter(e=>r.target.matches(e.selector));e.map(e=>{e&&(t.innerHTML+=e.style,e.elements.forEach(e=>{e.setAttribute("data-rocket-lazy-bg","loaded"),o.unobserve(e)}))})}})},{rootMargin:r+"px"});e.forEach(r=>{try{const e=document.querySelectorAll(r.selector);e.forEach(e=>{o.observe(e),(r.elements||(r.elements=[])).push(e)})}catch(e){console.error(e)}})}()},{}]},{},[1]); diff --git a/src/js/global/lazyload-css.js b/src/js/global/lazyload-css.js index 926607aee1..157dc45ef2 100644 --- a/src/js/global/lazyload-css.js +++ b/src/js/global/lazyload-css.js @@ -1,6 +1,6 @@ -function rocket_css_lazyload() { +function rocket_css_lazyload_launch() { - const pairs = typeof rocket_pairs !== 'undefined' && rocket_pairs !== null ? rocket_pairs : []; + const pairs = rocket_pairs || []; const styleElement = document.querySelector('#wpr-lazyload-bg'); @@ -26,18 +26,53 @@ function rocket_css_lazyload() { rootMargin: threshold + 'px' }); - pairs.forEach(pair => { - try { - const elements = document.querySelectorAll(pair.selector); - elements.forEach(el => { - observer.observe(el); - // Save el in the pair object (create a new empty array if it doesn't exist) - (pair.elements ||= []).push(el); - }); - } catch (error) { - console.error(error); + function lazyload() { + + + pairs.forEach(pair => { + try { + const elements = document.querySelectorAll(pair.selector); + elements.forEach(el => { + observer.observe(el); + // Save el in the pair object (create a new empty array if it doesn't exist) + (pair.elements ||= []).push(el); + }); + } catch (error) { + console.error(error); + } + }); + } + + lazyload(); + + const observe_DOM = (function(){ + const MutationObserver = window.MutationObserver || window.WebKitMutationObserver; + + return function( obj, callback ){ + if( !obj || obj.nodeType !== 1 ) return; + + if( MutationObserver ){ + // define a new observer + const mutationObserver = new MutationObserver(callback); + + // have the observer observe for changes in children + mutationObserver.observe( obj, { childList:true, subtree:true }) + return mutationObserver + } + + // browser support fallback + else if( window.addEventListener ){ + obj.addEventListener('DOMNodeInserted', callback, false) + obj.addEventListener('DOMNodeRemoved', callback, false) + } } - }); + })() + + const body = document.querySelector('body'); + + observe_DOM(body, lazyload) + body.addEventListener('DOMSubtreeModified', lazyload, false) + } -rocket_css_lazyload(); \ No newline at end of file +rocket_css_lazyload_launch(); \ No newline at end of file From 5ea52abe4b110423690e8b4ceee12ab12f0e7e90 Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Wed, 30 Aug 2023 16:03:27 +0200 Subject: [PATCH 13/27] Added working version from the script --- assets/js/lazyload-css.min.js | 1 + assets/js/lazyload-css.min.js.map | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 assets/js/lazyload-css.min.js diff --git a/assets/js/lazyload-css.min.js b/assets/js/lazyload-css.min.js new file mode 100644 index 0000000000..31b8a13c72 --- /dev/null +++ b/assets/js/lazyload-css.min.js @@ -0,0 +1 @@ +!function n(o,i,c){function s(t,e){if(!i[t]){if(!o[t]){var r="function"==typeof require&&require;if(!e&&r)return r(t,!0);if(u)return u(t,!0);throw(r=new Error("Cannot find module '"+t+"'")).code="MODULE_NOT_FOUND",r}r=i[t]={exports:{}},o[t][0].call(r.exports,function(e){return s(o[t][1][e]||e)},r,r.exports,n,o,i,c)}return i[t].exports}for(var u="function"==typeof require&&require,e=0;e{e.forEach(t=>{if(t.isIntersecting){const e=rocket_pairs.filter(e=>t.target.matches(e.selector));e.map(e=>{e&&(r.innerHTML+=e.style,e.elements.forEach(e=>{e.setAttribute("data-rocket-lazy-bg","loaded"),n.unobserve(e)}))})}})},{rootMargin:t+"px"});function o(){e.forEach(t=>{try{const e=document.querySelectorAll(t.selector);e.forEach(e=>{n.observe(e),(t.elements||(t.elements=[])).push(e)})}catch(e){console.error(e)}})}o();const i=function(){const n=window.MutationObserver||window.WebKitMutationObserver;return function(e,t){if(e&&1===e.nodeType){if(n){const r=new n(t);return r.observe(e,{childList:!0,subtree:!0}),r}window.addEventListener&&(e.addEventListener("DOMNodeInserted",t,!1),e.addEventListener("DOMNodeRemoved",t,!1))}}}(),c=document.querySelector("body");i(c,o),c.addEventListener("DOMSubtreeModified",o,!1)}()},{}]},{},[1]); diff --git a/assets/js/lazyload-css.min.js.map b/assets/js/lazyload-css.min.js.map index 62d99920d6..5c265dcdae 100644 --- a/assets/js/lazyload-css.min.js.map +++ b/assets/js/lazyload-css.min.js.map @@ -1 +1 @@ -{"version":3,"names":[],"mappings":"","sources":["lazyload-css.min.js"],"sourcesContent":["!function o(n,c,s){function i(r,e){if(!c[r]){if(!n[r]){var t=\"function\"==typeof require&&require;if(!e&&t)return t(r,!0);if(a)return a(r,!0);throw(t=new Error(\"Cannot find module '\"+r+\"'\")).code=\"MODULE_NOT_FOUND\",t}t=c[r]={exports:{}},n[r][0].call(t.exports,function(e){return i(n[r][1][e]||e)},t,t.exports,o,n,c,s)}return c[r].exports}for(var a=\"function\"==typeof require&&require,e=0;e{e.forEach(r=>{if(r.isIntersecting){const e=rocket_pairs.filter(e=>r.target.matches(e.selector));e.map(e=>{e&&(t.innerHTML+=e.style,e.elements.forEach(e=>{e.setAttribute(\"data-rocket-lazy-bg\",\"loaded\"),o.unobserve(e)}))})}})},{rootMargin:r+\"px\"});e.forEach(r=>{try{const e=document.querySelectorAll(r.selector);e.forEach(e=>{o.observe(e),(r.elements||(r.elements=[])).push(e)})}catch(e){console.error(e)}})}()},{}]},{},[1]);"],"file":"lazyload-css.min.js"} \ No newline at end of file +{"version":3,"names":[],"mappings":"","sources":["lazyload-css.min.js"],"sourcesContent":["!function n(o,i,c){function s(t,e){if(!i[t]){if(!o[t]){var r=\"function\"==typeof require&&require;if(!e&&r)return r(t,!0);if(u)return u(t,!0);throw(r=new Error(\"Cannot find module '\"+t+\"'\")).code=\"MODULE_NOT_FOUND\",r}r=i[t]={exports:{}},o[t][0].call(r.exports,function(e){return s(o[t][1][e]||e)},r,r.exports,n,o,i,c)}return i[t].exports}for(var u=\"function\"==typeof require&&require,e=0;e{e.forEach(t=>{if(t.isIntersecting){const e=rocket_pairs.filter(e=>t.target.matches(e.selector));e.map(e=>{e&&(r.innerHTML+=e.style,e.elements.forEach(e=>{e.setAttribute(\"data-rocket-lazy-bg\",\"loaded\"),n.unobserve(e)}))})}})},{rootMargin:t+\"px\"});function o(){e.forEach(t=>{try{const e=document.querySelectorAll(t.selector);e.forEach(e=>{n.observe(e),(t.elements||(t.elements=[])).push(e)})}catch(e){console.error(e)}})}o();const i=function(){const n=window.MutationObserver||window.WebKitMutationObserver;return function(e,t){if(e&&1===e.nodeType){if(n){const r=new n(t);return r.observe(e,{childList:!0,subtree:!0}),r}window.addEventListener&&(e.addEventListener(\"DOMNodeInserted\",t,!1),e.addEventListener(\"DOMNodeRemoved\",t,!1))}}}(),c=document.querySelector(\"body\");i(c,o),c.addEventListener(\"DOMSubtreeModified\",o,!1)}()},{}]},{},[1]);"],"file":"lazyload-css.min.js"} \ No newline at end of file From 6f3cd1a10a984b2cb55bad2dc63b150a34bdf6fe Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Wed, 30 Aug 2023 16:45:39 +0200 Subject: [PATCH 14/27] Fixed warning on chrome --- assets/js/lazyload-css.js | 5 ++--- assets/js/lazyload-css.min.js | 2 +- assets/js/lazyload-css.min.js.map | 2 +- src/js/global/lazyload-css.js | 9 ++++----- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/assets/js/lazyload-css.js b/assets/js/lazyload-css.js index 6b8c6df5c4..8d00986ab7 100644 --- a/assets/js/lazyload-css.js +++ b/assets/js/lazyload-css.js @@ -56,7 +56,7 @@ function rocket_css_lazyload_launch() { const mutationObserver = new MutationObserver(callback); // have the observer observe for changes in children - mutationObserver.observe( obj, { childList:true, subtree:true }) + mutationObserver.observe( obj, { attributes: true, childList:true, subtree:true }) return mutationObserver } @@ -64,6 +64,7 @@ function rocket_css_lazyload_launch() { else if( window.addEventListener ){ obj.addEventListener('DOMNodeInserted', callback, false) obj.addEventListener('DOMNodeRemoved', callback, false) + obj.addEventListener('DOMSubtreeModified', callback, false) } } })() @@ -71,8 +72,6 @@ function rocket_css_lazyload_launch() { const body = document.querySelector('body'); observe_DOM(body, lazyload) - body.addEventListener('DOMSubtreeModified', lazyload, false) - } rocket_css_lazyload_launch(); \ No newline at end of file diff --git a/assets/js/lazyload-css.min.js b/assets/js/lazyload-css.min.js index 31b8a13c72..5637204849 100644 --- a/assets/js/lazyload-css.min.js +++ b/assets/js/lazyload-css.min.js @@ -1 +1 @@ -!function n(o,i,c){function s(t,e){if(!i[t]){if(!o[t]){var r="function"==typeof require&&require;if(!e&&r)return r(t,!0);if(u)return u(t,!0);throw(r=new Error("Cannot find module '"+t+"'")).code="MODULE_NOT_FOUND",r}r=i[t]={exports:{}},o[t][0].call(r.exports,function(e){return s(o[t][1][e]||e)},r,r.exports,n,o,i,c)}return i[t].exports}for(var u="function"==typeof require&&require,e=0;e{e.forEach(t=>{if(t.isIntersecting){const e=rocket_pairs.filter(e=>t.target.matches(e.selector));e.map(e=>{e&&(r.innerHTML+=e.style,e.elements.forEach(e=>{e.setAttribute("data-rocket-lazy-bg","loaded"),n.unobserve(e)}))})}})},{rootMargin:t+"px"});function o(){e.forEach(t=>{try{const e=document.querySelectorAll(t.selector);e.forEach(e=>{n.observe(e),(t.elements||(t.elements=[])).push(e)})}catch(e){console.error(e)}})}o();const i=function(){const n=window.MutationObserver||window.WebKitMutationObserver;return function(e,t){if(e&&1===e.nodeType){if(n){const r=new n(t);return r.observe(e,{childList:!0,subtree:!0}),r}window.addEventListener&&(e.addEventListener("DOMNodeInserted",t,!1),e.addEventListener("DOMNodeRemoved",t,!1))}}}(),c=document.querySelector("body");i(c,o),c.addEventListener("DOMSubtreeModified",o,!1)}()},{}]},{},[1]); +!function o(n,s,c){function i(t,e){if(!s[t]){if(!n[t]){var r="function"==typeof require&&require;if(!e&&r)return r(t,!0);if(a)return a(t,!0);throw(r=new Error("Cannot find module '"+t+"'")).code="MODULE_NOT_FOUND",r}r=s[t]={exports:{}},n[t][0].call(r.exports,function(e){return i(n[t][1][e]||e)},r,r.exports,o,n,s,c)}return s[t].exports}for(var a="function"==typeof require&&require,e=0;e{e.forEach(t=>{if(t.isIntersecting){const e=rocket_pairs.filter(e=>t.target.matches(e.selector));e.map(e=>{e&&(r.innerHTML+=e.style,e.elements.forEach(e=>{e.setAttribute("data-rocket-lazy-bg","loaded"),rocket_lazyload_css_observer.unobserve(e)}))})}})},{rootMargin:t+"px"});function n(){e.forEach(t=>{try{const e=document.querySelectorAll(t.selector);e.forEach(e=>{o.observe(e),(t.elements||(t.elements=[])).push(e)})}catch(e){console.error(e)}})}n();const s=function(){const o=window.MutationObserver||window.WebKitMutationObserver;return function(e,t){if(e&&1===e.nodeType){if(o){const r=new o(t);return r.observe(e,{attributes:!0,childList:!0,subtree:!0}),r}window.addEventListener&&(e.addEventListener("DOMNodeInserted",t,!1),e.addEventListener("DOMNodeRemoved",t,!1),e.addEventListener("DOMSubtreeModified",t,!1))}}}();t=document.querySelector("body"),s(t,n)}()},{}]},{},[1]); diff --git a/assets/js/lazyload-css.min.js.map b/assets/js/lazyload-css.min.js.map index 5c265dcdae..9c3c3cd318 100644 --- a/assets/js/lazyload-css.min.js.map +++ b/assets/js/lazyload-css.min.js.map @@ -1 +1 @@ -{"version":3,"names":[],"mappings":"","sources":["lazyload-css.min.js"],"sourcesContent":["!function n(o,i,c){function s(t,e){if(!i[t]){if(!o[t]){var r=\"function\"==typeof require&&require;if(!e&&r)return r(t,!0);if(u)return u(t,!0);throw(r=new Error(\"Cannot find module '\"+t+\"'\")).code=\"MODULE_NOT_FOUND\",r}r=i[t]={exports:{}},o[t][0].call(r.exports,function(e){return s(o[t][1][e]||e)},r,r.exports,n,o,i,c)}return i[t].exports}for(var u=\"function\"==typeof require&&require,e=0;e{e.forEach(t=>{if(t.isIntersecting){const e=rocket_pairs.filter(e=>t.target.matches(e.selector));e.map(e=>{e&&(r.innerHTML+=e.style,e.elements.forEach(e=>{e.setAttribute(\"data-rocket-lazy-bg\",\"loaded\"),n.unobserve(e)}))})}})},{rootMargin:t+\"px\"});function o(){e.forEach(t=>{try{const e=document.querySelectorAll(t.selector);e.forEach(e=>{n.observe(e),(t.elements||(t.elements=[])).push(e)})}catch(e){console.error(e)}})}o();const i=function(){const n=window.MutationObserver||window.WebKitMutationObserver;return function(e,t){if(e&&1===e.nodeType){if(n){const r=new n(t);return r.observe(e,{childList:!0,subtree:!0}),r}window.addEventListener&&(e.addEventListener(\"DOMNodeInserted\",t,!1),e.addEventListener(\"DOMNodeRemoved\",t,!1))}}}(),c=document.querySelector(\"body\");i(c,o),c.addEventListener(\"DOMSubtreeModified\",o,!1)}()},{}]},{},[1]);"],"file":"lazyload-css.min.js"} \ No newline at end of file +{"version":3,"names":[],"mappings":"","sources":["lazyload-css.min.js"],"sourcesContent":["!function o(n,s,c){function i(t,e){if(!s[t]){if(!n[t]){var r=\"function\"==typeof require&&require;if(!e&&r)return r(t,!0);if(a)return a(t,!0);throw(r=new Error(\"Cannot find module '\"+t+\"'\")).code=\"MODULE_NOT_FOUND\",r}r=s[t]={exports:{}},n[t][0].call(r.exports,function(e){return i(n[t][1][e]||e)},r,r.exports,o,n,s,c)}return s[t].exports}for(var a=\"function\"==typeof require&&require,e=0;e{e.forEach(t=>{if(t.isIntersecting){const e=rocket_pairs.filter(e=>t.target.matches(e.selector));e.map(e=>{e&&(r.innerHTML+=e.style,e.elements.forEach(e=>{e.setAttribute(\"data-rocket-lazy-bg\",\"loaded\"),rocket_lazyload_css_observer.unobserve(e)}))})}})},{rootMargin:t+\"px\"});function n(){e.forEach(t=>{try{const e=document.querySelectorAll(t.selector);e.forEach(e=>{o.observe(e),(t.elements||(t.elements=[])).push(e)})}catch(e){console.error(e)}})}n();const s=function(){const o=window.MutationObserver||window.WebKitMutationObserver;return function(e,t){if(e&&1===e.nodeType){if(o){const r=new o(t);return r.observe(e,{attributes:!0,childList:!0,subtree:!0}),r}window.addEventListener&&(e.addEventListener(\"DOMNodeInserted\",t,!1),e.addEventListener(\"DOMNodeRemoved\",t,!1),e.addEventListener(\"DOMSubtreeModified\",t,!1))}}}();t=document.querySelector(\"body\"),s(t,n)}()},{}]},{},[1]);"],"file":"lazyload-css.min.js"} \ No newline at end of file diff --git a/src/js/global/lazyload-css.js b/src/js/global/lazyload-css.js index 157dc45ef2..8d00986ab7 100644 --- a/src/js/global/lazyload-css.js +++ b/src/js/global/lazyload-css.js @@ -6,7 +6,7 @@ function rocket_css_lazyload_launch() { const threshold = rocket_lazyload_css_data.threshold || 300; - const observer = new IntersectionObserver(entries => { + const observer = rocket_lazyload_css_observer = new IntersectionObserver(entries => { entries.forEach(entry => { if (entry.isIntersecting) { const pairs = rocket_pairs.filter(s => entry.target.matches(s.selector)); @@ -16,7 +16,7 @@ function rocket_css_lazyload_launch() { pair.elements.forEach(el => { el.setAttribute('data-rocket-lazy-bg', 'loaded'); // Stop observing the target element - observer.unobserve(el); + rocket_lazyload_css_observer.unobserve(el); }); } }) @@ -56,7 +56,7 @@ function rocket_css_lazyload_launch() { const mutationObserver = new MutationObserver(callback); // have the observer observe for changes in children - mutationObserver.observe( obj, { childList:true, subtree:true }) + mutationObserver.observe( obj, { attributes: true, childList:true, subtree:true }) return mutationObserver } @@ -64,6 +64,7 @@ function rocket_css_lazyload_launch() { else if( window.addEventListener ){ obj.addEventListener('DOMNodeInserted', callback, false) obj.addEventListener('DOMNodeRemoved', callback, false) + obj.addEventListener('DOMSubtreeModified', callback, false) } } })() @@ -71,8 +72,6 @@ function rocket_css_lazyload_launch() { const body = document.querySelector('body'); observe_DOM(body, lazyload) - body.addEventListener('DOMSubtreeModified', lazyload, false) - } rocket_css_lazyload_launch(); \ No newline at end of file From 80896b196419afbdae8c55685c9bd9435411be71 Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Wed, 30 Aug 2023 18:04:27 +0200 Subject: [PATCH 15/27] Fixed phpcs and js --- assets/js/lazyload-css.js | 4 ++-- assets/js/lazyload-css.min.js | 2 +- assets/js/lazyload-css.min.js.map | 2 +- inc/Engine/Media/Lazyload/CSS/Front/Extractor.php | 1 - inc/Engine/Media/Lazyload/CSS/Subscriber.php | 2 +- src/js/global/lazyload-css.js | 4 ++-- 6 files changed, 7 insertions(+), 8 deletions(-) diff --git a/assets/js/lazyload-css.js b/assets/js/lazyload-css.js index 8d00986ab7..a143061d16 100644 --- a/assets/js/lazyload-css.js +++ b/assets/js/lazyload-css.js @@ -6,7 +6,7 @@ function rocket_css_lazyload_launch() { const threshold = rocket_lazyload_css_data.threshold || 300; - const observer = rocket_lazyload_css_observer = new IntersectionObserver(entries => { + const observer = new IntersectionObserver(entries => { entries.forEach(entry => { if (entry.isIntersecting) { const pairs = rocket_pairs.filter(s => entry.target.matches(s.selector)); @@ -16,7 +16,7 @@ function rocket_css_lazyload_launch() { pair.elements.forEach(el => { el.setAttribute('data-rocket-lazy-bg', 'loaded'); // Stop observing the target element - rocket_lazyload_css_observer.unobserve(el); + observer.unobserve(el); }); } }) diff --git a/assets/js/lazyload-css.min.js b/assets/js/lazyload-css.min.js index 5637204849..b745bc5dba 100644 --- a/assets/js/lazyload-css.min.js +++ b/assets/js/lazyload-css.min.js @@ -1 +1 @@ -!function o(n,s,c){function i(t,e){if(!s[t]){if(!n[t]){var r="function"==typeof require&&require;if(!e&&r)return r(t,!0);if(a)return a(t,!0);throw(r=new Error("Cannot find module '"+t+"'")).code="MODULE_NOT_FOUND",r}r=s[t]={exports:{}},n[t][0].call(r.exports,function(e){return i(n[t][1][e]||e)},r,r.exports,o,n,s,c)}return s[t].exports}for(var a="function"==typeof require&&require,e=0;e{e.forEach(t=>{if(t.isIntersecting){const e=rocket_pairs.filter(e=>t.target.matches(e.selector));e.map(e=>{e&&(r.innerHTML+=e.style,e.elements.forEach(e=>{e.setAttribute("data-rocket-lazy-bg","loaded"),rocket_lazyload_css_observer.unobserve(e)}))})}})},{rootMargin:t+"px"});function n(){e.forEach(t=>{try{const e=document.querySelectorAll(t.selector);e.forEach(e=>{o.observe(e),(t.elements||(t.elements=[])).push(e)})}catch(e){console.error(e)}})}n();const s=function(){const o=window.MutationObserver||window.WebKitMutationObserver;return function(e,t){if(e&&1===e.nodeType){if(o){const r=new o(t);return r.observe(e,{attributes:!0,childList:!0,subtree:!0}),r}window.addEventListener&&(e.addEventListener("DOMNodeInserted",t,!1),e.addEventListener("DOMNodeRemoved",t,!1),e.addEventListener("DOMSubtreeModified",t,!1))}}}();t=document.querySelector("body"),s(t,n)}()},{}]},{},[1]); +!function n(o,i,s){function c(t,e){if(!i[t]){if(!o[t]){var r="function"==typeof require&&require;if(!e&&r)return r(t,!0);if(u)return u(t,!0);throw(r=new Error("Cannot find module '"+t+"'")).code="MODULE_NOT_FOUND",r}r=i[t]={exports:{}},o[t][0].call(r.exports,function(e){return c(o[t][1][e]||e)},r,r.exports,n,o,i,s)}return i[t].exports}for(var u="function"==typeof require&&require,e=0;e{e.forEach(t=>{if(t.isIntersecting){const e=rocket_pairs.filter(e=>t.target.matches(e.selector));e.map(e=>{e&&(r.innerHTML+=e.style,e.elements.forEach(e=>{e.setAttribute("data-rocket-lazy-bg","loaded"),n.unobserve(e)}))})}})},{rootMargin:t+"px"});function o(){e.forEach(t=>{try{const e=document.querySelectorAll(t.selector);e.forEach(e=>{n.observe(e),(t.elements||(t.elements=[])).push(e)})}catch(e){console.error(e)}})}o();const i=function(){const n=window.MutationObserver||window.WebKitMutationObserver;return function(e,t){if(e&&1===e.nodeType){if(n){const r=new n(t);return r.observe(e,{attributes:!0,childList:!0,subtree:!0}),r}window.addEventListener&&(e.addEventListener("DOMNodeInserted",t,!1),e.addEventListener("DOMNodeRemoved",t,!1),e.addEventListener("DOMSubtreeModified",t,!1))}}}();t=document.querySelector("body"),i(t,o)}()},{}]},{},[1]); diff --git a/assets/js/lazyload-css.min.js.map b/assets/js/lazyload-css.min.js.map index 9c3c3cd318..962f166df3 100644 --- a/assets/js/lazyload-css.min.js.map +++ b/assets/js/lazyload-css.min.js.map @@ -1 +1 @@ -{"version":3,"names":[],"mappings":"","sources":["lazyload-css.min.js"],"sourcesContent":["!function o(n,s,c){function i(t,e){if(!s[t]){if(!n[t]){var r=\"function\"==typeof require&&require;if(!e&&r)return r(t,!0);if(a)return a(t,!0);throw(r=new Error(\"Cannot find module '\"+t+\"'\")).code=\"MODULE_NOT_FOUND\",r}r=s[t]={exports:{}},n[t][0].call(r.exports,function(e){return i(n[t][1][e]||e)},r,r.exports,o,n,s,c)}return s[t].exports}for(var a=\"function\"==typeof require&&require,e=0;e{e.forEach(t=>{if(t.isIntersecting){const e=rocket_pairs.filter(e=>t.target.matches(e.selector));e.map(e=>{e&&(r.innerHTML+=e.style,e.elements.forEach(e=>{e.setAttribute(\"data-rocket-lazy-bg\",\"loaded\"),rocket_lazyload_css_observer.unobserve(e)}))})}})},{rootMargin:t+\"px\"});function n(){e.forEach(t=>{try{const e=document.querySelectorAll(t.selector);e.forEach(e=>{o.observe(e),(t.elements||(t.elements=[])).push(e)})}catch(e){console.error(e)}})}n();const s=function(){const o=window.MutationObserver||window.WebKitMutationObserver;return function(e,t){if(e&&1===e.nodeType){if(o){const r=new o(t);return r.observe(e,{attributes:!0,childList:!0,subtree:!0}),r}window.addEventListener&&(e.addEventListener(\"DOMNodeInserted\",t,!1),e.addEventListener(\"DOMNodeRemoved\",t,!1),e.addEventListener(\"DOMSubtreeModified\",t,!1))}}}();t=document.querySelector(\"body\"),s(t,n)}()},{}]},{},[1]);"],"file":"lazyload-css.min.js"} \ No newline at end of file +{"version":3,"names":[],"mappings":"","sources":["lazyload-css.min.js"],"sourcesContent":["!function n(o,i,s){function c(t,e){if(!i[t]){if(!o[t]){var r=\"function\"==typeof require&&require;if(!e&&r)return r(t,!0);if(u)return u(t,!0);throw(r=new Error(\"Cannot find module '\"+t+\"'\")).code=\"MODULE_NOT_FOUND\",r}r=i[t]={exports:{}},o[t][0].call(r.exports,function(e){return c(o[t][1][e]||e)},r,r.exports,n,o,i,s)}return i[t].exports}for(var u=\"function\"==typeof require&&require,e=0;e{e.forEach(t=>{if(t.isIntersecting){const e=rocket_pairs.filter(e=>t.target.matches(e.selector));e.map(e=>{e&&(r.innerHTML+=e.style,e.elements.forEach(e=>{e.setAttribute(\"data-rocket-lazy-bg\",\"loaded\"),n.unobserve(e)}))})}})},{rootMargin:t+\"px\"});function o(){e.forEach(t=>{try{const e=document.querySelectorAll(t.selector);e.forEach(e=>{n.observe(e),(t.elements||(t.elements=[])).push(e)})}catch(e){console.error(e)}})}o();const i=function(){const n=window.MutationObserver||window.WebKitMutationObserver;return function(e,t){if(e&&1===e.nodeType){if(n){const r=new n(t);return r.observe(e,{attributes:!0,childList:!0,subtree:!0}),r}window.addEventListener&&(e.addEventListener(\"DOMNodeInserted\",t,!1),e.addEventListener(\"DOMNodeRemoved\",t,!1),e.addEventListener(\"DOMSubtreeModified\",t,!1))}}}();t=document.querySelector(\"body\"),i(t,o)}()},{}]},{},[1]);"],"file":"lazyload-css.min.js"} \ No newline at end of file diff --git a/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php b/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php index c531720428..71ffbd2154 100644 --- a/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php +++ b/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php @@ -41,7 +41,6 @@ function ( $matches ) { $background_image_regex = '(?[ \-,:\w.()\n\r>^[*"\'=\]#]+)\s?{[^{}]*background-image\s*:(?[^;}]*)[^}]*}'; - /** * Lazyload background property regex. * diff --git a/inc/Engine/Media/Lazyload/CSS/Subscriber.php b/inc/Engine/Media/Lazyload/CSS/Subscriber.php index 1b1f9d310c..56b9e1a780 100644 --- a/inc/Engine/Media/Lazyload/CSS/Subscriber.php +++ b/inc/Engine/Media/Lazyload/CSS/Subscriber.php @@ -253,7 +253,7 @@ public function insert_lazyload_script() { return; } - wp_register_script( 'rocket_lazyload_css', '', [], false, true ); + wp_register_script( 'rocket_lazyload_css', '', [], false, true ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NoExplicitVersion wp_enqueue_script( 'rocket_lazyload_css' ); wp_add_inline_script( 'rocket_lazyload_css', $content, 'after' ); wp_localize_script( diff --git a/src/js/global/lazyload-css.js b/src/js/global/lazyload-css.js index 8d00986ab7..a143061d16 100644 --- a/src/js/global/lazyload-css.js +++ b/src/js/global/lazyload-css.js @@ -6,7 +6,7 @@ function rocket_css_lazyload_launch() { const threshold = rocket_lazyload_css_data.threshold || 300; - const observer = rocket_lazyload_css_observer = new IntersectionObserver(entries => { + const observer = new IntersectionObserver(entries => { entries.forEach(entry => { if (entry.isIntersecting) { const pairs = rocket_pairs.filter(s => entry.target.matches(s.selector)); @@ -16,7 +16,7 @@ function rocket_css_lazyload_launch() { pair.elements.forEach(el => { el.setAttribute('data-rocket-lazy-bg', 'loaded'); // Stop observing the target element - rocket_lazyload_css_observer.unobserve(el); + observer.unobserve(el); }); } }) From 0a320a08164eba9c050ffb704b39bc826e354b7d Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Thu, 31 Aug 2023 09:42:09 +0200 Subject: [PATCH 16/27] Fixed script --- assets/js/lazyload-css.min.js | 2 +- assets/js/lazyload-css.min.js.map | 2 +- src/js/global/lazyload-css.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/assets/js/lazyload-css.min.js b/assets/js/lazyload-css.min.js index b745bc5dba..e88b2b95bb 100644 --- a/assets/js/lazyload-css.min.js +++ b/assets/js/lazyload-css.min.js @@ -1 +1 @@ -!function n(o,i,s){function c(t,e){if(!i[t]){if(!o[t]){var r="function"==typeof require&&require;if(!e&&r)return r(t,!0);if(u)return u(t,!0);throw(r=new Error("Cannot find module '"+t+"'")).code="MODULE_NOT_FOUND",r}r=i[t]={exports:{}},o[t][0].call(r.exports,function(e){return c(o[t][1][e]||e)},r,r.exports,n,o,i,s)}return i[t].exports}for(var u="function"==typeof require&&require,e=0;e{e.forEach(t=>{if(t.isIntersecting){const e=rocket_pairs.filter(e=>t.target.matches(e.selector));e.map(e=>{e&&(r.innerHTML+=e.style,e.elements.forEach(e=>{e.setAttribute("data-rocket-lazy-bg","loaded"),n.unobserve(e)}))})}})},{rootMargin:t+"px"});function o(){e.forEach(t=>{try{const e=document.querySelectorAll(t.selector);e.forEach(e=>{n.observe(e),(t.elements||(t.elements=[])).push(e)})}catch(e){console.error(e)}})}o();const i=function(){const n=window.MutationObserver||window.WebKitMutationObserver;return function(e,t){if(e&&1===e.nodeType){if(n){const r=new n(t);return r.observe(e,{attributes:!0,childList:!0,subtree:!0}),r}window.addEventListener&&(e.addEventListener("DOMNodeInserted",t,!1),e.addEventListener("DOMNodeRemoved",t,!1),e.addEventListener("DOMSubtreeModified",t,!1))}}}();t=document.querySelector("body"),i(t,o)}()},{}]},{},[1]); +!function n(o,i,s){function c(t,e){if(!i[t]){if(!o[t]){var r="function"==typeof require&&require;if(!e&&r)return r(t,!0);if(u)return u(t,!0);throw(r=new Error("Cannot find module '"+t+"'")).code="MODULE_NOT_FOUND",r}r=i[t]={exports:{}},o[t][0].call(r.exports,function(e){return c(o[t][1][e]||e)},r,r.exports,n,o,i,s)}return i[t].exports}for(var u="function"==typeof require&&require,e=0;e{e.forEach(t=>{if(t.isIntersecting){const e=rocket_pairs.filter(e=>t.target.matches(e.selector));e.map(e=>{e&&(r.innerHTML+=e.style,e.elements.forEach(e=>{e.setAttribute("data-rocket-lazy-bg","loaded"),n.unobserve(e)}))})}})},{rootMargin:t+"px"});function o(){e.forEach(t=>{try{const e=document.querySelectorAll(t.selector);e.forEach(e=>{n.observe(e),(t.elements||(t.elements=[])).push(e)})}catch(e){console.error(e)}})}o();const i=function(){const n=window.MutationObserver||window.WebKitMutationObserver;return function(e,t){if(e&&1===e.nodeType){if(n){const r=new n(t);return r.observe(e,{attributes:!0,childList:!0,subtree:!0}),r}window.addEventListener&&(e.addEventListener("DOMNodeInserted",t,!1),e.addEventListener("DOMNodeRemoved",t,!1),e.addEventListener("DOMSubtreeModified",t,!1))}}}();t=document.querySelector("body"),i(t,o)}()},{}]},{},[1]); diff --git a/assets/js/lazyload-css.min.js.map b/assets/js/lazyload-css.min.js.map index 962f166df3..4327488383 100644 --- a/assets/js/lazyload-css.min.js.map +++ b/assets/js/lazyload-css.min.js.map @@ -1 +1 @@ -{"version":3,"names":[],"mappings":"","sources":["lazyload-css.min.js"],"sourcesContent":["!function n(o,i,s){function c(t,e){if(!i[t]){if(!o[t]){var r=\"function\"==typeof require&&require;if(!e&&r)return r(t,!0);if(u)return u(t,!0);throw(r=new Error(\"Cannot find module '\"+t+\"'\")).code=\"MODULE_NOT_FOUND\",r}r=i[t]={exports:{}},o[t][0].call(r.exports,function(e){return c(o[t][1][e]||e)},r,r.exports,n,o,i,s)}return i[t].exports}for(var u=\"function\"==typeof require&&require,e=0;e{e.forEach(t=>{if(t.isIntersecting){const e=rocket_pairs.filter(e=>t.target.matches(e.selector));e.map(e=>{e&&(r.innerHTML+=e.style,e.elements.forEach(e=>{e.setAttribute(\"data-rocket-lazy-bg\",\"loaded\"),n.unobserve(e)}))})}})},{rootMargin:t+\"px\"});function o(){e.forEach(t=>{try{const e=document.querySelectorAll(t.selector);e.forEach(e=>{n.observe(e),(t.elements||(t.elements=[])).push(e)})}catch(e){console.error(e)}})}o();const i=function(){const n=window.MutationObserver||window.WebKitMutationObserver;return function(e,t){if(e&&1===e.nodeType){if(n){const r=new n(t);return r.observe(e,{attributes:!0,childList:!0,subtree:!0}),r}window.addEventListener&&(e.addEventListener(\"DOMNodeInserted\",t,!1),e.addEventListener(\"DOMNodeRemoved\",t,!1),e.addEventListener(\"DOMSubtreeModified\",t,!1))}}}();t=document.querySelector(\"body\"),i(t,o)}()},{}]},{},[1]);"],"file":"lazyload-css.min.js"} \ No newline at end of file +{"version":3,"names":[],"mappings":"","sources":["lazyload-css.min.js"],"sourcesContent":["!function n(o,i,s){function c(t,e){if(!i[t]){if(!o[t]){var r=\"function\"==typeof require&&require;if(!e&&r)return r(t,!0);if(u)return u(t,!0);throw(r=new Error(\"Cannot find module '\"+t+\"'\")).code=\"MODULE_NOT_FOUND\",r}r=i[t]={exports:{}},o[t][0].call(r.exports,function(e){return c(o[t][1][e]||e)},r,r.exports,n,o,i,s)}return i[t].exports}for(var u=\"function\"==typeof require&&require,e=0;e{e.forEach(t=>{if(t.isIntersecting){const e=rocket_pairs.filter(e=>t.target.matches(e.selector));e.map(e=>{e&&(r.innerHTML+=e.style,e.elements.forEach(e=>{e.setAttribute(\"data-rocket-lazy-bg\",\"loaded\"),n.unobserve(e)}))})}})},{rootMargin:t+\"px\"});function o(){e.forEach(t=>{try{const e=document.querySelectorAll(t.selector);e.forEach(e=>{n.observe(e),(t.elements||(t.elements=[])).push(e)})}catch(e){console.error(e)}})}o();const i=function(){const n=window.MutationObserver||window.WebKitMutationObserver;return function(e,t){if(e&&1===e.nodeType){if(n){const r=new n(t);return r.observe(e,{attributes:!0,childList:!0,subtree:!0}),r}window.addEventListener&&(e.addEventListener(\"DOMNodeInserted\",t,!1),e.addEventListener(\"DOMNodeRemoved\",t,!1),e.addEventListener(\"DOMSubtreeModified\",t,!1))}}}();t=document.querySelector(\"body\"),i(t,o)}()},{}]},{},[1]);"],"file":"lazyload-css.min.js"} \ No newline at end of file diff --git a/src/js/global/lazyload-css.js b/src/js/global/lazyload-css.js index a143061d16..b565e53835 100644 --- a/src/js/global/lazyload-css.js +++ b/src/js/global/lazyload-css.js @@ -1,6 +1,6 @@ function rocket_css_lazyload_launch() { - const pairs = rocket_pairs || []; + const pairs = typeof rocket_pairs === 'undefined' ? [] : rocket_pairs; const styleElement = document.querySelector('#wpr-lazyload-bg'); From 7541f70ccbf7669e5db754217ad2c6bef75f8ffa Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Thu, 31 Aug 2023 11:54:56 +0200 Subject: [PATCH 17/27] Fixed script --- assets/js/lazyload-css.js | 39 +++++++++++++++++-------------- assets/js/lazyload-css.min.js | 2 +- assets/js/lazyload-css.min.js.map | 2 +- src/js/global/lazyload-css.js | 39 +++++++++++++++++-------------- 4 files changed, 44 insertions(+), 38 deletions(-) diff --git a/assets/js/lazyload-css.js b/assets/js/lazyload-css.js index a143061d16..e0465f9f35 100644 --- a/assets/js/lazyload-css.js +++ b/assets/js/lazyload-css.js @@ -1,6 +1,8 @@ function rocket_css_lazyload_launch() { - const pairs = rocket_pairs || []; + const usable_pairs = typeof rocket_pairs === 'undefined' ? [] : rocket_pairs; + + const styleElement = document.querySelector('#wpr-lazyload-bg'); @@ -9,7 +11,7 @@ function rocket_css_lazyload_launch() { const observer = new IntersectionObserver(entries => { entries.forEach(entry => { if (entry.isIntersecting) { - const pairs = rocket_pairs.filter(s => entry.target.matches(s.selector)); + const pairs = usable_pairs.filter(s => entry.target.matches(s.selector)); pairs.map(pair => { if (pair) { styleElement.innerHTML += pair.style; @@ -26,13 +28,22 @@ function rocket_css_lazyload_launch() { rootMargin: threshold + 'px' }); - function lazyload() { + function lazyload(e = []) { + const pass = e.length === 0 || e.find(e => e.type !== 'attributes' || e.attributeName === 'class'); + + if(! pass ) { + return; + } - pairs.forEach(pair => { + usable_pairs.forEach(pair => { try { + const elements = document.querySelectorAll(pair.selector); elements.forEach(el => { + if(el.getAttribute('data-rocket-lazy-bg') === 'loaded') { + return; + } observer.observe(el); // Save el in the pair object (create a new empty array if it doesn't exist) (pair.elements ||= []).push(el); @@ -46,26 +57,18 @@ function rocket_css_lazyload_launch() { lazyload(); const observe_DOM = (function(){ - const MutationObserver = window.MutationObserver || window.WebKitMutationObserver; + const MutationObserver = window.MutationObserver; return function( obj, callback ){ if( !obj || obj.nodeType !== 1 ) return; - if( MutationObserver ){ - // define a new observer - const mutationObserver = new MutationObserver(callback); + // define a new observer + const mutationObserver = new MutationObserver(callback); - // have the observer observe for changes in children - mutationObserver.observe( obj, { attributes: true, childList:true, subtree:true }) - return mutationObserver - } + // have the observer observe for changes in children + mutationObserver.observe( obj, { attributes: true, childList:true, subtree:true }) + return mutationObserver - // browser support fallback - else if( window.addEventListener ){ - obj.addEventListener('DOMNodeInserted', callback, false) - obj.addEventListener('DOMNodeRemoved', callback, false) - obj.addEventListener('DOMSubtreeModified', callback, false) - } } })() diff --git a/assets/js/lazyload-css.min.js b/assets/js/lazyload-css.min.js index e88b2b95bb..274036445d 100644 --- a/assets/js/lazyload-css.min.js +++ b/assets/js/lazyload-css.min.js @@ -1 +1 @@ -!function n(o,i,s){function c(t,e){if(!i[t]){if(!o[t]){var r="function"==typeof require&&require;if(!e&&r)return r(t,!0);if(u)return u(t,!0);throw(r=new Error("Cannot find module '"+t+"'")).code="MODULE_NOT_FOUND",r}r=i[t]={exports:{}},o[t][0].call(r.exports,function(e){return c(o[t][1][e]||e)},r,r.exports,n,o,i,s)}return i[t].exports}for(var u="function"==typeof require&&require,e=0;e{e.forEach(t=>{if(t.isIntersecting){const e=rocket_pairs.filter(e=>t.target.matches(e.selector));e.map(e=>{e&&(r.innerHTML+=e.style,e.elements.forEach(e=>{e.setAttribute("data-rocket-lazy-bg","loaded"),n.unobserve(e)}))})}})},{rootMargin:t+"px"});function o(){e.forEach(t=>{try{const e=document.querySelectorAll(t.selector);e.forEach(e=>{n.observe(e),(t.elements||(t.elements=[])).push(e)})}catch(e){console.error(e)}})}o();const i=function(){const n=window.MutationObserver||window.WebKitMutationObserver;return function(e,t){if(e&&1===e.nodeType){if(n){const r=new n(t);return r.observe(e,{attributes:!0,childList:!0,subtree:!0}),r}window.addEventListener&&(e.addEventListener("DOMNodeInserted",t,!1),e.addEventListener("DOMNodeRemoved",t,!1),e.addEventListener("DOMSubtreeModified",t,!1))}}}();t=document.querySelector("body"),i(t,o)}()},{}]},{},[1]); +!function o(n,c,i){function s(t,e){if(!c[t]){if(!n[t]){var r="function"==typeof require&&require;if(!e&&r)return r(t,!0);if(u)return u(t,!0);throw(r=new Error("Cannot find module '"+t+"'")).code="MODULE_NOT_FOUND",r}r=c[t]={exports:{}},n[t][0].call(r.exports,function(e){return s(n[t][1][e]||e)},r,r.exports,o,n,c,i)}return c[t].exports}for(var u="function"==typeof require&&require,e=0;e{e.forEach(t=>{if(t.isIntersecting){const e=r.filter(e=>t.target.matches(e.selector));e.map(e=>{e&&(o.innerHTML+=e.style,e.elements.forEach(e=>{e.setAttribute("data-rocket-lazy-bg","loaded"),n.unobserve(e)}))})}})},{rootMargin:e+"px"});function t(){let e=0"attributes"!==e.type||"class"===e.attributeName)||r.forEach(t=>{try{const e=document.querySelectorAll(t.selector);e.forEach(e=>{"loaded"!==e.getAttribute("data-rocket-lazy-bg")&&(n.observe(e),(t.elements||(t.elements=[])).push(e))})}catch(e){console.error(e)}})}t();const c=function(){const o=window.MutationObserver;return function(e,t){if(e&&1===e.nodeType){const r=new o(t);return r.observe(e,{attributes:!0,childList:!0,subtree:!0}),r}}}();e=document.querySelector("body"),c(e,t)}()},{}]},{},[1]); diff --git a/assets/js/lazyload-css.min.js.map b/assets/js/lazyload-css.min.js.map index 4327488383..ede360a47f 100644 --- a/assets/js/lazyload-css.min.js.map +++ b/assets/js/lazyload-css.min.js.map @@ -1 +1 @@ -{"version":3,"names":[],"mappings":"","sources":["lazyload-css.min.js"],"sourcesContent":["!function n(o,i,s){function c(t,e){if(!i[t]){if(!o[t]){var r=\"function\"==typeof require&&require;if(!e&&r)return r(t,!0);if(u)return u(t,!0);throw(r=new Error(\"Cannot find module '\"+t+\"'\")).code=\"MODULE_NOT_FOUND\",r}r=i[t]={exports:{}},o[t][0].call(r.exports,function(e){return c(o[t][1][e]||e)},r,r.exports,n,o,i,s)}return i[t].exports}for(var u=\"function\"==typeof require&&require,e=0;e{e.forEach(t=>{if(t.isIntersecting){const e=rocket_pairs.filter(e=>t.target.matches(e.selector));e.map(e=>{e&&(r.innerHTML+=e.style,e.elements.forEach(e=>{e.setAttribute(\"data-rocket-lazy-bg\",\"loaded\"),n.unobserve(e)}))})}})},{rootMargin:t+\"px\"});function o(){e.forEach(t=>{try{const e=document.querySelectorAll(t.selector);e.forEach(e=>{n.observe(e),(t.elements||(t.elements=[])).push(e)})}catch(e){console.error(e)}})}o();const i=function(){const n=window.MutationObserver||window.WebKitMutationObserver;return function(e,t){if(e&&1===e.nodeType){if(n){const r=new n(t);return r.observe(e,{attributes:!0,childList:!0,subtree:!0}),r}window.addEventListener&&(e.addEventListener(\"DOMNodeInserted\",t,!1),e.addEventListener(\"DOMNodeRemoved\",t,!1),e.addEventListener(\"DOMSubtreeModified\",t,!1))}}}();t=document.querySelector(\"body\"),i(t,o)}()},{}]},{},[1]);"],"file":"lazyload-css.min.js"} \ No newline at end of file +{"version":3,"names":[],"mappings":"","sources":["lazyload-css.min.js"],"sourcesContent":["!function o(n,c,i){function s(t,e){if(!c[t]){if(!n[t]){var r=\"function\"==typeof require&&require;if(!e&&r)return r(t,!0);if(u)return u(t,!0);throw(r=new Error(\"Cannot find module '\"+t+\"'\")).code=\"MODULE_NOT_FOUND\",r}r=c[t]={exports:{}},n[t][0].call(r.exports,function(e){return s(n[t][1][e]||e)},r,r.exports,o,n,c,i)}return c[t].exports}for(var u=\"function\"==typeof require&&require,e=0;e{e.forEach(t=>{if(t.isIntersecting){const e=r.filter(e=>t.target.matches(e.selector));e.map(e=>{e&&(o.innerHTML+=e.style,e.elements.forEach(e=>{e.setAttribute(\"data-rocket-lazy-bg\",\"loaded\"),n.unobserve(e)}))})}})},{rootMargin:e+\"px\"});function t(){let e=0\"attributes\"!==e.type||\"class\"===e.attributeName)||r.forEach(t=>{try{const e=document.querySelectorAll(t.selector);e.forEach(e=>{\"loaded\"!==e.getAttribute(\"data-rocket-lazy-bg\")&&(n.observe(e),(t.elements||(t.elements=[])).push(e))})}catch(e){console.error(e)}})}t();const c=function(){const o=window.MutationObserver;return function(e,t){if(e&&1===e.nodeType){const r=new o(t);return r.observe(e,{attributes:!0,childList:!0,subtree:!0}),r}}}();e=document.querySelector(\"body\"),c(e,t)}()},{}]},{},[1]);"],"file":"lazyload-css.min.js"} \ No newline at end of file diff --git a/src/js/global/lazyload-css.js b/src/js/global/lazyload-css.js index b565e53835..e0465f9f35 100644 --- a/src/js/global/lazyload-css.js +++ b/src/js/global/lazyload-css.js @@ -1,6 +1,8 @@ function rocket_css_lazyload_launch() { - const pairs = typeof rocket_pairs === 'undefined' ? [] : rocket_pairs; + const usable_pairs = typeof rocket_pairs === 'undefined' ? [] : rocket_pairs; + + const styleElement = document.querySelector('#wpr-lazyload-bg'); @@ -9,7 +11,7 @@ function rocket_css_lazyload_launch() { const observer = new IntersectionObserver(entries => { entries.forEach(entry => { if (entry.isIntersecting) { - const pairs = rocket_pairs.filter(s => entry.target.matches(s.selector)); + const pairs = usable_pairs.filter(s => entry.target.matches(s.selector)); pairs.map(pair => { if (pair) { styleElement.innerHTML += pair.style; @@ -26,13 +28,22 @@ function rocket_css_lazyload_launch() { rootMargin: threshold + 'px' }); - function lazyload() { + function lazyload(e = []) { + const pass = e.length === 0 || e.find(e => e.type !== 'attributes' || e.attributeName === 'class'); + + if(! pass ) { + return; + } - pairs.forEach(pair => { + usable_pairs.forEach(pair => { try { + const elements = document.querySelectorAll(pair.selector); elements.forEach(el => { + if(el.getAttribute('data-rocket-lazy-bg') === 'loaded') { + return; + } observer.observe(el); // Save el in the pair object (create a new empty array if it doesn't exist) (pair.elements ||= []).push(el); @@ -46,26 +57,18 @@ function rocket_css_lazyload_launch() { lazyload(); const observe_DOM = (function(){ - const MutationObserver = window.MutationObserver || window.WebKitMutationObserver; + const MutationObserver = window.MutationObserver; return function( obj, callback ){ if( !obj || obj.nodeType !== 1 ) return; - if( MutationObserver ){ - // define a new observer - const mutationObserver = new MutationObserver(callback); + // define a new observer + const mutationObserver = new MutationObserver(callback); - // have the observer observe for changes in children - mutationObserver.observe( obj, { attributes: true, childList:true, subtree:true }) - return mutationObserver - } + // have the observer observe for changes in children + mutationObserver.observe( obj, { attributes: true, childList:true, subtree:true }) + return mutationObserver - // browser support fallback - else if( window.addEventListener ){ - obj.addEventListener('DOMNodeInserted', callback, false) - obj.addEventListener('DOMNodeRemoved', callback, false) - obj.addEventListener('DOMSubtreeModified', callback, false) - } } })() From fc4e1bd3274ebc28d84ef796436230725b72858d Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Thu, 31 Aug 2023 12:26:33 +0200 Subject: [PATCH 18/27] Fixed issue with relative urls --- inc/Engine/Media/Lazyload/CSS/Front/Extractor.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php b/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php index 71ffbd2154..8eb9529d86 100644 --- a/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php +++ b/inc/Engine/Media/Lazyload/CSS/Front/Extractor.php @@ -133,7 +133,7 @@ protected function extract_urls( string $content ): array { continue; } - if ( ! $this->is_url_external( $url ) ) { + if ( ! $this->is_url_external( $url ) && ! $this->is_relative( $url ) ) { $url = $this->apply_string_filter( 'css_url', $url ); } @@ -193,7 +193,7 @@ protected function is_url_ignored( string $url, array $ignored_urls ): bool { protected function make_url_complete( string $url ): string { $host = wp_parse_url( $url, PHP_URL_HOST ); - if ( $host ) { + if ( $host || $this->is_relative( $url ) ) { return $url; } @@ -217,4 +217,14 @@ protected function is_url_external( string $url ): bool { return $host !== $home_host; } + + /** + * Check if the URL is relative. + * + * @param string $url URL to check. + * @return bool + */ + protected function is_relative( string $url ): bool { + return preg_match( '/^\./', $url ); + } } From 78bbb1d7dd1eddfd47253ac2d8bb3e9953a94c78 Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Thu, 31 Aug 2023 15:12:43 +0200 Subject: [PATCH 19/27] Added fix to lazyload --- assets/js/lazyload-css.js | 5 +++++ assets/js/lazyload-css.min.js | 3 ++- assets/js/lazyload-css.min.js.map | 2 +- inc/Engine/Media/Lazyload/CanLazyloadTrait.php | 2 ++ src/js/global/lazyload-css.js | 5 +++++ .../Media/Lazyload/Subscriber/insertLazyloadScript.php | 5 +++-- 6 files changed, 18 insertions(+), 4 deletions(-) diff --git a/assets/js/lazyload-css.js b/assets/js/lazyload-css.js index e0465f9f35..b49de1343a 100644 --- a/assets/js/lazyload-css.js +++ b/assets/js/lazyload-css.js @@ -15,6 +15,11 @@ function rocket_css_lazyload_launch() { pairs.map(pair => { if (pair) { styleElement.innerHTML += pair.style; + + if(pair.elements === undefined) { + return; + } + pair.elements.forEach(el => { el.setAttribute('data-rocket-lazy-bg', 'loaded'); // Stop observing the target element diff --git a/assets/js/lazyload-css.min.js b/assets/js/lazyload-css.min.js index 274036445d..a269b592a1 100644 --- a/assets/js/lazyload-css.min.js +++ b/assets/js/lazyload-css.min.js @@ -1 +1,2 @@ -!function o(n,c,i){function s(t,e){if(!c[t]){if(!n[t]){var r="function"==typeof require&&require;if(!e&&r)return r(t,!0);if(u)return u(t,!0);throw(r=new Error("Cannot find module '"+t+"'")).code="MODULE_NOT_FOUND",r}r=c[t]={exports:{}},n[t][0].call(r.exports,function(e){return s(n[t][1][e]||e)},r,r.exports,o,n,c,i)}return c[t].exports}for(var u="function"==typeof require&&require,e=0;e{e.forEach(t=>{if(t.isIntersecting){const e=r.filter(e=>t.target.matches(e.selector));e.map(e=>{e&&(o.innerHTML+=e.style,e.elements.forEach(e=>{e.setAttribute("data-rocket-lazy-bg","loaded"),n.unobserve(e)}))})}})},{rootMargin:e+"px"});function t(){let e=0"attributes"!==e.type||"class"===e.attributeName)||r.forEach(t=>{try{const e=document.querySelectorAll(t.selector);e.forEach(e=>{"loaded"!==e.getAttribute("data-rocket-lazy-bg")&&(n.observe(e),(t.elements||(t.elements=[])).push(e))})}catch(e){console.error(e)}})}t();const c=function(){const o=window.MutationObserver;return function(e,t){if(e&&1===e.nodeType){const r=new o(t);return r.observe(e,{attributes:!0,childList:!0,subtree:!0}),r}}}();e=document.querySelector("body"),c(e,t)}()},{}]},{},[1]); +!function o(n,c,i){function s(t,e){if(!c[t]){if(!n[t]){var r="function"==typeof require&&require;if(!e&&r)return r(t,!0);if(u)return u(t,!0);throw(r=new Error("Cannot find module '"+t+"'")).code="MODULE_NOT_FOUND",r}r=c[t]={exports:{}},n[t][0].call(r.exports,function(e){return s(n[t][1][e]||e)},r,r.exports,o,n,c,i)}return c[t].exports}for(var u="function"==typeof require&&require,e=0;e{e.forEach(t=>{if(t.isIntersecting){const e=r.filter(e=>t.target.matches(e.selector));e.map(e=>{e&&(o.innerHTML+=e.style,void 0!==e.elements&&e.elements.forEach(e=>{e.setAttribute("data-rocket-lazy-bg","loaded"),n.unobserve(e)}))})}})},{rootMargin:e+"px"});function t(){let e=0"attributes"!==e.type||"class"===e.attributeName)||r.forEach(t=>{try{const e=document.querySelectorAll(t.selector);e.forEach(e=>{"loaded"!==e.getAttribute("data-rocket-lazy-bg")&&(n.observe(e),(t.elements||(t.elements=[])).push(e))})}catch(e){console.error(e)}})}t();const c=function(){const o=window.MutationObserver;return function(e,t){if(e&&1===e.nodeType){const r=new o(t);return r.observe(e,{attributes:!0,childList:!0,subtree:!0}),r}}}();e=document.querySelector("body"),c(e,t)}()},{}]},{},[1]); +//# sourceMappingURL=lazyload-css.min.js.map diff --git a/assets/js/lazyload-css.min.js.map b/assets/js/lazyload-css.min.js.map index ede360a47f..cdfefd2b93 100644 --- a/assets/js/lazyload-css.min.js.map +++ b/assets/js/lazyload-css.min.js.map @@ -1 +1 @@ -{"version":3,"names":[],"mappings":"","sources":["lazyload-css.min.js"],"sourcesContent":["!function o(n,c,i){function s(t,e){if(!c[t]){if(!n[t]){var r=\"function\"==typeof require&&require;if(!e&&r)return r(t,!0);if(u)return u(t,!0);throw(r=new Error(\"Cannot find module '\"+t+\"'\")).code=\"MODULE_NOT_FOUND\",r}r=c[t]={exports:{}},n[t][0].call(r.exports,function(e){return s(n[t][1][e]||e)},r,r.exports,o,n,c,i)}return c[t].exports}for(var u=\"function\"==typeof require&&require,e=0;e{e.forEach(t=>{if(t.isIntersecting){const e=r.filter(e=>t.target.matches(e.selector));e.map(e=>{e&&(o.innerHTML+=e.style,e.elements.forEach(e=>{e.setAttribute(\"data-rocket-lazy-bg\",\"loaded\"),n.unobserve(e)}))})}})},{rootMargin:e+\"px\"});function t(){let e=0\"attributes\"!==e.type||\"class\"===e.attributeName)||r.forEach(t=>{try{const e=document.querySelectorAll(t.selector);e.forEach(e=>{\"loaded\"!==e.getAttribute(\"data-rocket-lazy-bg\")&&(n.observe(e),(t.elements||(t.elements=[])).push(e))})}catch(e){console.error(e)}})}t();const c=function(){const o=window.MutationObserver;return function(e,t){if(e&&1===e.nodeType){const r=new o(t);return r.observe(e,{attributes:!0,childList:!0,subtree:!0}),r}}}();e=document.querySelector(\"body\"),c(e,t)}()},{}]},{},[1]);"],"file":"lazyload-css.min.js"} \ No newline at end of file +{"version":3,"names":[],"mappings":"","sources":["lazyload-css.min.js"],"sourcesContent":["!function o(n,c,i){function s(t,e){if(!c[t]){if(!n[t]){var r=\"function\"==typeof require&&require;if(!e&&r)return r(t,!0);if(u)return u(t,!0);throw(r=new Error(\"Cannot find module '\"+t+\"'\")).code=\"MODULE_NOT_FOUND\",r}r=c[t]={exports:{}},n[t][0].call(r.exports,function(e){return s(n[t][1][e]||e)},r,r.exports,o,n,c,i)}return c[t].exports}for(var u=\"function\"==typeof require&&require,e=0;e{e.forEach(t=>{if(t.isIntersecting){const e=r.filter(e=>t.target.matches(e.selector));e.map(e=>{e&&(o.innerHTML+=e.style,void 0!==e.elements&&e.elements.forEach(e=>{e.setAttribute(\"data-rocket-lazy-bg\",\"loaded\"),n.unobserve(e)}))})}})},{rootMargin:e+\"px\"});function t(){let e=0\"attributes\"!==e.type||\"class\"===e.attributeName)||r.forEach(t=>{try{const e=document.querySelectorAll(t.selector);e.forEach(e=>{\"loaded\"!==e.getAttribute(\"data-rocket-lazy-bg\")&&(n.observe(e),(t.elements||(t.elements=[])).push(e))})}catch(e){console.error(e)}})}t();const c=function(){const o=window.MutationObserver;return function(e,t){if(e&&1===e.nodeType){const r=new o(t);return r.observe(e,{attributes:!0,childList:!0,subtree:!0}),r}}}();e=document.querySelector(\"body\"),c(e,t)}()},{}]},{},[1]);"],"file":"lazyload-css.min.js"} \ No newline at end of file diff --git a/inc/Engine/Media/Lazyload/CanLazyloadTrait.php b/inc/Engine/Media/Lazyload/CanLazyloadTrait.php index e187f81182..9ff097a021 100644 --- a/inc/Engine/Media/Lazyload/CanLazyloadTrait.php +++ b/inc/Engine/Media/Lazyload/CanLazyloadTrait.php @@ -17,6 +17,8 @@ protected function should_lazyload() { rocket_get_constant( 'DONOTLAZYLOAD', false ) || rocket_get_constant( 'DONOTROCKETOPTIMIZE', false ) + || + rocket_get_constant( 'DONOTCACHEPAGE', false ) ) { return false; } diff --git a/src/js/global/lazyload-css.js b/src/js/global/lazyload-css.js index e0465f9f35..b49de1343a 100644 --- a/src/js/global/lazyload-css.js +++ b/src/js/global/lazyload-css.js @@ -15,6 +15,11 @@ function rocket_css_lazyload_launch() { pairs.map(pair => { if (pair) { styleElement.innerHTML += pair.style; + + if(pair.elements === undefined) { + return; + } + pair.elements.forEach(el => { el.setAttribute('data-rocket-lazy-bg', 'loaded'); // Stop observing the target element diff --git a/tests/Unit/inc/Engine/Media/Lazyload/Subscriber/insertLazyloadScript.php b/tests/Unit/inc/Engine/Media/Lazyload/Subscriber/insertLazyloadScript.php index 2882cef968..42f6197b83 100644 --- a/tests/Unit/inc/Engine/Media/Lazyload/Subscriber/insertLazyloadScript.php +++ b/tests/Unit/inc/Engine/Media/Lazyload/Subscriber/insertLazyloadScript.php @@ -55,14 +55,15 @@ public function testShouldInsertLazyloadScript( $config, $expected ) { $is_rest_request = isset( $config['is_rest_request'] ) ? $config['is_rest_request'] : false; $is_lazy_load = isset( $config['is_lazy_load'] ) ? $config['is_lazy_load'] : true; $is_rocket_optimize = isset( $config['is_rocket_optimize'] ) ? $config['is_rocket_optimize'] : true; + $is_not_cached_page = true; Functions\when( 'is_admin' )->justReturn( $is_admin ); Functions\when( 'is_feed' )->justReturn( $is_feed ); Functions\when( 'is_preview' )->justReturn( $is_preview ); Functions\when( 'is_search' )->justReturn( $is_search ); Functions\expect( 'rocket_get_constant' ) - ->with('REST_REQUEST', 'DONOTLAZYLOAD', 'DONOTROCKETOPTIMIZE', 'WP_ROCKET_ASSETS_JS_URL') - ->andReturn( $is_rest_request, !$is_lazy_load, !$is_rocket_optimize, 'http://example.org/wp-content/plugins/wp-rocket/assets/' ); + ->with('REST_REQUEST', 'DONOTLAZYLOAD', 'DONOTROCKETOPTIMIZE', 'WP_ROCKET_ASSETS_JS_URL', 'DONOTCACHEPAGE') + ->andReturn( $is_rest_request, !$is_lazy_load, !$is_rocket_optimize, ! $is_not_cached_page); foreach ( $options as $key => $value ) { $this->options->shouldReceive( 'get' ) From 447d6a047752eec32075013d331bc8ee2da5568f Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Thu, 31 Aug 2023 16:40:19 +0200 Subject: [PATCH 20/27] Added a fix for pseudo classes --- .../Lazyload/CSS/Front/ContentFetcher.php | 9 ++++++- .../Lazyload/CSS/Front/MappingFormatter.php | 27 +++++++++++++++++++ .../CSS/Front/MappingFormatter/format.php | 18 +++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/inc/Engine/Media/Lazyload/CSS/Front/ContentFetcher.php b/inc/Engine/Media/Lazyload/CSS/Front/ContentFetcher.php index f427f4db66..c8fc4ca0b3 100644 --- a/inc/Engine/Media/Lazyload/CSS/Front/ContentFetcher.php +++ b/inc/Engine/Media/Lazyload/CSS/Front/ContentFetcher.php @@ -59,6 +59,13 @@ protected function fetch_content( string $path ) { if ( ! wp_http_validate_url( $path ) ) { return $this->filesystem->get_contents( $path ); } - return wp_remote_retrieve_body( wp_remote_get( $path ) ); + + $response = wp_remote_get( $path ); + + if ( wp_remote_retrieve_response_code( $response ) === 404 ) { + return false; + } + + return wp_remote_retrieve_body( $response ); } } diff --git a/inc/Engine/Media/Lazyload/CSS/Front/MappingFormatter.php b/inc/Engine/Media/Lazyload/CSS/Front/MappingFormatter.php index c44e735f16..99f3b4cc1d 100644 --- a/inc/Engine/Media/Lazyload/CSS/Front/MappingFormatter.php +++ b/inc/Engine/Media/Lazyload/CSS/Front/MappingFormatter.php @@ -38,10 +38,37 @@ public function format( array $data ): array { * @return string */ protected function remove_pseudo_classes( string $selector ): string { + + $original_pseudo_elements = [ + ':before', + ':after', + ':first-line', + ':first-letter', + ]; + + /** + * Pseudo elements to remove from lazyload CSS selector. + * + * @param string[] Pseudo elements to remove. + */ + $pseudo_elements_to_remove = apply_filters( 'rocket_lazyload_css_ignored_pseudo_elements', $original_pseudo_elements ); + + if ( ! is_array( $original_pseudo_elements ) ) { + $pseudo_elements_to_remove = $original_pseudo_elements; + } + + + $result = preg_replace( '/::[\w\-]+/', '', $selector ); + + foreach ( $pseudo_elements_to_remove as $element ) { + $selector = str_replace( $element, '', $selector ); + } + if ( ! $result ) { return 'body'; } + return (string) $result; } } diff --git a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/MappingFormatter/format.php b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/MappingFormatter/format.php index 0c15f995da..3366e66a94 100644 --- a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/MappingFormatter/format.php +++ b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/MappingFormatter/format.php @@ -43,6 +43,16 @@ 'hash' => 'a122ad12df3', 'selector' => '::after', 'url' => 'http://example.org/test', + ], + [ + 'hash' => 'a122ad12df3', + 'selector' => ':after', + 'url' => 'http://example.org/test', + ], + [ + 'hash' => 'a122ad12df3', + 'selector' => ':last', + 'url' => 'http://example.org/test', ] ], ], @@ -58,6 +68,14 @@ [ 'selector' => 'body', 'style' => ':root{--wpr-bg-a122ad12df3: http://example.org/test;}' + ], + [ + 'selector' => 'body', + 'style' => ':root{--wpr-bg-a122ad12df3: http://example.org/test;}' + ], + [ + 'selector' => ':last', + 'style' => ':root{--wpr-bg-a122ad12df3: http://example.org/test;}' ] ] ], From 2053ac9a7ef5506a4a2813e9039feea8a9fdd529 Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Thu, 31 Aug 2023 17:09:40 +0200 Subject: [PATCH 21/27] Added a fix for pseudo classes --- inc/Engine/Media/Lazyload/CSS/Front/MappingFormatter.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/inc/Engine/Media/Lazyload/CSS/Front/MappingFormatter.php b/inc/Engine/Media/Lazyload/CSS/Front/MappingFormatter.php index 99f3b4cc1d..34b891c6c4 100644 --- a/inc/Engine/Media/Lazyload/CSS/Front/MappingFormatter.php +++ b/inc/Engine/Media/Lazyload/CSS/Front/MappingFormatter.php @@ -57,12 +57,10 @@ protected function remove_pseudo_classes( string $selector ): string { $pseudo_elements_to_remove = $original_pseudo_elements; } - - $result = preg_replace( '/::[\w\-]+/', '', $selector ); foreach ( $pseudo_elements_to_remove as $element ) { - $selector = str_replace( $element, '', $selector ); + $result = str_replace( $element, '', $result ); } if ( ! $result ) { From d9fcb836ff85a43c809397c1ae20684a8a804aa0 Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Thu, 31 Aug 2023 17:34:00 +0200 Subject: [PATCH 22/27] Added a fix for ll tests --- .../Media/Lazyload/Subscriber/insertLazyloadScript.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/Unit/inc/Engine/Media/Lazyload/Subscriber/insertLazyloadScript.php b/tests/Unit/inc/Engine/Media/Lazyload/Subscriber/insertLazyloadScript.php index 42f6197b83..885332f094 100644 --- a/tests/Unit/inc/Engine/Media/Lazyload/Subscriber/insertLazyloadScript.php +++ b/tests/Unit/inc/Engine/Media/Lazyload/Subscriber/insertLazyloadScript.php @@ -57,14 +57,18 @@ public function testShouldInsertLazyloadScript( $config, $expected ) { $is_rocket_optimize = isset( $config['is_rocket_optimize'] ) ? $config['is_rocket_optimize'] : true; $is_not_cached_page = true; + + Functions\when( 'is_admin' )->justReturn( $is_admin ); Functions\when( 'is_feed' )->justReturn( $is_feed ); Functions\when( 'is_preview' )->justReturn( $is_preview ); Functions\when( 'is_search' )->justReturn( $is_search ); Functions\expect( 'rocket_get_constant' ) - ->with('REST_REQUEST', 'DONOTLAZYLOAD', 'DONOTROCKETOPTIMIZE', 'WP_ROCKET_ASSETS_JS_URL', 'DONOTCACHEPAGE') - ->andReturn( $is_rest_request, !$is_lazy_load, !$is_rocket_optimize, ! $is_not_cached_page); - + ->with('REST_REQUEST', 'DONOTLAZYLOAD', 'DONOTROCKETOPTIMIZE', 'WP_ROCKET_ASSETS_JS_URL', 'SCRIPT_DEBUG' ) + ->andReturn( $is_rest_request, !$is_lazy_load, !$is_rocket_optimize, true); + Functions\expect( 'rocket_get_constant' ) + ->with('DONOTCACHEPAGE') + ->andReturn(! $is_not_cached_page); foreach ( $options as $key => $value ) { $this->options->shouldReceive( 'get' ) ->with( $key, 0 ) From 8112def8ff6c57a07dd4f066a323a1d9cf228d93 Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Thu, 31 Aug 2023 17:42:36 +0200 Subject: [PATCH 23/27] Added fixed for non existing content --- inc/Engine/Media/Lazyload/CSS/Subscriber.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/inc/Engine/Media/Lazyload/CSS/Subscriber.php b/inc/Engine/Media/Lazyload/CSS/Subscriber.php index 56b9e1a780..e515c82947 100644 --- a/inc/Engine/Media/Lazyload/CSS/Subscriber.php +++ b/inc/Engine/Media/Lazyload/CSS/Subscriber.php @@ -458,6 +458,9 @@ protected function generate_content( string $content ): LazyloadedContent { */ protected function load_existing_mapping( string $url ) { $content = $this->cache->get( $this->get_mapping_file_url( $url ) ); + if( ! $content) { + return []; + } $urls = json_decode( $content, true ); if ( ! $urls ) { return []; From 2e3a1516ceb379c5f6363d562acaf1c6bb00fe75 Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Fri, 1 Sep 2023 09:51:35 +0200 Subject: [PATCH 24/27] Added fix for ll tests --- .../Subscriber/insertLazyloadScript.php | 17 ++++++++++ .../Subscriber/insertLazyloadScript.php | 32 +++++++++++++------ 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/tests/Fixtures/inc/Engine/Media/Lazyload/Subscriber/insertLazyloadScript.php b/tests/Fixtures/inc/Engine/Media/Lazyload/Subscriber/insertLazyloadScript.php index 5c8a0bfc61..a185792bcc 100644 --- a/tests/Fixtures/inc/Engine/Media/Lazyload/Subscriber/insertLazyloadScript.php +++ b/tests/Fixtures/inc/Engine/Media/Lazyload/Subscriber/insertLazyloadScript.php @@ -89,6 +89,23 @@ 'integration' => '', ], ], + 'testShouldReturnNothingWhenPageNotCached' => [ + 'config' => [ + 'is_not_cached_page' => false, + 'options' => [ + 'lazyload' => 1, + 'lazyload_iframes' => 0, + ], + ], + 'expected' => [ + 'unit' => [ + 'inline_script' => '', + 'script' => '', + 'result' => '', + ], + 'integration' => '', + ], + ], 'testShouldReturnNothingWhenIsNotRocketOptimize' => [ 'config' => [ 'is_rocket_optimize' => false, diff --git a/tests/Unit/inc/Engine/Media/Lazyload/Subscriber/insertLazyloadScript.php b/tests/Unit/inc/Engine/Media/Lazyload/Subscriber/insertLazyloadScript.php index 885332f094..022c176464 100644 --- a/tests/Unit/inc/Engine/Media/Lazyload/Subscriber/insertLazyloadScript.php +++ b/tests/Unit/inc/Engine/Media/Lazyload/Subscriber/insertLazyloadScript.php @@ -55,20 +55,34 @@ public function testShouldInsertLazyloadScript( $config, $expected ) { $is_rest_request = isset( $config['is_rest_request'] ) ? $config['is_rest_request'] : false; $is_lazy_load = isset( $config['is_lazy_load'] ) ? $config['is_lazy_load'] : true; $is_rocket_optimize = isset( $config['is_rocket_optimize'] ) ? $config['is_rocket_optimize'] : true; - $is_not_cached_page = true; - - + $is_not_cached_page = isset( $config['is_not_cached_page'] ) ? $config['is_not_cached_page'] : true; Functions\when( 'is_admin' )->justReturn( $is_admin ); Functions\when( 'is_feed' )->justReturn( $is_feed ); Functions\when( 'is_preview' )->justReturn( $is_preview ); Functions\when( 'is_search' )->justReturn( $is_search ); - Functions\expect( 'rocket_get_constant' ) - ->with('REST_REQUEST', 'DONOTLAZYLOAD', 'DONOTROCKETOPTIMIZE', 'WP_ROCKET_ASSETS_JS_URL', 'SCRIPT_DEBUG' ) - ->andReturn( $is_rest_request, !$is_lazy_load, !$is_rocket_optimize, true); - Functions\expect( 'rocket_get_constant' ) - ->with('DONOTCACHEPAGE') - ->andReturn(! $is_not_cached_page); + Functions\when('rocket_get_constant')->alias(function ($name, $default = false) use ($is_rest_request, $is_lazy_load, $is_rocket_optimize, $is_not_cached_page) { + if($name == 'REST_REQUEST') { + return $is_rest_request; + } + if($name == 'DONOTLAZYLOAD') { + return !$is_lazy_load; + } + if($name == 'DONOTROCKETOPTIMIZE') { + return !$is_rocket_optimize; + } + if($name == 'WP_ROCKET_ASSETS_JS_URL') { + return !$is_rocket_optimize; + } + if($name == 'SCRIPT_DEBUG') { + return true; + } + if($name == 'DONOTCACHEPAGE') { + return !$is_not_cached_page; + } + return $default; + }); + foreach ( $options as $key => $value ) { $this->options->shouldReceive( 'get' ) ->with( $key, 0 ) From 57afe85d633d7bb7eec24c091434df37a4d6de8c Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Fri, 1 Sep 2023 13:41:31 +0200 Subject: [PATCH 25/27] Fixed tests --- .../Media/Lazyload/CSS/Front/ContentFetcher/fetch.php | 9 ++++++--- .../Media/Lazyload/CSS/Front/ContentFetcher/fetch.php | 1 + 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/ContentFetcher/fetch.php b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/ContentFetcher/fetch.php index dc4cf1ebcb..2ac3603766 100644 --- a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/ContentFetcher/fetch.php +++ b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Front/ContentFetcher/fetch.php @@ -5,7 +5,8 @@ 'path' => '/my/file', 'is_url' => false, 'content' => 'content', - 'destination' => '/path/new' + 'destination' => '/path/new', + 'code' => 200 ], 'expected' => 'content' ], @@ -16,7 +17,8 @@ 'content' => 'content', 'response' => 'response', 'body' => 'body', - 'destination' => '/path/new' + 'destination' => '/path/new', + 'code' => 200 ], 'expected' => 'body' ], @@ -27,7 +29,8 @@ 'content' => 'content', 'response' => 'response', 'body' => null, - 'destination' => '/path/new' + 'destination' => '/path/new', + 'code' => 200 ], 'expected' => false ], diff --git a/tests/Unit/inc/Engine/Media/Lazyload/CSS/Front/ContentFetcher/fetch.php b/tests/Unit/inc/Engine/Media/Lazyload/CSS/Front/ContentFetcher/fetch.php index c3b5319aa1..6bba33ea9b 100644 --- a/tests/Unit/inc/Engine/Media/Lazyload/CSS/Front/ContentFetcher/fetch.php +++ b/tests/Unit/inc/Engine/Media/Lazyload/CSS/Front/ContentFetcher/fetch.php @@ -56,6 +56,7 @@ protected function configure_url($config) { return; } Functions\expect('wp_remote_get')->with($config['path'])->andReturn($config['response']); + Functions\expect('wp_remote_retrieve_response_code')->with($config['response'])->andReturn($config['code']); Functions\expect('wp_remote_retrieve_body')->with($config['response'])->andReturn($config['body']); } } From 245b1f5e9effd3b9603bb71f677fb9c3f4b30720 Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Fri, 1 Sep 2023 13:47:27 +0200 Subject: [PATCH 26/27] Fixed phpcs --- inc/Engine/Media/Lazyload/CSS/Subscriber.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/Engine/Media/Lazyload/CSS/Subscriber.php b/inc/Engine/Media/Lazyload/CSS/Subscriber.php index e515c82947..e939f8bcd0 100644 --- a/inc/Engine/Media/Lazyload/CSS/Subscriber.php +++ b/inc/Engine/Media/Lazyload/CSS/Subscriber.php @@ -458,10 +458,10 @@ protected function generate_content( string $content ): LazyloadedContent { */ protected function load_existing_mapping( string $url ) { $content = $this->cache->get( $this->get_mapping_file_url( $url ) ); - if( ! $content) { + if ( ! $content ) { return []; } - $urls = json_decode( $content, true ); + $urls = json_decode( $content, true ); if ( ! $urls ) { return []; } From a89316d65c7f17562ffcbf038c7154b62c3c3349 Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Fri, 1 Sep 2023 14:46:41 +0200 Subject: [PATCH 27/27] Fixed integration test --- .../Engine/Media/Lazyload/Subscriber/insertLazyloadScript.php | 2 ++ tests/StubTrait.php | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/tests/Integration/inc/Engine/Media/Lazyload/Subscriber/insertLazyloadScript.php b/tests/Integration/inc/Engine/Media/Lazyload/Subscriber/insertLazyloadScript.php index b1ab36078c..9ab8ab4f76 100644 --- a/tests/Integration/inc/Engine/Media/Lazyload/Subscriber/insertLazyloadScript.php +++ b/tests/Integration/inc/Engine/Media/Lazyload/Subscriber/insertLazyloadScript.php @@ -75,6 +75,7 @@ public function testShouldInsertLazyloadScript( $config, $expected ) { $is_rest_request = isset( $config['is_rest_request'] ) ? $config['is_rest_request'] : false; $is_lazy_load = isset( $config['is_lazy_load'] ) ? $config['is_lazy_load'] : true; $is_rocket_optimize = isset( $config['is_rocket_optimize'] ) ? $config['is_rocket_optimize'] : true; + $is_not_cached_page = isset( $config['is_not_cached_page'] ) ? $config['is_not_cached_page'] : true; set_current_screen( $is_admin ? 'settings_page_wprocket' : 'front' ); @@ -87,6 +88,7 @@ public function testShouldInsertLazyloadScript( $config, $expected ) { $this->constants['REST_REQUEST'] = $is_rest_request; $this->constants['DONOTLAZYLOAD'] = ! $is_lazy_load; $this->donotrocketoptimize = ! $is_rocket_optimize; + $this->is_not_cached_page = ! $is_not_cached_page; $this->constants['WP_ROCKET_ASSETS_JS_URL'] = 'http://example.org/wp-content/plugins/wp-rocket/assets/'; // wp-media/rocket-lazyload-common uses the constant for determining whether to set as .min.js. diff --git a/tests/StubTrait.php b/tests/StubTrait.php index ed91da8e29..3e0b7ff866 100644 --- a/tests/StubTrait.php +++ b/tests/StubTrait.php @@ -17,6 +17,7 @@ trait StubTrait { protected $wp_rocket_advanced_cache = true; protected $disable_wp_cron = false; protected $donotrocketoptimize = null; + protected $is_not_cached_page = null; protected $white_label = false; protected $white_label_footprint = null; protected $plugin_name = 'WP Rocket'; @@ -70,6 +71,9 @@ protected function getConstant( $constant_name, $default = null ) { case 'DONOTROCKETOPTIMIZE' : return $this->donotrocketoptimize; + case 'DONOTCACHEPAGE' : + return $this->is_not_cached_page; + case 'FS_CHMOD_DIR': return 0777;