|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
c960657 Smarty Regular
Joined: 07 May 2003 Posts: 75 Location: Copenhagen, Denmark
|
Posted: Tue Jul 27, 2004 1:02 pm Post subject: {strip} does not work with DOS/Mac newlines |
|
|
I have noticed some oddities with {strip}.
When using DOS newlines (\r\n, CR LF) in the template file, trailing spaces on a line are not stripped.
When using Mac newlines (\r, CR) in the template file, nothing is stripped at all (I think).
Only with Unix newlines (\n, LF) {strip} works as expected.
I think support for DOS newlines and perhaps also Mac newlines would be convenient. |
|
Back to top |
|
c960657 Smarty Regular
Joined: 07 May 2003 Posts: 75 Location: Copenhagen, Denmark
|
Posted: Mon Aug 09, 2004 11:58 am Post subject: |
|
|
The following change to Smarty_Compiler.class.php should make CR and CR LF behave like LF:
Code: |
@@ -324,8 +324,9 @@
/* Reformat data between 'strip' and '/strip' tags, removing spaces, tabs and newlines. */
if (preg_match_all("!{$ldq}strip{$rdq}.*?{$ldq}/strip{$rdq}!s", $compiled_content, $_match)) {
$strip_tags = $_match[0];
- $strip_tags_modified = preg_replace("!{$ldq}/?strip{$rdq}|[\t ]+$|^[\t ]+!m", '', $strip_tags);
- $strip_tags_modified = preg_replace('![\r\n]+!m', '', $strip_tags_modified);
+ $strip_tags_modified = str_replace("\r", "\n", $strip_tags);
+ $strip_tags_modified = preg_replace("!{$ldq}/?strip{$rdq}|[\t ]+$|^[\t ]+!m", '', $strip_tags_modified);
+ $strip_tags_modified = str_replace("\n", '', $strip_tags_modified);
for ($i = 0, $for_max = count($strip_tags); $i < $for_max; $i++)
$compiled_content = preg_replace("!{$ldq}strip{$rdq}.*?{$ldq}/strip{$rdq}!s",
$this->_quote_replace($strip_tags_modified[$i]),
|
|
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Sat Aug 14, 2004 8:51 pm Post subject: |
|
|
i looked into that. i think the reason why dos and mac newlines aren't handled correctly is the "m"-modifier (multiline) in the preg_replace() call.
the multiline modifier (which tells php to apply to each line of the string, not globally) looks for os dependend newlines.
that means strip would work with windows-newlines when smarty runs on a windows-php. (i didn't test that, though.)
i agree that it is desirable to handle newlines from foreign systems.
i tested your patch and it looks fine to me. nontheless i digged a bit further and think was able to simplify strip's logic a little more (especially get rid of the for-loop)
here is my patch
Code: | Index: Smarty_Compiler.class.php
===================================================================
RCS file: /repository/smarty/libs/Smarty_Compiler.class.php,v
retrieving revision 1.331
diff -u -r1.331 Smarty_Compiler.class.php
--- Smarty_Compiler.class.php 13 Aug 2004 13:32:27 -0000 1.331
+++ Smarty_Compiler.class.php 14 Aug 2004 20:48:03 -0000
@@ -324,12 +324,12 @@
/* Reformat data between 'strip' and '/strip' tags, removing spaces, tabs and newlines. */
if (preg_match_all("!{$ldq}strip{$rdq}.*?{$ldq}/strip{$rdq}!s", $compiled_content, $_match)) {
$strip_tags = $_match[0];
- $strip_tags_modified = preg_replace("!{$ldq}/?strip{$rdq}|[\t ]+$|^[\t ]+!m", '', $strip_tags);
- $strip_tags_modified = preg_replace('![\r\n]+!m', '', $strip_tags_modified);
- for ($i = 0, $for_max = count($strip_tags); $i < $for_max; $i++)
- $compiled_content = preg_replace("!{$ldq}strip{$rdq}.*?{$ldq}/strip{$rdq}!s",
- $this->_quote_replace($strip_tags_modified[$i]),
- $compiled_content, 1);
+ /* sort by length to avoid duplicate replacement */
+ usort($strip_tags, '_smarty_sort_length');
+ /* remove all leading and trailing whitespace characters and all newlines */
+ $strip_tags_modified = preg_replace("!\s*[\r\n]+\s*|^{$ldq}strip{$rdq}\s*|\s*{$ldq}/strip{$rdq}$!s", '', $strip_tags);
+ /* merge stripped blocks into the compiled content */
+ $compiled_content = str_replace($strip_tags, $strip_tags_modified, $compiled_content);
}
// remove \n from the end of the file, if any
|
what do you think about it?
greetings
messju |
|
Back to top |
|
c960657 Smarty Regular
Joined: 07 May 2003 Posts: 75 Location: Copenhagen, Denmark
|
Posted: Tue Aug 17, 2004 7:02 am Post subject: |
|
|
messju wrote: | what do you think about it? |
Looks good to me, though my understanding of the parsing code is limited. |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|