Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazyload background images on additional elements #38

Merged
merged 2 commits into from May 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Image.php
Expand Up @@ -72,7 +72,7 @@ public function lazyloadImages($html, $buffer)
*/
public function lazyloadBackgroundImages($html, $buffer)
{
if (! preg_match_all('#<div\s+(?<before>[^>]+[\'"\s])?style\s*=\s*([\'"])(?<styles>.*?)\2(?<after>[^>]*)>#is', $buffer, $elements, PREG_SET_ORDER)) {
if (! preg_match_all('#<(?<tag>div|section|span|li)\s+(?<before>[^>]+[\'"\s])?style\s*=\s*([\'"])(?<styles>.*?)\3(?<after>[^>]*)>#is', $buffer, $elements, PREG_SET_ORDER)) {
return $html;
}

Expand All @@ -93,7 +93,7 @@ public function lazyloadBackgroundImages($html, $buffer)

$lazy_bg = $this->addLazyCLass($element[0]);
$lazy_bg = str_replace($url[0], '', $lazy_bg);
$lazy_bg = str_replace('<div', '<div data-bg="url(' . esc_attr($url['url']) . ')"', $lazy_bg);
$lazy_bg = str_replace('<' . $element['tag'], '<' . $element['tag'] . ' data-bg="url(' . esc_attr($url['url']) . ')"', $lazy_bg);

$html = str_replace($element[0], $lazy_bg, $html);
unset($lazy_bg);
Expand All @@ -117,7 +117,7 @@ private function addLazyClass($element)
return $element;
}

return preg_replace('#<(img|div)([^>]*)>#is', '<\1 class="rocket-lazyload"\2>', $element);
return preg_replace('#<(img|div|section|li|span)([^>]*)>#is', '<\1 class="rocket-lazyload"\2>', $element);
}

/**
Expand Down
8 changes: 7 additions & 1 deletion tests/Unit/fixtures/Image/bgimages.html
Expand Up @@ -77,7 +77,7 @@
<style>
a { color: #0000FF; }
</style>
<noscript><style>.woocommerce-product-gallery{ opacity: 1 !important; }</style></noscript>
<noscript><style>.woocommerce-product-gallery{ opacity: 1 !important; }</style></noscript>
<link rel="amphtml" href="http://wordpress.test/page-alignement-image/?amp"><style type="text/css" media="print">#wpadminbar { display:none; }</style>
<style type="text/css" media="screen">
html { margin-top: 32px !important; }
Expand All @@ -103,5 +103,11 @@
<div data-test="bla" style="color:#000; background-image:url(test.jpg);" data-toto="tata"></div>
<div class="fusion-fullwidth fullwidth-box fusion-parallax-none nonhundred-percent-fullwidth non-hundred-percent-height-scrolling" style="background-color: #ffffff;background-image: url(http://mega.wp-rocket.me/avada/wp-content/uploads/2014/11/review_bkgd321-compressor.jpg);background-position: center center;background-repeat: no-repeat;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;-webkit-background-size:cover;-moz-background-size:cover;-o-background-size:cover;background-size:cover;"></div>
<div style='background-image:url(https://concretegenius.ca/wp-content/uploads/2019/02/Overhead-17-Korova-East.png);background-repeat:no-repeat;background-position:center;background-attachment:;border-top-width:px;border-top-style:solid;border-top-color:;border-bottom-width:px;border-bottom-style:solid;border-bottom-color:;' class="row two-columns cf ui-sortable section " id="le_body_row_3" data-style="eyJlbGVtZW50SWQiOiJsZV9ib2R5X3Jvd18zIiwiYmFja2dyb3VuZEltYWdlIjoidXJsKGh0dHBzOi8vY29uY3JldGVnZW5pdXMuY2Evd3AtY29udGVudC91cGxvYWRzLzIwMTkvMDIvT3ZlcmhlYWQtMTctS29yb3ZhLUVhc3QucG5nKSIsImJhY2tncm91bmRQb3NpdGlvbiI6ImNlbnRlciIsImJhY2tncm91bmRJbWFnZUNvbG9yIjoiIiwiYmFja2dyb3VuZEltYWdlQ29sb3JPcGFjaXR5IjoiMCIsImJhY2tncm91bmRQYXJhbGF4IjpmYWxzZSwiYm9yZGVyVG9wV2lkdGgiOiIiLCJib3JkZXJUb3BDb2xvciI6IiIsImJvcmRlckJvdHRvbVdpZHRoIjoiIiwiYm9yZGVyQm90dG9tQ29sb3IiOiIiLCJzZWN0aW9uU2VwYXJhdG9yVHlwZSI6Im5vbmUiLCJzZWN0aW9uU2VwYXJhdG9yU3R5bGUiOiIiLCJleHRyYXMiOnsiYW5pbWF0aW9uRWZmZWN0IjoiIiwiYW5pbWF0aW9uRGVsYXkiOiIifSwicm93U2Nyb2xsRml4ZWRQb3NpdGlvbiI6Im5vbmUiLCJyb3dTY3JvbGxGaXhlZE1vYmlsZSI6ZmFsc2UsImFkZG9uIjp7InZpZGVvX2JhY2tncm91bmRfdHlwZSI6IiIsInZpZGVvX2JhY2tncm91bmRfeW91dHViZSI6IiIsInZpZGVvX2JhY2tncm91bmRfeW91dHViZV93aWR0aCI6IiIsInZpZGVvX2JhY2tncm91bmRfeW91dHViZV9oZWlnaHQiOiIiLCJ2aWRlb19iYWNrZ3JvdW5kX3VybF9tcDQiOiIiLCJ2aWRlb19iYWNrZ3JvdW5kX3VybF93ZWJtIjoiIiwidmlkZW9fYmFja2dyb3VuZF91cmxfb2d2IjoiIiwidmlkZW9fYmFja2dyb3VuZF92ZXJ0aWNhbF9hbGlnbiI6IiIsInZpZGVvX2JhY2tncm91bmRfb3ZlcmxheV9jb2xvciI6IiIsInZpZGVvX2JhY2tncm91bmRfb3ZlcmxheV9vcGFjaXR5IjoiMCIsInZpZGVvX2JhY2tncm91bmRfb3ZlcmxheV9pbWFnZSI6IiIsInZpZGVvX2JhY2tncm91bmRfaW1hZ2VfcG9zaXRpb24iOiIiLCJ2aWRlb19iYWNrZ3JvdW5kX2FsdGVybmF0aXZlX2ltYWdlIjoiIn19"></div>
<section class="test" style="background-image: url(test.jpg)">Test section</section>
<span style="background-image: url(test.jpg)">Test span</span>
<ul>
<li style="background-image: url(test.jpg)">Test li</li>
<li>Test li 2</li>
</ul>
</body>
</html>
8 changes: 7 additions & 1 deletion tests/Unit/fixtures/Image/bgimageslazyloaded.html
Expand Up @@ -77,7 +77,7 @@
<style>
a { color: #0000FF; }
</style>
<noscript><style>.woocommerce-product-gallery{ opacity: 1 !important; }</style></noscript>
<noscript><style>.woocommerce-product-gallery{ opacity: 1 !important; }</style></noscript>
<link rel="amphtml" href="http://wordpress.test/page-alignement-image/?amp"><style type="text/css" media="print">#wpadminbar { display:none; }</style>
<style type="text/css" media="screen">
html { margin-top: 32px !important; }
Expand All @@ -103,5 +103,11 @@
<div data-bg="url(test.jpg)" class="rocket-lazyload" data-test="bla" style="color:#000; " data-toto="tata"></div>
<div data-bg="url(http://mega.wp-rocket.me/avada/wp-content/uploads/2014/11/review_bkgd321-compressor.jpg)" class="fusion-fullwidth fullwidth-box fusion-parallax-none nonhundred-percent-fullwidth non-hundred-percent-height-scrolling rocket-lazyload" style="background-color: #ffffff;background-position: center center;background-repeat: no-repeat;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;-webkit-background-size:cover;-moz-background-size:cover;-o-background-size:cover;background-size:cover;"></div>
<div data-bg="url(https://concretegenius.ca/wp-content/uploads/2019/02/Overhead-17-Korova-East.png)" style='background-repeat:no-repeat;background-position:center;background-attachment:;border-top-width:px;border-top-style:solid;border-top-color:;border-bottom-width:px;border-bottom-style:solid;border-bottom-color:;' class="row two-columns cf ui-sortable section rocket-lazyload" id="le_body_row_3" data-style="eyJlbGVtZW50SWQiOiJsZV9ib2R5X3Jvd18zIiwiYmFja2dyb3VuZEltYWdlIjoidXJsKGh0dHBzOi8vY29uY3JldGVnZW5pdXMuY2Evd3AtY29udGVudC91cGxvYWRzLzIwMTkvMDIvT3ZlcmhlYWQtMTctS29yb3ZhLUVhc3QucG5nKSIsImJhY2tncm91bmRQb3NpdGlvbiI6ImNlbnRlciIsImJhY2tncm91bmRJbWFnZUNvbG9yIjoiIiwiYmFja2dyb3VuZEltYWdlQ29sb3JPcGFjaXR5IjoiMCIsImJhY2tncm91bmRQYXJhbGF4IjpmYWxzZSwiYm9yZGVyVG9wV2lkdGgiOiIiLCJib3JkZXJUb3BDb2xvciI6IiIsImJvcmRlckJvdHRvbVdpZHRoIjoiIiwiYm9yZGVyQm90dG9tQ29sb3IiOiIiLCJzZWN0aW9uU2VwYXJhdG9yVHlwZSI6Im5vbmUiLCJzZWN0aW9uU2VwYXJhdG9yU3R5bGUiOiIiLCJleHRyYXMiOnsiYW5pbWF0aW9uRWZmZWN0IjoiIiwiYW5pbWF0aW9uRGVsYXkiOiIifSwicm93U2Nyb2xsRml4ZWRQb3NpdGlvbiI6Im5vbmUiLCJyb3dTY3JvbGxGaXhlZE1vYmlsZSI6ZmFsc2UsImFkZG9uIjp7InZpZGVvX2JhY2tncm91bmRfdHlwZSI6IiIsInZpZGVvX2JhY2tncm91bmRfeW91dHViZSI6IiIsInZpZGVvX2JhY2tncm91bmRfeW91dHViZV93aWR0aCI6IiIsInZpZGVvX2JhY2tncm91bmRfeW91dHViZV9oZWlnaHQiOiIiLCJ2aWRlb19iYWNrZ3JvdW5kX3VybF9tcDQiOiIiLCJ2aWRlb19iYWNrZ3JvdW5kX3VybF93ZWJtIjoiIiwidmlkZW9fYmFja2dyb3VuZF91cmxfb2d2IjoiIiwidmlkZW9fYmFja2dyb3VuZF92ZXJ0aWNhbF9hbGlnbiI6IiIsInZpZGVvX2JhY2tncm91bmRfb3ZlcmxheV9jb2xvciI6IiIsInZpZGVvX2JhY2tncm91bmRfb3ZlcmxheV9vcGFjaXR5IjoiMCIsInZpZGVvX2JhY2tncm91bmRfb3ZlcmxheV9pbWFnZSI6IiIsInZpZGVvX2JhY2tncm91bmRfaW1hZ2VfcG9zaXRpb24iOiIiLCJ2aWRlb19iYWNrZ3JvdW5kX2FsdGVybmF0aXZlX2ltYWdlIjoiIn19"></div>
<section data-bg="url(test.jpg)" class="test rocket-lazyload" style="">Test section</section>
<span data-bg="url(test.jpg)" class="rocket-lazyload" style="">Test span</span>
<ul>
<li data-bg="url(test.jpg)" class="rocket-lazyload" style="">Test li</li>
<li>Test li 2</li>
</ul>
</body>
</html>