mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Merged Text::ContextSummary() changes from branches/2.3 - r82035 and r82036
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@86669 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
0a953ba4e5
commit
2fdb548594
@ -262,18 +262,12 @@ class Text extends DBField {
|
||||
}
|
||||
|
||||
$summary = substr($text, $position, $characters);
|
||||
|
||||
$stringPieces = explode(' ', $string);
|
||||
|
||||
if($highlight) {
|
||||
// Add a span around all occurences of the search term
|
||||
$summary = str_ireplace($string, "<span class=\"highlight\">$string</span>", $summary);
|
||||
|
||||
// Add a span around all key words from the search term as well
|
||||
if($stringPieces) {
|
||||
foreach($stringPieces as $stringPiece) {
|
||||
$summary = str_ireplace($stringPiece, "<span class=\"highlight\">$stringPiece</span>", $summary);
|
||||
}
|
||||
if($stringPieces) foreach($stringPieces as $stringPiece) {
|
||||
$summary = str_ireplace($stringPiece, "<span class=\"highlight\">$stringPiece</span>", $summary);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -104,5 +104,25 @@ class TextTest extends SapphireTest {
|
||||
}
|
||||
}
|
||||
|
||||
function testContextSummary() {
|
||||
$testString1 = '<p>This is some text. It is a test</p>';
|
||||
$testKeywords1 = 'test';
|
||||
$testString2 = '<p>This is some test text. Test test what if you have multiple keywords.</p>';
|
||||
$testKeywords2 = 'some test';
|
||||
$textObj = DBField::create('Text', $testString1, 'Text');
|
||||
|
||||
$this->assertEquals(
|
||||
'text. It is a <span class="highlight">test</span>',
|
||||
$textObj->ContextSummary(20, $testKeywords1)
|
||||
);
|
||||
|
||||
$textObj->setValue($testString2);
|
||||
|
||||
$this->assertEquals(
|
||||
'This is <span class="highlight">some</span> <span class="highlight">test</span> text. <span class="highlight">test</span> <span class="highlight">test</span> what if you have',
|
||||
$textObj->ContextSummary(50, $testKeywords2)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
?>
|
||||
|
Loading…
x
Reference in New Issue
Block a user