Merge pull request #136 from simonwelsh/54_fixes

BUGFIX Generate valid PHP when $includeDebuggingComments is true.
This commit is contained in:
Sam Minnée 2012-01-09 19:09:45 -08:00
commit c2da56d4a1
7 changed files with 59 additions and 12 deletions

View File

@ -0,0 +1,5 @@
<!doctype html>
<html>
<head></head>
<body></body>
</html>

View File

@ -0,0 +1 @@
Included

View File

@ -0,0 +1 @@
<div class='typography'></div>

View File

@ -0,0 +1 @@
<div class='typography'><% include SSViewerTestCommentsInclude %></div>

View File

@ -1,6 +1,10 @@
<?php <?php
class SSViewerTest extends SapphireTest { class SSViewerTest extends SapphireTest {
function setUp() {
parent::setUp();
SSViewer::set_source_file_comments(false);
}
/** /**
* Tests for {@link SSViewer::current_theme()} for different behaviour * Tests for {@link SSViewer::current_theme()} for different behaviour
@ -588,6 +592,38 @@ after')
SSViewer::setOption('rewriteHashlinks', $oldRewriteHashLinks); SSViewer::setOption('rewriteHashlinks', $oldRewriteHashLinks);
} }
function testRenderWithSourceFileComments() {
SSViewer::set_source_file_comments(true);
$view = new SSViewer(array('SSViewerTestCommentsFullSource'));
$data = new ArrayData(array());
$result = $view->process($data);
$expected = '<!doctype html>
<html><!-- template ' . BASE_PATH . '/sapphire/tests/templates/SSViewerTestCommentsFullSource.ss -->
<head></head>
<body></body>
<!-- end template ' . BASE_PATH . '/sapphire/tests/templates/SSViewerTestCommentsFullSource.ss --></html>
';
$this->assertEquals($result, $expected);
$view = new SSViewer(array('SSViewerTestCommentsPartialSource'));
$data = new ArrayData(array());
$result = $view->process($data);
$expected = '<!-- template ' . BASE_PATH . '/sapphire/tests/templates/SSViewerTestCommentsPartialSource.ss --><div class=\'typography\'></div><!-- end template ' . BASE_PATH . '/sapphire/tests/templates/SSViewerTestCommentsPartialSource.ss -->';
$this->assertEquals($result, $expected);
$view = new SSViewer(array('SSViewerTestCommentsWithInclude'));
$data = new ArrayData(array());
$result = $view->process($data);
$expected = '<!-- template ' . BASE_PATH . '/sapphire/tests/templates/SSViewerTestCommentsWithInclude.ss --><div class=\'typography\'><!-- include \'SSViewerTestCommentsInclude\' --><!-- template ' . BASE_PATH . '/sapphire/tests/templates/SSViewerTestCommentsInclude.ss -->Included<!-- end template ' . BASE_PATH . '/sapphire/tests/templates/SSViewerTestCommentsInclude.ss --><!-- end include \'SSViewerTestCommentsInclude\' --></div><!-- end template ' . BASE_PATH . '/sapphire/tests/templates/SSViewerTestCommentsWithInclude.ss -->';
$this->assertEquals($result, $expected);
SSViewer::set_source_file_comments(false);
}
} }
/** /**

View File

@ -2949,7 +2949,7 @@ class SSTemplateParser extends Parser {
function ClosedBlock__finalise(&$res) { function ClosedBlock__finalise(&$res) {
$blockname = $res['BlockName']['text']; $blockname = $res['BlockName']['text'];
$method = 'ClosedBlock_Handle_'.ucfirst(strtolower($blockname)); $method = 'ClosedBlock_Handle_'.$blockname;
if (method_exists($this, $method)) $res['php'] = $this->$method($res); if (method_exists($this, $method)) $res['php'] = $this->$method($res);
else { else {
throw new SSTemplateParseException('Unknown closed block "'.$blockname.'" encountered. Perhaps you are not supposed to close this block, or have mis-spelled it?', $this); 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 * @deprecated
*/ */
function ClosedBlock_Handle_Control(&$res) { function ClosedBlock_Handle_Control(&$res) {
Deprecation::notice('3.1', 'Use <% with %> or <% loop %> instead.');
return $this->ClosedBlock_Handle_Loop($res); return $this->ClosedBlock_Handle_Loop($res);
} }
@ -3086,7 +3087,7 @@ class SSTemplateParser extends Parser {
function OpenBlock__finalise(&$res) { function OpenBlock__finalise(&$res) {
$blockname = $res['BlockName']['text']; $blockname = $res['BlockName']['text'];
$method = 'OpenBlock_Handle_'.ucfirst(strtolower($blockname)); $method = 'OpenBlock_Handle_'.$blockname;
if (method_exists($this, $method)) $res['php'] = $this->$method($res); if (method_exists($this, $method)) $res['php'] = $this->$method($res);
else { else {
throw new SSTemplateParseException('Unknown open block "'.$blockname.'" encountered. Perhaps you missed the closing tag or have mis-spelled it?', $this); 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 if($this->includeDebuggingComments) { // Add include filename comments on dev sites
return return
'$val .= \'<!-- include '.$php.' -->\';'. "\n". '$val .= \'<!-- include '.addslashes($php).' -->\';'. "\n".
'$val .= SSViewer::parse_template('.$php.', $scope->getItem());'. "\n". '$val .= SSViewer::execute_template('.$php.', $scope->getItem());'. "\n".
'$val .= \'<!-- end include '.$php.' -->\';'. "\n"; '$val .= \'<!-- end include '.addslashes($php).' -->\';'. "\n";
} }
else { else {
return return
@ -3930,7 +3931,8 @@ class SSTemplateParser extends Parser {
$code = preg_replace('/(<html[^>]*>)/i', "\\1<!-- template $templateName -->", $code); $code = preg_replace('/(<html[^>]*>)/i', "\\1<!-- template $templateName -->", $code);
$code = preg_replace('/(<\/html[^>]*>)/i', "<!-- end template $templateName -->\\1", $code); $code = preg_replace('/(<\/html[^>]*>)/i', "<!-- end template $templateName -->\\1", $code);
} else { } else {
$code = "<!-- template $templateName -->\n" . $code . "\n<!-- end template $templateName -->"; $code = str_replace('<?php' . PHP_EOL, '<?php' . PHP_EOL . '$val .= \'<!-- template ' . $templateName . ' -->\';' . "\n", $code);
$code .= "\n" . '$val .= \'<!-- end template ' . $templateName . ' -->\';';
} }
} }

View File

@ -627,7 +627,7 @@ class SSTemplateParser extends Parser {
function ClosedBlock__finalise(&$res) { function ClosedBlock__finalise(&$res) {
$blockname = $res['BlockName']['text']; $blockname = $res['BlockName']['text'];
$method = 'ClosedBlock_Handle_'.ucfirst(strtolower($blockname)); $method = 'ClosedBlock_Handle_'.$blockname;
if (method_exists($this, $method)) $res['php'] = $this->$method($res); if (method_exists($this, $method)) $res['php'] = $this->$method($res);
else { else {
throw new SSTemplateParseException('Unknown closed block "'.$blockname.'" encountered. Perhaps you are not supposed to close this block, or have mis-spelled it?', $this); throw new SSTemplateParseException('Unknown closed block "'.$blockname.'" encountered. Perhaps you are not supposed to close this block, or have mis-spelled it?', $this);
@ -708,7 +708,7 @@ class SSTemplateParser extends Parser {
function OpenBlock__finalise(&$res) { function OpenBlock__finalise(&$res) {
$blockname = $res['BlockName']['text']; $blockname = $res['BlockName']['text'];
$method = 'OpenBlock_Handle_'.ucfirst(strtolower($blockname)); $method = 'OpenBlock_Handle_'.$blockname;
if (method_exists($this, $method)) $res['php'] = $this->$method($res); if (method_exists($this, $method)) $res['php'] = $this->$method($res);
else { else {
throw new SSTemplateParseException('Unknown open block "'.$blockname.'" encountered. Perhaps you missed the closing tag or have mis-spelled it?', $this); throw new SSTemplateParseException('Unknown open block "'.$blockname.'" encountered. Perhaps you missed the closing tag or have mis-spelled it?', $this);
@ -726,9 +726,9 @@ class SSTemplateParser extends Parser {
if($this->includeDebuggingComments) { // Add include filename comments on dev sites if($this->includeDebuggingComments) { // Add include filename comments on dev sites
return return
'$val .= \'<!-- include '.$php.' -->\';'. "\n". '$val .= \'<!-- include '.addslashes($php).' -->\';'. "\n".
'$val .= SSViewer::parse_template('.$php.', $scope->getItem());'. "\n". '$val .= SSViewer::execute_template('.$php.', $scope->getItem());'. "\n".
'$val .= \'<!-- end include '.$php.' -->\';'. "\n"; '$val .= \'<!-- end include '.addslashes($php).' -->\';'. "\n";
} }
else { else {
return return
@ -920,7 +920,8 @@ class SSTemplateParser extends Parser {
$code = preg_replace('/(<html[^>]*>)/i', "\\1<!-- template $templateName -->", $code); $code = preg_replace('/(<html[^>]*>)/i', "\\1<!-- template $templateName -->", $code);
$code = preg_replace('/(<\/html[^>]*>)/i', "<!-- end template $templateName -->\\1", $code); $code = preg_replace('/(<\/html[^>]*>)/i', "<!-- end template $templateName -->\\1", $code);
} else { } else {
$code = "<!-- template $templateName -->\n" . $code . "\n<!-- end template $templateName -->"; $code = str_replace('<?php' . PHP_EOL, '<?php' . PHP_EOL . '$val .= \'<!-- template ' . $templateName . ' -->\';' . "\n", $code);
$code .= "\n" . '$val .= \'<!-- end template ' . $templateName . ' -->\';';
} }
} }