Prevent space before ellipsis when truncating
authorUltrasonicNXT <adamr_carter@btinternet.com>
Sun, 10 Nov 2013 08:55:10 +0000 (08:55 +0000)
committerBrian Wolff <bawolff+wn@gmail.com>
Fri, 15 Nov 2013 18:29:18 +0000 (14:29 -0400)
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
tests/phpunit/languages/LanguageTest.php

index 6bbd8bf..2038bac 100644 (file)
@@ -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;
                        }
                }
index 78929e2..aa09def 100644 (file)
@@ -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'
+               );
        }
 
        /**