mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
parent
6a04824ef9
commit
5f0bbbc0da
@ -27,7 +27,7 @@ class Text extends DBField {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
function LimitWordCount($numWords = 26, $add = '...') {
|
function LimitWordCount($numWords = 26, $add = '...') {
|
||||||
$this->value = Convert::xml2raw($this->value);
|
$this->value = trim(Convert::xml2raw($this->value));
|
||||||
$ret = explode(' ', $this->value, $numWords + 1);
|
$ret = explode(' ', $this->value, $numWords + 1);
|
||||||
|
|
||||||
if(count($ret) <= $numWords - 1) {
|
if(count($ret) <= $numWords - 1) {
|
||||||
|
@ -12,6 +12,7 @@ class TextTest extends SapphireTest {
|
|||||||
$cases = array(
|
$cases = array(
|
||||||
/* Standard words limited, ellipsis added if truncated */
|
/* Standard words limited, ellipsis added if truncated */
|
||||||
'The little brown fox jumped over the lazy cow.' => 'The little brown...',
|
'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 */
|
/* 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
|
'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 */
|
/* HTML tags get stripped out, leaving the raw text */
|
||||||
'<p>Text inside a paragraph tag should also work</p>' => 'Text inside a...',
|
'<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) {
|
foreach($cases as $originalValue => $expectedValue) {
|
||||||
|
Loading…
Reference in New Issue
Block a user