mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-05 23:58:49 +02:00
Merge pull request #1793 from Zauberfisch/3.1
API: made more robust SSTemplateParser->includeDebuggingComments()
This commit is contained in:
commit
59dbdcfc55
@ -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>
|
@ -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>
|
@ -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>
|
@ -0,0 +1,4 @@
|
|||||||
|
<html>
|
||||||
|
<head></head>
|
||||||
|
<body></body>
|
||||||
|
</html>
|
@ -1062,43 +1062,98 @@ after')
|
|||||||
$origEnv = Config::inst()->get('Director', 'environment_type');
|
$origEnv = Config::inst()->get('Director', 'environment_type');
|
||||||
Config::inst()->update('Director', 'environment_type', 'dev');
|
Config::inst()->update('Director', 'environment_type', 'dev');
|
||||||
Config::inst()->update('SSViewer', 'source_file_comments', true);
|
Config::inst()->update('SSViewer', 'source_file_comments', true);
|
||||||
|
$f = FRAMEWORK_PATH . '/tests/templates/SSViewerTestComments';
|
||||||
$view = new SSViewer(array('SSViewerTestCommentsFullSource'));
|
$templates = array(
|
||||||
$data = new ArrayData(array());
|
array(
|
||||||
|
'name' => 'SSViewerTestCommentsFullSource',
|
||||||
$result = $view->process($data);
|
'expected' => ""
|
||||||
$expected = '<!doctype html>
|
. "<!doctype html>"
|
||||||
<!-- template ' . FRAMEWORK_PATH . '/tests/templates/SSViewerTestCommentsFullSource.ss --><html>
|
. "<!-- template $f/SSViewerTestCommentsFullSource.ss -->"
|
||||||
<head></head>
|
. "<html>"
|
||||||
<body></body>
|
. "\t<head></head>"
|
||||||
</html><!-- end template ' . FRAMEWORK_PATH . '/tests/templates/SSViewerTestCommentsFullSource.ss -->
|
. "\t<body></body>"
|
||||||
';
|
. "</html>"
|
||||||
$this->assertEquals($result, $expected);
|
. "<!-- end template $f/SSViewerTestCommentsFullSource.ss -->",
|
||||||
|
),
|
||||||
$view = new SSViewer(array('SSViewerTestCommentsPartialSource'));
|
array(
|
||||||
$data = new ArrayData(array());
|
'name' => 'SSViewerTestCommentsFullSourceHTML4Doctype',
|
||||||
|
'expected' => ""
|
||||||
$result = $view->process($data);
|
. "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\t\t\"http://www.w3.org/TR/html4/strict.dtd\">"
|
||||||
$expected = '<!-- template ' . FRAMEWORK_PATH . '/tests/templates/SSViewerTestCommentsPartialSource.ss -->'
|
. "<!-- template $f/SSViewerTestCommentsFullSourceHTML4Doctype.ss -->"
|
||||||
. '<div class=\'typography\'></div><!-- end template ' . FRAMEWORK_PATH
|
. "<html>"
|
||||||
. '/tests/templates/SSViewerTestCommentsPartialSource.ss -->';
|
. "\t<head></head>"
|
||||||
$this->assertEquals($result, $expected);
|
. "\t<body></body>"
|
||||||
|
. "</html>"
|
||||||
$view = new SSViewer(array('SSViewerTestCommentsWithInclude'));
|
. "<!-- end template $f/SSViewerTestCommentsFullSourceHTML4Doctype.ss -->",
|
||||||
$data = new ArrayData(array());
|
),
|
||||||
|
array(
|
||||||
$result = $view->process($data);
|
'name' => 'SSViewerTestCommentsFullSourceNoDoctype',
|
||||||
$expected = '<!-- template ' . FRAMEWORK_PATH . '/tests/templates/SSViewerTestCommentsWithInclude.ss -->'
|
'expected' => ""
|
||||||
. '<div class=\'typography\'><!-- include \'SSViewerTestCommentsInclude\' --><!-- template '
|
. "<html><!-- template $f/SSViewerTestCommentsFullSourceNoDoctype.ss -->"
|
||||||
. FRAMEWORK_PATH . '/tests/templates/SSViewerTestCommentsInclude.ss -->Included<!-- end template '
|
. "\t<head></head>"
|
||||||
. FRAMEWORK_PATH . '/tests/templates/SSViewerTestCommentsInclude.ss -->'
|
. "\t<body></body>"
|
||||||
. '<!-- end include \'SSViewerTestCommentsInclude\' --></div><!-- end template ' . FRAMEWORK_PATH
|
. "<!-- end template $f/SSViewerTestCommentsFullSourceNoDoctype.ss --></html>",
|
||||||
. '/tests/templates/SSViewerTestCommentsWithInclude.ss -->';
|
),
|
||||||
$this->assertEquals($result, $expected);
|
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('SSViewer', 'source_file_comments', false);
|
||||||
Config::inst()->update('Director', 'environment_type', $origEnv);
|
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() {
|
public function testLoopIteratorIterator() {
|
||||||
$list = new PaginatedList(new ArrayList());
|
$list = new PaginatedList(new ArrayList());
|
||||||
|
@ -4593,21 +4593,45 @@ class SSTemplateParser extends Parser {
|
|||||||
|
|
||||||
// Include top level debugging comments if desired
|
// Include top level debugging comments if desired
|
||||||
if($includeDebuggingComments && $templateName && stripos($code, "<?xml") === false) {
|
if($includeDebuggingComments && $templateName && stripos($code, "<?xml") === false) {
|
||||||
// If this template is a full HTML page, then put the comments just outside the HTML tag,
|
$code = $parser->includeDebuggingComments($code, $templateName);
|
||||||
// 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 . ' -->\';';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $code;
|
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
|
* Compiles some file that contains template source code, and returns the php code that will execute as per that
|
||||||
* source
|
* source
|
||||||
|
@ -1047,21 +1047,45 @@ class SSTemplateParser extends Parser {
|
|||||||
|
|
||||||
// Include top level debugging comments if desired
|
// Include top level debugging comments if desired
|
||||||
if($includeDebuggingComments && $templateName && stripos($code, "<?xml") === false) {
|
if($includeDebuggingComments && $templateName && stripos($code, "<?xml") === false) {
|
||||||
// If this template is a full HTML page, then put the comments just outside the HTML tag,
|
$code = $parser->includeDebuggingComments($code, $templateName);
|
||||||
// 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 . ' -->\';';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $code;
|
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
|
* Compiles some file that contains template source code, and returns the php code that will execute as per that
|
||||||
* source
|
* source
|
||||||
|
Loading…
Reference in New Issue
Block a user