Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improves truncate twig filter #12347

Merged
merged 2 commits into from Dec 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions core/Twig.php
Expand Up @@ -25,11 +25,11 @@

function piwik_filter_truncate($string, $size)
{
if (strlen($string) < $size) {
if (Common::mb_strlen(html_entity_decode($string)) <= $size) {
return $string;
} else {
$array = str_split($string, $size);
return array_shift($array) . "...";
preg_match('/^(&(?:[a-z\d]+|#\d+|#x[a-f\d]+);|.){'.$size.'}/i', $string, $shortenString);
return reset($shortenString) . "...";
}
}

Expand Down
40 changes: 40 additions & 0 deletions tests/PHPUnit/Unit/TwigTest.php
@@ -0,0 +1,40 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/

namespace Piwik\Tests\Unit;

require_once(PIWIK_INCLUDE_PATH . '/core/Twig.php');

/**
* @group Twig
*/
class TwigTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider getTruncateTests
*/
public function testPiwikFilterTruncate($in, $size, $out)
{
$truncated = \Piwik\piwik_filter_truncate($in, $size);
$this->assertEquals($out, $truncated);
}

public function getTruncateTests()
{
return [
['abc', 4, 'abc'],
['abc&quot;', 4, 'abc&quot;'],
['abc&nbsp;', 4, 'abc&nbsp;'],
['abcdef', 3, 'abc...'],
['ab&amp;ef', 3, 'ab&amp;...'],
['some&#9660;thing', 5, 'some&#9660;...'],
['ab&ef ;', 3, 'ab&...'],
['&lt;&gt;&#9660;&nbsp;', 4, '&lt;&gt;&#9660;&nbsp;']
];
}
}