diff --git a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php index 929629c687..1da1644c27 100644 --- a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php +++ b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php @@ -146,12 +146,23 @@ private function set_fetchpriority( $lcp, string $html ): string { return $html; } - $url = preg_quote( $lcp->src, '/' ); + $html = $this->replace_html_comments( $html ); + $url = preg_quote( $lcp->src, '/' ); + $pattern = '#]*?\s+)?src=["\']' . $url . '["\'](?:\s+[^>]*?)?>#'; + if ( wp_http_validate_url( $lcp->src ) && ! $this->is_external_file( $lcp->src ) ) { + $url = preg_quote( + wp_parse_url( $lcp->src, PHP_URL_PATH ), + '/' + ); + + $pattern = '#]*?\s+)?src\s*=\s*["\'](?:https?:)?(?:\/\/(?:[^\/]+)\/?)?\/?' . $url . '["\'](?:\s+[^>]*?)?>#i'; + } + $html = preg_replace_callback( - '#]*?\s+)?src=["\']' . $url . '["\'](?:\s+[^>]*?)?>#', + $pattern, function ( $matches ) { // Check if the fetchpriority attribute already exists. - if ( preg_match( '/fetchpriority\s*=\s*[\'"]([^\'"]+)[\'"]/i', $matches[0] ) ) { + if ( preg_match( '/]*\sfetchpriority(?:\s*=\s*["\'][^"\']*["\'])?[^>]*>/i', $matches[0] ) ) { // If it exists, don't modify the tag. return $matches[0]; } @@ -163,7 +174,7 @@ function ( $matches ) { 1 ); - return $html; + return $this->restore_html_comments( $html ); } /** diff --git a/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/input_lcp_image.html b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/input_lcp_image.html new file mode 100644 index 0000000000..b37262bcbb --- /dev/null +++ b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/input_lcp_image.html @@ -0,0 +1,8 @@ + + + LCP/ATF image test + + +Sample alt + + diff --git a/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/input_lcp_with_fetchpriority.html b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/input_lcp_with_fetchpriority.html new file mode 100644 index 0000000000..9bc7925893 --- /dev/null +++ b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/input_lcp_with_fetchpriority.html @@ -0,0 +1,8 @@ + + + Relative test + + +Relative url + + diff --git a/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/input_lcp_with_markup_comment.html b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/input_lcp_with_markup_comment.html new file mode 100644 index 0000000000..4cdec86c78 --- /dev/null +++ b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/input_lcp_with_markup_comment.html @@ -0,0 +1,9 @@ + + + Relative test + + + +Relative url + + diff --git a/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/input_with_absolute_img_lcp.html b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/input_with_absolute_img_lcp.html new file mode 100644 index 0000000000..88ea96c722 --- /dev/null +++ b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/input_with_absolute_img_lcp.html @@ -0,0 +1,8 @@ + + + Absolute url test + + +Absolute url + + diff --git a/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/input_with_relative_img_lcp.html b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/input_with_relative_img_lcp.html new file mode 100644 index 0000000000..82da18954a --- /dev/null +++ b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/input_with_relative_img_lcp.html @@ -0,0 +1,8 @@ + + + Relative test + + +Relative url + + diff --git a/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_lcp_image.php b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_lcp_image.php new file mode 100644 index 0000000000..021af68064 --- /dev/null +++ b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_lcp_image.php @@ -0,0 +1,8 @@ + + + LCP/ATF image test + + +Sample alt + + diff --git a/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_lcp_with_fetchpriority.html b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_lcp_with_fetchpriority.html new file mode 100644 index 0000000000..4c7eedce71 --- /dev/null +++ b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_lcp_with_fetchpriority.html @@ -0,0 +1,8 @@ + + + Relative test + + +Relative url + + diff --git a/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_lcp_with_markup_comment.html b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_lcp_with_markup_comment.html new file mode 100644 index 0000000000..82287a8ca1 --- /dev/null +++ b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_lcp_with_markup_comment.html @@ -0,0 +1,9 @@ + + + Relative test + + + +Relative url + + diff --git a/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_with_absolute_img_lcp.php b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_with_absolute_img_lcp.php new file mode 100644 index 0000000000..0b0de0aa60 --- /dev/null +++ b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_with_absolute_img_lcp.php @@ -0,0 +1,8 @@ + + + Absolute url test + + +Absolute url + + diff --git a/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_with_relative_img_lcp.php b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_with_relative_img_lcp.php new file mode 100644 index 0000000000..755ade4e5b --- /dev/null +++ b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_with_relative_img_lcp.php @@ -0,0 +1,8 @@ + + + Relative test + + +Relative url + + diff --git a/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/lcp.php b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/lcp.php index c41f24493b..a78d5d7d92 100644 --- a/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/lcp.php +++ b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/lcp.php @@ -10,6 +10,9 @@ $html_output_with_picture_img_lcp = file_get_contents(__DIR__ . '/HTML/output_w_picture_img_lcp.html'); $html_input_with_img_lcp = file_get_contents(__DIR__ . '/HTML/input_w_img_lcp.html'); $html_output_with_img_lcp = file_get_contents(__DIR__ . '/HTML/output_w_img_lcp.html'); +$html_input_with_relative_img_lcp = file_get_contents(__DIR__ . '/HTML/input_with_relative_img_lcp.html'); +$html_input_with_absolute_img_lcp = file_get_contents(__DIR__ . '/HTML/input_with_absolute_img_lcp.html'); +$html_input_with_domain_img_lcp = file_get_contents(__DIR__ . '/HTML/input_lcp_image.html'); return [ 'test_data' => [ @@ -142,6 +145,81 @@ ], 'expected' => file_get_contents(__DIR__ . '/HTML/output_lcp_responsive.php'), ], + 'shouldApplyFetchPriorityToReturnRelativeImage' => [ + 'config' => [ + 'html' => $html_input_with_relative_img_lcp, + 'row' => [ + 'status' => 'completed', + 'url' => 'http://example.org', + 'lcp' => json_encode( (object) [ + 'type' => 'img', + 'src' => 'http://example.org/wp-content/uploads/sample_relative_image.jpg', + ] ), + 'viewport' => json_encode ( [] ), + ], + ], + 'expected' => file_get_contents(__DIR__ . '/HTML/output_with_relative_img_lcp.php'), + ], + 'shouldApplyFetchPriorityToAbsoluteImage' => [ + 'config' => [ + 'html' => $html_input_with_absolute_img_lcp, + 'row' => [ + 'status' => 'completed', + 'url' => 'http://example.org', + 'lcp' => json_encode( (object) [ + 'type' => 'img', + 'src' => 'http://example.com/wp-content/uploads/sample_absolute_image.jpg', + ] ), + 'viewport' => json_encode ( [] ), + ], + ], + 'expected' => file_get_contents(__DIR__ . '/HTML/output_with_absolute_img_lcp.php'), + ], + 'shouldApplyFetchPriorityToImageWithDomain' => [ + 'config' => [ + 'html' => $html_input_with_domain_img_lcp, + 'row' => [ + 'status' => 'completed', + 'url' => 'http://example.org', + 'lcp' => json_encode( (object) [ + 'type' => 'img', + 'src' => 'http://example.org/wp-content/uploads/sample_url_image.png', + ] ), + 'viewport' => json_encode ( [] ), + ], + ], + 'expected' => file_get_contents(__DIR__ . '/HTML/output_lcp_image.php'), + ], + 'shouldNotApplyFetchPriorityToImageWithFetchpriority' => [ + 'config' => [ + 'html' => file_get_contents(__DIR__ . '/HTML/input_lcp_with_fetchpriority.html'), + 'row' => [ + 'status' => 'completed', + 'url' => 'http://example.org', + 'lcp' => json_encode( (object) [ + 'type' => 'img', + 'src' => 'http://example.org/wp-content/uploads/sample_relative_image.jpg', + ] ), + 'viewport' => json_encode ( [] ), + ], + ], + 'expected' => file_get_contents(__DIR__ . '/HTML/output_lcp_with_fetchpriority.html'), + ], + 'shouldNotApplyFetchPriorityToImageWithDuplicateMarkup' => [ + 'config' => [ + 'html' => file_get_contents(__DIR__ . '/HTML/input_lcp_with_markup_comment.html'), + 'row' => [ + 'status' => 'completed', + 'url' => 'http://example.org', + 'lcp' => json_encode( (object) [ + 'type' => 'img', + 'src' => 'http://example.org/wp-content/uploads/sample_relative_image.jpg', + ] ), + 'viewport' => json_encode ( [] ), + ], + ], + 'expected' => file_get_contents(__DIR__ . '/HTML/output_lcp_with_markup_comment.html'), + ], ], 'shouldPreloadPictureTag' => [ 'config' => [