mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
Added autoParagraph to TextHelper with proper test cases and made it non-static
This commit is contained in:
parent
d1c88ebf8a
commit
d260f4a5b3
2 changed files with 77 additions and 0 deletions
|
@ -334,4 +334,58 @@ class TextHelperTest extends CakeTestCase {
|
||||||
$this->assertEquals($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* testAutoParagraph method
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testAutoParagraph() {
|
||||||
|
$text = 'This is a test text';
|
||||||
|
$expected = <<<TEXT
|
||||||
|
<p>This is a test text</p>
|
||||||
|
|
||||||
|
TEXT;
|
||||||
|
$result = $this->Text->autoParagraph($text);
|
||||||
|
$text = 'This is a <br/> <BR> test text';
|
||||||
|
$expected = <<<TEXT
|
||||||
|
<p>This is a </p>
|
||||||
|
<p> test text</p>
|
||||||
|
|
||||||
|
TEXT;
|
||||||
|
$result = $this->Text->autoParagraph($text);
|
||||||
|
$this->assertEquals($expected, $result);
|
||||||
|
$result = $this->Text->autoParagraph($text);
|
||||||
|
$text = 'This is a <BR id="test"/><br class="test"> test text';
|
||||||
|
$expected = <<<TEXT
|
||||||
|
<p>This is a </p>
|
||||||
|
<p> test text</p>
|
||||||
|
|
||||||
|
TEXT;
|
||||||
|
$result = $this->Text->autoParagraph($text);
|
||||||
|
$this->assertEquals($expected, $result);
|
||||||
|
$text = <<<TEXT
|
||||||
|
This is a test text.
|
||||||
|
This is a line return.
|
||||||
|
TEXT;
|
||||||
|
$expected = <<<TEXT
|
||||||
|
<p>This is a test text.<br />
|
||||||
|
This is a line return.</p>
|
||||||
|
|
||||||
|
TEXT;
|
||||||
|
$result = $this->Text->autoParagraph($text);
|
||||||
|
$this->assertEquals($expected, $result);
|
||||||
|
$text = <<<TEXT
|
||||||
|
This is a test text.
|
||||||
|
|
||||||
|
This is a new line.
|
||||||
|
TEXT;
|
||||||
|
$expected = <<<TEXT
|
||||||
|
<p>This is a test text.</p>
|
||||||
|
<p>This is a new line.</p>
|
||||||
|
|
||||||
|
TEXT;
|
||||||
|
$result = $this->Text->autoParagraph($text);
|
||||||
|
$this->assertEquals($expected, $result);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -228,6 +228,29 @@ class TextHelper extends AppHelper {
|
||||||
return $this->_engine->highlight($text, $phrase, $options);
|
return $this->_engine->highlight($text, $phrase, $options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Formats paragraphs around given text for all line breaks
|
||||||
|
* <br /> added for single line return
|
||||||
|
* <p> added for double line return
|
||||||
|
*
|
||||||
|
* @param string $text Text
|
||||||
|
* @return string The text with proper <p> and <br /> 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('|<br[^>]*>\s*<br[^>]*>|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 .= '<p>' . nl2br(trim($txt, "\n")) . "</p>\n";
|
||||||
|
}
|
||||||
|
$text = preg_replace('|<p>\s*</p>|', '', $text);
|
||||||
|
}
|
||||||
|
return $text;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see String::stripLinks()
|
* @see String::stripLinks()
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue