Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Ensure source_file_comments works without throwing errors #5

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this right? The string preg_match is here twice, once as a function call and once as part of the regex

If this is correct, please an inline comment to at least say that it's intentional

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we're using preg_match to look for a match, and the match we're looking for must not be prepended with preg_match(. I'll add a comment about it

$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