mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUG Use proper ellipsis character in the various summary method.
This commit is contained in:
parent
a166ba0ebd
commit
896c0e4388
@ -119,16 +119,16 @@ abstract class DBString extends DBField
|
||||
* HTML tags in the string of text.
|
||||
*
|
||||
* @param int $limit Number of characters to limit by
|
||||
* @param string $add Ellipsis to add to the end of truncated string
|
||||
* @param string|false $add Ellipsis to add to the end of truncated string
|
||||
* @return string
|
||||
*/
|
||||
public function LimitCharacters($limit = 20, $add = '...')
|
||||
public function LimitCharacters($limit = 20, $add = false)
|
||||
{
|
||||
$value = $this->Plain();
|
||||
if (mb_strlen($value) <= $limit) {
|
||||
return $value;
|
||||
}
|
||||
return mb_substr($value, 0, $limit) . $add;
|
||||
return $this->addEllipsis(mb_substr($value, 0, $limit), $add);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -137,10 +137,10 @@ abstract class DBString extends DBField
|
||||
* from the field.
|
||||
*
|
||||
* @param int $limit Number of characters to limit by
|
||||
* @param string $add Ellipsis to add to the end of truncated string
|
||||
* @param string|false $add Ellipsis to add to the end of truncated string
|
||||
* @return string Plain text value with limited characters
|
||||
*/
|
||||
public function LimitCharactersToClosestWord($limit = 20, $add = '...')
|
||||
public function LimitCharactersToClosestWord($limit = 20, $add = false)
|
||||
{
|
||||
// Safely convert to plain text
|
||||
$value = $this->Plain();
|
||||
@ -154,11 +154,14 @@ abstract class DBString extends DBField
|
||||
$value = mb_substr($value, 0, $limit);
|
||||
|
||||
// If value exceeds limit, strip punctuation off the end to the last space and apply ellipsis
|
||||
$value = preg_replace(
|
||||
$value = $this->addEllipsis(
|
||||
preg_replace(
|
||||
'/[^\w_]+$/',
|
||||
'',
|
||||
mb_substr($value, 0, mb_strrpos($value, " "))
|
||||
) . $add;
|
||||
),
|
||||
$add
|
||||
);
|
||||
return $value;
|
||||
}
|
||||
|
||||
@ -166,11 +169,11 @@ abstract class DBString extends DBField
|
||||
* Limit this field's content by a number of words.
|
||||
*
|
||||
* @param int $numWords Number of words to limit by.
|
||||
* @param string $add Ellipsis to add to the end of truncated string.
|
||||
* @param false $add Ellipsis to add to the end of truncated string.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function LimitWordCount($numWords = 26, $add = '...')
|
||||
public function LimitWordCount($numWords = 26, $add = false)
|
||||
{
|
||||
$value = $this->Plain();
|
||||
$words = explode(' ', $value);
|
||||
@ -180,7 +183,7 @@ abstract class DBString extends DBField
|
||||
|
||||
// Limit
|
||||
$words = array_slice($words, 0, $numWords);
|
||||
return implode(' ', $words) . $add;
|
||||
return $this->addEllipsis(implode(' ', $words), $add);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -212,4 +215,28 @@ abstract class DBString extends DBField
|
||||
{
|
||||
return trim($this->RAW());
|
||||
}
|
||||
|
||||
/**
|
||||
* Swap add for DefaultEllipsis if need be
|
||||
* @param string $string
|
||||
* @param false|string $add
|
||||
* @return string
|
||||
*/
|
||||
private function addEllipsis(string $string, $add): string
|
||||
{
|
||||
if ($add === false) {
|
||||
$add = $this->DefaultEllipsis();
|
||||
}
|
||||
|
||||
return $string . $add;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default string to indicate that a string was cut off.
|
||||
* @return string
|
||||
*/
|
||||
public function DefaultEllipsis(): string
|
||||
{
|
||||
return _t(self::class . '.ELLIPSIS', '…');
|
||||
}
|
||||
}
|
||||
|
@ -115,10 +115,10 @@ class DBText extends DBString
|
||||
* Builds a basic summary, up to a maximum number of words
|
||||
*
|
||||
* @param int $maxWords
|
||||
* @param string $add
|
||||
* @param string|false $add
|
||||
* @return string
|
||||
*/
|
||||
public function Summary($maxWords = 50, $add = '...')
|
||||
public function Summary($maxWords = 50, $add = false)
|
||||
{
|
||||
// Get plain-text version
|
||||
$value = $this->Plain();
|
||||
@ -126,6 +126,11 @@ class DBText extends DBString
|
||||
return '';
|
||||
}
|
||||
|
||||
// If no $elipsis string is provided, use the default one.
|
||||
if ($add === false) {
|
||||
$add = $this->DefaultEllipsis();
|
||||
}
|
||||
|
||||
// Split on sentences (don't remove period)
|
||||
$sentences = array_filter(array_map(function ($str) {
|
||||
return trim($str);
|
||||
@ -176,16 +181,16 @@ class DBText extends DBString
|
||||
* @param int $characters Number of characters in the summary
|
||||
* @param string $keywords Supplied string ("keywords"). Will fall back to 'Search' querystring arg.
|
||||
* @param bool $highlight Add a highlight <mark> element around search query?
|
||||
* @param string $prefix Prefix text
|
||||
* @param string $suffix Suffix text
|
||||
* @param string|false $prefix Prefix text
|
||||
* @param string|false $suffix Suffix text
|
||||
* @return string HTML string with context
|
||||
*/
|
||||
public function ContextSummary(
|
||||
$characters = 500,
|
||||
$keywords = null,
|
||||
$highlight = true,
|
||||
$prefix = "... ",
|
||||
$suffix = "..."
|
||||
$prefix = false,
|
||||
$suffix = false
|
||||
) {
|
||||
|
||||
if (!$keywords) {
|
||||
@ -193,6 +198,14 @@ class DBText extends DBString
|
||||
$keywords = isset($_REQUEST['Search']) ? $_REQUEST['Search'] : '';
|
||||
}
|
||||
|
||||
if ($prefix === false) {
|
||||
$prefix = $this->getDefaultValue() . ' ';
|
||||
}
|
||||
|
||||
if ($suffix === false) {
|
||||
$prefix = $this->getDefaultValue();
|
||||
}
|
||||
|
||||
// Get raw text value, but XML encode it (as we'll be merging with HTML tags soon)
|
||||
$text = Convert::raw2xml($this->Plain());
|
||||
$keywords = Convert::raw2xml($keywords);
|
||||
@ -257,4 +270,5 @@ class DBText extends DBString
|
||||
{
|
||||
return new TextField($this->name, $title);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -38,10 +38,10 @@ class DBTextTest extends SapphireTest
|
||||
// Plain text values always encoded safely
|
||||
// HTML stored in non-html fields is treated literally.
|
||||
return [
|
||||
['The little brown fox jumped over the lazy cow.', 'The little brown fox...'],
|
||||
['The little brown fox jumped over the lazy cow.', 'The little brown fox…'],
|
||||
['<p>Short & Sweet</p>', '<p>Short & Sweet</p>'],
|
||||
['This text contains & in it', 'This text contains &...'],
|
||||
['Is an umault in schön?', 'Is an umault in schö...'],
|
||||
['This text contains & in it', 'This text contains &…'],
|
||||
['Is an umault in schön?', 'Is an umault in schö…'],
|
||||
];
|
||||
}
|
||||
|
||||
@ -66,7 +66,7 @@ class DBTextTest extends SapphireTest
|
||||
{
|
||||
return [
|
||||
// Standard words limited, ellipsis added if truncated
|
||||
['Lorem ipsum dolor sit amet', 24, 'Lorem ipsum dolor sit...'],
|
||||
['Lorem ipsum dolor sit amet', 24, 'Lorem ipsum dolor sit…'],
|
||||
|
||||
// Complete words less than the character limit don't get truncated, ellipsis not added
|
||||
['Lorem ipsum', 24, 'Lorem ipsum'],
|
||||
@ -78,12 +78,12 @@ class DBTextTest extends SapphireTest
|
||||
|
||||
// HTML stored in non-html fields is treated literally.
|
||||
// If storing HTML you should use DBHTMLText instead
|
||||
['<p>Lorem ipsum dolor sit amet</p>', 24, '<p>Lorem ipsum dolor...'],
|
||||
['<p><span>Lorem ipsum dolor sit amet</span></p>', 24, '<p><span>Lorem ipsum...'],
|
||||
['<p>Lorem ipsum dolor sit amet</p>', 24, '<p>Lorem ipsum dolor…'],
|
||||
['<p><span>Lorem ipsum dolor sit amet</span></p>', 24, '<p><span>Lorem ipsum…'],
|
||||
['<p>Lorem ipsum</p>', 24, '<p>Lorem ipsum</p>'],
|
||||
['Lorem & ipsum dolor sit amet', 24, 'Lorem &amp; ipsum dolor...'],
|
||||
['Lorem & ipsum dolor sit amet', 24, 'Lorem &amp; ipsum dolor…'],
|
||||
|
||||
['Is an umault in schön or not?', 22, 'Is an umault in schön...'],
|
||||
['Is an umault in schön or not?', 22, 'Is an umault in schön…'],
|
||||
|
||||
];
|
||||
}
|
||||
@ -111,8 +111,8 @@ class DBTextTest extends SapphireTest
|
||||
{
|
||||
return [
|
||||
// Standard words limited, ellipsis added if truncated
|
||||
['The little brown fox jumped over the lazy cow.', 3, 'The little brown...'],
|
||||
[' This text has white space around the ends ', 3, 'This text has...'],
|
||||
['The little brown fox jumped over the lazy cow.', 3, 'The little brown…'],
|
||||
[' This text has white space around the ends ', 3, 'This text has…'],
|
||||
|
||||
// Words less than the limt word count don't get truncated, ellipsis not added
|
||||
['Two words', 3, 'Two words'], // Two words shouldn't have an ellipsis
|
||||
@ -122,15 +122,15 @@ class DBTextTest extends SapphireTest
|
||||
|
||||
// Text with special characters
|
||||
['Nice & Easy', 3, 'Nice & Easy'],
|
||||
['One & Two & Three', 3, 'One & Two...'],
|
||||
['One & Two & Three', 3, 'One & Two…'],
|
||||
|
||||
// HTML stored in non-html fields is treated literally.
|
||||
// If storing HTML you should use DBHTMLText instead
|
||||
['<p>Text inside a paragraph tag should also work</p>', 3, '<p>Text inside a...'],
|
||||
['<p>Text inside a paragraph tag should also work</p>', 3, '<p>Text inside a…'],
|
||||
['<p>Two words</p>', 3, '<p>Two words</p>'],
|
||||
|
||||
// Check UTF8
|
||||
['Is an umault in schön or not?', 5, 'Is an umault in schön...'],
|
||||
['Is an umault in schön or not?', 5, 'Is an umault in schön…'],
|
||||
];
|
||||
}
|
||||
|
||||
@ -227,7 +227,7 @@ class DBTextTest extends SapphireTest
|
||||
'This is some text. It is a test',
|
||||
20,
|
||||
'test',
|
||||
'... text. It is a <mark>test</mark>'
|
||||
'… text. It is a <mark>test</mark>'
|
||||
],
|
||||
[
|
||||
// Retains case of original string
|
||||
@ -235,13 +235,13 @@ class DBTextTest extends SapphireTest
|
||||
50,
|
||||
'some test',
|
||||
'This is <mark>some</mark> <mark>test</mark> text.'
|
||||
. ' <mark>Test</mark> <mark>test</mark> what if you have...'
|
||||
. ' <mark>Test</mark> <mark>test</mark> what if you have…'
|
||||
],
|
||||
[
|
||||
'Here is some text & HTML included',
|
||||
20,
|
||||
'html',
|
||||
'... text & <mark>HTML</mark> inc...'
|
||||
'… text & <mark>HTML</mark> inc…'
|
||||
],
|
||||
[
|
||||
'A dog ate a cat while looking at a Foobar',
|
||||
@ -262,14 +262,14 @@ class DBTextTest extends SapphireTest
|
||||
21,
|
||||
'schön',
|
||||
// check UTF8 support
|
||||
'both <mark>schön</mark> and können...',
|
||||
'both <mark>schön</mark> and können…',
|
||||
],
|
||||
[
|
||||
'both schön and können have umlauts',
|
||||
21,
|
||||
'',
|
||||
// check non existant search term
|
||||
'both schön and können...',
|
||||
'both schön and können…',
|
||||
]
|
||||
|
||||
|
||||
@ -346,4 +346,10 @@ class DBTextTest extends SapphireTest
|
||||
|
||||
$this->assertTrue(mb_check_encoding($textObj->FirstSentence(), 'UTF-8'));
|
||||
}
|
||||
|
||||
public function testDefaultEllipsis()
|
||||
{
|
||||
$textObj = new DBText('Test');
|
||||
$this->assertEquals('…', $textObj->DefaultEllipsis());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user