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
This commit is contained in:
Sean Harvey 2008-12-16 11:07:44 +00:00 committed by Sam Minnee
parent 6a04824ef9
commit 5f0bbbc0da
2 changed files with 4 additions and 2 deletions

View File

@ -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) {

View File

@ -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 */
'<p>Text inside a paragraph tag should also work</p>' => 'Text inside a...',
'<p><span>Text nested inside another tag should also work</span></p>' => 'Text nested inside...'
'<p><span>Text nested inside another tag should also work</span></p>' => 'Text nested inside...',
'<p>Two words</p>' => 'Two words'
);
foreach($cases as $originalValue => $expectedValue) {