diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index d7d81ac0e18..c287ddad528 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -3459,25 +3459,25 @@ function dol_nl2br($stringtoencode,$nl2brmode=0,$forxml=false) * 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. + * This function also remove last EOL or BR if $removelasteolbr=1 (default). * 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 + * is used to build PDF, nl2brmode must be 1. * * @param string $stringtoencode String to encode * @param int $nl2brmode 0=Adding br before \n, 1=Replacing \n by br (for use with FPDF writeHTMLCell function for example) * @param string $pagecodefrom Pagecode stringtoencode is encoded * @return string String encoded */ -function dol_htmlentitiesbr($stringtoencode,$nl2brmode=0,$pagecodefrom='UTF-8') +function dol_htmlentitiesbr($stringtoencode,$nl2brmode=0,$pagecodefrom='UTF-8',$removelasteolbr=1) { + $newstring=$stringtoencode; if (dol_textishtml($stringtoencode)) { - $newstring=$stringtoencode; $newstring=preg_replace('//i','
',$newstring); // Replace "
" by "
". It's same and avoid pb with FPDF. - $newstring=preg_replace('/
$/i','',$newstring); // Remove last
+ if ($removelasteolbr) $newstring=preg_replace('/
$/i','',$newstring); // Remove last
(remove only last one) $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__'=>'"')); @@ -3485,7 +3485,8 @@ function dol_htmlentitiesbr($stringtoencode,$nl2brmode=0,$pagecodefrom='UTF-8') } else { - $newstring=dol_nl2br(dol_htmlentities($stringtoencode,ENT_COMPAT,$pagecodefrom),$nl2brmode); + if ($removelasteolbr) $newstring=preg_replace('/(\r\n|\r|\n)$/i','',$newstring); // Remove last \n (may remove several) + $newstring=dol_nl2br(dol_htmlentities($newstring,ENT_COMPAT,$pagecodefrom),$nl2brmode); } // Other substitutions that htmlentities does not do //$newstring=str_replace(chr(128),'€',$newstring); // 128 = 0x80. Not in html entity table. // Seems useles with TCPDF. Make bug with UTF8 languages diff --git a/test/phpunit/FunctionsTest.php b/test/phpunit/FunctionsTest.php index 1fa41f0c460..c58da9efa38 100755 --- a/test/phpunit/FunctionsTest.php +++ b/test/phpunit/FunctionsTest.php @@ -192,15 +192,30 @@ class FunctionsTest extends PHPUnit_Framework_TestCase */ public function testDolHtmlCleanLastBr() { + $input="A string\n"; + $after=dol_htmlcleanlastbr($input); + $this->assertEquals("A string",$after); + + $input="A string first\nA string second\n"; + $after=dol_htmlcleanlastbr($input); + $this->assertEquals("A string first\nA string second",$after); + + $input="A string\n\n\n"; + $after=dol_htmlcleanlastbr($input); + $this->assertEquals("A string",$after); + $input="A string
"; $after=dol_htmlcleanlastbr($input); $this->assertEquals("A string",$after); + $input="A string first
\nA string second
"; $after=dol_htmlcleanlastbr($input); $this->assertEquals("A string first
\nA string second",$after); + $input="A string\n
\n"; $after=dol_htmlcleanlastbr($input); $this->assertEquals("A string",$after); + $input="A string\n

\n\n"; $after=dol_htmlcleanlastbr($input); $this->assertEquals("A string",$after); @@ -215,22 +230,44 @@ class FunctionsTest extends PHPUnit_Framework_TestCase */ public function testDolHtmlEntitiesBr() { - $input="A string\nwith a é, &, < and >."; // Text not already HTML + // Text not already HTML + + $input="A string\nwith a é, &, < and >."; $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 + $input="A string\nwith a é, &, < and >."; $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 + $input="A string\nwith a é, &, < and >.\n\n"; // With some \n at end that should be cleaned + $after=dol_htmlentitiesbr($input,0); // Add
before \n + $this->assertEquals("A string
\nwith a é, &, < and >.",$after); + + $input="A string\nwith a é, &, < and >.\n\n"; // With some \n at end that should be cleaned + $after=dol_htmlentitiesbr($input,1); // Replace \n with
+ $this->assertEquals("A string
with a é, &, < and >.",$after); + + // Text already HTML, so &,<,> should not be converted + + $input="A string
\nwith a é, &, < and >."; $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 + $input="
  • \nA string with a é, &, < and >.
  • \nAnother string"; $after=dol_htmlentitiesbr($input); $this->assertEquals("
  • \nA string with a é, &, < and >.
  • \nAnother string",$after); + $input="A string
    \nwith a é, &, < and >.
    "; // With some
    at end that should be cleaned + $after=dol_htmlentitiesbr($input); + $this->assertEquals("A string
    \nwith a é, &, < and >.",$after); + + $input="
  • \nA string with a é, &, < and >.
  • \nAnother string
    "; // With some
    at end that should be cleaned + $after=dol_htmlentitiesbr($input); + $this->assertEquals("
  • \nA string with a é, &, < and >.
  • \nAnother string",$after); + + // TODO Add test with param $removelasteolbr = 0 + return true; }