Skip to content

Commit

Permalink
FIX Ensure source_file_comments works without throwing errors (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored Nov 18, 2024
1 parent 8804180 commit 4276205
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/SSTemplateParser.peg
Original file line number Diff line number Diff line change
Expand Up @@ -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 <html> tag to avoid IE glitches
if (stripos($code ?? '', "<!doctype") !== false) {
$code = preg_replace('/(<!doctype[^>]*("[^"]")*[^>]*>)/im', "$1\r\n<!-- template $templateName -->", $code ?? '');
// if not, put it after the <html> 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('/(?<!preg_match\(\'\/)<!doctype/i', $code)) {
$code = preg_replace('/((?<!preg_match\(\'\/)<!doctype[^>]*("[^"]")*[^>]*>)/im', "$1\r\n<!-- template $templateName -->", $code ?? '');
$code .= "\r\n" . '$val .= \'<!-- end template ' . $templateName . ' -->\';';
} elseif (stripos($code ?? '', "<html") !== false) {
$code = preg_replace_callback('/(.*)(<html[^>]*>)(.*)/i', function ($matches) use ($templateName) {
Expand Down
8 changes: 5 additions & 3 deletions src/SSTemplateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <html> tag to avoid IE glitches
if (stripos($code ?? '', "<!doctype") !== false) {
$code = preg_replace('/(<!doctype[^>]*("[^"]")*[^>]*>)/im', "$1\r\n<!-- template $templateName -->", $code ?? '');
// if not, put it after the <html> 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('/(?<!preg_match\(\'\/)<!doctype/i', $code)) {
$code = preg_replace('/((?<!preg_match\(\'\/)<!doctype[^>]*("[^"]")*[^>]*>)/im', "$1\r\n<!-- template $templateName -->", $code ?? '');
$code .= "\r\n" . '$val .= \'<!-- end template ' . $templateName . ' -->\';';
} elseif (stripos($code ?? '', "<html") !== false) {
$code = preg_replace_callback('/(.*)(<html[^>]*>)(.*)/i', function ($matches) use ($templateName) {
Expand Down

0 comments on commit 4276205

Please sign in to comment.