diff --git a/composer.json b/composer.json index 8882a4d..086e3e1 100644 --- a/composer.json +++ b/composer.json @@ -44,6 +44,7 @@ "@test-unit", "@test-integration" ], - "phpcs": "phpcs --basepath=." + "phpcs": "phpcs --basepath=.", + "phpcs:fix": "phpcbf" } } diff --git a/src/Image.php b/src/Image.php index 1f89140..b25bb3c 100644 --- a/src/Image.php +++ b/src/Image.php @@ -22,7 +22,7 @@ class Image { public function lazyloadImages( $html, $buffer ) { $clean_buffer = preg_replace( '/]*)>(?:.+)?<\/script>/Umsi', '', $html ); $clean_buffer = preg_replace( '##Umsi', '', $clean_buffer ); - if (! preg_match_all('#\s.+)\s?/?>#iUs', $clean_buffer, $images, PREG_SET_ORDER)) { + if ( ! preg_match_all( '#\s.+)\s?/?>#iUs', $clean_buffer, $images, PREG_SET_ORDER ) ) { return $html; } @@ -62,17 +62,19 @@ public function lazyloadBackgroundImages( $html, $buffer ) { continue; } - if ( ! preg_match( '#background-image\s*:\s*(?\s*url\s*\((?[^)]+)\))\s*;?#is', $element['styles'], $url ) ) { + if ( ! preg_match( '#(?background(?:-image)?)\s*:(?.*)(?\s*url\s*\((?[^)]+)\))(?[\w,\s*,-]*);?#is', $element['styles'], $url ) ) { continue; } $url['url'] = esc_url( trim( - strip_tags( + wp_strip_all_tags( html_entity_decode( - $url['url'], ENT_QUOTES|ENT_HTML5 + $url['url'], + ENT_QUOTES | ENT_HTML5 ) - ), '\'" ' + ), + '\'" ' ) ); @@ -81,7 +83,20 @@ public function lazyloadBackgroundImages( $html, $buffer ) { } $lazy_bg = $this->addLazyCLass( $element[0] ); - $lazy_bg = str_replace( $url[0], '', $lazy_bg ); + switch ( $url['property'] ) { + case 'background-image': + $lazy_bg = str_replace( $url[0], '', $lazy_bg ); + break; + + case 'background': + if ( empty( trim( $url['before'] ) ) && empty( trim( $url['after'] ) ) ) { + $lazy_bg = str_replace( $url[0], '', $lazy_bg ); + }else { + $lazy_bg = str_replace( $url['attr'], '', $lazy_bg ); + } + break; + } + $lazy_bg = str_replace( '<' . $element['tag'], '<' . $element['tag'] . ' data-bg="' . esc_attr( $url['url'] ) . '"', $lazy_bg ); $html = str_replace( $element[0], $lazy_bg, $html ); @@ -99,7 +114,7 @@ public function lazyloadBackgroundImages( $html, $buffer ) { */ private function addLazyClass( $element ) { $class = $this->getClasses( $element ); - if ( empty( $class ) ) { + if ( empty( $class ) ) { return preg_replace( '#<(img|div|figure|section|li|span|a)([^>]*)>#is', '<\1 class="rocket-lazyload"\2>', $element ); } @@ -132,7 +147,7 @@ private function addLazyClass( $element ) { */ private function getAttributeQuotes( $attribute_value ) { $attribute_value = trim( $attribute_value ); - $first_char = $attribute_value[0]; + $first_char = $attribute_value[0]; if ( '"' === $first_char || "'" === $first_char ) { return $first_char; @@ -230,7 +245,7 @@ private function stringToArray( $string, $delimiter = ' ' ) { } $array = explode( $delimiter, $string ); - $array = array_map('trim', $array ); + $array = array_map( 'trim', $array ); // Remove empties. return array_filter( $array ); @@ -286,7 +301,7 @@ public function lazyloadPictures( $html, $buffer ) { $img_lazy = $this->replaceImage( $img ); $img_lazy .= $this->noscript( $img[0] ); - $safe_img = str_replace('/', '\/', preg_quote( $img[0], '#' )); + $safe_img = str_replace( '/', '\/', preg_quote( $img[0], '#' ) ); $html = preg_replace( '#]*>.*' . $safe_img . '.*<\/noscript>(*SKIP)(*FAIL)|' . $safe_img . '#i', $img_lazy, $html ); unset( $img_lazy ); diff --git a/tests/Unit/Image/TestLazyloadBackgroundImages.php b/tests/Unit/Image/TestLazyloadBackgroundImages.php index 6b24fc0..23100ed 100644 --- a/tests/Unit/Image/TestLazyloadBackgroundImages.php +++ b/tests/Unit/Image/TestLazyloadBackgroundImages.php @@ -37,10 +37,22 @@ public function testShouldReturnSameWhenNoBackgroundImage() { public function testShouldReturnBackgroundImagesLazyloaded() { Functions\when('esc_attr')->returnArg(); + Functions\expect('esc_url')->andReturnUsing( function ( $url ){ return str_replace(['"', "'"], [""", "'"], $url ); } ); + Functions\expect('wp_strip_all_tags')->andReturnUsing( function ( $string, $remove_breaks = false ){ + $string = preg_replace( '@<(script|style)[^>]*?>.*?@si', '', $string ); + $string = strip_tags( $string ); + + if ( $remove_breaks ) { + $string = preg_replace( '/[\r\n\t ]+/', ' ', $string ); + } + + return trim( $string ); + } ); + $original = \file_get_contents( RLL_COMMON_TESTS_ROOT . '/fixtures/Image/bgimages.html'); $expected = \file_get_contents( RLL_COMMON_TESTS_ROOT . '/fixtures/Image/bgimageslazyloaded.html'); diff --git a/tests/Unit/fixtures/Image/bgimages.html b/tests/Unit/fixtures/Image/bgimages.html index 1a9317f..8ef6bf6 100644 --- a/tests/Unit/fixtures/Image/bgimages.html +++ b/tests/Unit/fixtures/Image/bgimages.html @@ -124,5 +124,14 @@
+Test a +Test a +Test a +Test a +Test a +Test a +Test a + + diff --git a/tests/Unit/fixtures/Image/bgimageslazyloaded.html b/tests/Unit/fixtures/Image/bgimageslazyloaded.html index 1f31403..5942557 100644 --- a/tests/Unit/fixtures/Image/bgimageslazyloaded.html +++ b/tests/Unit/fixtures/Image/bgimageslazyloaded.html @@ -124,5 +124,14 @@
+Test a +Test a +Test a +Test a +Test a +Test a +Test a + +