Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #358 from shama/1.3

Fix TextHelper excerpt method to work as documented
  • Loading branch information...
commit 6836ce5118c337122b7e014c9c9c5f7a4533fe1c 2 parents ca098eb + d3e8f9f
@markstory markstory authored
View
29 cake/libs/view/helpers/text.php
@@ -287,34 +287,31 @@ function excerpt($text, $phrase, $radius = 100, $ending = '...') {
return $this->truncate($text, $radius * 2, array('ending' => $ending));
}
+ $append = $prepend = $ending;
+
$phraseLen = mb_strlen($phrase);
- if ($radius < $phraseLen) {
- $radius = $phraseLen;
- }
+ $textLen = mb_strlen($text);
$pos = mb_strpos(mb_strtolower($text), mb_strtolower($phrase));
-
- $startPos = 0;
- if ($pos > $radius) {
- $startPos = $pos - $radius;
+ if ($pos === false) {
+ return mb_substr($text, 0, $radius) . $ending;
}
- $textLen = mb_strlen($text);
+ $startPos = $pos - $radius;
+ if ($startPos <= 0) {
+ $startPos = 0;
+ $prepend = '';
+ }
$endPos = $pos + $phraseLen + $radius;
if ($endPos >= $textLen) {
$endPos = $textLen;
+ $append = '';
}
$excerpt = mb_substr($text, $startPos, $endPos - $startPos);
- if ($startPos != 0) {
- $excerpt = substr_replace($excerpt, $ending, 0, $phraseLen);
- }
-
- if ($endPos != $textLen) {
- $excerpt = substr_replace($excerpt, $ending, -$phraseLen);
- }
-
+ $excerpt = $prepend . $excerpt . $append;
+
return $excerpt;
}
View
19 cake/tests/cases/libs/view/helpers/text.test.php
@@ -362,7 +362,7 @@ function testHighlightCaseInsensitivity() {
function testExcerpt() {
$text = 'This is a phrase with test text to play with';
- $expected = '...with test text...';
+ $expected = '...ase with test text to ...';
$result = $this->Text->excerpt($text, 'test', 9, '...');
$this->assertEqual($expected, $result);
@@ -378,18 +378,19 @@ function testExcerpt() {
$result = $this->Text->excerpt($text, null, 200, '...');
$this->assertEqual($expected, $result);
- $expected = '...phrase...';
+ $expected = '...a phrase w...';
$result = $this->Text->excerpt($text, 'phrase', 2, '...');
$this->assertEqual($expected, $result);
- $phrase = 'This is a phrase with test';
+ $phrase = 'This is a phrase with test text';
$expected = $text;
- $result = $this->Text->excerpt($text, $phrase, strlen($phrase) + 3, '...');
+ $result = $this->Text->excerpt($text, $phrase, 13, '...');
$this->assertEqual($expected, $result);
-
- $phrase = 'This is a phrase with text';
- $expected = $text;
- $result = $this->Text->excerpt($text, $phrase, 10, '...');
+
+ $text = 'aaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaa';
+ $phrase = 'bbbbbbbb';
+ $result = $this->Text->excerpt($text, $phrase, 10);
+ $expected = '...aaaaaaaaaabbbbbbbbaaaaaaaaaa...';
$this->assertEqual($expected, $result);
}
@@ -402,7 +403,7 @@ function testExcerpt() {
function testExcerptCaseInsensitivity() {
$text = 'This is a phrase with test text to play with';
- $expected = '...with test text...';
+ $expected = '...ase with test text to ...';
$result = $this->Text->excerpt($text, 'TEST', 9, '...');
$this->assertEqual($expected, $result);
Please sign in to comment.
Something went wrong with that request. Please try again.