mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
BUGFIX Fixed double up of <span> highlight tags around keywords in Text::ContextSummary()
MINOR Added test for Text::ContextSummary() git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@82035 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
6b35a34f91
commit
5ffd79cb79
@ -274,18 +274,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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -66,5 +66,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