2
0
forked from Wavyzz/dolibarr

Fix dol_trunc

This commit is contained in:
Laurent Destailleur
2017-06-30 18:30:01 +02:00
parent d55213286e
commit b308cfef1d
2 changed files with 46 additions and 29 deletions

View File

@@ -2243,12 +2243,12 @@ function dol_print_graph($htmlid,$width,$height,$data,$showlegend=0,$type='pie',
* MAIN_DISABLE_TRUNC=1 can disable all truncings * MAIN_DISABLE_TRUNC=1 can disable all truncings
* *
* @param string $string String to truncate * @param string $string String to truncate
* @param int $size Max string size visible. 0 for no limit. Final string size can be 1 more (if size was max+1) or 3 more (if we added ...) * @param int $size Max string size visible (excluding ...). 0 for no limit. WARNING: Final string size can have 3 more chars (if we added ..., or if size was max+1 or max+2 or max+3 so it does not worse to replace with ...)
* @param string $trunc Where to trunc: right, left, middle (size must be a 2 power), wrap * @param string $trunc Where to trunc: right, left, middle (size must be a 2 power), wrap
* @param string $stringencoding Tell what is source string encoding * @param string $stringencoding Tell what is source string encoding
* @param int $nodot Truncation do not add ... after truncation. So it's an exact truncation. * @param int $nodot Truncation do not add ... after truncation. So it's an exact truncation.
* @param int $display Trunc is use to display and can be changed for small screen. TODO Remove this param (must be dealt with CSS) * @param int $display Trunc is use to display and can be changed for small screen. TODO Remove this param (must be dealt with CSS)
* @return string Truncated string * @return string Truncated string. WARNING: length is never higher than $size if $nodot is set, but can be 3 chars higher otherwise.
*/ */
function dol_trunc($string,$size=40,$trunc='right',$stringencoding='UTF-8',$nodot=0, $display=0) function dol_trunc($string,$size=40,$trunc='right',$stringencoding='UTF-8',$nodot=0, $display=0)
{ {
@@ -2264,9 +2264,10 @@ function dol_trunc($string,$size=40,$trunc='right',$stringencoding='UTF-8',$nodo
if ($trunc == 'right') if ($trunc == 'right')
{ {
$newstring=dol_textishtml($string)?dol_string_nohtmltag($string,1):$string; $newstring=dol_textishtml($string)?dol_string_nohtmltag($string,1):$string;
if (dol_strlen($newstring,$stringencoding) > ($size+($nodot?0:1))) if (dol_strlen($newstring,$stringencoding) > ($size+($nodot?0:3))) // If nodot is 0 and size is 1,2 or 3 chars more, we don't trunc and don't add ...
return dol_substr($newstring,0,$size,$stringencoding).($nodot?'':'...'); return dol_substr($newstring,0,$size,$stringencoding).($nodot?'':'...');
else else
//return 'u'.$size.'-'.$newstring.'-'.dol_strlen($newstring,$stringencoding).'-'.$string;
return $string; return $string;
} }
elseif ($trunc == 'middle') elseif ($trunc == 'middle')
@@ -2284,7 +2285,7 @@ function dol_trunc($string,$size=40,$trunc='right',$stringencoding='UTF-8',$nodo
elseif ($trunc == 'left') elseif ($trunc == 'left')
{ {
$newstring=dol_textishtml($string)?dol_string_nohtmltag($string,1):$string; $newstring=dol_textishtml($string)?dol_string_nohtmltag($string,1):$string;
if (dol_strlen($newstring,$stringencoding) > ($size+1)) if (dol_strlen($newstring,$stringencoding) > ($size+($nodot?0:3))) // If nodot is 0 and size is 1,2 or 3 chars more, we don't trunc and don't add ...
return '...'.dol_substr($newstring,dol_strlen($newstring,$stringencoding) - $size,$size,$stringencoding); return '...'.dol_substr($newstring,dol_strlen($newstring,$stringencoding) - $size,$size,$stringencoding);
else else
return $string; return $string;

View File

@@ -130,21 +130,21 @@ class FunctionsLibTest extends PHPUnit_Framework_TestCase
/*$tmp=dol_buildpath('/google/oauth2callback.php', 0); /*$tmp=dol_buildpath('/google/oauth2callback.php', 0);
var_dump($tmp); var_dump($tmp);
*/ */
/*$tmp=dol_buildpath('/google/oauth2callback.php', 1); /*$tmp=dol_buildpath('/google/oauth2callback.php', 1);
var_dump($tmp); var_dump($tmp);
*/ */
$result=dol_buildpath('/google/oauth2callback.php', 2); $result=dol_buildpath('/google/oauth2callback.php', 2);
print __METHOD__." result=".$result."\n"; print __METHOD__." result=".$result."\n";
$this->assertStringStartsWith('http', $result); $this->assertStringStartsWith('http', $result);
$result=dol_buildpath('/google/oauth2callback.php', 3); $result=dol_buildpath('/google/oauth2callback.php', 3);
print __METHOD__." result=".$result."\n"; print __METHOD__." result=".$result."\n";
$this->assertStringStartsWith('http', $result); $this->assertStringStartsWith('http', $result);
} }
/** /**
* testGetBrowserInfo * testGetBrowserInfo
* *
@@ -502,32 +502,48 @@ class FunctionsLibTest extends PHPUnit_Framework_TestCase
// Default trunc (will add ... if truncation truncation or keep last char if only one char) // Default trunc (will add ... if truncation truncation or keep last char if only one char)
$input="éeéeéeàa"; $input="éeéeéeàa";
$after=dol_trunc($input,3); $after=dol_trunc($input,3);
$this->assertEquals("éeé...",$after); $this->assertEquals("éeé...",$after,'Test A1');
$after=dol_trunc($input,2); $after=dol_trunc($input,2);
$this->assertEquals("ée...",$after); $this->assertEquals("ée...",$after,'Test A2');
$after=dol_trunc($input,1);
$this->assertEquals("é...",$after,'Test A3');
$input="éeéeé";
$after=dol_trunc($input,3);
$this->assertEquals("éeéeé",$after,'Test B1');
$after=dol_trunc($input,2);
$this->assertEquals("éeéeé",$after,'Test B2');
$after=dol_trunc($input,1);
$this->assertEquals("é...",$after,'Test B3');
$input="éeée";
$after=dol_trunc($input,3);
$this->assertEquals("éeée",$after,'Test C1');
$after=dol_trunc($input,2);
$this->assertEquals("éeée",$after,'Test C2');
$after=dol_trunc($input,1);
$this->assertEquals("éeée",$after,'Test C3');
$input="éeé"; $input="éeé";
$after=dol_trunc($input,3); $after=dol_trunc($input,3);
$this->assertEquals("éeé",$after); $this->assertEquals("éeé",$after,'Test C');
$after=dol_trunc($input,2); $after=dol_trunc($input,2);
$this->assertEquals("éeé",$after); $this->assertEquals("éeé",$after,'Test D');
$after=dol_trunc($input,1); $after=dol_trunc($input,1);
$this->assertEquals("é...",$after); $this->assertEquals("é",$after,'Test E');
// Trunc with no ... // Trunc with no ...
$input="éeéeéeàa"; $input="éeéeéeàa";
$after=dol_trunc($input,3,'right','UTF-8',1); $after=dol_trunc($input,3,'right','UTF-8',1);
$this->assertEquals("éeé",$after); $this->assertEquals("éeé",$after,'Test F');
$after=dol_trunc($input,2,'right','UTF-8',1); $after=dol_trunc($input,2,'right','UTF-8',1);
$this->assertEquals("ée",$after); $this->assertEquals("ée",$after,'Test G');
$input="éeé"; $input="éeé";
$after=dol_trunc($input,3,'right','UTF-8',1); $after=dol_trunc($input,3,'right','UTF-8',1);
$this->assertEquals("éeé",$after); $this->assertEquals("éeé",$after,'Test H');
$after=dol_trunc($input,2,'right','UTF-8',1); $after=dol_trunc($input,2,'right','UTF-8',1);
$this->assertEquals("ée",$after); $this->assertEquals("ée",$after,'Test I');
$after=dol_trunc($input,1,'right','UTF-8',1); $after=dol_trunc($input,1,'right','UTF-8',1);
$this->assertEquals("é",$after); $this->assertEquals("é",$after,'Test J');
$input="éeéeéeàa"; $input="éeéeéeàa";
$after=dol_trunc($input,4,'middle'); $after=dol_trunc($input,4,'middle');
$this->assertEquals("ée...àa",$after); $this->assertEquals("ée...àa",$after,'Test K');
return true; return true;
} }
@@ -658,7 +674,7 @@ class FunctionsLibTest extends PHPUnit_Framework_TestCase
$this->assertEquals("21 jump street\nMyTown, MyState, 99999",$address); $this->assertEquals("21 jump street\nMyTown, MyState, 99999",$address);
} }
/** /**
* testDolFormatAddress * testDolFormatAddress
* *
@@ -671,18 +687,18 @@ class FunctionsLibTest extends PHPUnit_Framework_TestCase
$user=$this->savuser; $user=$this->savuser;
$langs=$this->savlangs; $langs=$this->savlangs;
$db=$this->savdb; $db=$this->savdb;
$object=new Societe($db); $object=new Societe($db);
$object->initAsSpecimen(); $object->initAsSpecimen();
$object->country_code='FR'; $object->country_code='FR';
$phone=dol_print_phone('1234567890', $object->country_code); $phone=dol_print_phone('1234567890', $object->country_code);
$this->assertEquals('<span style="margin-right: 10px;">12&nbsp;34&nbsp;56&nbsp;78&nbsp;90</span>', $phone, 'Phone for FR 1'); $this->assertEquals('<span style="margin-right: 10px;">12&nbsp;34&nbsp;56&nbsp;78&nbsp;90</span>', $phone, 'Phone for FR 1');
$object->country_code='FR'; $object->country_code='FR';
$phone=dol_print_phone('1234567890', $object->country_code, 0, 0, 0, ''); $phone=dol_print_phone('1234567890', $object->country_code, 0, 0, 0, '');
$this->assertEquals('<span style="margin-right: 10px;">1234567890</span>', $phone, 'Phone for FR 2'); $this->assertEquals('<span style="margin-right: 10px;">1234567890</span>', $phone, 'Phone for FR 2');
$object->country_code='FR'; $object->country_code='FR';
$phone=dol_print_phone('1234567890', $object->country_code, 0, 0, 0, ' '); $phone=dol_print_phone('1234567890', $object->country_code, 0, 0, 0, ' ');
$this->assertEquals('<span style="margin-right: 10px;">12 34 56 78 90</span>', $phone, 'Phone for FR 3'); $this->assertEquals('<span style="margin-right: 10px;">12 34 56 78 90</span>', $phone, 'Phone for FR 3');
@@ -690,10 +706,10 @@ class FunctionsLibTest extends PHPUnit_Framework_TestCase
$object->country_code='CA'; $object->country_code='CA';
$phone=dol_print_phone('1234567890', $object->country_code, 0, 0, 0, ' '); $phone=dol_print_phone('1234567890', $object->country_code, 0, 0, 0, ' ');
$this->assertEquals('<span style="margin-right: 10px;">(123) 456-7890</span>', $phone, 'Phone for CA 1'); $this->assertEquals('<span style="margin-right: 10px;">(123) 456-7890</span>', $phone, 'Phone for CA 1');
} }
/** /**
* testImgPicto * testImgPicto
* *
@@ -1043,5 +1059,5 @@ class FunctionsLibTest extends PHPUnit_Framework_TestCase
return true; return true;
} }
} }