From 69667f07d07fff69673e188d2589c196a2390899 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 1 May 2011 10:24:46 +0000 Subject: [PATCH] Try to fix a lot of regression. Add also more phpunit test to avoid them in future. --- htdocs/lib/functions.lib.php | 40 +++++++++++++++++++--------------- test/phpunit/FunctionsTest.php | 32 +++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 21 deletions(-) diff --git a/htdocs/lib/functions.lib.php b/htdocs/lib/functions.lib.php index 75182127803..52d3d30ea9b 100644 --- a/htdocs/lib/functions.lib.php +++ b/htdocs/lib/functions.lib.php @@ -3064,36 +3064,42 @@ function dol_nl2br($stringtoencode,$nl2brmode=0,$forxml=false) } else { - $ret=preg_replace('/(
||)\s*(\r\n|\r|\n)+/i',($forxml?'
':'
'),$stringtoencode); - $ret=preg_replace('/([^])+(\r\n|\r|\n)+/i',($forxml?'
':'
'),$ret); + $ret=str_replace("\r","",$stringtoencode); + $ret=str_replace("\n",($forxml?'
':'
'),$ret); + //$ret=preg_replace('/(
||)\s*(\r\n|\r|\n)+/i',($forxml?'
':'
'),$stringtoencode); + //$ret=preg_replace('/([^]+)(\r\n|\r|\n)+/i',($forxml?'$1
':'$1
'),$ret); return $ret; } } /** - * \brief This function is called to encode a string into a HTML string but differs from htmlentities because - * all entities but &,<,> are converted. This permits to encode special chars to entities with no double - * encoding for already encoded HTML strings. - * This function also remove last CR/BR. - * \param stringtoencode String to encode - * \param nl2brmode 0=Adding br before \n, 1=Replacing \n by br (for use with FPDF writeHTMLCell function for example) - * \remarks For PDF usage, you can show text by 2 ways: - * - writeHTMLCell -> param must be encoded into HTML. - * - MultiCell -> param must not be encoded into HTML. - * Because writeHTMLCell convert also \n into
, if function - * is used to build PDF, nl2brmode must be 1. + * This function is called to encode a string into a HTML string but differs from htmlentities because + * all entities but &,<,> are converted. This permits to encode special chars to entities with no double + * encoding for already encoded HTML strings. + * This function also remove last CR/BR. + * For PDF usage, you can show text by 2 ways: + * - writeHTMLCell -> param must be encoded into HTML. + * - MultiCell -> param must not be encoded into HTML. + * Because writeHTMLCell convert also \n into
, if function + * is used to build PDF, nl2brmode must be 1. + * @param stringtoencode String to encode + * @param nl2brmode 0=Adding br before \n, 1=Replacing \n by br (for use with FPDF writeHTMLCell function for example) + * @param pagecodefrom Pagecode stringtoencode is encoded */ function dol_htmlentitiesbr($stringtoencode,$nl2brmode=0,$pagecodefrom='UTF-8') { if (dol_textishtml($stringtoencode)) { - $newstring=preg_replace('/([^])+(\r\n|\r|\n)+/i',($forxml?'
':'
'),$stringtoencode); // Don't replace if in list - $newstring=preg_replace('//i','
',$newstring); // Replace "
" by "
". It's same and avoid pb with FPDF. - $newstring=preg_replace('/
$/i','',$newstring); // Replace "
" by "
". It's same and avoid pb with FPDF. + $newstring=$stringtoencode; + //$newstring=preg_replace('/([^]+)(\r\n|\r|\n)+/i',($forxml?'$1
':'$1
'),$stringtoencode); // Don't replace if in list + //$newstring=preg_replace('/(\r\n|\r|\n)+/','__li__',$newstring); // Don't replace if \n is just after a li + //$newstring=preg_replace('/(\r\n|\r|\n)+/i',($forxml?'
':'
'),$newstring); // If already HTML, CR should be
so we don't change \n + $newstring=preg_replace('//i','
',$newstring); // Replace "
" by "
". It's same and avoid pb with FPDF. + $newstring=preg_replace('/
$/i','',$newstring); // Remove last
$newstring=strtr($newstring,array('&'=>'__and__','<'=>'__lt__','>'=>'__gt__','"'=>'__dquot__')); $newstring=dol_htmlentities($newstring,ENT_COMPAT,$pagecodefrom); // Make entity encoding $newstring=strtr($newstring,array('__and__'=>'&','__lt__'=>'<','__gt__'=>'>','__dquot__'=>'"')); - // If already HTML, CR should be
so we don't change \n + //$newstring=strtr($newstring,array('__li__'=>"
  • \n")); // Restore
  • \n } else { $newstring=dol_nl2br(dol_htmlentities($stringtoencode,ENT_COMPAT,$pagecodefrom),$nl2brmode); diff --git a/test/phpunit/FunctionsTest.php b/test/phpunit/FunctionsTest.php index d8f95c67b0b..42fb460d444 100755 --- a/test/phpunit/FunctionsTest.php +++ b/test/phpunit/FunctionsTest.php @@ -115,19 +115,43 @@ class FunctionsTest extends PHPUnit_Framework_TestCase { $input="A string
    "; $after=dol_htmlcleanlastbr($input); - $this->assertEquals($after,"A string"); + $this->assertEquals("A string",$after); $input="A string first
    \nA string second
    "; $after=dol_htmlcleanlastbr($input); - $this->assertEquals($after,"A string first
    \nA string second"); + $this->assertEquals("A string first
    \nA string second",$after); $input="A string\n
    \n"; $after=dol_htmlcleanlastbr($input); - $this->assertEquals($after,"A string"); + $this->assertEquals("A string",$after); $input="A string\n

    \n\n"; $after=dol_htmlcleanlastbr($input); - $this->assertEquals($after,"A string"); + $this->assertEquals("A string",$after); return true; } + /** + */ + public function testHtmlEntitiesBr() + { + $input="A string\nwith a é, &, < and >."; // Text not already HTML + $after=dol_htmlentitiesbr($input,0); // Add
    before \n + $this->assertEquals("A string
    \nwith a é, &, < and >.",$after); + + $input="A string\nwith a é, &, < and >."; // Text not already HTML + $after=dol_htmlentitiesbr($input,1); // Replace \n with
    + $this->assertEquals("A string
    with a é, &, < and >.",$after); + + $input="A string
    \nwith a é, &, < and >."; // Text already HTML, so &,<,> should not be converted + $after=dol_htmlentitiesbr($input); + $this->assertEquals("A string
    \nwith a é, &, < and >.",$after); + + $input="
  • \nA string with a é, &, < and >.
  • \nAnother string"; // Text already HTML, so &,<,> should not be converted + $after=dol_htmlentitiesbr($input); + $this->assertEquals("
  • \nA string with a é, &, < and >.
  • \nAnother string",$after); + + return true; + } + + } ?> \ No newline at end of file