mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
BUGFIX #3228 Fixed undefined offset error in Text::BigSummary() if trying to summarise text that is smaller than the requested word limit
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@85076 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
19769e3841
commit
a1d20b8ae9
@ -181,7 +181,6 @@ class Text extends DBField {
|
|||||||
return "";
|
return "";
|
||||||
|
|
||||||
$sentences = explode( '.', $data );
|
$sentences = explode( '.', $data );
|
||||||
|
|
||||||
$count = count( explode( ' ', $sentences[0] ) );
|
$count = count( explode( ' ', $sentences[0] ) );
|
||||||
|
|
||||||
// if the first sentence is too long, show only the first $maxWords words
|
// if the first sentence is too long, show only the first $maxWords words
|
||||||
@ -190,13 +189,15 @@ class Text extends DBField {
|
|||||||
}
|
}
|
||||||
// add each sentence while there are enough words to do so
|
// add each sentence while there are enough words to do so
|
||||||
do {
|
do {
|
||||||
$result .= trim(array_shift( $sentences )).'. ' ;
|
$result .= trim(array_shift($sentences));
|
||||||
$count += count( explode( ' ', $sentences[0] ) );
|
if($sentences) {
|
||||||
|
$result .= '. ';
|
||||||
|
$count += count(explode(' ', $sentences[0]));
|
||||||
|
}
|
||||||
|
|
||||||
// Ensure that we don't trim half way through a tag or a link
|
// Ensure that we don't trim half way through a tag or a link
|
||||||
$brokenLink = (substr_count($result,'<') != substr_count($result,'>')) ||
|
$brokenLink = (substr_count($result,'<') != substr_count($result,'>')) ||
|
||||||
(substr_count($result,'<a') != substr_count($result,'</a'));
|
(substr_count($result,'<a') != substr_count($result,'</a'));
|
||||||
|
|
||||||
} while( ($count < $maxWords || $brokenLink) && $sentences && trim( $sentences[0] ) );
|
} while( ($count < $maxWords || $brokenLink) && $sentences && trim( $sentences[0] ) );
|
||||||
|
|
||||||
if( preg_match( '/<a[^>]*>/', $result ) && !preg_match( '/<\/a>/', $result ) )
|
if( preg_match( '/<a[^>]*>/', $result ) && !preg_match( '/<\/a>/', $result ) )
|
||||||
|
@ -87,5 +87,22 @@ class TextTest extends SapphireTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test {@link Text->BigSummary()}
|
||||||
|
*/
|
||||||
|
function testBigSummary() {
|
||||||
|
$cases = array(
|
||||||
|
'This text has multiple sentences. Big Summary uses this to split sentences up.' => 'This text has multiple...',
|
||||||
|
'This text does not have multiple sentences' => 'This text does not...',
|
||||||
|
'Very short' => 'Very short',
|
||||||
|
'' => ''
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach($cases as $originalValue => $expectedValue) {
|
||||||
|
$textObj = DBField::create('Text', $originalValue);
|
||||||
|
$this->assertEquals($expectedValue, $textObj->BigSummary(4));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
?>
|
?>
|
Loading…
x
Reference in New Issue
Block a user