From 8c2037a67749c6c3dbb3650821825f79ab615fa3 Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Thu, 19 Mar 2009 03:06:15 +0000 Subject: [PATCH] ENHANCEMENT Text->FirstParagraph?() now works for
containers in HTML, as you may not always have paragraph tags. MINOR Added phpDoc comments above Text->FirstParagraph?() to note down the assumptions made with this method. (Merged from r64271) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.2@73365 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/fieldtypes/Text.php | 36 +++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/core/model/fieldtypes/Text.php b/core/model/fieldtypes/Text.php index ae4927252..4307a46a9 100644 --- a/core/model/fieldtypes/Text.php +++ b/core/model/fieldtypes/Text.php @@ -197,30 +197,44 @@ class Text extends DBField { } /** - * Caution: Not XML/HTML-safe - does not respect closing tags. + * Take the first paragraph (the first

or

block in + * the content HTML that can be found) and returns it. + * + * CAUTION: Not XML/HTML-safe - does not respect closing tags. + * + * @param boolean $plain Return plain text (1) or (0) HTML text + * @return string The first paragraph of content */ function FirstParagraph($plain = 1) { // get first sentence? // this needs to be more robust if($plain && $plain != 'html') { $data = Convert::xml2raw( $this->value, true ); - if( !$data ) return ""; + if(!$data) return ''; // grab the first paragraph, or, failing that, the whole content - if( strpos( $data, "\n\n" ) ) - $data = substr( $data, 0, strpos( $data, "\n\n" ) ); + if(strpos( $data, "\n\n")) $data = substr( $data, 0, strpos( $data, "\n\n" ) ); return $data; - } else { - if(strpos( $this->value, "

" ) === false) return $this->value; - $data = substr( $this->value, 0, strpos( $this->value, "

" ) + 4 ); - - - if(strlen($data) < 20 && strpos( $this->value, "

", strlen($data) )) $data = substr( $this->value, 0, strpos( $this->value, "

", strlen($data) ) + 4 ); + // Find out what the container element for limit paragraphs should be + // (div or p is supported) + if(strpos($this->value, '
')) { + $containerElement = '
'; + } elseif(strpos($this->value, '

')) { + $containerElement = '

'; + } else { + return $this->value; + } - return $data; + $data = substr($this->value, 0, strpos($this->value, $containerElement) + strlen($containerElement)); + + if(strlen($data) < 20 && strpos($this->value, $containerElement, strlen($data))) { + $data = substr($this->value, 0, strpos($this->value, $containerElement, strlen($data)) + strlen($containerElement)); + } + + return $data; } }