From 7c66e8e7a2f30f29a47aa255a378bdfc360d92b1 Mon Sep 17 00:00:00 2001 From: Zauberfisch Date: Mon, 22 Apr 2013 13:53:20 +0000 Subject: [PATCH 1/4] API: try to place source_file_comments after doctype instead of html --- view/SSTemplateParser.php | 17 +++++++++-------- view/SSTemplateParser.php.inc | 17 +++++++++-------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/view/SSTemplateParser.php b/view/SSTemplateParser.php index 9c48c8e7f..acdb0c2ff 100644 --- a/view/SSTemplateParser.php +++ b/view/SSTemplateParser.php @@ -4590,19 +4590,20 @@ class SSTemplateParser extends Parser { // Get the result $code = $result['php']; } - + // Include top level debugging comments if desired if($includeDebuggingComments && $templateName && stripos($code, "]*>)/i', "\\1", $code); - $code = preg_replace('/(<\/html[^>]*>)/i', "\\1", $code); + // If this template contains a doctype, put it right after it, + // if not, put it after the tag to avoid IE glitches + if(stripos($code, "]*("[^"]")*[^>]*>)/im', "$1\r\n", $code); + } elseif(stripos($code, "]*>)/i', "$1", $code); } else { $code = str_replace('\';' . "\n", $code); - $code .= "\n" . '$val .= \'\';'; + ' -->\';' . "\r\n", $code); } + $code .= "\r\n" . '$val .= \'\';'; } return $code; diff --git a/view/SSTemplateParser.php.inc b/view/SSTemplateParser.php.inc index ea01957c4..9aa35bd54 100644 --- a/view/SSTemplateParser.php.inc +++ b/view/SSTemplateParser.php.inc @@ -1044,19 +1044,20 @@ class SSTemplateParser extends Parser { // Get the result $code = $result['php']; } - + // Include top level debugging comments if desired if($includeDebuggingComments && $templateName && stripos($code, "]*>)/i', "\\1", $code); - $code = preg_replace('/(<\/html[^>]*>)/i', "\\1", $code); + // If this template contains a doctype, put it right after it, + // if not, put it after the tag to avoid IE glitches + if(stripos($code, "]*("[^"]")*[^>]*>)/im', "$1\r\n", $code); + } elseif(stripos($code, "]*>)/i', "$1", $code); } else { $code = str_replace('\';' . "\n", $code); - $code .= "\n" . '$val .= \'\';'; + ' -->\';' . "\r\n", $code); } + $code .= "\r\n" . '$val .= \'\';'; } return $code; From 94b37f9c856cdec42ac33fbd81b00d5314beac24 Mon Sep 17 00:00:00 2001 From: Zauberfisch Date: Mon, 22 Apr 2013 14:12:43 +0000 Subject: [PATCH 2/4] MINOR: moved includeDebuggingComments logic into own method to allow separated tests --- view/SSTemplateParser.php | 31 ++++++++++++++++++++----------- view/SSTemplateParser.php.inc | 31 ++++++++++++++++++++----------- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/view/SSTemplateParser.php b/view/SSTemplateParser.php index acdb0c2ff..662662107 100644 --- a/view/SSTemplateParser.php +++ b/view/SSTemplateParser.php @@ -4593,21 +4593,30 @@ class SSTemplateParser extends Parser { // Include top level debugging comments if desired if($includeDebuggingComments && $templateName && stripos($code, " tag to avoid IE glitches - if(stripos($code, "]*("[^"]")*[^>]*>)/im', "$1\r\n", $code); - } elseif(stripos($code, "]*>)/i', "$1", $code); - } else { - $code = str_replace('\';' . "\r\n", $code); - } - $code .= "\r\n" . '$val .= \'\';'; + $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 tag to avoid IE glitches + if(stripos($code, "]*("[^"]")*[^>]*>)/im', "$1\r\n", $code); + } elseif(stripos($code, "]*>)/i', "$1", $code); + } else { + $code = str_replace('\';' . "\r\n", $code); + } + $code .= "\r\n" . '$val .= \'\';'; + return $code; + } /** * Compiles some file that contains template source code, and returns the php code that will execute as per that diff --git a/view/SSTemplateParser.php.inc b/view/SSTemplateParser.php.inc index 9aa35bd54..75ccdaffa 100644 --- a/view/SSTemplateParser.php.inc +++ b/view/SSTemplateParser.php.inc @@ -1047,21 +1047,30 @@ class SSTemplateParser extends Parser { // Include top level debugging comments if desired if($includeDebuggingComments && $templateName && stripos($code, " tag to avoid IE glitches - if(stripos($code, "]*("[^"]")*[^>]*>)/im', "$1\r\n", $code); - } elseif(stripos($code, "]*>)/i', "$1", $code); - } else { - $code = str_replace('\';' . "\r\n", $code); - } - $code .= "\r\n" . '$val .= \'\';'; + $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 tag to avoid IE glitches + if(stripos($code, "]*("[^"]")*[^>]*>)/im', "$1\r\n", $code); + } elseif(stripos($code, "]*>)/i', "$1", $code); + } else { + $code = str_replace('\';' . "\r\n", $code); + } + $code .= "\r\n" . '$val .= \'\';'; + return $code; + } /** * Compiles some file that contains template source code, and returns the php code that will execute as per that From 034f5750033866e83db4b73104f686c18909e72e Mon Sep 17 00:00:00 2001 From: Zauberfisch Date: Mon, 22 Apr 2013 18:16:09 +0000 Subject: [PATCH 3/4] MINOR: if no doctyle is set, include comments inside MINOR: if tag is inside a html comment do not add a comment --- view/SSTemplateParser.php | 18 ++++++++++++++++-- view/SSTemplateParser.php.inc | 18 ++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/view/SSTemplateParser.php b/view/SSTemplateParser.php index 662662107..c3e3532b6 100644 --- a/view/SSTemplateParser.php +++ b/view/SSTemplateParser.php @@ -4608,13 +4608,27 @@ class SSTemplateParser extends Parser { // if not, put it after the tag to avoid IE glitches if(stripos($code, "]*("[^"]")*[^>]*>)/im', "$1\r\n", $code); + $code .= "\r\n" . '$val .= \'\';'; } elseif(stripos($code, "]*>)/i', "$1", $code); + $code = preg_replace_callback('/(.*)(]*>)(.*)/i', function($matches) use ($templateName) { + if (stripos($matches[3], '') !== false) { + // after this tag there is a comment close but no comment has been opened + // this most likely means that this 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: + return $matches[0]; + } else { + // all other cases, add the comment and return it + return "{$matches[1]}{$matches[2]}{$matches[3]}"; + } + }, $code); + $code = preg_replace('/(<\/html[^>]*>)/i', "$1", $code); } else { $code = str_replace('\';' . "\r\n", $code); + $code .= "\r\n" . '$val .= \'\';'; } - $code .= "\r\n" . '$val .= \'\';'; return $code; } diff --git a/view/SSTemplateParser.php.inc b/view/SSTemplateParser.php.inc index 75ccdaffa..68249b3ed 100644 --- a/view/SSTemplateParser.php.inc +++ b/view/SSTemplateParser.php.inc @@ -1062,13 +1062,27 @@ class SSTemplateParser extends Parser { // if not, put it after the tag to avoid IE glitches if(stripos($code, "]*("[^"]")*[^>]*>)/im', "$1\r\n", $code); + $code .= "\r\n" . '$val .= \'\';'; } elseif(stripos($code, "]*>)/i', "$1", $code); + $code = preg_replace_callback('/(.*)(]*>)(.*)/i', function($matches) use ($templateName) { + if (stripos($matches[3], '') !== false) { + // after this tag there is a comment close but no comment has been opened + // this most likely means that this 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: + return $matches[0]; + } else { + // all other cases, add the comment and return it + return "{$matches[1]}{$matches[2]}{$matches[3]}"; + } + }, $code); + $code = preg_replace('/(<\/html[^>]*>)/i', "$1", $code); } else { $code = str_replace('\';' . "\r\n", $code); + $code .= "\r\n" . '$val .= \'\';'; } - $code .= "\r\n" . '$val .= \'\';'; return $code; } From 809e0e547a1457abfdfcb9ec73c41c4de74035f5 Mon Sep 17 00:00:00 2001 From: Zauberfisch Date: Mon, 22 Apr 2013 18:17:31 +0000 Subject: [PATCH 4/4] MINOR: updated and extended Tests --- .../SSViewerTestCommentsFullSource.ss | 0 ...iewerTestCommentsFullSourceHTML4Doctype.ss | 6 + .../SSViewerTestCommentsFullSourceIfIE.ss | 7 + ...ewerTestCommentsFullSourceIfIENoDoctype.ss | 6 + ...SSViewerTestCommentsFullSourceNoDoctype.ss | 4 + .../SSViewerTestCommentsInclude.ss | 0 .../SSViewerTestCommentsPartialSource.ss | 0 .../SSViewerTestCommentsWithInclude.ss | 0 tests/view/SSViewerTest.php | 123 +++++++++++++----- 9 files changed, 112 insertions(+), 34 deletions(-) rename tests/templates/{ => SSViewerTestComments}/SSViewerTestCommentsFullSource.ss (100%) create mode 100644 tests/templates/SSViewerTestComments/SSViewerTestCommentsFullSourceHTML4Doctype.ss create mode 100644 tests/templates/SSViewerTestComments/SSViewerTestCommentsFullSourceIfIE.ss create mode 100644 tests/templates/SSViewerTestComments/SSViewerTestCommentsFullSourceIfIENoDoctype.ss create mode 100644 tests/templates/SSViewerTestComments/SSViewerTestCommentsFullSourceNoDoctype.ss rename tests/templates/{ => SSViewerTestComments}/SSViewerTestCommentsInclude.ss (100%) rename tests/templates/{ => SSViewerTestComments}/SSViewerTestCommentsPartialSource.ss (100%) rename tests/templates/{ => SSViewerTestComments}/SSViewerTestCommentsWithInclude.ss (100%) diff --git a/tests/templates/SSViewerTestCommentsFullSource.ss b/tests/templates/SSViewerTestComments/SSViewerTestCommentsFullSource.ss similarity index 100% rename from tests/templates/SSViewerTestCommentsFullSource.ss rename to tests/templates/SSViewerTestComments/SSViewerTestCommentsFullSource.ss diff --git a/tests/templates/SSViewerTestComments/SSViewerTestCommentsFullSourceHTML4Doctype.ss b/tests/templates/SSViewerTestComments/SSViewerTestCommentsFullSourceHTML4Doctype.ss new file mode 100644 index 000000000..ec38777ce --- /dev/null +++ b/tests/templates/SSViewerTestComments/SSViewerTestCommentsFullSourceHTML4Doctype.ss @@ -0,0 +1,6 @@ + + + + + diff --git a/tests/templates/SSViewerTestComments/SSViewerTestCommentsFullSourceIfIE.ss b/tests/templates/SSViewerTestComments/SSViewerTestCommentsFullSourceIfIE.ss new file mode 100644 index 000000000..676405f4a --- /dev/null +++ b/tests/templates/SSViewerTestComments/SSViewerTestCommentsFullSourceIfIE.ss @@ -0,0 +1,7 @@ + + + + + + + diff --git a/tests/templates/SSViewerTestComments/SSViewerTestCommentsFullSourceIfIENoDoctype.ss b/tests/templates/SSViewerTestComments/SSViewerTestCommentsFullSourceIfIENoDoctype.ss new file mode 100644 index 000000000..a34df2c3e --- /dev/null +++ b/tests/templates/SSViewerTestComments/SSViewerTestCommentsFullSourceIfIENoDoctype.ss @@ -0,0 +1,6 @@ + + + + + + diff --git a/tests/templates/SSViewerTestComments/SSViewerTestCommentsFullSourceNoDoctype.ss b/tests/templates/SSViewerTestComments/SSViewerTestCommentsFullSourceNoDoctype.ss new file mode 100644 index 000000000..2cb856a7f --- /dev/null +++ b/tests/templates/SSViewerTestComments/SSViewerTestCommentsFullSourceNoDoctype.ss @@ -0,0 +1,4 @@ + + + + diff --git a/tests/templates/SSViewerTestCommentsInclude.ss b/tests/templates/SSViewerTestComments/SSViewerTestCommentsInclude.ss similarity index 100% rename from tests/templates/SSViewerTestCommentsInclude.ss rename to tests/templates/SSViewerTestComments/SSViewerTestCommentsInclude.ss diff --git a/tests/templates/SSViewerTestCommentsPartialSource.ss b/tests/templates/SSViewerTestComments/SSViewerTestCommentsPartialSource.ss similarity index 100% rename from tests/templates/SSViewerTestCommentsPartialSource.ss rename to tests/templates/SSViewerTestComments/SSViewerTestCommentsPartialSource.ss diff --git a/tests/templates/SSViewerTestCommentsWithInclude.ss b/tests/templates/SSViewerTestComments/SSViewerTestCommentsWithInclude.ss similarity index 100% rename from tests/templates/SSViewerTestCommentsWithInclude.ss rename to tests/templates/SSViewerTestComments/SSViewerTestCommentsWithInclude.ss diff --git a/tests/view/SSViewerTest.php b/tests/view/SSViewerTest.php index a82ecee21..92b181b28 100644 --- a/tests/view/SSViewerTest.php +++ b/tests/view/SSViewerTest.php @@ -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 = ' - - - - -'; - $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); - + $f = FRAMEWORK_PATH . '/tests/templates/SSViewerTestComments'; + $templates = array( + array( + 'name' => 'SSViewerTestCommentsFullSource', + 'expected' => "" + . "" + . "" + . "" + . "\t" + . "\t" + . "" + . "", + ), + array( + 'name' => 'SSViewerTestCommentsFullSourceHTML4Doctype', + 'expected' => "" + . "" + . "" + . "" + . "\t" + . "\t" + . "" + . "", + ), + array( + 'name' => 'SSViewerTestCommentsFullSourceNoDoctype', + 'expected' => "" + . "" + . "\t" + . "\t" + . "", + ), + array( + 'name' => 'SSViewerTestCommentsFullSourceIfIE', + 'expected' => "" + . "" + . "" + . "" + . "" + . " " + . "\t" + . "\t" + . "" + . "", + ), + array( + 'name' => 'SSViewerTestCommentsFullSourceIfIENoDoctype', + 'expected' => "" + . "" + . "" + . " " + . "" + . " " + . "\t" + . "\t" + . "", + ), + array( + 'name' => 'SSViewerTestCommentsPartialSource', + 'expected' => + "" + . "
" + . "", + ), + array( + 'name' => 'SSViewerTestCommentsWithInclude', + 'expected' => + "" + . "
" + . "" + . "" + . "Included" + . "" + . "" + . "
" + . "", + ), + ); + 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());