Try to fix a lot of regression. Add also more phpunit test to avoid them in future.

This commit is contained in:
Laurent Destailleur
2011-05-01 10:24:46 +00:00
parent 0b82d5f4a3
commit 69667f07d0
2 changed files with 51 additions and 21 deletions

View File

@@ -3064,36 +3064,42 @@ function dol_nl2br($stringtoencode,$nl2brmode=0,$forxml=false)
} }
else else
{ {
$ret=preg_replace('/(<br>|<br\s*\/>|<br\/>)\s*(\r\n|\r|\n)+/i',($forxml?'<br />':'<br>'),$stringtoencode); $ret=str_replace("\r","",$stringtoencode);
$ret=preg_replace('/([^<li\s*>])+(\r\n|\r|\n)+/i',($forxml?'<br />':'<br>'),$ret); $ret=str_replace("\n",($forxml?'<br />':'<br>'),$ret);
//$ret=preg_replace('/(<br>|<br\s*\/>|<br\/>)\s*(\r\n|\r|\n)+/i',($forxml?'<br />':'<br>'),$stringtoencode);
//$ret=preg_replace('/([^<li\s*>]+)(\r\n|\r|\n)+/i',($forxml?'$1<br />':'$1<br>'),$ret);
return $ret; return $ret;
} }
} }
/** /**
* \brief This function is called to encode a string into a HTML string but differs from htmlentities because * 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 * all entities but &,<,> are converted. This permits to encode special chars to entities with no double
* encoding for already encoded HTML strings. * encoding for already encoded HTML strings.
* This function also remove last CR/BR. * This function also remove last CR/BR.
* \param stringtoencode String to encode * For PDF usage, you can show text by 2 ways:
* \param nl2brmode 0=Adding br before \n, 1=Replacing \n by br (for use with FPDF writeHTMLCell function for example) * - writeHTMLCell -> param must be encoded into HTML.
* \remarks For PDF usage, you can show text by 2 ways: * - MultiCell -> param must not be encoded into HTML.
* - writeHTMLCell -> param must be encoded into HTML. * Because writeHTMLCell convert also \n into <br>, if function
* - MultiCell -> param must not be encoded into HTML. * is used to build PDF, nl2brmode must be 1.
* Because writeHTMLCell convert also \n into <br>, if function * @param stringtoencode String to encode
* is used to build PDF, nl2brmode must be 1. * @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') function dol_htmlentitiesbr($stringtoencode,$nl2brmode=0,$pagecodefrom='UTF-8')
{ {
if (dol_textishtml($stringtoencode)) if (dol_textishtml($stringtoencode))
{ {
$newstring=preg_replace('/([^<li\s*>])+(\r\n|\r|\n)+/i',($forxml?'<br />':'<br>'),$stringtoencode); // Don't replace if in list $newstring=$stringtoencode;
$newstring=preg_replace('/<br(\s[\sa-zA-Z_="]*)?\/?>/i','<br>',$newstring); // Replace "<br type="_moz" />" by "<br>". It's same and avoid pb with FPDF. //$newstring=preg_replace('/([^<li\s*>]+)(\r\n|\r|\n)+/i',($forxml?'$1<br />':'$1<br>'),$stringtoencode); // Don't replace if in list
$newstring=preg_replace('/<br>$/i','',$newstring); // Replace "<br type="_moz" />" by "<br>". It's same and avoid pb with FPDF. //$newstring=preg_replace('/<li\s*>(\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?'<br />':'<br>'),$newstring); // If already HTML, CR should be <br> so we don't change \n
$newstring=preg_replace('/<br(\s[\sa-zA-Z_="]*)?\/?>/i','<br>',$newstring); // Replace "<br type="_moz" />" by "<br>". It's same and avoid pb with FPDF.
$newstring=preg_replace('/<br>$/i','',$newstring); // Remove last <br>
$newstring=strtr($newstring,array('&'=>'__and__','<'=>'__lt__','>'=>'__gt__','"'=>'__dquot__')); $newstring=strtr($newstring,array('&'=>'__and__','<'=>'__lt__','>'=>'__gt__','"'=>'__dquot__'));
$newstring=dol_htmlentities($newstring,ENT_COMPAT,$pagecodefrom); // Make entity encoding $newstring=dol_htmlentities($newstring,ENT_COMPAT,$pagecodefrom); // Make entity encoding
$newstring=strtr($newstring,array('__and__'=>'&','__lt__'=>'<','__gt__'=>'>','__dquot__'=>'"')); $newstring=strtr($newstring,array('__and__'=>'&','__lt__'=>'<','__gt__'=>'>','__dquot__'=>'"'));
// If already HTML, CR should be <br> so we don't change \n //$newstring=strtr($newstring,array('__li__'=>"<li>\n")); // Restore <li>\n
} }
else { else {
$newstring=dol_nl2br(dol_htmlentities($stringtoencode,ENT_COMPAT,$pagecodefrom),$nl2brmode); $newstring=dol_nl2br(dol_htmlentities($stringtoencode,ENT_COMPAT,$pagecodefrom),$nl2brmode);

View File

@@ -115,19 +115,43 @@ class FunctionsTest extends PHPUnit_Framework_TestCase
{ {
$input="A string<br>"; $input="A string<br>";
$after=dol_htmlcleanlastbr($input); $after=dol_htmlcleanlastbr($input);
$this->assertEquals($after,"A string"); $this->assertEquals("A string",$after);
$input="A string first<br>\nA string second<br>"; $input="A string first<br>\nA string second<br>";
$after=dol_htmlcleanlastbr($input); $after=dol_htmlcleanlastbr($input);
$this->assertEquals($after,"A string first<br>\nA string second"); $this->assertEquals("A string first<br>\nA string second",$after);
$input="A string\n<br type=\"_moz\" />\n"; $input="A string\n<br type=\"_moz\" />\n";
$after=dol_htmlcleanlastbr($input); $after=dol_htmlcleanlastbr($input);
$this->assertEquals($after,"A string"); $this->assertEquals("A string",$after);
$input="A string\n<br><br />\n\n"; $input="A string\n<br><br />\n\n";
$after=dol_htmlcleanlastbr($input); $after=dol_htmlcleanlastbr($input);
$this->assertEquals($after,"A string"); $this->assertEquals("A string",$after);
return true; return true;
} }
/**
*/
public function testHtmlEntitiesBr()
{
$input="A string\nwith a é, &, < and >."; // Text not already HTML
$after=dol_htmlentitiesbr($input,0); // Add <br> before \n
$this->assertEquals("A string<br>\nwith a &eacute;, &amp;, &lt; and &gt;.",$after);
$input="A string\nwith a é, &, < and >."; // Text not already HTML
$after=dol_htmlentitiesbr($input,1); // Replace \n with <br>
$this->assertEquals("A string<br>with a &eacute;, &amp;, &lt; and &gt;.",$after);
$input="A string<br>\nwith a é, &, < and >."; // Text already HTML, so &,<,> should not be converted
$after=dol_htmlentitiesbr($input);
$this->assertEquals("A string<br>\nwith a &eacute;, &, < and >.",$after);
$input="<li>\nA string with a é, &, < and >.</li>\nAnother string"; // Text already HTML, so &,<,> should not be converted
$after=dol_htmlentitiesbr($input);
$this->assertEquals("<li>\nA string with a &eacute;, &, < and >.</li>\nAnother string",$after);
return true;
}
} }
?> ?>