mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Merge branch 'dnadesign-FirstSentancePunctationFix' into 3.1
Closes #3159
This commit is contained in:
commit
a0211b5f50
@ -165,7 +165,7 @@ class HTMLText extends Text {
|
|||||||
/* Then look for the first sentence ending. We could probably use a nice regex, but for now this will do */
|
/* Then look for the first sentence ending. We could probably use a nice regex, but for now this will do */
|
||||||
$words = preg_split('/\s+/', $paragraph);
|
$words = preg_split('/\s+/', $paragraph);
|
||||||
foreach ($words as $i => $word) {
|
foreach ($words as $i => $word) {
|
||||||
if (preg_match('/\.$/', $word) && !preg_match('/(Dr|Mr|Mrs|Ms|Miss|Sr|Jr|No)\.$/i', $word)) {
|
if (preg_match('/(!|\?|\.)$/', $word) && !preg_match('/(Dr|Mr|Mrs|Ms|Miss|Sr|Jr|No)\.$/i', $word)) {
|
||||||
return implode(' ', array_slice($words, 0, $i+1));
|
return implode(' ', array_slice($words, 0, $i+1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -101,15 +101,18 @@ class Text extends StringField {
|
|||||||
* Caution: Not XML/HTML-safe - does not respect closing tags.
|
* Caution: Not XML/HTML-safe - does not respect closing tags.
|
||||||
*/
|
*/
|
||||||
public function FirstSentence() {
|
public function FirstSentence() {
|
||||||
$data = Convert::xml2raw( $this->value );
|
$paragraph = Convert::xml2raw( $this->value );
|
||||||
if( !$data ) return "";
|
if( !$paragraph ) return "";
|
||||||
|
|
||||||
|
$words = preg_split('/\s+/', $paragraph);
|
||||||
|
foreach ($words as $i => $word) {
|
||||||
|
if (preg_match('/(!|\?|\.)$/', $word) && !preg_match('/(Dr|Mr|Mrs|Ms|Miss|Sr|Jr|No)\.$/i', $word)) {
|
||||||
|
return implode(' ', array_slice($words, 0, $i+1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$sentences = explode( '.', $data );
|
/* If we didn't find a sentence ending, use the summary. We re-call rather than using paragraph so that
|
||||||
|
* Summary will limit the result this time */
|
||||||
if( count( $sentences ) )
|
|
||||||
return $sentences[0] . '.';
|
|
||||||
else
|
|
||||||
return $this->Summary(20);
|
return $this->Summary(20);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,6 +88,27 @@ class TextTest extends SapphireTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testFirstSentance() {
|
||||||
|
$cases = array(
|
||||||
|
'' => '',
|
||||||
|
'First sentence.' => 'First sentence.',
|
||||||
|
'First sentence. Second sentence' => 'First sentence.',
|
||||||
|
'First sentence? Second sentence' => 'First sentence?',
|
||||||
|
'First sentence! Second sentence' => 'First sentence!',
|
||||||
|
'<p>First sentence.</p>' => 'First sentence.',
|
||||||
|
'<p>First sentence. Second sentence. Third sentence</p>' => 'First sentence.',
|
||||||
|
'<p>First sentence. <em>Second sentence</em>. Third sentence</p>' => 'First sentence.',
|
||||||
|
'<p>First sentence. <em class="dummyClass">Second sentence</em>. Third sentence</p>'
|
||||||
|
=> 'First sentence.'
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach($cases as $originalValue => $expectedValue) {
|
||||||
|
$textObj = new Text('Test');
|
||||||
|
$textObj->setValue($originalValue);
|
||||||
|
$this->assertEquals($expectedValue, $textObj->FirstSentence());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test {@link Text->BigSummary()}
|
* Test {@link Text->BigSummary()}
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user