Merge branch '1.3-misc' of dev@code.cakephp.org:cakephp into 1.3-misc

This commit is contained in:
mark_story 2009-10-29 09:51:06 -04:00
commit 84ec53a039
2 changed files with 85 additions and 72 deletions

View file

@ -46,43 +46,51 @@ class TextHelper extends AppHelper {
* Highlights a given phrase in a text. You can specify any expression in highlighter that
* may include the \1 expression to include the $phrase found.
*
* Options:
*
* - 'format' The piece of html with that the phrase will be highlighted
* - 'html' If true, will ignore any HTML tags, ensuring that only the correct text is highlighted
*
* @param string $text Text to search the phrase in
* @param string $phrase The phrase that will be searched
* @param string $highlighter The piece of html with that the phrase will be highlighted
* @param boolean $considerHtml If true, will ignore any HTML tags, ensuring that only the correct text is highlighted
* @param array $options An array of html attributes and options.
* @return string The highlighted text
* @access public
*/
function highlight($text, $phrase, $highlighter = '<span class="highlight">\1</span>', $considerHtml = false) {
function highlight($text, $phrase, $options = array()) {
if (empty($phrase)) {
return $text;
}
if (is_array($phrase)) {
$default = array(
'format' => '<span class="highlight">\1</span>',
'html' => false
);
$options = array_merge($default, $options);
extract($options);
if (is_array($phrase)) {
$replace = array();
$with = array();
foreach ($phrase as $key => $segment) {
$segment = "($segment)";
if ($considerHtml) {
if ($html) {
$segment = "(?![^<]+>)$segment(?![^<]+>)";
}
$with[] = (is_array($highlighter)) ? $highlighter[$key] : $highlighter;
$with[] = (is_array($format)) ? $format[$key] : $format;
$replace[] = "|$segment|iu";
}
return preg_replace($replace, $with, $text);
} else {
$phrase = "($phrase)";
if ($considerHtml) {
if ($html) {
$phrase = "(?![^<]+>)$phrase(?![^<]+>)";
}
return preg_replace("|$phrase|iu", $highlighter, $text);
return preg_replace("|$phrase|iu", $format, $text);
}
}
@ -102,56 +110,55 @@ class TextHelper extends AppHelper {
* strings like http:// and ftp://.
*
* @param string $text Text to add links to
* @param array $htmlOptions Array of HTML options.
* @param array $options Array of HTML options.
* @return string The text with links
* @access public
*/
function autoLinkUrls($text, $htmlOptions = array()) {
$options = 'array(';
foreach ($htmlOptions as $option => $value) {
function autoLinkUrls($text, $options = array()) {
$linkOptions = 'array(';
foreach ($options as $option => $value) {
$value = var_export($value, true);
$options .= "'$option' => $value, ";
$linkOptions .= "'$option' => $value, ";
}
$options .= ')';
$linkOptions .= ')';
$text = preg_replace_callback('#(?<!href="|">)((?:http|https|ftp|nntp)://[^ <]+)#i', create_function('$matches',
'$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], $matches[0],' . $options . ');'), $text);
'$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], $matches[0],' . $linkOptions . ');'), $text);
return preg_replace_callback('#(?<!href="|">)(?<!http://|https://|ftp://|nntp://)(www\.[^\n\%\ <]+[^<\n\%\,\.\ <])(?<!\))#i',
create_function('$matches', '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], "http://" . strtolower($matches[0]),' . $options . ');'), $text);
create_function('$matches', '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], "http://" . strtolower($matches[0]),' . $linkOptions . ');'), $text);
}
/**
* Adds email links (<a href="mailto:....) to a given text.
*
* @param string $text Text
* @param array $htmlOptions Array of HTML options.
* @param array $options Array of HTML options.
* @return string The text with links
* @access public
*/
function autoLinkEmails($text, $htmlOptions = array()) {
$options = 'array(';
foreach ($htmlOptions as $option => $value) {
function autoLinkEmails($text, $options = array()) {
$linkOptions = 'array(';
foreach ($options as $option => $value) {
$value = var_export($value, true);
$options .= "'$option' => $value, ";
$linkOptions .= "'$option' => $value, ";
}
$options .= ')';
$linkOptions .= ')';
return preg_replace_callback('#([_A-Za-z0-9+-]+(?:\.[_A-Za-z0-9+-]+)*@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*)#',
create_function('$matches', '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], "mailto:" . $matches[0],' . $options . ');'), $text);
create_function('$matches', '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], "mailto:" . $matches[0],' . $linkOptions . ');'), $text);
}
/**
* Convert all links and email adresses to HTML links.
*
* @param string $text Text
* @param array $htmlOptions Array of HTML options.
* @param array $options Array of HTML options.
* @return string The text with links
* @access public
*/
function autoLink($text, $htmlOptions = array()) {
return $this->autoLinkEmails($this->autoLinkUrls($text, $htmlOptions), $htmlOptions);
function autoLink($text, $options = array()) {
return $this->autoLinkEmails($this->autoLinkUrls($text, $options), $options);
}
/**
@ -160,24 +167,32 @@ class TextHelper extends AppHelper {
* Cuts a string to the length of $length and replaces the last characters
* with the ending if the text is longer than length.
*
* Options:
*
* - 'ending' Will be used as Ending and appended to the trimmed string
* - 'exact' If false, $text will not be cut mid-word
* - 'html' If true, HTML tags would be handled correctly
*
* @param string $text String to truncate.
* @param integer $length Length of returned string, including ellipsis.
* @param mixed $ending If string, will be used as Ending and appended to the trimmed string. Can also be an associative array that can contain the last three params of this method.
* @param boolean $exact If false, $text will not be cut mid-word
* @param boolean $considerHtml If true, HTML tags would be handled correctly
* @param array $options An array of html attributes and options.
* @return string Trimmed string.
*/
function truncate($text, $length = 100, $ending = '...', $exact = true, $considerHtml = false) {
if (is_array($ending)) {
extract($ending);
}
if ($considerHtml) {
function truncate($text, $length = 100, $options = array()) {
$default = array(
'ending' => '...', 'exact' => true, 'html' => false
);
$options = array_merge($default, $options);
extract($options);
if ($html) {
if (mb_strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
return $text;
}
$totalLength = mb_strlen($ending);
$openTags = array();
$truncate = '';
preg_match_all('/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER);
foreach ($tags as $tag) {
if (!preg_match('/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s', $tag[2])) {
@ -217,7 +232,6 @@ class TextHelper extends AppHelper {
break;
}
}
} else {
if (mb_strlen($text) <= $length) {
return $text;
@ -228,7 +242,7 @@ class TextHelper extends AppHelper {
if (!$exact) {
$spacepos = mb_strrpos($truncate, ' ');
if (isset($spacepos)) {
if ($considerHtml) {
if ($html) {
$bits = mb_substr($truncate, $spacepos);
preg_match_all('/<\/([a-z]+)>/', $bits, $droppedTags, PREG_SET_ORDER);
if (!empty($droppedTags)) {
@ -242,10 +256,9 @@ class TextHelper extends AppHelper {
$truncate = mb_substr($truncate, 0, $spacepos);
}
}
$truncate .= $ending;
if ($considerHtml) {
if ($html) {
foreach ($openTags as $tag) {
$truncate .= '</'.$tag.'>';
}
@ -276,9 +289,9 @@ class TextHelper extends AppHelper {
* @return string Modified string
* @access public
*/
function excerpt($text, $phrase, $radius = 100, $ending = "...") {
function excerpt($text, $phrase, $radius = 100, $ending = '...') {
if (empty($text) or empty($phrase)) {
return $this->truncate($text, $radius * 2, $ending);
return $this->truncate($text, $radius * 2, array('ending' => $ending));
}
$phraseLen = mb_strlen($phrase);

View file

@ -77,25 +77,24 @@ class TextHelperTest extends CakeTestCase {
$text9 = 'НОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыь';
$this->assertIdentical($this->Text->truncate($text1, 15), 'The quick br...');
$this->assertIdentical($this->Text->truncate($text1, 15, '...', false), 'The quick...');
$this->assertIdentical($this->Text->truncate($text1, 15, array('exact' => false)), 'The quick...');
$this->assertIdentical($this->Text->truncate($text1, 100), 'The quick brown fox jumps over the lazy dog');
$this->assertIdentical($this->Text->truncate($text2, 10, '...'), 'Heiz&ou...');
$this->assertIdentical($this->Text->truncate($text2, 10, '...', false), '...');
$this->assertIdentical($this->Text->truncate($text2, 10), 'Heiz&ou...');
$this->assertIdentical($this->Text->truncate($text2, 10, array('exact' => false)), '...');
$this->assertIdentical($this->Text->truncate($text3, 20), '<b>&copy; 2005-20...');
$this->assertIdentical($this->Text->truncate($text4, 15), '<img src="my...');
$this->assertIdentical($this->Text->truncate($text5, 6, ''), '0<b>1<');
$this->assertIdentical($this->Text->truncate($text1, 15, array('ending' => '...', 'exact' => true, 'considerHtml' => true)), 'The quick br...');
$this->assertIdentical($this->Text->truncate($text1, 15, '...', true, true), 'The quick br...');
$this->assertIdentical($this->Text->truncate($text1, 15, '...', false, true), 'The quick...');
$this->assertIdentical($this->Text->truncate($text2, 10, '...', true, true), 'Heiz&ouml;lr...');
$this->assertIdentical($this->Text->truncate($text2, 10, '...', false, true), '...');
$this->assertIdentical($this->Text->truncate($text3, 20, '...', true, true), '<b>&copy; 2005-2007, Cake...</b>');
$this->assertIdentical($this->Text->truncate($text4, 15, '...', true, true), '<img src="mypic.jpg"> This image ...');
$this->assertIdentical($this->Text->truncate($text4, 45, '...', true, true), '<img src="mypic.jpg"> This image tag is not XHTML conform!<br><hr/><b>But t...</b>');
$this->assertIdentical($this->Text->truncate($text4, 90, '...', true, true), '<img src="mypic.jpg"> This image tag is not XHTML conform!<br><hr/><b>But the following image tag should be conform <img src="mypic.jpg" alt="Me, myself and I" /></b><br />Grea...');
$this->assertIdentical($this->Text->truncate($text5, 6, '', true, true), '0<b>1<i>2<span class="myclass">3</span>4<u>5</u></i></b>');
$this->assertIdentical($this->Text->truncate($text5, 20, '', true, true), $text5);
$this->assertIdentical($this->Text->truncate($text6, 57, '...', false, true), "<p><strong>Extra dates have been announced for this year's...</strong></p>");
$this->assertIdentical($this->Text->truncate($text5, 6, array('ending' => '')), '0<b>1<');
$this->assertIdentical($this->Text->truncate($text1, 15, array('html' => true)), 'The quick br...');
$this->assertIdentical($this->Text->truncate($text1, 15, array('exact' => false, 'html' => true)), 'The quick...');
$this->assertIdentical($this->Text->truncate($text2, 10, array('html' => true)), 'Heiz&ouml;lr...');
$this->assertIdentical($this->Text->truncate($text2, 10, array('exact' => false, 'html' => true)), '...');
$this->assertIdentical($this->Text->truncate($text3, 20, array('html' => true)), '<b>&copy; 2005-2007, Cake...</b>');
$this->assertIdentical($this->Text->truncate($text4, 15, array('html' => true)), '<img src="mypic.jpg"> This image ...');
$this->assertIdentical($this->Text->truncate($text4, 45, array('html' => true)), '<img src="mypic.jpg"> This image tag is not XHTML conform!<br><hr/><b>But t...</b>');
$this->assertIdentical($this->Text->truncate($text4, 90, array('html' => true)), '<img src="mypic.jpg"> This image tag is not XHTML conform!<br><hr/><b>But the following image tag should be conform <img src="mypic.jpg" alt="Me, myself and I" /></b><br />Grea...');
$this->assertIdentical($this->Text->truncate($text5, 6, array('ending' => '', 'html' => true)), '0<b>1<i>2<span class="myclass">3</span>4<u>5</u></i></b>');
$this->assertIdentical($this->Text->truncate($text5, 20, array('ending' => '', 'html' => true)), $text5);
$this->assertIdentical($this->Text->truncate($text6, 57, array('exact' => false, 'html' => true)), "<p><strong>Extra dates have been announced for this year's...</strong></p>");
$this->assertIdentical($this->Text->truncate($text7, 255), $text7);
$this->assertIdentical($this->Text->truncate($text7, 15), 'El moño está...');
$this->assertIdentical($this->Text->truncate($text8, 15), 'Vive la R'.chr(195).chr(169).'pu...');
@ -111,46 +110,47 @@ class TextHelperTest extends CakeTestCase {
function testHighlight() {
$text = 'This is a test text';
$phrases = array('This', 'text');
$result = $this->Text->highlight($text, $phrases, '<b>\1</b>');
$result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
$expected = '<b>This</b> is a test <b>text</b>';
$this->assertEqual($expected, $result);
$text = 'This is a test text';
$phrases = null;
$result = $this->Text->highlight($text, $phrases, '<b>\1</b>');
$result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
$this->assertEqual($result, $text);
$text = 'Ich saß in einem Café am Übergang';
$expected = 'Ich <b>saß</b> in einem <b>Café</b> am <b>Übergang</b>';
$phrases = array('saß', 'café', 'übergang');
$result = $this->Text->highlight($text, $phrases, '<b>\1</b>');
$result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
$this->assertEqual($result, $expected);
}
/**
* testHighlightConsiderHtml method
* testHighlightHtml method
*
* @access public
* @return void
*/
function testHighlightConsiderHtml() {
function testHighlightHtml() {
$text1 = '<p>strongbow isn&rsquo;t real cider</p>';
$text2 = '<p>strongbow <strong>isn&rsquo;t</strong> real cider</p>';
$text3 = '<img src="what-a-strong-mouse.png" alt="What a strong mouse!" />';
$text4 = 'What a strong mouse: <img src="what-a-strong-mouse.png" alt="What a strong mouse!" />';
$options = array('format' => '<b>\1</b>', 'html' => true);
$expected = '<p><b>strong</b>bow isn&rsquo;t real cider</p>';
$this->assertEqual($this->Text->highlight($text1, 'strong', '<b>\1</b>', true), $expected);
$this->assertEqual($this->Text->highlight($text1, 'strong', $options), $expected);
$expected = '<p><b>strong</b>bow <strong>isn&rsquo;t</strong> real cider</p>';
$this->assertEqual($this->Text->highlight($text2, 'strong', '<b>\1</b>', true), $expected);
$this->assertEqual($this->Text->highlight($text2, 'strong', $options), $expected);
$this->assertEqual($this->Text->highlight($text3, 'strong', '<b>\1</b>', true), $text3);
$this->assertEqual($this->Text->highlight($text3, 'strong', $options), $text3);
$this->assertEqual($this->Text->highlight($text3, array('strong', 'what'), '<b>\1</b>', true), $text3);
$this->assertEqual($this->Text->highlight($text3, array('strong', 'what'), $options), $text3);
$expected = '<b>What</b> a <b>strong</b> mouse: <img src="what-a-strong-mouse.png" alt="What a strong mouse!" />';
$this->assertEqual($this->Text->highlight($text4, array('strong', 'what'), '<b>\1</b>', true), $expected);
$this->assertEqual($this->Text->highlight($text4, array('strong', 'what'), $options), $expected);
}
/**
@ -162,7 +162,7 @@ class TextHelperTest extends CakeTestCase {
function testHighlightMulti() {
$text = 'This is a test text';
$phrases = array('This', 'text');
$result = $this->Text->highlight($text, $phrases, array('<b>\1</b>', '<em>\1</em>'));
$result = $this->Text->highlight($text, $phrases, array('format' => array('<b>\1</b>', '<em>\1</em>')));
$expected = '<b>This</b> is a test <em>text</em>';
$this->assertEqual($expected, $result);
@ -286,10 +286,10 @@ class TextHelperTest extends CakeTestCase {
$text = 'This is a Test text';
$expected = 'This is a <b>Test</b> text';
$result = $this->Text->highlight($text, 'test', '<b>\1</b>');
$result = $this->Text->highlight($text, 'test', array('format' => '<b>\1</b>'));
$this->assertEqual($expected, $result);
$result = $this->Text->highlight($text, array('test'), '<b>\1</b>');
$result = $this->Text->highlight($text, array('test'), array('format' => '<b>\1</b>'));
$this->assertEqual($expected, $result);
}