From 12c65f1b4511fd99e06d1f87dd6c49e446111c0e Mon Sep 17 00:00:00 2001
From: Khadreal
Date: Thu, 16 Jan 2025 13:19:43 +0100
Subject: [PATCH] :closes: #6960 Fix issue with multiple body tag in the page
---
.../Common/PerformanceHints/Frontend/Processor.php | 2 +-
.../Frontend/Subscriber/HTML/double_body_tag.html | 9 +++++++++
.../Subscriber/HTML/output_double_body_tag.html | 9 +++++++++
.../Subscriber/maybe_apply_optimizations.php | 14 ++++++++++++++
4 files changed, 33 insertions(+), 1 deletion(-)
create mode 100644 tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/double_body_tag.html
create mode 100644 tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/output_double_body_tag.html
diff --git a/inc/Engine/Common/PerformanceHints/Frontend/Processor.php b/inc/Engine/Common/PerformanceHints/Frontend/Processor.php
index 4540404392..4056268771 100644
--- a/inc/Engine/Common/PerformanceHints/Frontend/Processor.php
+++ b/inc/Engine/Common/PerformanceHints/Frontend/Processor.php
@@ -192,7 +192,7 @@ private function inject_beacon( $html, $url, $is_mobile ): string {
$script_tag = ""; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
// Append the script tag just before the closing body tag.
- return str_replace( '
+
+', $inline_script . $script_tag . '', $html );
+ return preg_replace( '/<\/body>/', $inline_script . $script_tag . '', $html, 1 );
}
/**
diff --git a/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/double_body_tag.html b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/double_body_tag.html
new file mode 100644
index 0000000000..5ac6a6d823
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/double_body_tag.html
@@ -0,0 +1,9 @@
+
+
+
Test
+
+
+
+