Skip to content
This repository

use html ellipsis if possible #779

Merged
merged 4 commits into from over 1 year ago

3 participants

Mark Mark Story ADmad
Mark
Collaborator

use html ellipsis if possible (only a single char and therefore does not line break or mess up the strlen count for length inside html content) .

Mark Story
Owner

Won't using u2026 cause invalid characters for any encoding that isn't UTF-8? I like this change, as people should be using utf-8 everywhere now. But there are still many legacy systems not using it :(

Mark
Collaborator

hmm, good point. what about:

if (!empty($options['html']) && Configure::read('App.encoding') == 'UTF-8') {}

at least until utf8 is really everybodys first choice

ADmad
Collaborator

In the interest of keeping options to a minimum we can just check if app uses UTF-8 and set default value for ellipsis to chr(226) and do away with the new 'html' option.

ADmad
Collaborator

Sorry my bad, 'html' option already exists.

Mark
Collaborator

thats what I did, ADmad :)

Mark Story
Owner

I think checking the encoding makes sense. I'm good with this as it stands.

Mark Story markstory merged commit 064fef1 into from
Mark Story markstory closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
41  lib/Cake/Test/Case/Utility/StringTest.php
@@ -380,17 +380,17 @@ public function testTruncate() {
380 380
 		$this->assertSame($this->Text->truncate($text3, 20), '<b>&copy; 2005-20...');
381 381
 		$this->assertSame($this->Text->truncate($text4, 15), '<img src="my...');
382 382
 		$this->assertSame($this->Text->truncate($text5, 6, array('ellipsis' => '')), '0<b>1<');
383  
-		$this->assertSame($this->Text->truncate($text1, 15, array('html' => true)), 'The quick br...');
384  
-		$this->assertSame($this->Text->truncate($text1, 15, array('exact' => false, 'html' => true)), 'The quick...');
385  
-		$this->assertSame($this->Text->truncate($text2, 10, array('html' => true)), 'Heiz&ouml;lr...');
386  
-		$this->assertSame($this->Text->truncate($text2, 10, array('exact' => false, 'html' => true)), '...');
387  
-		$this->assertSame($this->Text->truncate($text3, 20, array('html' => true)), '<b>&copy; 2005-2007, Cake...</b>');
388  
-		$this->assertSame($this->Text->truncate($text4, 15, array('html' => true)), '<img src="mypic.jpg"> This image ...');
389  
-		$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>');
390  
-		$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...');
  383
+		$this->assertSame($this->Text->truncate($text1, 15, array('html' => true)), 'The quick brow' . chr(226));
  384
+		$this->assertSame($this->Text->truncate($text1, 15, array('exact' => false, 'html' => true)), 'The quick' . chr(226));
  385
+		$this->assertSame($this->Text->truncate($text2, 10, array('html' => true)), 'Heiz&ouml;lr&uuml;c' . chr(226));
  386
+		$this->assertSame($this->Text->truncate($text2, 10, array('exact' => false, 'html' => true)), chr(226));
  387
+		$this->assertSame($this->Text->truncate($text3, 20, array('html' => true)), '<b>&copy; 2005-2007, Cake S' . chr(226) . '</b>');
  388
+		$this->assertSame($this->Text->truncate($text4, 15, array('html' => true)), '<img src="mypic.jpg"> This image ta' . chr(226));
  389
+		$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>');
  390
+		$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));
391 391
 		$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>');
392 392
 		$this->assertSame($this->Text->truncate($text5, 20, array('ellipsis' => '', 'html' => true)), $text5);
393  
-		$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>");
  393
+		$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>");
394 394
 		$this->assertSame($this->Text->truncate($text7, 255), $text7);
395 395
 		$this->assertSame($this->Text->truncate($text7, 15), 'El moño está...');
396 396
 		$this->assertSame($this->Text->truncate($text8, 15), 'Vive la R' . chr(195) . chr(169) . 'pu...');
@@ -460,6 +460,29 @@ public function testTruncate() {
460 460
 	}
461 461
 
462 462
 /**
  463
+ * testTruncate method with non utf8 sites
  464
+ *
  465
+ * @return void
  466
+ */
  467
+	public function testTruncateLegacy() {
  468
+		Configure::write('App.encoding', 'ISO-8859-1');
  469
+		$text = '<b>&copy; 2005-2007, Cake Software Foundation, Inc.</b><br />written by Alexander Wegener';
  470
+		$result = $this->Text->truncate($text, 31, array(
  471
+			'html' => true,
  472
+			'exact' => false,
  473
+		));
  474
+		$expected = '<b>&copy; 2005-2007, Cake Software...</b>';
  475
+		$this->assertEquals($expected, $result);
  476
+		
  477
+		$result = $this->Text->truncate($text, 31, array(
  478
+			'html' => true,
  479
+			'exact' => true,
  480
+		));
  481
+		$expected = '<b>&copy; 2005-2007, Cake Software F...</b>';
  482
+		$this->assertEquals($expected, $result);
  483
+	}
  484
+
  485
+/**
463 486
  * testTail method
464 487
  *
465 488
  * @return void
2  lib/Cake/Utility/String.php
@@ -482,6 +482,8 @@ public static function truncate($text, $length = 100, $options = array()) {
482 482
 		);
483 483
 		if (isset($options['ending'])) {
484 484
 			$default['ellipsis'] = $options['ending'];
  485
+		} elseif (!empty($options['html']) && Configure::read('App.encoding') == 'UTF-8') {
  486
+			$default['ellipsis'] = chr(226);
485 487
 		}
486 488
 		$options = array_merge($default, $options);
487 489
 		extract($options);
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.