View Issue Details

IDProjectCategoryView StatusLast Update
0001146mantisbtbugtrackerpublic2004-08-29 02:12
Reporterprescience Assigned Tothraxisp  
PrioritynormalSeverityminorReproducibilityN/A
Status closedResolutionfixed 
Fixed in Version0.19.0rc1 
Summary0001146: PRE tags in text
Description

The input system should deal with PRE tags better. Right now it does a double convert on the linefeeds ( through nl2br).

TagsNo tags attached.
Attached Files
pre.diff (890 bytes)   
Index: core/string_api.php
===================================================================
RCS file: /cvsroot/mantisbt/mantisbt/core/string_api.php,v
retrieving revision 1.55
diff -u -r1.55 string_api.php
--- core/string_api.php	22 Jul 2004 13:42:02 -0000	1.55
+++ core/string_api.php	30 Jul 2004 22:36:19 -0000
@@ -55,6 +55,15 @@
 		$p_string = string_preserve_spaces_at_bol( $p_string );
 		$p_string = nl2br( $p_string );
 
+		# fix up eols within <pre> tags (#1146)
+		preg_match_all("/<pre[^>]*?>(.|\n)*?<\/pre>/", $p_string, $pre1);
+		for ($x = 0; $x < count($pre1[0]); $x++) {
+			$pre2[$x] = preg_replace("/<br[^>]*?>/", "", $pre1[0][$x]);
+			$pre2[$x] = preg_replace("/([^\n]{".$wrap."})(?!<\/pre>)/", "$1\n", $pre2[$x]);
+			$pre1[0][$x] = "/" . preg_quote($pre1[0][$x], "/") . "/";
+		}
+		$p_string = preg_replace($pre1[0], $pre2, $p_string);
+
 		return $p_string;
 	}
 
pre.diff (890 bytes)   

Relationships

has duplicate 0001260 closedvboctor PRE tags should work 
has duplicate 0003656 closedgrangeway br tags incorrectly inserted inside preformatted text 
child of 0003987 closedvboctor Mantis 0.19.0 Release 

Activities

svenlat

svenlat

2002-07-03 02:16

reporter   ~0002930

Last edited: 2002-07-03 02:21

I'd like to second this! I tried a pre tag over multiple lines and ended up with a blank line inbetween each 'real' line (because both the newline and br tags cause a new line). When I edited the HTML so that it read something like 'this is line one(br)this is line two' (which isn't pretty in any case) the input system worked the first time - (IE the br tag worked) but whenever I went to edit whatever I had just posted it had removed the br tag!

FYI browser is IE4.0 (no chance of upgrading) :( and Mantis version is 0.17.3

edited on: 07-03 02:21

hacker

hacker

2004-04-08 12:17

reporter   ~0005357

This will fix it. Just replace the nl2br() call in core/string_api.php with the following function:

function nl2br_pre($string, $wrap = 80) {
$string = nl2br($string);

preg_match_all(&quot;/&lt;pre[^>]*?>(.|\n)*?&lt;\/pre>/&quot;, $string, $pre1);

for ($x = 0; $x &lt; count($pre1[0]); $x++) {
    $pre2[$x] = preg_replace(&quot;/&lt;br[^>]*?>/&quot;, &quot;&quot;, $pre1[0][$x]);
    $pre2[$x] = preg_replace(&quot;/([^\n]{&quot;.$wrap.&quot;})(?!&lt;\/pre>)/&quot;, &quot;$1\n&quot;, $pre2[$x]);
    $pre1[0][$x] = &quot;/&quot;.preg_quote($pre1[0][$x], &quot;/&quot;).&quot;/&quot;;
}
return preg_replace($pre1[0], $pre2, $string);

}

morganparry

morganparry

2004-04-14 10:52

reporter   ~0005381

Bug 3656, which I submitted a little while ago, is a duplicate of this. Sorry about that.

goemon1

goemon1

2004-04-19 15:07

reporter   ~0005391

hacker's patch works great.

would be nice if it were integrated into mantis...

morganparry

morganparry

2004-06-08 10:33

reporter   ~0005672

Last edited: 2004-06-08 10:38

That patch didn't work for me.

I used the search pattern

'|{pre}.*?{/pre}|is'

(where the { brackets are actually < brackets - sorry!)

instead and it was fine.

edited on: 06-08-04 10:38

virtuetek

virtuetek

2004-06-10 12:37

reporter   ~0005681

Last edited: 2004-06-10 12:39

When I use <b>hacker</b>'s function I get the following error:

<B>SYSTEM NOTICE: Undefined vasriable: pre2</b>

Where do I define this variable for usage?

NOTE: I placed the function in the top of the core\strings_api.php
Than called to it: $p_string = nl2br_pre( $p_string );

edited on: 06-10-04 12:39

hacker

hacker

2004-06-10 14:52

reporter   ~0005682

virtuetek, looks like you cut-and-pasted wrong. Check the code again, $pre2 is defined there.

virtuetek

virtuetek

2004-06-10 15:30

reporter   ~0005684

Last edited: 2004-06-12 23:18

<pre>
function nl2br_pre($string, $wrap = 80) {
$string = nl2br($string);

            preg_match_all(&quot;/&lt;pre[^>]*?>(.|\n)*?&lt;\/pre>/&quot;, $string, $pre1);

            for ($x = 0; $x &lt; count($pre1[0]); $x++) { 
                    $pre2[$x] = preg_replace(&quot;/&lt;br[^>]*?>/&quot;, &quot;&quot;, $pre1[0][$x]);
                    $pre2[$x] = preg_replace(&quot;/([^\n]{&quot;.$wrap.&quot;})(?!&lt;\/pre>)/&quot;, &quot;$1\n&quot;, $pre2[$x]);
                    $pre1[0][$x] = &quot;/&quot;.preg_quote($pre1[0][$x], &quot;/&quot;).&quot;/&quot;;
            }
            return preg_replace($pre1[0], $pre2, $string);
    }

</pre>

Looks ok to me.

<b>When I add a new variable to mantis....does it need to be pre-defined somewhere else in the code?
</b>
Thanks.

edited on: 06-12-04 23:18

thraxisp

thraxisp

2004-07-27 13:06

reporter   ~0006399

In fixing this, I noticed that string_display is used for virtually all text fields, including those (like title) that probably shouldn't have html in them in the first place.

Is this a concern?

grangeway

grangeway

2004-07-30 08:31

reporter   ~0006477

thraxisp - from your last comment, did you have a patch for this seperate to notes in the bugtracker?

thraxisp

thraxisp

2004-07-30 17:35

reporter   ~0006486

The attached patch is my suggested implementation.

vboctor

vboctor

2004-08-01 06:03

manager   ~0006521

I applied the patch uploaded by thraxisp after fixing a couple of notices.

  1. $pre2 was not defined if the loop was not entered.
  2. Defined $wrap as a parameter of the method and changed its name to $p_wrap.

Also I added this in a separate method called string_nl2br() which calls the standard nl2br(), then executes this custom code. Then changed string_display() to use string_nl2br().