Permalink
Browse files

Merge pull request #779 from dereuromark/2.3-ellipsis-html

use html ellipsis if possible
  • Loading branch information...
2 parents fe3d99c + 07d92ef commit 064fef1a15f90578ba560027ae42a24ba2bdcaa2 @markstory markstory committed Aug 23, 2012
Showing with 34 additions and 9 deletions.
  1. +32 −9 lib/Cake/Test/Case/Utility/StringTest.php
  2. +2 −0 lib/Cake/Utility/String.php
View
41 lib/Cake/Test/Case/Utility/StringTest.php
@@ -380,17 +380,17 @@ public function testTruncate() {
$this->assertSame($this->Text->truncate($text3, 20), '<b>&copy; 2005-20...');
$this->assertSame($this->Text->truncate($text4, 15), '<img src="my...');
$this->assertSame($this->Text->truncate($text5, 6, array('ellipsis' => '')), '0<b>1<');
- $this->assertSame($this->Text->truncate($text1, 15, array('html' => true)), 'The quick br...');
- $this->assertSame($this->Text->truncate($text1, 15, array('exact' => false, 'html' => true)), 'The quick...');
- $this->assertSame($this->Text->truncate($text2, 10, array('html' => true)), 'Heiz&ouml;lr...');
- $this->assertSame($this->Text->truncate($text2, 10, array('exact' => false, 'html' => true)), '...');
- $this->assertSame($this->Text->truncate($text3, 20, array('html' => true)), '<b>&copy; 2005-2007, Cake...</b>');
- $this->assertSame($this->Text->truncate($text4, 15, array('html' => true)), '<img src="mypic.jpg"> This image ...');
- $this->assertSame($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->assertSame($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->assertSame($this->Text->truncate($text1, 15, array('html' => true)), 'The quick brow' . chr(226));
+ $this->assertSame($this->Text->truncate($text1, 15, array('exact' => false, 'html' => true)), 'The quick' . chr(226));
+ $this->assertSame($this->Text->truncate($text2, 10, array('html' => true)), 'Heiz&ouml;lr&uuml;c' . chr(226));
+ $this->assertSame($this->Text->truncate($text2, 10, array('exact' => false, 'html' => true)), chr(226));
+ $this->assertSame($this->Text->truncate($text3, 20, array('html' => true)), '<b>&copy; 2005-2007, Cake S' . chr(226) . '</b>');
+ $this->assertSame($this->Text->truncate($text4, 15, array('html' => true)), '<img src="mypic.jpg"> This image ta' . chr(226));
+ $this->assertSame($this->Text->truncate($text4, 45, array('html' => true)), '<img src="mypic.jpg"> This image tag is not XHTML conform!<br><hr/><b>But the' . chr(226) . '</b>');
+ $this->assertSame($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 />Great,' . chr(226));
$this->assertSame($this->Text->truncate($text5, 6, array('ellipsis' => '', 'html' => true)), '0<b>1<i>2<span class="myclass">3</span>4<u>5</u></i></b>');
$this->assertSame($this->Text->truncate($text5, 20, array('ellipsis' => '', 'html' => true)), $text5);
- $this->assertSame($this->Text->truncate($text6, 57, array('exact' => false, 'html' => true)), "<p><strong>Extra dates have been announced for this year's...</strong></p>");
+ $this->assertSame($this->Text->truncate($text6, 57, array('exact' => false, 'html' => true)), "<p><strong>Extra dates have been announced for this year's" . chr(226) . "</strong></p>");
$this->assertSame($this->Text->truncate($text7, 255), $text7);
$this->assertSame($this->Text->truncate($text7, 15), 'El moño está...');
$this->assertSame($this->Text->truncate($text8, 15), 'Vive la R' . chr(195) . chr(169) . 'pu...');
@@ -460,6 +460,29 @@ public function testTruncate() {
}
/**
+ * testTruncate method with non utf8 sites
+ *
+ * @return void
+ */
+ public function testTruncateLegacy() {
+ Configure::write('App.encoding', 'ISO-8859-1');
+ $text = '<b>&copy; 2005-2007, Cake Software Foundation, Inc.</b><br />written by Alexander Wegener';
+ $result = $this->Text->truncate($text, 31, array(
+ 'html' => true,
+ 'exact' => false,
+ ));
+ $expected = '<b>&copy; 2005-2007, Cake Software...</b>';
+ $this->assertEquals($expected, $result);
+
+ $result = $this->Text->truncate($text, 31, array(
+ 'html' => true,
+ 'exact' => true,
+ ));
+ $expected = '<b>&copy; 2005-2007, Cake Software F...</b>';
+ $this->assertEquals($expected, $result);
+ }
+
+/**
* testTail method
*
* @return void
View
2 lib/Cake/Utility/String.php
@@ -482,6 +482,8 @@ public static function truncate($text, $length = 100, $options = array()) {
);
if (isset($options['ending'])) {
$default['ellipsis'] = $options['ending'];
+ } elseif (!empty($options['html']) && Configure::read('App.encoding') == 'UTF-8') {
+ $default['ellipsis'] = chr(226);
}
$options = array_merge($default, $options);
extract($options);

0 comments on commit 064fef1

Please sign in to comment.