From 5f0bbbc0da59360cd024815de659065de99adbbb Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Tue, 16 Dec 2008 11:07:44 +0000 Subject: [PATCH] BUGFIX Strip potential whitespace from the beginning and end of string before limiting word count in Text->LimitWordCount(), fixing potential interference with truncation process git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@69222 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/fieldtypes/Text.php | 2 +- tests/fieldtypes/TextTest.php | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/core/model/fieldtypes/Text.php b/core/model/fieldtypes/Text.php index 823e3bfdb..a74394e56 100644 --- a/core/model/fieldtypes/Text.php +++ b/core/model/fieldtypes/Text.php @@ -27,7 +27,7 @@ class Text extends DBField { * @return string */ function LimitWordCount($numWords = 26, $add = '...') { - $this->value = Convert::xml2raw($this->value); + $this->value = trim(Convert::xml2raw($this->value)); $ret = explode(' ', $this->value, $numWords + 1); if(count($ret) <= $numWords - 1) { diff --git a/tests/fieldtypes/TextTest.php b/tests/fieldtypes/TextTest.php index 73f91c4e0..33cdd617f 100644 --- a/tests/fieldtypes/TextTest.php +++ b/tests/fieldtypes/TextTest.php @@ -12,6 +12,7 @@ class TextTest extends SapphireTest { $cases = array( /* Standard words limited, ellipsis added if truncated */ 'The little brown fox jumped over the lazy cow.' => 'The little brown...', + ' This text has white space around the ends ' => 'This text has...', /* Words less than the limt word count don't get truncated, ellipsis not added */ 'Two words' => 'Two words', // Two words shouldn't have an ellipsis @@ -20,7 +21,8 @@ class TextTest extends SapphireTest { /* HTML tags get stripped out, leaving the raw text */ '

Text inside a paragraph tag should also work

' => 'Text inside a...', - '

Text nested inside another tag should also work

' => 'Text nested inside...' + '

Text nested inside another tag should also work

' => 'Text nested inside...', + '

Two words

' => 'Two words' ); foreach($cases as $originalValue => $expectedValue) {