From 22b514c42196dca9aa088048314d190e5fc74a13 Mon Sep 17 00:00:00 2001 From: Simon Gow Date: Fri, 19 Jul 2019 12:43:20 +1200 Subject: [PATCH] #9114 - DBText::ContextSummary() cuts line breaks ContextSummary() was cutting the HTML which was added by nl2br because it expected plain text elements as it's stripping and replacing text. Instead this fix changes the behaviour to apply the nl2br after the text changes have been made. That way we can't cut anything in the middle of a HTML tag, but new lines, or paragraphs are replaced by BRs after, should they exist. - Added tests to ensure text is not cut in the middle of a sentence. - Added test to ensure that
's are added in the correct place should the summary span between new lines. --- src/ORM/FieldType/DBText.php | 4 ++-- tests/php/ORM/DBHTMLTextTest.php | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/ORM/FieldType/DBText.php b/src/ORM/FieldType/DBText.php index e64d4c421..88edf16a8 100644 --- a/src/ORM/FieldType/DBText.php +++ b/src/ORM/FieldType/DBText.php @@ -194,7 +194,7 @@ class DBText extends DBString } // Get raw text value, but XML encode it (as we'll be merging with HTML tags soon) - $text = nl2br(Convert::raw2xml($this->Plain())); + $text = Convert::raw2xml($this->Plain()); $keywords = Convert::raw2xml($keywords); // Find the search string @@ -239,7 +239,7 @@ class DBText extends DBString $summary = $summary . $suffix; } - return $summary; + return nl2br($summary); } public function scaffoldFormField($title = null, $params = null) diff --git a/tests/php/ORM/DBHTMLTextTest.php b/tests/php/ORM/DBHTMLTextTest.php index 1070755b1..068951707 100644 --- a/tests/php/ORM/DBHTMLTextTest.php +++ b/tests/php/ORM/DBHTMLTextTest.php @@ -333,6 +333,22 @@ class DBHTMLTextTest extends SapphireTest 'ate', // it should highlight 3 letters or more. 'A dog ate a cat while looking at a Foobar', + ], + [ + '

This is a lot of text before this but really, this is a test sentence

+

with about more stuff after the line break

', + 35, + 'test', + '... really, this is a test sentence...' + ], + [ + '

This is a lot of text before this but really, this is a test sentence

+

with about more stuff after the line break

', + 50, + 'with', + '... sentence
+
+ with about more stuff...' ] ]; }