From 427620551c543286da3a9d17a6f7a0172fb85f98 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Tue, 19 Nov 2024 10:45:38 +1300 Subject: [PATCH] FIX Ensure source_file_comments works without throwing errors (#5) --- src/SSTemplateParser.peg | 8 +++++--- src/SSTemplateParser.php | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/SSTemplateParser.peg b/src/SSTemplateParser.peg index 8ac1059..607fa42 100644 --- a/src/SSTemplateParser.peg +++ b/src/SSTemplateParser.peg @@ -1308,9 +1308,11 @@ EOC; protected function includeDebuggingComments(string $code, string $templateName): string { // If this template contains a doctype, put it right after it, - // if not, put it after the tag to avoid IE glitches - if (stripos($code ?? '', "]*("[^"]")*[^>]*>)/im', "$1\r\n", $code ?? ''); + // if not, put it after the tag to avoid IE glitches. + // Some cached templates will have a preg_match looking for the doctype, so we use a + // negative lookbehind to exclude that from our matches. + if (preg_match('/(?]*("[^"]")*[^>]*>)/im', "$1\r\n", $code ?? ''); $code .= "\r\n" . '$val .= \'\';'; } elseif (stripos($code ?? '', "]*>)(.*)/i', function ($matches) use ($templateName) { diff --git a/src/SSTemplateParser.php b/src/SSTemplateParser.php index 27eb4b6..1274845 100644 --- a/src/SSTemplateParser.php +++ b/src/SSTemplateParser.php @@ -5342,9 +5342,11 @@ public function compileString(string $string, string $templateName = "", bool $i protected function includeDebuggingComments(string $code, string $templateName): string { // If this template contains a doctype, put it right after it, - // if not, put it after the tag to avoid IE glitches - if (stripos($code ?? '', "]*("[^"]")*[^>]*>)/im', "$1\r\n", $code ?? ''); + // if not, put it after the tag to avoid IE glitches. + // Some cached templates will have a preg_match looking for the doctype, so we use a + // negative lookbehind to exclude that from our matches. + if (preg_match('/(?]*("[^"]")*[^>]*>)/im', "$1\r\n", $code ?? ''); $code .= "\r\n" . '$val .= \'\';'; } elseif (stripos($code ?? '', "]*>)(.*)/i', function ($matches) use ($templateName) {