Merge pull request #1793 from Zauberfisch/3.1

API: made more robust SSTemplateParser->includeDebuggingComments()
This commit is contained in:
Ingo Schommer 2013-04-22 12:20:08 -07:00
commit 59dbdcfc55
11 changed files with 182 additions and 56 deletions

View File

@ -0,0 +1,6 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head></head>
<body></body>
</html>

View File

@ -0,0 +1,7 @@
<!doctype html>
<!--[if lte IE 8]> <html class='old-ie'> <![endif]-->
<!--[if gt IE 8]> <html class='new-ie'> <![endif]-->
<!--[if !IE]><!--> <html class='no-ie'> <!--<![endif]-->
<head></head>
<body></body>
</html>

View File

@ -0,0 +1,6 @@
<!--[if lte IE 8]> <html class='old-ie'> <![endif]-->
<!--[if gt IE 8]> <html class='new-ie'> <![endif]-->
<!--[if !IE]><!--> <html class='no-ie'> <!--<![endif]-->
<head></head>
<body></body>
</html>

View File

@ -0,0 +1,4 @@
<html>
<head></head>
<body></body>
</html>

View File

@ -1062,43 +1062,98 @@ after')
$origEnv = Config::inst()->get('Director', 'environment_type');
Config::inst()->update('Director', 'environment_type', 'dev');
Config::inst()->update('SSViewer', 'source_file_comments', true);
$view = new SSViewer(array('SSViewerTestCommentsFullSource'));
$data = new ArrayData(array());
$result = $view->process($data);
$expected = '<!doctype html>
<!-- template ' . FRAMEWORK_PATH . '/tests/templates/SSViewerTestCommentsFullSource.ss --><html>
<head></head>
<body></body>
</html><!-- end template ' . FRAMEWORK_PATH . '/tests/templates/SSViewerTestCommentsFullSource.ss -->
';
$this->assertEquals($result, $expected);
$view = new SSViewer(array('SSViewerTestCommentsPartialSource'));
$data = new ArrayData(array());
$result = $view->process($data);
$expected = '<!-- template ' . FRAMEWORK_PATH . '/tests/templates/SSViewerTestCommentsPartialSource.ss -->'
. '<div class=\'typography\'></div><!-- end template ' . FRAMEWORK_PATH
. '/tests/templates/SSViewerTestCommentsPartialSource.ss -->';
$this->assertEquals($result, $expected);
$view = new SSViewer(array('SSViewerTestCommentsWithInclude'));
$data = new ArrayData(array());
$result = $view->process($data);
$expected = '<!-- template ' . FRAMEWORK_PATH . '/tests/templates/SSViewerTestCommentsWithInclude.ss -->'
. '<div class=\'typography\'><!-- include \'SSViewerTestCommentsInclude\' --><!-- template '
. FRAMEWORK_PATH . '/tests/templates/SSViewerTestCommentsInclude.ss -->Included<!-- end template '
. FRAMEWORK_PATH . '/tests/templates/SSViewerTestCommentsInclude.ss -->'
. '<!-- end include \'SSViewerTestCommentsInclude\' --></div><!-- end template ' . FRAMEWORK_PATH
. '/tests/templates/SSViewerTestCommentsWithInclude.ss -->';
$this->assertEquals($result, $expected);
$f = FRAMEWORK_PATH . '/tests/templates/SSViewerTestComments';
$templates = array(
array(
'name' => 'SSViewerTestCommentsFullSource',
'expected' => ""
. "<!doctype html>"
. "<!-- template $f/SSViewerTestCommentsFullSource.ss -->"
. "<html>"
. "\t<head></head>"
. "\t<body></body>"
. "</html>"
. "<!-- end template $f/SSViewerTestCommentsFullSource.ss -->",
),
array(
'name' => 'SSViewerTestCommentsFullSourceHTML4Doctype',
'expected' => ""
. "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\t\t\"http://www.w3.org/TR/html4/strict.dtd\">"
. "<!-- template $f/SSViewerTestCommentsFullSourceHTML4Doctype.ss -->"
. "<html>"
. "\t<head></head>"
. "\t<body></body>"
. "</html>"
. "<!-- end template $f/SSViewerTestCommentsFullSourceHTML4Doctype.ss -->",
),
array(
'name' => 'SSViewerTestCommentsFullSourceNoDoctype',
'expected' => ""
. "<html><!-- template $f/SSViewerTestCommentsFullSourceNoDoctype.ss -->"
. "\t<head></head>"
. "\t<body></body>"
. "<!-- end template $f/SSViewerTestCommentsFullSourceNoDoctype.ss --></html>",
),
array(
'name' => 'SSViewerTestCommentsFullSourceIfIE',
'expected' => ""
. "<!doctype html>"
. "<!-- template $f/SSViewerTestCommentsFullSourceIfIE.ss -->"
. "<!--[if lte IE 8]> <html class='old-ie'> <![endif]-->"
. "<!--[if gt IE 8]> <html class='new-ie'> <![endif]-->"
. "<!--[if !IE]><!--> <html class='no-ie'> <!--<![endif]-->"
. "\t<head></head>"
. "\t<body></body>"
. "</html>"
. "<!-- end template $f/SSViewerTestCommentsFullSourceIfIE.ss -->",
),
array(
'name' => 'SSViewerTestCommentsFullSourceIfIENoDoctype',
'expected' => ""
. "<!--[if lte IE 8]> <html class='old-ie'> <![endif]-->"
. "<!--[if gt IE 8]> <html class='new-ie'> <![endif]-->"
. "<!--[if !IE]><!--> <html class='no-ie'>"
. "<!-- template $f/SSViewerTestCommentsFullSourceIfIENoDoctype.ss -->"
. " <!--<![endif]-->"
. "\t<head></head>"
. "\t<body></body>"
. "<!-- end template $f/SSViewerTestCommentsFullSourceIfIENoDoctype.ss --></html>",
),
array(
'name' => 'SSViewerTestCommentsPartialSource',
'expected' =>
"<!-- template $f/SSViewerTestCommentsPartialSource.ss -->"
. "<div class='typography'></div>"
. "<!-- end template $f/SSViewerTestCommentsPartialSource.ss -->",
),
array(
'name' => 'SSViewerTestCommentsWithInclude',
'expected' =>
"<!-- template $f/SSViewerTestCommentsWithInclude.ss -->"
. "<div class='typography'>"
. "<!-- include 'SSViewerTestCommentsInclude' -->"
. "<!-- template $f/SSViewerTestCommentsInclude.ss -->"
. "Included"
. "<!-- end template $f/SSViewerTestCommentsInclude.ss -->"
. "<!-- end include 'SSViewerTestCommentsInclude' -->"
. "</div>"
. "<!-- end template $f/SSViewerTestCommentsWithInclude.ss -->",
),
);
foreach ($templates as $template) {
$this->_renderWithSourceFileComments($template['name'], $template['expected']);
}
Config::inst()->update('SSViewer', 'source_file_comments', false);
Config::inst()->update('Director', 'environment_type', $origEnv);
}
private function _renderWithSourceFileComments($name, $expected) {
$viewer = new SSViewer(array($name));
$data = new ArrayData(array());
$result = $viewer->process($data);
$expected = str_replace(array("\r", "\n"), '', $expected);
$result = str_replace(array("\r", "\n"), '', $result);
$this->assertEquals($result, $expected);
}
public function testLoopIteratorIterator() {
$list = new PaginatedList(new ArrayList());

View File

@ -4590,23 +4590,47 @@ class SSTemplateParser extends Parser {
// Get the result
$code = $result['php'];
}
// Include top level debugging comments if desired
if($includeDebuggingComments && $templateName && stripos($code, "<?xml") === false) {
// If this template is a full HTML page, then put the comments just outside the HTML tag,
// this is still no problem for IE as long as the DOCTYPE-declaration is done before this comment
if(stripos($code, "<html") !== false) {
$code = preg_replace('/(<html[^>]*>)/i', "<!-- template $templateName -->\\1", $code);
$code = preg_replace('/(<\/html[^>]*>)/i', "\\1<!-- end template $templateName -->", $code);
} else {
$code = str_replace('<?php' . PHP_EOL, '<?php' . PHP_EOL . '$val .= \'<!-- template ' . $templateName .
' -->\';' . "\n", $code);
$code .= "\n" . '$val .= \'<!-- end template ' . $templateName . ' -->\';';
}
$code = $parser->includeDebuggingComments($code, $templateName);
}
return $code;
}
/**
* @param string $code
* @return string $code
*/
protected function includeDebuggingComments($code, $templateName) {
// 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);
$code .= "\r\n" . '$val .= \'<!-- end template ' . $templateName . ' -->\';';
} elseif(stripos($code, "<html") !== false) {
$code = preg_replace_callback('/(.*)(<html[^>]*>)(.*)/i', function($matches) use ($templateName) {
if (stripos($matches[3], '<!--') === false && stripos($matches[3], '-->') !== false) {
// after this <html> tag there is a comment close but no comment has been opened
// this most likely means that this <html> tag is inside a comment
// we should not add a comment inside a comment (invalid html)
// lets append it at the end of the comment
// an example case for this is the html5boilerplate: <!--[if IE]><html class="ie"><![endif]-->
return $matches[0];
} else {
// all other cases, add the comment and return it
return "{$matches[1]}{$matches[2]}<!-- template $templateName -->{$matches[3]}";
}
}, $code);
$code = preg_replace('/(<\/html[^>]*>)/i', "<!-- end template $templateName -->$1", $code);
} else {
$code = str_replace('<?php' . PHP_EOL, '<?php' . PHP_EOL . '$val .= \'<!-- template ' . $templateName .
' -->\';' . "\r\n", $code);
$code .= "\r\n" . '$val .= \'<!-- end template ' . $templateName . ' -->\';';
}
return $code;
}
/**
* Compiles some file that contains template source code, and returns the php code that will execute as per that

View File

@ -1044,23 +1044,47 @@ class SSTemplateParser extends Parser {
// Get the result
$code = $result['php'];
}
// Include top level debugging comments if desired
if($includeDebuggingComments && $templateName && stripos($code, "<?xml") === false) {
// If this template is a full HTML page, then put the comments just outside the HTML tag,
// this is still no problem for IE as long as the DOCTYPE-declaration is done before this comment
if(stripos($code, "<html") !== false) {
$code = preg_replace('/(<html[^>]*>)/i', "<!-- template $templateName -->\\1", $code);
$code = preg_replace('/(<\/html[^>]*>)/i', "\\1<!-- end template $templateName -->", $code);
} else {
$code = str_replace('<?php' . PHP_EOL, '<?php' . PHP_EOL . '$val .= \'<!-- template ' . $templateName .
' -->\';' . "\n", $code);
$code .= "\n" . '$val .= \'<!-- end template ' . $templateName . ' -->\';';
}
$code = $parser->includeDebuggingComments($code, $templateName);
}
return $code;
}
/**
* @param string $code
* @return string $code
*/
protected function includeDebuggingComments($code, $templateName) {
// 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);
$code .= "\r\n" . '$val .= \'<!-- end template ' . $templateName . ' -->\';';
} elseif(stripos($code, "<html") !== false) {
$code = preg_replace_callback('/(.*)(<html[^>]*>)(.*)/i', function($matches) use ($templateName) {
if (stripos($matches[3], '<!--') === false && stripos($matches[3], '-->') !== false) {
// after this <html> tag there is a comment close but no comment has been opened
// this most likely means that this <html> tag is inside a comment
// we should not add a comment inside a comment (invalid html)
// lets append it at the end of the comment
// an example case for this is the html5boilerplate: <!--[if IE]><html class="ie"><![endif]-->
return $matches[0];
} else {
// all other cases, add the comment and return it
return "{$matches[1]}{$matches[2]}<!-- template $templateName -->{$matches[3]}";
}
}, $code);
$code = preg_replace('/(<\/html[^>]*>)/i', "<!-- end template $templateName -->$1", $code);
} else {
$code = str_replace('<?php' . PHP_EOL, '<?php' . PHP_EOL . '$val .= \'<!-- template ' . $templateName .
' -->\';' . "\r\n", $code);
$code .= "\r\n" . '$val .= \'<!-- end template ' . $templateName . ' -->\';';
}
return $code;
}
/**
* Compiles some file that contains template source code, and returns the php code that will execute as per that