Skip to content

Commit

Permalink
Closes #6199: Lazyload CSS Background Images triggers DOMException er…
Browse files Browse the repository at this point in the history
…rors because of incorrect selector (#6225)
  • Loading branch information
Miraeld authored Nov 2, 2023
1 parent 7d56c41 commit 5ca0902
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
4 changes: 2 additions & 2 deletions inc/Engine/Media/Lazyload/CSS/Front/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ function ( $matches ) {
$content
);

$background_regex = '(?<selector>[ \-,:\w.()\n\r^>[*"\'=\]#]+)\s?{[^{}]*background\s*:(?<property>[^;}]*)[^}]*}';
$background_regex = '(?<selector>(?:[ \-,:\w.()\n\r^>[*"\'=\]#]|(?:\[[^\]]+\]))+)\s?{[^{}]*background\s*:(?<property>[^;}]*)[^}]*}';

$background_image_regex = '(?<selector>[ \-,:\w.()\n\r>^[*"\'=\]#]+)\s?{[^{}]*background-image\s*:(?<property>[^;}]*)[^}]*}';
$background_image_regex = '(?<selector>(?:[ \-,:\w.()\n\r^>[*"\'=\]#]|(?:\[[^\]]+\]))+)\s?{[^{}]*background-image\s*:(?<property>[^;}]*)[^}]*}';

/**
* Lazyload background property regex.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,11 @@
background-image: url(https://www.villmarksbua.no/wp-content/uploads/2022/03/nordic-pocket-saw-foldbar-tursag-for-tre-og-metall.jpg);
}
}

[title~="wp-rocket"] {
background: url("/wp-content/rocket-test-data/images/wp-rocket.svg") no-repeat;
}

[title~="wp-rocket-image"] {
background-image: url("/wp-content/rocket-test-data/images/wp-rocket.svg") no-repeat;
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,26 @@
background-image: url("../img/glyphicons-halflings.png");
background-position: 14px 14px;
background-repeat: no-repeat
}'
]
],
'[title~="wp-rocket"]' => [
[
'selector' => '[title~="wp-rocket"]',
'url' => "url('/wp-content/rocket-test-data/images/wp-rocket.svg')",
'original' => 'url("/wp-content/rocket-test-data/images/wp-rocket.svg")',
'block' => '[title~="wp-rocket"] {
background: url("/wp-content/rocket-test-data/images/wp-rocket.svg") no-repeat;
}'
]
],
'[title~="wp-rocket-image"]' => [
[
'selector' => '[title~="wp-rocket-image"]',
'url' => "url('/wp-content/rocket-test-data/images/wp-rocket.svg')",
'original' => 'url("/wp-content/rocket-test-data/images/wp-rocket.svg")',
'block' => '[title~="wp-rocket-image"] {
background-image: url("/wp-content/rocket-test-data/images/wp-rocket.svg") no-repeat;
}'
]
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
$tags = <<<TAG
<style id="wpr-lazyload-bg"></style><style id="wpr-lazyload-bg-exclusion">:root{--wpr-bg-15ef8: url(https://domain.ext/path/to/background.ext); }</style>
<noscript>
<style id="wpr-lazyload-bg-nostyle">:root{--wpr-bg-16ef9: url(https://domain.ext/path/to/background2.ext); }</style>
<style id="wpr-lazyload-bg-nostyle">:root{--wpr-bg-16ef9: url(https://domain.ext/path/to/background2.ext); }:root{--wpr-bg-17ef6: url(https://domain.ext/path/to/background3.ext); }:root{--wpr-bg-18ef6: url(https://domain.ext/path/to/background3.ext); }</style>
</noscript>
<script type="application/javascript">const rocket_pairs = [{"selector":"#section_2_hash","style":":root{--wpr-bg-16ef9: url(https:\/\/domain.ext\/path\/to\/background2.ext); }"}]; const rocket_excluded_pairs = [{"selector":".internal-css-background-image","style":":root{--wpr-bg-15ef8: url(https:\/\/domain.ext\/path\/to\/background.ext); }"}];</script>
<script type="application/javascript">const rocket_pairs = [{"selector":"#section_2_hash","style":":root{--wpr-bg-16ef9: url(https:\/\/domain.ext\/path\/to\/background2.ext); }"},{"selector":"title~=\"wp-rocket\"","style":":root{--wpr-bg-17ef6: url(https:\/\/domain.ext\/path\/to\/background3.ext); }"},{"selector":"title~=\"wp-rocket-image\"","style":":root{--wpr-bg-18ef6: url(https:\/\/domain.ext\/path\/to\/background3.ext); }"}]; const rocket_excluded_pairs = [{"selector":".internal-css-background-image","style":":root{--wpr-bg-15ef8: url(https:\/\/domain.ext\/path\/to\/background.ext); }"}];</script>
TAG;


Expand All @@ -21,6 +21,14 @@
'selector' => '#section_2_hash',
'style' => ":root{--wpr-bg-16ef9: url(https://domain.ext/path/to/background2.ext); }"
],
[
'selector' => 'title~="wp-rocket"',
'style' => ':root{--wpr-bg-17ef6: url(https://domain.ext/path/to/background3.ext); }'
],
[
'selector' => 'title~="wp-rocket-image"',
'style' => ':root{--wpr-bg-18ef6: url(https://domain.ext/path/to/background3.ext); }'
]
],
'loaded' => [
[
Expand Down

0 comments on commit 5ca0902

Please sign in to comment.