Skip to content

Commit

Permalink
fix: edge case for optimole failed to lazyload certain images
Browse files Browse the repository at this point in the history
  • Loading branch information
preda-bogdan authored and selul committed Nov 28, 2019
1 parent 3ce87e2 commit 0f8de69
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion inc/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,14 +435,15 @@ public static function parse_images_from_html( $content ) {
$header_end = $header_start + strlen( $matches[0][0] );
}

if ( preg_match_all( '/(?:<a[^>]+?href=["|\'](?P<link_url>[^\s]+?)["|\'][^>]*?>\s*)?(?P<img_tag>(?:<noscript\s*>\s*)?<img[^>]*?\s+?(?:' . implode( '|', array_merge( [ 'src' ], Optml_Tag_Replacer::possible_src_attributes() ) ) . ')=\\\\?["|\'](?P<img_url>[^\s]+?)["|\'].*?>){1}(?:<\/noscript\s*>)?(?:\s*<\/a>)?/ism', $content, $images, PREG_OFFSET_CAPTURE ) ) {
if ( preg_match_all( '/(?:<a[^>]+?href=["|\'](?P<link_url>[^\s]+?)["|\'][^>]*?>\s*)?(?P<img_tag>(?:<noscript\s*>\s*)?<img[^>]*?\s+?(?:' . implode( '|', array_merge( [ 'src' ], Optml_Tag_Replacer::possible_src_attributes() ) ) . ')=\\\\?["|\'](?P<img_url>[^\s]+?)["|\'].*?>(?:<\/noscript\s*>)?){1}(?:\s*<\/a>)?/ism', $content, $images, PREG_OFFSET_CAPTURE ) ) {

foreach ( $images as $key => $unused ) {
// Simplify the output as much as possible, mostly for confirming test results.
if ( is_numeric( $key ) && $key > 0 ) {
unset( $images[ $key ] );
continue;
}

$is_no_script = false;
foreach ( $unused as $url_key => $url_value ) {
if ( $key === 'img_url' ) {
Expand Down
16 changes: 16 additions & 0 deletions tests/test-lazyload.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,22 @@ public function test_check_with_multiple_images_in_no_script() {
$this->assertEquals( 4, substr_count( $replaced_content, 'i.optimole.com' ) );
}

public function test_check_lazy_load_after_no_script() {
$content = '<noscript><img height="1" width="1" style="display:none" alt="fbpx" src="https://www.facebook.com/tr?id=dasda&ev=PageView&noscript=1" /></noscript>
<a href="/project/test-one"><span class="et_pb_image_wrap"><img src="http://example.org/wp-content/uploads/2018/11/gradient.png" alt="" /></span></a>
<a href="/project/test-two"><span class="et_pb_image_wrap"><img src="http://example.org/wp-content/uploads/2018/11/gradient.png" alt="" /></span></a>
<a href="/project/test-three"><span class="et_pb_image_wrap"><img src="http://example.org/wp-content/uploads/2018/11/gradient.png" alt="" /></span></a>
';

$replaced_content = Optml_Manager::instance()->replace_content( $content );

$this->assertContains( '<noscript>', $replaced_content );
$this->assertEquals( 4, substr_count( $replaced_content, '<noscript>' ) );
$this->assertEquals( 9, substr_count( $replaced_content, 'i.optimole.com' ) );
$this->assertEquals( 3, substr_count( $replaced_content, 'data-opt-src' ) );
$this->assertEquals( 3, substr_count( $replaced_content, '/q:eco/' ) );
$this->assertEquals( 6, substr_count( $replaced_content, '/q:auto/' ) );
}

public function test_replacement_with_data_attr() {
$content = '<div class="before-footer">
Expand Down

0 comments on commit 0f8de69

Please sign in to comment.