From 87fe16c445fb82262cd505ae73b6f202cf860164 Mon Sep 17 00:00:00 2001 From: UltrasonicNXT Date: Sun, 10 Nov 2013 08:55:10 +0000 Subject: [PATCH] Prevent space before ellipsis when truncating When truncating a string at a point where it contains a space (ie "hello world" to 9 chars), the resultant string will have a space before the ellipsis ("hello ..."). This is both gramatically incorrect and just looks wrong, and is fixed by trimming the string before appending the ellipsis. Change-Id: Iec86b17bfc8c50e4c1a96fd373861841fc57848d --- languages/Language.php | 2 ++ tests/phpunit/languages/LanguageTest.php | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/languages/Language.php b/languages/Language.php index 6bbd8bf56e..2038baca98 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -3325,11 +3325,13 @@ class Language { $length -= $eLength; $string = substr( $string, 0, $length ); // xyz... $string = $this->removeBadCharLast( $string ); + $string = rtrim( $string ); $string = $string . $ellipsis; } else { $length += $eLength; $string = substr( $string, $length ); // ...xyz $string = $this->removeBadCharFirst( $string ); + $string = ltrim( $string ); $string = $ellipsis . $string; } } diff --git a/tests/phpunit/languages/LanguageTest.php b/tests/phpunit/languages/LanguageTest.php index 78929e23ed..aa09deffb6 100644 --- a/tests/phpunit/languages/LanguageTest.php +++ b/tests/phpunit/languages/LanguageTest.php @@ -241,6 +241,26 @@ class LanguageTest extends LanguageClassesTestCase { $this->getLang()->truncate( "123456789", -5, 'XXXXXXXXXXXXXXX' ), 'truncate suffix, large ellipsis' ); + $this->assertEquals( + "123XXX", + $this->getLang()->truncate( "123 ", 9, 'XXX' ), + 'truncate prefix, with spaces' + ); + $this->assertEquals( + "12345XXX", + $this->getLang()->truncate( "12345 8", 11, 'XXX' ), + 'truncate prefix, with spaces and non-space ending' + ); + $this->assertEquals( + "XXX234", + $this->getLang()->truncate( "1 234", -8, 'XXX' ), + 'truncate suffix, with spaces' + ); + $this->assertEquals( + "12345XXX", + $this->getLang()->truncate( "1234567890", 5, 'XXX', false ), + 'truncate without adjustment' + ); } /** -- 2.20.1