Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
{strip} tag bug

 
Post new topic   Reply to topic    Smarty Forum Index -> Bugs
View previous topic :: View next topic  
Author Message
danberlyoung
Smarty n00b


Joined: 07 Dec 2005
Posts: 1

PostPosted: Wed Dec 07, 2005 7:38 pm    Post subject: {strip} tag bug Reply with quote

The {strip} form tag seems to have a bug in it. When a tag has its attributes seperated with new line characters to spread the tag out over several lines, {strip} takes out all white space and mashes the attributes together. For example:
Code:

<foo attr1="one"
     attr2="two"
     attr3="three" >

... becomes ...
Code:

<foo attr1="one"attr2="two"attr3="three" >

... it should be ...
Code:

<foo attr1="one" attr2="two" attr3="three" >

This is with Smarty 2.6.9 on PHP 4.3.11 on Mac OS X Server.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7265
Location: Lincoln Nebraska, USA

PostPosted: Wed Dec 07, 2005 9:21 pm    Post subject: Reply with quote

That is how {strip} behaves. You do not want to put {strip} tags around HTML tags that span several lines.
Back to top
View user's profile Send private message Visit poster's website
rob.desbois
Smarty Rookie


Joined: 25 Jan 2006
Posts: 10

PostPosted: Wed Apr 19, 2006 10:50 am    Post subject: Reply with quote

This foxed me for a bit too - obviously {strip} also does the same to lines of text running over several lines.
A possible suggested solution would be to take advantage of wordwrap in your editor / IDE however that isn't something I want to do.

My solution was the following: in Smarty_Compiler.php find the line that looks like:
<pre> $text_blocks[$j] = preg_replace('![\t ]*[\r\n]+[\t ]*!', '', $text_blocks[$j]);</pre>
(Note: this is from Smarty 2.6.10, and is line 335 in that version)

in the preg_replace call, modify the second argument from '' to ' ' - that will collapse all trailing whitespace,newline characters, and leading whitespace to a single space character. Result: no concatenated words or attributes.
Back to top
View user's profile Send private message
rob.desbois
Smarty Rookie


Joined: 25 Jan 2006
Posts: 10

PostPosted: Wed Apr 19, 2006 10:53 am    Post subject: Reply with quote

Mohrt: what is the reasoning behind {strip} performing the way it does? What is its originally specified purpose / speculated usage.
Given my hack above, granted it reduces the removal of unnecessary whitespace as *all* EOLs result in a single character, but this seems far more useful to me than otherwise - given that HTML often contains long sections of prose that may be split over several lines in source.

Do most people program with word-wrap on?
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5613
Location: Toronto, Canada

PostPosted: Wed Apr 19, 2006 4:49 pm    Post subject: Reply with quote

Actually, I think this is probably fairly good solution. It does have one drawback in that it will break current scripts that rely on all whitespace to be eaten. For example, IE will render <a...><img...> differently than <a...> <img...> (where the second form has the space).
Back to top
View user's profile Send private message
rob.desbois
Smarty Rookie


Joined: 25 Jan 2006
Posts: 10

PostPosted: Thu Apr 20, 2006 8:30 am    Post subject: Reply with quote

Hmmm that's a pain in the backside...without making it more complex and collapsing all WS if contained in start/end tags I think it'll gave to stay this way. Out of interest, do you know if:
a) this would work if <a...> and <img...> were separated by a newline instead of a space? I assume not, as different types of WS in HTML should be treated generically, but hey, this is I.E.
b) is this an I.E. bug?

My main purpose for using {strip} is simply so that I can fully indent my HTML source which I find extremely useful for editing -- the strip then removes all that WS, effectively compressing it - and usually to about 40% of size!
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5613
Location: Toronto, Canada

PostPosted: Thu Apr 20, 2006 5:29 pm    Post subject: Reply with quote

rob.desbois wrote:
a) this would work if <a...> and <img...> were separated by a newline instead of a space? I assume not, as different types of WS in HTML should be treated generically, but hey, this is I.E.
b) is this an I.E. bug?


a) they are the same
b) I don't know. I suppose the 'official' way is to use a & nbsp; but there is a perverse logic to the way it works. *shrug*

rob.desbois wrote:
My main purpose for using {strip} is simply so that I can fully indent my HTML source which I find extremely useful for editing -- the strip then removes all that WS, effectively compressing it - and usually to about 40% of size!


Have you tried the strip whitespace output filter?
Back to top
View user's profile Send private message
rob.desbois
Smarty Rookie


Joined: 25 Jan 2006
Posts: 10

PostPosted: Mon Apr 24, 2006 11:36 am    Post subject: Reply with quote

Hmmm I hadn't - TBH I haven't looked at Smarty's filtering capabilities.

Am I right in thinking {strip} is performed during compilation? Would there be any benefit in using a prefilter (which I'd use over output filter) to do this? Other than not having to hack Smarty's codebase Wink
Back to top
View user's profile Send private message
mistic100
Smarty n00b


Joined: 12 May 2011
Posts: 3

PostPosted: Sat Oct 12, 2013 11:28 am    Post subject: Reply with quote

if someone needs the same modifications, here the the diff for Smarty 3

Code:
Index: distribution/libs/sysplugins/smarty_internal_templateparser.php
===================================================================
--- distribution/libs/sysplugins/smarty_internal_templateparser.php   (revision 4788)
+++ distribution/libs/sysplugins/smarty_internal_templateparser.php   (working copy)
@@ -2260,7 +2260,7 @@
 #line 225 "smarty_internal_templateparser.y"
     function yy_r11(){
     if ($this->strip) {
-        $this->_retvalue = new _smarty_text($this, preg_replace('![\t ]*[\r\n]+[\t ]*!', '', self::escape_start_tag($this->yystack[$this->yyidx + 0]->minor)));
+        $this->_retvalue = new _smarty_text($this, preg_replace('![\t ]*[\r\n]+[\t ]*!', ' ', self::escape_start_tag($this->yystack[$this->yyidx + 0]->minor)));
     } else {
         $this->_retvalue = new _smarty_text($this, self::escape_start_tag($this->yystack[$this->yyidx + 0]->minor)); 
     }
@@ -2278,7 +2278,7 @@
 #line 243 "smarty_internal_templateparser.y"
     function yy_r13(){
         if ($this->strip) {
-            $this->_retvalue = new _smarty_text($this, preg_replace('![\t ]*[\r\n]+[\t ]*!', '', $this->yystack[$this->yyidx + 0]->minor));
+            $this->_retvalue = new _smarty_text($this, preg_replace('![\t ]*[\r\n]+[\t ]*!', ' ', $this->yystack[$this->yyidx + 0]->minor));
         } else {
             $this->_retvalue = new _smarty_text($this, $this->yystack[$this->yyidx + 0]->minor);
         }

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Bugs All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP