diff --git a/lib/Cake/Test/Case/View/Helper/TextHelperTest.php b/lib/Cake/Test/Case/View/Helper/TextHelperTest.php index 6b8ec77a5..20c8728d7 100644 --- a/lib/Cake/Test/Case/View/Helper/TextHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/TextHelperTest.php @@ -334,4 +334,58 @@ class TextHelperTest extends CakeTestCase { $this->assertEquals($expected, $result); } +/** + * testAutoParagraph method + * + * @return void + */ + public function testAutoParagraph() { + $text = 'This is a test text'; + $expected = <<This is a test text

+ +TEXT; + $result = $this->Text->autoParagraph($text); + $text = 'This is a

test text'; + $expected = <<This is a

+

test text

+ +TEXT; + $result = $this->Text->autoParagraph($text); + $this->assertEquals($expected, $result); + $result = $this->Text->autoParagraph($text); + $text = 'This is a

test text'; + $expected = <<This is a

+

test text

+ +TEXT; + $result = $this->Text->autoParagraph($text); + $this->assertEquals($expected, $result); + $text = <<This is a test text.
+This is a line return.

+ +TEXT; + $result = $this->Text->autoParagraph($text); + $this->assertEquals($expected, $result); + $text = <<This is a test text.

+

This is a new line.

+ +TEXT; + $result = $this->Text->autoParagraph($text); + $this->assertEquals($expected, $result); + } + } diff --git a/lib/Cake/View/Helper/TextHelper.php b/lib/Cake/View/Helper/TextHelper.php index 4951583b0..d5df9288f 100644 --- a/lib/Cake/View/Helper/TextHelper.php +++ b/lib/Cake/View/Helper/TextHelper.php @@ -228,6 +228,29 @@ class TextHelper extends AppHelper { return $this->_engine->highlight($text, $phrase, $options); } +/** + * Formats paragraphs around given text for all line breaks + *
added for single line return + *

added for double line return + * + * @param string $text Text + * @return string The text with proper

and
tags + * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoParagraph + */ + public function autoParagraph($text) { + if (trim($text) !== '') { + $text = preg_replace('|]*>\s*]*>|i', "\n\n", $text . "\n"); + $text = preg_replace("/\n\n+/", "\n\n", str_replace(array("\r\n", "\r"), "\n", $text)); + $texts = preg_split('/\n\s*\n/', $text, -1, PREG_SPLIT_NO_EMPTY); + $text = ''; + foreach ($texts as $txt) { + $text .= '

' . nl2br(trim($txt, "\n")) . "

\n"; + } + $text = preg_replace('|

\s*

|', '', $text); + } + return $text; + } + /** * @see String::stripLinks() *