From 7d97df9db8b3e93e12f2872ead3375bfd71c8e67 Mon Sep 17 00:00:00 2001 From: John Milmine Date: Mon, 26 May 2014 12:57:12 +1200 Subject: [PATCH] adding exclamation mark and question mark to delimiters, made text work the same as HTML Text --- model/fieldtypes/HTMLText.php | 2 +- model/fieldtypes/Text.php | 25 ++++++++++++++----------- tests/model/TextTest.php | 23 ++++++++++++++++++++++- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/model/fieldtypes/HTMLText.php b/model/fieldtypes/HTMLText.php index b47049744..21bb8548a 100644 --- a/model/fieldtypes/HTMLText.php +++ b/model/fieldtypes/HTMLText.php @@ -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 */ $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)) { + if (preg_match('/(!|\?|\.)$/', $word) && !preg_match('/(Dr|Mr|Mrs|Ms|Miss|Sr|Jr|No)\.$/i', $word)) { return implode(' ', array_slice($words, 0, $i+1)); } } diff --git a/model/fieldtypes/Text.php b/model/fieldtypes/Text.php index 63d057381..4829565f2 100644 --- a/model/fieldtypes/Text.php +++ b/model/fieldtypes/Text.php @@ -101,17 +101,20 @@ class Text extends StringField { * Caution: Not XML/HTML-safe - does not respect closing tags. */ public function FirstSentence() { - $data = Convert::xml2raw( $this->value ); - if( !$data ) return ""; - - - $sentences = explode( '.', $data ); - - if( count( $sentences ) ) - return $sentences[0] . '.'; - else - return $this->Summary(20); - } + $paragraph = Convert::xml2raw( $this->value ); + 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)); + } + } + + /* 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 */ + return $this->Summary(20); + } /** * Caution: Not XML/HTML-safe - does not respect closing tags. diff --git a/tests/model/TextTest.php b/tests/model/TextTest.php index 282792d65..d9eac9b9e 100644 --- a/tests/model/TextTest.php +++ b/tests/model/TextTest.php @@ -87,7 +87,28 @@ class TextTest extends SapphireTest { $this->assertEquals($expectedValue, $textObj->LimitSentences(2)); } } - + + 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!', + '

First sentence.

' => 'First sentence.', + '

First sentence. Second sentence. Third sentence

' => 'First sentence.', + '

First sentence. Second sentence. Third sentence

' => 'First sentence.', + '

First sentence. Second sentence. Third sentence

' + => 'First sentence.' + ); + + foreach($cases as $originalValue => $expectedValue) { + $textObj = new Text('Test'); + $textObj->setValue($originalValue); + $this->assertEquals($expectedValue, $textObj->FirstSentence()); + } + } + /** * Test {@link Text->BigSummary()} */