mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Merged r71795 from trunk
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@73613 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
84c16c66c8
commit
e933d61a1c
@ -8,6 +8,20 @@
|
||||
*/
|
||||
class HTMLText extends Text {
|
||||
|
||||
/**
|
||||
* Limit this field's content by a number of characters.
|
||||
* This makes use of strip_tags() to avoid malforming the
|
||||
* 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
|
||||
* @return string
|
||||
*/
|
||||
function LimitCharacters($limit = 20, $add = "...") {
|
||||
$value = trim(strip_tags($this->value));
|
||||
return (strlen($value) > $limit) ? substr($value, 0, $limit) . $add : $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a summary of the content. This will either be the first paragraph, or the first $maxWords
|
||||
* words, whichever is shorter
|
||||
|
25
tests/fieldtypes/HTMLTextTest.php
Normal file
25
tests/fieldtypes/HTMLTextTest.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
/**
|
||||
* @package sapphire
|
||||
* @subpackage tests
|
||||
*/
|
||||
class HTMLTextTest extends SapphireTest {
|
||||
|
||||
/**
|
||||
* Test {@link Text->LimitCharacters()}
|
||||
*/
|
||||
function testLimitCharacters() {
|
||||
$cases = array(
|
||||
'The little brown fox jumped over the lazy cow.' => 'The little brown fox...',
|
||||
'<p>This is some text in a paragraph.</p>' => 'This is some text in...'
|
||||
);
|
||||
|
||||
foreach($cases as $originalValue => $expectedValue) {
|
||||
$textObj = new HTMLText('Test');
|
||||
$textObj->setValue($originalValue);
|
||||
$this->assertEquals($expectedValue, $textObj->LimitCharacters());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
@ -4,6 +4,22 @@
|
||||
* @subpackage tests
|
||||
*/
|
||||
class TextTest extends SapphireTest {
|
||||
|
||||
/**
|
||||
* Test {@link Text->LimitCharacters()}
|
||||
*/
|
||||
function testLimitCharacters() {
|
||||
$cases = array(
|
||||
'The little brown fox jumped over the lazy cow.' => 'The little brown fox...',
|
||||
'<p>This is some text in a paragraph.</p>' => '<p>This is some text...'
|
||||
);
|
||||
|
||||
foreach($cases as $originalValue => $expectedValue) {
|
||||
$textObj = new Text('Test');
|
||||
$textObj->setValue($originalValue);
|
||||
$this->assertEquals($expectedValue, $textObj->LimitCharacters());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test {@link Text->LimitWordCount()}
|
||||
|
Loading…
x
Reference in New Issue
Block a user