diff --git a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php
index f7390cc681..c99b49bbf9 100644
--- a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php
+++ b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php
@@ -146,7 +146,7 @@ private function set_fetchpriority( $lcp, string $html ): string {
$url = preg_quote( $lcp->src, '/' );
$html = preg_replace_callback(
- '#]*?\s+src=["\']' . $url . '["\'][^>]*?|[^>]*?)>#',
+ '#]*?\s+)?src=["\']' . $url . '["\'](?:\s+[^>]*?)?>#',
function ( $matches ) {
// Check if the fetchpriority attribute already exists.
if ( preg_match( '/fetchpriority\s*=\s*[\'"]([^\'"]+)[\'"]/i', $matches[0] ) ) {
diff --git a/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/input_w_bg_image_lcp.html b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/input_w_bg_image_lcp.html
new file mode 100644
index 0000000000..4f3f51e61f
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/input_w_bg_image_lcp.html
@@ -0,0 +1,17 @@
+
+
+ Test
+
+
+
+
+
+
+
+
diff --git a/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/input_w_img_lcp.html b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/input_w_img_lcp.html
new file mode 100644
index 0000000000..f776a50625
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/input_w_img_lcp.html
@@ -0,0 +1,10 @@
+
+
+ Test
+
+
+
+
+
+
+
diff --git a/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/input_w_picture_img_lcp.html b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/input_w_picture_img_lcp.html
new file mode 100644
index 0000000000..a52a68e59c
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/input_w_picture_img_lcp.html
@@ -0,0 +1,12 @@
+
+
+ Test
+
+
+
+
+
+
+
diff --git a/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_w_bg_image_lcp.html b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_w_bg_image_lcp.html
new file mode 100644
index 0000000000..2194716718
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_w_bg_image_lcp.html
@@ -0,0 +1,17 @@
+
+
+ Test
+
+
+
+
+
+
+
+
diff --git a/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_w_img_lcp.html b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_w_img_lcp.html
new file mode 100644
index 0000000000..571d05d1a4
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_w_img_lcp.html
@@ -0,0 +1,10 @@
+
+
+ Test
+
+
+
+
+
+
+
diff --git a/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_w_picture_img_lcp.html b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_w_picture_img_lcp.html
new file mode 100644
index 0000000000..5ed03dd1b2
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_w_picture_img_lcp.html
@@ -0,0 +1,12 @@
+
+
+ Test
+
+
+
+
+
+
+
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 76a0aef574..88e4352171 100644
--- a/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/lcp.php
+++ b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/lcp.php
@@ -4,6 +4,12 @@
$html_output = file_get_contents(__DIR__ . '/HTML/output.html');
$html_output_with_preload = file_get_contents(__DIR__ . '/HTML/output_w_preload.html');
$html_output_with_beacon = file_get_contents(__DIR__ . '/HTML/output_w_beacon.html');
+$html_input_with_bg_image_lcp = file_get_contents(__DIR__ . '/HTML/input_w_bg_image_lcp.html');
+$html_output_with_bg_image_lcp = file_get_contents(__DIR__ . '/HTML/output_w_bg_image_lcp.html');
+$html_input_with_picture_img_lcp = file_get_contents(__DIR__ . '/HTML/input_w_picture_img_lcp.html');
+$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');
return [
'test_data' => [
@@ -27,7 +33,7 @@
'viewport' => json_encode( [
0 => (object) [
'type' => 'img',
- 'src' => 'http://example.org/wp-content/uploads/image.jpg',
+ 'src' => 'http://example.org/wp-content/uploads/image2.jpg',
],
] ),
],
@@ -49,4 +55,76 @@
'expected' => $html_output,
],
],
+ 'shouldNotApplyFetchPriorityToTheWrongElement' => [
+ 'config' => [
+ 'html' => $html_input_with_bg_image_lcp,
+ 'row' => [
+ 'status' => 'completed',
+ 'url' => 'http://example.org',
+ 'lcp' => json_encode( (object) [
+ 'type' => 'img',
+ 'src' => 'http://example.org/wp-content/uploads/image.jpg',
+ ] ),
+ 'viewport' => json_encode( [
+ 0 => (object) [
+ 'type' => 'img',
+ 'src' => 'http://example.org/wp-content/uploads/image2.jpg',
+ ],
+ 1 => (object) [
+ 'type' => 'img',
+ 'src' => 'http://example.org/wp-content/uploads/image3.jpg',
+ ],
+ ] ),
+ ],
+ ],
+ 'expected' => $html_output_with_bg_image_lcp,
+ ],
+ 'shouldApplyFetchPriorityToTheImgTagWithPictureElement' => [
+ 'config' => [
+ 'html' => $html_input_with_picture_img_lcp,
+ 'row' => [
+ 'status' => 'completed',
+ 'url' => 'http://example.org',
+ 'lcp' => json_encode( (object) [
+ 'type' => 'img',
+ 'src' => 'http://example.org/wp-content/uploads/image.jpg',
+ ] ),
+ 'viewport' => json_encode( [
+ 0 => (object) [
+ 'type' => 'img',
+ 'src' => 'http://example.org/wp-content/uploads/image2.jpg',
+ ],
+ 1 => (object) [
+ 'type' => 'img',
+ 'src' => 'http://example.org/wp-content/uploads/image3.jpg',
+ ],
+ ] ),
+ ],
+ ],
+ 'expected' => $html_output_with_picture_img_lcp,
+ ],
+ 'shouldApplyFetchPriorityToTheImgElement' => [
+ 'config' => [
+ 'html' => $html_input_with_img_lcp,
+ 'row' => [
+ 'status' => 'completed',
+ 'url' => 'http://example.org',
+ 'lcp' => json_encode( (object) [
+ 'type' => 'img',
+ 'src' => 'http://example.org/wp-content/uploads/image.jpg',
+ ] ),
+ 'viewport' => json_encode( [
+ 0 => (object) [
+ 'type' => 'img',
+ 'src' => 'http://example.org/wp-content/uploads/image2.jpg',
+ ],
+ 1 => (object) [
+ 'type' => 'img',
+ 'src' => 'http://example.org/wp-content/uploads/image3.jpg',
+ ],
+ ] ),
+ ],
+ ],
+ 'expected' => $html_output_with_img_lcp,
+ ],
];