Skip to content

Commit

Permalink
ellipsis instead of ending/beginning for core wide consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
euromark committed Aug 4, 2012
1 parent 853fa7d commit 2a570e6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
29 changes: 24 additions & 5 deletions lib/Cake/Test/Case/Utility/StringTest.php
Expand Up @@ -379,7 +379,7 @@ public function testTruncate() {
$this->assertSame($this->Text->truncate($text2, 10, array('exact' => false)), '...');
$this->assertSame($this->Text->truncate($text3, 20), '<b>&copy; 2005-20...');
$this->assertSame($this->Text->truncate($text4, 15), '<img src="my...');
$this->assertSame($this->Text->truncate($text5, 6, array('ending' => '')), '0<b>1<');
$this->assertSame($this->Text->truncate($text5, 6, array('ellipsis' => '')), '0<b>1<');
$this->assertSame($this->Text->truncate($text1, 15, array('html' => true)), 'The quick br...');
$this->assertSame($this->Text->truncate($text1, 15, array('exact' => false, 'html' => true)), 'The quick...');
$this->assertSame($this->Text->truncate($text2, 10, array('html' => true)), 'Heiz&ouml;lr...');
Expand All @@ -388,8 +388,8 @@ public function testTruncate() {
$this->assertSame($this->Text->truncate($text4, 15, array('html' => true)), '<img src="mypic.jpg"> This image ...');
$this->assertSame($this->Text->truncate($text4, 45, array('html' => true)), '<img src="mypic.jpg"> This image tag is not XHTML conform!<br><hr/><b>But t...</b>');
$this->assertSame($this->Text->truncate($text4, 90, array('html' => true)), '<img src="mypic.jpg"> This image tag is not XHTML conform!<br><hr/><b>But the following image tag should be conform <img src="mypic.jpg" alt="Me, myself and I" /></b><br />Grea...');
$this->assertSame($this->Text->truncate($text5, 6, array('ending' => '', 'html' => true)), '0<b>1<i>2<span class="myclass">3</span>4<u>5</u></i></b>');
$this->assertSame($this->Text->truncate($text5, 20, array('ending' => '', 'html' => true)), $text5);
$this->assertSame($this->Text->truncate($text5, 6, array('ellipsis' => '', 'html' => true)), '0<b>1<i>2<span class="myclass">3</span>4<u>5</u></i></b>');
$this->assertSame($this->Text->truncate($text5, 20, array('ellipsis' => '', 'html' => true)), $text5);
$this->assertSame($this->Text->truncate($text6, 57, array('exact' => false, 'html' => true)), "<p><strong>Extra dates have been announced for this year's...</strong></p>");
$this->assertSame($this->Text->truncate($text7, 255), $text7);
$this->assertSame($this->Text->truncate($text7, 15), 'El moño está...');
Expand All @@ -399,7 +399,7 @@ public function testTruncate() {

$text = '<p><span style="font-size: medium;"><a>Iamatestwithnospacesandhtml</a></span></p>';
$result = $this->Text->truncate($text, 10, array(
'ending' => '...',
'ellipsis' => '...',
'exact' => false,
'html' => true
));
Expand All @@ -422,7 +422,7 @@ public function testTruncate() {
<p><span style="font-size: medium;"><a>http://www.amazon.com/Steve-
Jobs-Walter-Isaacson/dp/1451648537</a></span></p>';
$result = $this->Text->truncate($text, 500, array(
'ending' => '... ',
'ellipsis' => '... ',
'exact' => false,
'html' => true
));
Expand All @@ -441,6 +441,22 @@ public function testTruncate() {
podeís adquirirla.</span></p>
<p><span style="font-size: medium;"><a>... </a></span></p>';
$this->assertEquals($expected, $result);

// test deprecated `ending` (`ellipsis` taking precedence if both are defined)
$result = $this->Text->truncate($text1, 31, array(
'ending' => '.',
'exact' => false,
));
$expected = 'The quick brown fox jumps.';
$this->assertEquals($expected, $result);

$result = $this->Text->truncate($text1, 31, array(
'ellipsis' => '..',
'ending' => '.',
'exact' => false,
));
$expected = 'The quick brown fox jumps..';
$this->assertEquals($expected, $result);
}

/**
Expand Down Expand Up @@ -481,6 +497,9 @@ public function testTail() {

$result = $this->Text->tail($text5, 10);
$this->assertEquals('...цчшщъыь', $result);

$result = $this->Text->tail($text5, 6, array('ellipsis' => ''));
$this->assertEquals('чшщъыь', $result);
}

/**
Expand Down
35 changes: 19 additions & 16 deletions lib/Cake/Utility/String.php
Expand Up @@ -422,11 +422,11 @@ public static function stripLinks($text) {
* Truncates text starting from the end.
*
* Cuts a string to the length of $length and replaces the first characters
* with the beginning if the text is longer than length.
* with the ellipsis if the text is longer than length.
*
* ### Options:
*
* - `beginning` Will be used as Beginning and prepended to the trimmed string
* - `ellipsis` Will be used as Beginning and prepended to the trimmed string
* - `exact` If false, $text will not be cut mid-word
*
* @param string $text String to truncate.
Expand All @@ -436,7 +436,7 @@ public static function stripLinks($text) {
*/
public static function tail($text, $length = 100, $options = array()) {
$default = array(
'beginning' => '...', 'exact' => true
'ellipsis' => '...', 'exact' => true
);
$options = array_merge($default, $options);
extract($options);
Expand All @@ -448,25 +448,25 @@ class_exists('Multibyte');
if (mb_strlen($text) <= $length) {
return $text;
} else {
$truncate = mb_substr($text, mb_strlen($text) - $length + mb_strlen($beginning));
$truncate = mb_substr($text, mb_strlen($text) - $length + mb_strlen($ellipsis));
}
if (!$exact) {
$spacepos = mb_strpos($truncate, ' ');
$truncate = $spacepos === false ? '' : trim(mb_substr($truncate, $spacepos));
}

return $beginning . $truncate;
return $ellipsis . $truncate;
}

/**
* Truncates text.
*
* Cuts a string to the length of $length and replaces the last characters
* with the ending if the text is longer than length.
* with the ellipsis if the text is longer than length.
*
* ### Options:
*
* - `ending` Will be used as Ending and appended to the trimmed string
* - `ellipsis` Will be used as Ending and appended to the trimmed string (`ending` is deprecated)
* - `exact` If false, $text will not be cut mid-word
* - `html` If true, HTML tags would be handled correctly
*
Expand All @@ -478,8 +478,11 @@ class_exists('Multibyte');
*/
public static function truncate($text, $length = 100, $options = array()) {
$default = array(
'ending' => '...', 'exact' => true, 'html' => false
'ellipsis' => '...', 'exact' => true, 'html' => false
);
if (isset($options['ending'])) {
$default['ellipsis'] = $options['ending'];
}
$options = array_merge($default, $options);
extract($options);

Expand All @@ -491,7 +494,7 @@ class_exists('Multibyte');
if (mb_strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
return $text;
}
$totalLength = mb_strlen(strip_tags($ending));
$totalLength = mb_strlen(strip_tags($ellipsis));
$openTags = array();
$truncate = '';

Expand Down Expand Up @@ -538,7 +541,7 @@ class_exists('Multibyte');
if (mb_strlen($text) <= $length) {
return $text;
} else {
$truncate = mb_substr($text, 0, $length - mb_strlen($ending));
$truncate = mb_substr($text, 0, $length - mb_strlen($ellipsis));
}
}
if (!$exact) {
Expand Down Expand Up @@ -570,7 +573,7 @@ class_exists('Multibyte');
}
$truncate = mb_substr($truncate, 0, $spacepos);
}
$truncate .= $ending;
$truncate .= $ellipsis;

if ($html) {
foreach ($openTags as $tag) {
Expand All @@ -588,23 +591,23 @@ class_exists('Multibyte');
* @param string $text String to search the phrase in
* @param string $phrase Phrase that will be searched for
* @param integer $radius The amount of characters that will be returned on each side of the founded phrase
* @param string $ending Ending that will be appended
* @param string $ellipsis Ending that will be appended
* @return string Modified string
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::excerpt
*/
public static function excerpt($text, $phrase, $radius = 100, $ending = '...') {
public static function excerpt($text, $phrase, $radius = 100, $ellipsis = '...') {
if (empty($text) || empty($phrase)) {
return self::truncate($text, $radius * 2, array('ending' => $ending));
return self::truncate($text, $radius * 2, array('ellipsis' => $ellipsis));
}

$append = $prepend = $ending;
$append = $prepend = $ellipsis;

$phraseLen = mb_strlen($phrase);
$textLen = mb_strlen($text);

$pos = mb_strpos(mb_strtolower($text), mb_strtolower($phrase));
if ($pos === false) {
return mb_substr($text, 0, $radius) . $ending;
return mb_substr($text, 0, $radius) . $ellipsis;
}

$startPos = $pos - $radius;
Expand Down

0 comments on commit 2a570e6

Please sign in to comment.