From d3a98c7680178da9811a579773c47aeb580254f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Sun, 21 Feb 2021 13:00:31 +0100 Subject: [PATCH 1/7] replace ... by one char in dol_trunc --- htdocs/core/lib/functions.lib.php | 75 ++++++++++++++++++------------- 1 file changed, 43 insertions(+), 32 deletions(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 7d994616e5c..39ac3b7a80e 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -3178,10 +3178,10 @@ function dol_substr($string, $start, $length, $stringencoding = '', $trunconbyte * MAIN_DISABLE_TRUNC=1 can disable all truncings * * @param string $string String to truncate - * @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 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 $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 used to display data and can be changed for small screen. TODO Remove this param (must be dealt with CSS) * @return string Truncated string. WARNING: length is never higher than $size if $nodot is set, but can be 3 chars higher otherwise. */ @@ -3189,42 +3189,53 @@ function dol_trunc($string, $size = 40, $trunc = 'right', $stringencoding = 'UTF { global $conf; - if ($size == 0 || !empty($conf->global->MAIN_DISABLE_TRUNC)) return $string; + if ($size == 0 || !empty($conf->global->MAIN_DISABLE_TRUNC)) { + return $string; + } - if (empty($stringencoding)) $stringencoding = 'UTF-8'; + if (empty($stringencoding)) { + $stringencoding = 'UTF-8'; + } // reduce for small screen - if ($conf->dol_optimize_smallscreen == 1 && $display == 1) $size = round($size / 3); + // if ($conf->dol_optimize_smallscreen == 1 && $display == 1) $size = round($size / 3); // We go always here - if ($trunc == 'right') - { - $newstring = dol_textishtml($string) ?dol_string_nohtmltag($string, 1) : $string; - 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 ? '' : '...'); - else //return 'u'.$size.'-'.$newstring.'-'.dol_strlen($newstring,$stringencoding).'-'.$string; - return $string; - } elseif ($trunc == 'middle') - { - $newstring = dol_textishtml($string) ?dol_string_nohtmltag($string, 1) : $string; - if (dol_strlen($newstring, $stringencoding) > 2 && dol_strlen($newstring, $stringencoding) > ($size + 1)) - { + if ($trunc == 'right') { + $newstring = dol_textishtml($string) ? dol_string_nohtmltag($string, 1) : $string; + if (dol_strlen($newstring, $stringencoding) > ($size + ($nodot ? 0 : 1))) { + // If nodot is 0 and size is 1 chars more, we don't trunc and don't add … + return dol_substr($newstring, 0, $size, $stringencoding).($nodot ? '' : '…'); + } else { + //return 'u'.$size.'-'.$newstring.'-'.dol_strlen($newstring,$stringencoding).'-'.$string; + return $string; + } + } elseif ($trunc == 'middle') { + $newstring = dol_textishtml($string) ? dol_string_nohtmltag($string, 1) : $string; + if (dol_strlen($newstring, $stringencoding) > 2 && dol_strlen($newstring, $stringencoding) > ($size + 1)) { $size1 = round($size / 2); $size2 = round($size / 2); - return dol_substr($newstring, 0, $size1, $stringencoding).'...'.dol_substr($newstring, dol_strlen($newstring, $stringencoding) - $size2, $size2, $stringencoding); - } else return $string; - } elseif ($trunc == 'left') - { - $newstring = dol_textishtml($string) ?dol_string_nohtmltag($string, 1) : $string; - 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); - else return $string; - } elseif ($trunc == 'wrap') - { - $newstring = dol_textishtml($string) ?dol_string_nohtmltag($string, 1) : $string; - if (dol_strlen($newstring, $stringencoding) > ($size + 1)) - return dol_substr($newstring, 0, $size, $stringencoding)."\n".dol_trunc(dol_substr($newstring, $size, dol_strlen($newstring, $stringencoding) - $size, $stringencoding), $size, $trunc); - else return $string; - } else return 'BadParam3CallingDolTrunc'; + return dol_substr($newstring, 0, $size1, $stringencoding).'…'.dol_substr($newstring, dol_strlen($newstring, $stringencoding) - $size2, $size2, $stringencoding); + } else { + return $string; + } + } elseif ($trunc == 'left') { + $newstring = dol_textishtml($string) ? dol_string_nohtmltag($string, 1) : $string; + if (dol_strlen($newstring, $stringencoding) > ($size + ($nodot ? 0 : 1))) { + // If nodot is 0 and size is 1 chars more, we don't trunc and don't add … + return '…'.dol_substr($newstring, dol_strlen($newstring, $stringencoding) - $size, $size, $stringencoding); + } else { + return $string; + } + } elseif ($trunc == 'wrap') { + $newstring = dol_textishtml($string) ? dol_string_nohtmltag($string, 1) : $string; + if (dol_strlen($newstring, $stringencoding) > ($size + 1)) { + return dol_substr($newstring, 0, $size, $stringencoding)."\n".dol_trunc(dol_substr($newstring, $size, dol_strlen($newstring, $stringencoding) - $size, $stringencoding), $size, $trunc); + } else { + return $string; + } + } else { + return 'BadParam3CallingDolTrunc'; + } } /** From 6e3a1a1d418a083e15c95aff536bd2a811f7d577 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Sun, 21 Feb 2021 13:32:54 +0100 Subject: [PATCH 2/7] fix tests --- htdocs/core/lib/functions.lib.php | 6 +++--- test/phpunit/FunctionsLibTest.php | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 39ac3b7a80e..efe3824c5c4 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -3173,12 +3173,12 @@ function dol_substr($string, $start, $length, $stringencoding = '', $trunconbyte /** - * Truncate a string to a particular length adding '...' if string larger than length. - * If length = max length+1, we do no truncate to avoid having just 1 char replaced with '...'. + * Truncate a string to a particular length adding '…' if string larger than length. + * If length = max length+1, we do no truncate to avoid having just 1 char replaced with '…'. * MAIN_DISABLE_TRUNC=1 can disable all truncings * * @param string $string String to truncate - * @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 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 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 $stringencoding Tell what is source string encoding * @param int $nodot Truncation do not add … after truncation. So it's an exact truncation. diff --git a/test/phpunit/FunctionsLibTest.php b/test/phpunit/FunctionsLibTest.php index 4f6bfb1963e..fe9d0563167 100644 --- a/test/phpunit/FunctionsLibTest.php +++ b/test/phpunit/FunctionsLibTest.php @@ -765,21 +765,21 @@ class FunctionsLibTest extends PHPUnit\Framework\TestCase */ public function testDolTrunc() { - // 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"; $after=dol_trunc($input, 3); - $this->assertEquals("éeé...", $after, 'Test A1'); + $this->assertEquals("éeé…", $after, 'Test A1'); $after=dol_trunc($input, 2); - $this->assertEquals("ée...", $after, 'Test A2'); + $this->assertEquals("ée…", $after, 'Test A2'); $after=dol_trunc($input, 1); - $this->assertEquals("é...", $after, 'Test A3'); + $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'); + $this->assertEquals("é…", $after, 'Test B3'); $input="éeée"; $after=dol_trunc($input, 3); $this->assertEquals("éeée", $after, 'Test C1'); @@ -794,7 +794,7 @@ class FunctionsLibTest extends PHPUnit\Framework\TestCase $this->assertEquals("éeé", $after, 'Test D'); $after=dol_trunc($input, 1); $this->assertEquals("éeé", $after, 'Test E'); - // Trunc with no ... + // Trunc with no … $input="éeéeéeàa"; $after=dol_trunc($input, 3, 'right', 'UTF-8', 1); $this->assertEquals("éeé", $after, 'Test F'); @@ -809,7 +809,7 @@ class FunctionsLibTest extends PHPUnit\Framework\TestCase $this->assertEquals("é", $after, 'Test J'); $input="éeéeéeàa"; $after=dol_trunc($input, 4, 'middle'); - $this->assertEquals("ée...àa", $after, 'Test K'); + $this->assertEquals("ée…àa", $after, 'Test K'); return true; } From 548820011a702f2bafc086fbe0da4836bb7f7175 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Sun, 21 Feb 2021 13:39:59 +0100 Subject: [PATCH 3/7] fix tests --- htdocs/core/lib/functions.lib.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index efe3824c5c4..3604308708b 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -3202,7 +3202,7 @@ function dol_trunc($string, $size = 40, $trunc = 'right', $stringencoding = 'UTF // We go always here if ($trunc == 'right') { $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 : 2))) { // If nodot is 0 and size is 1 chars more, we don't trunc and don't add … return dol_substr($newstring, 0, $size, $stringencoding).($nodot ? '' : '…'); } else { @@ -3220,7 +3220,7 @@ function dol_trunc($string, $size = 40, $trunc = 'right', $stringencoding = 'UTF } } elseif ($trunc == 'left') { $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 : 2))) { // If nodot is 0 and size is 1 chars more, we don't trunc and don't add … return '…'.dol_substr($newstring, dol_strlen($newstring, $stringencoding) - $size, $size, $stringencoding); } else { From 5ceb8163de1f7c3399c45bdd8ec1017a84926690 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Sun, 21 Feb 2021 15:40:03 +0100 Subject: [PATCH 4/7] fix tests --- htdocs/core/lib/functions.lib.php | 6 +++--- test/phpunit/FunctionsLibTest.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 3604308708b..128f6ffff16 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -3197,12 +3197,12 @@ function dol_trunc($string, $size = 40, $trunc = 'right', $stringencoding = 'UTF $stringencoding = 'UTF-8'; } // reduce for small screen - // if ($conf->dol_optimize_smallscreen == 1 && $display == 1) $size = round($size / 3); + if ($conf->dol_optimize_smallscreen == 1 && $display == 1) $size = round($size / 3); // We go always here if ($trunc == 'right') { $newstring = dol_textishtml($string) ? dol_string_nohtmltag($string, 1) : $string; - if (dol_strlen($newstring, $stringencoding) > ($size + ($nodot ? 0 : 2))) { + if (dol_strlen($newstring, $stringencoding) > ($size + ($nodot ? 0 : 3))) { // If nodot is 0 and size is 1 chars more, we don't trunc and don't add … return dol_substr($newstring, 0, $size, $stringencoding).($nodot ? '' : '…'); } else { @@ -3220,7 +3220,7 @@ function dol_trunc($string, $size = 40, $trunc = 'right', $stringencoding = 'UTF } } elseif ($trunc == 'left') { $newstring = dol_textishtml($string) ? dol_string_nohtmltag($string, 1) : $string; - if (dol_strlen($newstring, $stringencoding) > ($size + ($nodot ? 0 : 2))) { + if (dol_strlen($newstring, $stringencoding) > ($size + ($nodot ? 0 : 3))) { // If nodot is 0 and size is 1 chars more, we don't trunc and don't add … return '…'.dol_substr($newstring, dol_strlen($newstring, $stringencoding) - $size, $size, $stringencoding); } else { diff --git a/test/phpunit/FunctionsLibTest.php b/test/phpunit/FunctionsLibTest.php index fe9d0563167..9d626208af5 100644 --- a/test/phpunit/FunctionsLibTest.php +++ b/test/phpunit/FunctionsLibTest.php @@ -773,11 +773,11 @@ class FunctionsLibTest extends PHPUnit\Framework\TestCase $this->assertEquals("ée…", $after, 'Test A2'); $after=dol_trunc($input, 1); $this->assertEquals("é…", $after, 'Test A3'); - $input="éeéeé"; + $input="éeée"; $after=dol_trunc($input, 3); - $this->assertEquals("éeéeé", $after, 'Test B1'); + $this->assertEquals("éeée", $after, 'Test B1'); $after=dol_trunc($input, 2); - $this->assertEquals("éeéeé", $after, 'Test B2'); + $this->assertEquals("ée…", $after, 'Test B2'); $after=dol_trunc($input, 1); $this->assertEquals("é…", $after, 'Test B3'); $input="éeée"; From 71d32a7422c779d03d89ab07b60b742bdfaa2bc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Sun, 21 Feb 2021 15:59:19 +0100 Subject: [PATCH 5/7] fix tests --- htdocs/core/lib/functions.lib.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 128f6ffff16..a0533608020 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -3202,7 +3202,7 @@ function dol_trunc($string, $size = 40, $trunc = 'right', $stringencoding = 'UTF // We go always here if ($trunc == 'right') { $newstring = dol_textishtml($string) ? dol_string_nohtmltag($string, 1) : $string; - if (dol_strlen($newstring, $stringencoding) > ($size + ($nodot ? 0 : 3))) { + if (dol_strlen($newstring, $stringencoding) > ($size + ($nodot ? 0 : 1))) { // If nodot is 0 and size is 1 chars more, we don't trunc and don't add … return dol_substr($newstring, 0, $size, $stringencoding).($nodot ? '' : '…'); } else { @@ -3220,7 +3220,7 @@ function dol_trunc($string, $size = 40, $trunc = 'right', $stringencoding = 'UTF } } elseif ($trunc == 'left') { $newstring = dol_textishtml($string) ? dol_string_nohtmltag($string, 1) : $string; - if (dol_strlen($newstring, $stringencoding) > ($size + ($nodot ? 0 : 3))) { + if (dol_strlen($newstring, $stringencoding) > ($size + ($nodot ? 0 : 1))) { // If nodot is 0 and size is 1 chars more, we don't trunc and don't add … return '…'.dol_substr($newstring, dol_strlen($newstring, $stringencoding) - $size, $size, $stringencoding); } else { From 9ef56b53f2cf1d4e4097a9efd130ce94f1230015 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Sun, 21 Feb 2021 16:05:59 +0100 Subject: [PATCH 6/7] fix tests --- test/phpunit/FunctionsLibTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/phpunit/FunctionsLibTest.php b/test/phpunit/FunctionsLibTest.php index 9d626208af5..db3fead97c3 100644 --- a/test/phpunit/FunctionsLibTest.php +++ b/test/phpunit/FunctionsLibTest.php @@ -784,9 +784,9 @@ class FunctionsLibTest extends PHPUnit\Framework\TestCase $after=dol_trunc($input, 3); $this->assertEquals("éeée", $after, 'Test C1'); $after=dol_trunc($input, 2); - $this->assertEquals("éeée", $after, 'Test C2'); + $this->assertEquals("ée…", $after, 'Test C2'); $after=dol_trunc($input, 1); - $this->assertEquals("éeée", $after, 'Test C3'); + $this->assertEquals("é…", $after, 'Test C3'); $input="éeé"; $after=dol_trunc($input, 3); $this->assertEquals("éeé", $after, 'Test C'); From 398048da043b2ff64d3712fc152dcddb85d4d439 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Sun, 21 Feb 2021 16:10:35 +0100 Subject: [PATCH 7/7] fix tests --- test/phpunit/FunctionsLibTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/phpunit/FunctionsLibTest.php b/test/phpunit/FunctionsLibTest.php index db3fead97c3..f1341c9a042 100644 --- a/test/phpunit/FunctionsLibTest.php +++ b/test/phpunit/FunctionsLibTest.php @@ -793,7 +793,7 @@ class FunctionsLibTest extends PHPUnit\Framework\TestCase $after=dol_trunc($input, 2); $this->assertEquals("éeé", $after, 'Test D'); $after=dol_trunc($input, 1); - $this->assertEquals("éeé", $after, 'Test E'); + $this->assertEquals("é…", $after, 'Test E'); // Trunc with no … $input="éeéeéeàa"; $after=dol_trunc($input, 3, 'right', 'UTF-8', 1);