From 04c8e2b762146e3c2c04f1be852b2f67beee5329 Mon Sep 17 00:00:00 2001 From: Simon Welsh Date: Sat, 17 Dec 2011 17:35:26 +1300 Subject: [PATCH] BUGFIX Generate valid PHP when $includeDebuggingComments is true. --- .../SSViewerTestCommentsFullSource.ss | 5 +++ .../templates/SSViewerTestCommentsInclude.ss | 1 + .../SSViewerTestCommentsPartialSource.ss | 1 + .../SSViewerTestCommentsWithInclude.ss | 1 + tests/view/SSViewerTest.php | 36 +++++++++++++++++++ view/SSTemplateParser.php | 14 ++++---- view/SSTemplateParser.php.inc | 13 +++---- 7 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 tests/templates/SSViewerTestCommentsFullSource.ss create mode 100644 tests/templates/SSViewerTestCommentsInclude.ss create mode 100644 tests/templates/SSViewerTestCommentsPartialSource.ss create mode 100644 tests/templates/SSViewerTestCommentsWithInclude.ss diff --git a/tests/templates/SSViewerTestCommentsFullSource.ss b/tests/templates/SSViewerTestCommentsFullSource.ss new file mode 100644 index 000000000..82f008b01 --- /dev/null +++ b/tests/templates/SSViewerTestCommentsFullSource.ss @@ -0,0 +1,5 @@ + + + + + diff --git a/tests/templates/SSViewerTestCommentsInclude.ss b/tests/templates/SSViewerTestCommentsInclude.ss new file mode 100644 index 000000000..c709afed9 --- /dev/null +++ b/tests/templates/SSViewerTestCommentsInclude.ss @@ -0,0 +1 @@ +Included \ No newline at end of file diff --git a/tests/templates/SSViewerTestCommentsPartialSource.ss b/tests/templates/SSViewerTestCommentsPartialSource.ss new file mode 100644 index 000000000..4b99e88ab --- /dev/null +++ b/tests/templates/SSViewerTestCommentsPartialSource.ss @@ -0,0 +1 @@ +
\ No newline at end of file diff --git a/tests/templates/SSViewerTestCommentsWithInclude.ss b/tests/templates/SSViewerTestCommentsWithInclude.ss new file mode 100644 index 000000000..3a3b50cde --- /dev/null +++ b/tests/templates/SSViewerTestCommentsWithInclude.ss @@ -0,0 +1 @@ +
<% include SSViewerTestCommentsInclude %>
\ No newline at end of file diff --git a/tests/view/SSViewerTest.php b/tests/view/SSViewerTest.php index 08f3b6129..73813225b 100644 --- a/tests/view/SSViewerTest.php +++ b/tests/view/SSViewerTest.php @@ -1,6 +1,10 @@ process($data); + $expected = ' + + + + +'; + $this->assertEquals($result, $expected); + + $view = new SSViewer(array('SSViewerTestCommentsPartialSource')); + $data = new ArrayData(array()); + + $result = $view->process($data); + $expected = '
'; + $this->assertEquals($result, $expected); + + $view = new SSViewer(array('SSViewerTestCommentsWithInclude')); + $data = new ArrayData(array()); + + $result = $view->process($data); + $expected = '
Included
'; + $this->assertEquals($result, $expected); + + SSViewer::set_source_file_comments(false); + } } diff --git a/view/SSTemplateParser.php b/view/SSTemplateParser.php index 5aebb1388..15caa835c 100644 --- a/view/SSTemplateParser.php +++ b/view/SSTemplateParser.php @@ -2949,7 +2949,7 @@ class SSTemplateParser extends Parser { function ClosedBlock__finalise(&$res) { $blockname = $res['BlockName']['text']; - $method = 'ClosedBlock_Handle_'.ucfirst(strtolower($blockname)); + $method = 'ClosedBlock_Handle_'.$blockname; if (method_exists($this, $method)) $res['php'] = $this->$method($res); else { throw new SSTemplateParseException('Unknown closed block "'.$blockname.'" encountered. Perhaps you are not supposed to close this block, or have mis-spelled it?', $this); @@ -2981,6 +2981,7 @@ class SSTemplateParser extends Parser { * @deprecated */ function ClosedBlock_Handle_Control(&$res) { + Deprecation::notice('3.1', 'Use <% with %> or <% loop %> instead.'); return $this->ClosedBlock_Handle_Loop($res); } @@ -3086,7 +3087,7 @@ class SSTemplateParser extends Parser { function OpenBlock__finalise(&$res) { $blockname = $res['BlockName']['text']; - $method = 'OpenBlock_Handle_'.ucfirst(strtolower($blockname)); + $method = 'OpenBlock_Handle_'.$blockname; if (method_exists($this, $method)) $res['php'] = $this->$method($res); else { throw new SSTemplateParseException('Unknown open block "'.$blockname.'" encountered. Perhaps you missed the closing tag or have mis-spelled it?', $this); @@ -3104,9 +3105,9 @@ class SSTemplateParser extends Parser { if($this->includeDebuggingComments) { // Add include filename comments on dev sites return - '$val .= \'\';'. "\n". - '$val .= SSViewer::parse_template('.$php.', $scope->getItem());'. "\n". - '$val .= \'\';'. "\n"; + '$val .= \'\';'. "\n". + '$val .= SSViewer::execute_template('.$php.', $scope->getItem());'. "\n". + '$val .= \'\';'. "\n"; } else { return @@ -3930,7 +3931,8 @@ class SSTemplateParser extends Parser { $code = preg_replace('/(]*>)/i', "\\1", $code); $code = preg_replace('/(<\/html[^>]*>)/i', "\\1", $code); } else { - $code = "\n" . $code . "\n"; + $code = str_replace('\';' . "\n", $code); + $code .= "\n" . '$val .= \'\';'; } } diff --git a/view/SSTemplateParser.php.inc b/view/SSTemplateParser.php.inc index 63dca3116..258734cc1 100644 --- a/view/SSTemplateParser.php.inc +++ b/view/SSTemplateParser.php.inc @@ -626,7 +626,7 @@ class SSTemplateParser extends Parser { function ClosedBlock__finalise(&$res) { $blockname = $res['BlockName']['text']; - $method = 'ClosedBlock_Handle_'.ucfirst(strtolower($blockname)); + $method = 'ClosedBlock_Handle_'.$blockname; if (method_exists($this, $method)) $res['php'] = $this->$method($res); else { throw new SSTemplateParseException('Unknown closed block "'.$blockname.'" encountered. Perhaps you are not supposed to close this block, or have mis-spelled it?', $this); @@ -707,7 +707,7 @@ class SSTemplateParser extends Parser { function OpenBlock__finalise(&$res) { $blockname = $res['BlockName']['text']; - $method = 'OpenBlock_Handle_'.ucfirst(strtolower($blockname)); + $method = 'OpenBlock_Handle_'.$blockname; if (method_exists($this, $method)) $res['php'] = $this->$method($res); else { throw new SSTemplateParseException('Unknown open block "'.$blockname.'" encountered. Perhaps you missed the closing tag or have mis-spelled it?', $this); @@ -725,9 +725,9 @@ class SSTemplateParser extends Parser { if($this->includeDebuggingComments) { // Add include filename comments on dev sites return - '$val .= \'\';'. "\n". - '$val .= SSViewer::parse_template('.$php.', $scope->getItem());'. "\n". - '$val .= \'\';'. "\n"; + '$val .= \'\';'. "\n". + '$val .= SSViewer::execute_template('.$php.', $scope->getItem());'. "\n". + '$val .= \'\';'. "\n"; } else { return @@ -919,7 +919,8 @@ class SSTemplateParser extends Parser { $code = preg_replace('/(]*>)/i', "\\1", $code); $code = preg_replace('/(<\/html[^>]*>)/i', "\\1", $code); } else { - $code = "\n" . $code . "\n"; + $code = str_replace('\';' . "\n", $code); + $code .= "\n" . '$val .= \'\';'; } }