Skip to content
This repository
Browse code

ellipsis instead of ending/beginning for core wide consistency

  • Loading branch information...
commit 2a570e639ca6ecf359ff13c9358577e4079b827a 1 parent 853fa7d
Mark authored
29  lib/Cake/Test/Case/Utility/StringTest.php
@@ -379,7 +379,7 @@ public function testTruncate() {
379 379
 		$this->assertSame($this->Text->truncate($text2, 10, array('exact' => false)), '...');
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  
-		$this->assertSame($this->Text->truncate($text5, 6, array('ending' => '')), '0<b>1<');
  382
+		$this->assertSame($this->Text->truncate($text5, 6, array('ellipsis' => '')), '0<b>1<');
383 383
 		$this->assertSame($this->Text->truncate($text1, 15, array('html' => true)), 'The quick br...');
384 384
 		$this->assertSame($this->Text->truncate($text1, 15, array('exact' => false, 'html' => true)), 'The quick...');
385 385
 		$this->assertSame($this->Text->truncate($text2, 10, array('html' => true)), 'Heiz&ouml;lr...');
@@ -388,8 +388,8 @@ public function testTruncate() {
388 388
 		$this->assertSame($this->Text->truncate($text4, 15, array('html' => true)), '<img src="mypic.jpg"> This image ...');
389 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 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...');
391  
-		$this->assertSame($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>');
392  
-		$this->assertSame($this->Text->truncate($text5, 20, array('ending' => '', 'html' => true)), $text5);
  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
+		$this->assertSame($this->Text->truncate($text5, 20, array('ellipsis' => '', 'html' => true)), $text5);
393 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>");
394 394
 		$this->assertSame($this->Text->truncate($text7, 255), $text7);
395 395
 		$this->assertSame($this->Text->truncate($text7, 15), 'El moño está...');
@@ -399,7 +399,7 @@ public function testTruncate() {
399 399
 
400 400
 		$text = '<p><span style="font-size: medium;"><a>Iamatestwithnospacesandhtml</a></span></p>';
401 401
 		$result = $this->Text->truncate($text, 10, array(
402  
-			'ending' => '...',
  402
+			'ellipsis' => '...',
403 403
 			'exact' => false,
404 404
 			'html' => true
405 405
 		));
@@ -422,7 +422,7 @@ public function testTruncate() {
422 422
 <p><span style="font-size: medium;"><a>http://www.amazon.com/Steve-
423 423
 Jobs-Walter-Isaacson/dp/1451648537</a></span></p>';
424 424
 		$result = $this->Text->truncate($text, 500, array(
425  
-			'ending' => '... ',
  425
+			'ellipsis' => '... ',
426 426
 			'exact' => false,
427 427
 			'html' => true
428 428
 		));
@@ -441,6 +441,22 @@ public function testTruncate() {
441 441
 podeís adquirirla.</span></p>
442 442
 <p><span style="font-size: medium;"><a>... </a></span></p>';
443 443
 		$this->assertEquals($expected, $result);
  444
+
  445
+		// test deprecated `ending` (`ellipsis` taking precedence if both are defined)
  446
+		$result = $this->Text->truncate($text1, 31, array(
  447
+			'ending' => '.',
  448
+			'exact' => false,
  449
+		));
  450
+		$expected = 'The quick brown fox jumps.';
  451
+		$this->assertEquals($expected, $result);
  452
+
  453
+		$result = $this->Text->truncate($text1, 31, array(
  454
+			'ellipsis' => '..',
  455
+			'ending' => '.',
  456
+			'exact' => false,
  457
+		));
  458
+		$expected = 'The quick brown fox jumps..';
  459
+		$this->assertEquals($expected, $result);
444 460
 	}
445 461
 
446 462
 /**
@@ -481,6 +497,9 @@ public function testTail() {
481 497
 
482 498
 		$result = $this->Text->tail($text5, 10);
483 499
 		$this->assertEquals('...цчшщъыь', $result);
  500
+
  501
+		$result = $this->Text->tail($text5, 6, array('ellipsis' => ''));
  502
+		$this->assertEquals('чшщъыь', $result);
484 503
 	}
485 504
 
486 505
 /**
35  lib/Cake/Utility/String.php
@@ -422,11 +422,11 @@ public static function stripLinks($text) {
422 422
  * Truncates text starting from the end.
423 423
  *
424 424
  * Cuts a string to the length of $length and replaces the first characters
425  
- * with the beginning if the text is longer than length.
  425
+ * with the ellipsis if the text is longer than length.
426 426
  *
427 427
  * ### Options:
428 428
  *
429  
- * - `beginning` Will be used as Beginning and prepended to the trimmed string
  429
+ * - `ellipsis` Will be used as Beginning and prepended to the trimmed string
430 430
  * - `exact` If false, $text will not be cut mid-word
431 431
  *
432 432
  * @param string $text String to truncate.
@@ -436,7 +436,7 @@ public static function stripLinks($text) {
436 436
  */
437 437
 	public static function tail($text, $length = 100, $options = array()) {
438 438
 		$default = array(
439  
-			'beginning' => '...', 'exact' => true
  439
+			'ellipsis' => '...', 'exact' => true
440 440
 		);
441 441
 		$options = array_merge($default, $options);
442 442
 		extract($options);
@@ -448,25 +448,25 @@ class_exists('Multibyte');
448 448
 		if (mb_strlen($text) <= $length) {
449 449
 			return $text;
450 450
 		} else {
451  
-			$truncate = mb_substr($text, mb_strlen($text) - $length + mb_strlen($beginning));
  451
+			$truncate = mb_substr($text, mb_strlen($text) - $length + mb_strlen($ellipsis));
452 452
 		}
453 453
 		if (!$exact) {
454 454
 			$spacepos = mb_strpos($truncate, ' ');
455 455
 			$truncate = $spacepos === false ? '' : trim(mb_substr($truncate, $spacepos));
456 456
 		}
457 457
 
458  
-		return $beginning . $truncate;
  458
+		return $ellipsis . $truncate;
459 459
 	}
460 460
 
461 461
 /**
462 462
  * Truncates text.
463 463
  *
464 464
  * Cuts a string to the length of $length and replaces the last characters
465  
- * with the ending if the text is longer than length.
  465
+ * with the ellipsis if the text is longer than length.
466 466
  *
467 467
  * ### Options:
468 468
  *
469  
- * - `ending` Will be used as Ending and appended to the trimmed string
  469
+ * - `ellipsis` Will be used as Ending and appended to the trimmed string (`ending` is deprecated)
470 470
  * - `exact` If false, $text will not be cut mid-word
471 471
  * - `html` If true, HTML tags would be handled correctly
472 472
  *
@@ -478,8 +478,11 @@ class_exists('Multibyte');
478 478
  */
479 479
 	public static function truncate($text, $length = 100, $options = array()) {
480 480
 		$default = array(
481  
-			'ending' => '...', 'exact' => true, 'html' => false
  481
+			'ellipsis' => '...', 'exact' => true, 'html' => false
482 482
 		);
  483
+		if (isset($options['ending'])) {
  484
+			$default['ellipsis'] = $options['ending'];
  485
+		}
483 486
 		$options = array_merge($default, $options);
484 487
 		extract($options);
485 488
 
@@ -491,7 +494,7 @@ class_exists('Multibyte');
491 494
 			if (mb_strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
492 495
 				return $text;
493 496
 			}
494  
-			$totalLength = mb_strlen(strip_tags($ending));
  497
+			$totalLength = mb_strlen(strip_tags($ellipsis));
495 498
 			$openTags = array();
496 499
 			$truncate = '';
497 500
 
@@ -538,7 +541,7 @@ class_exists('Multibyte');
538 541
 			if (mb_strlen($text) <= $length) {
539 542
 				return $text;
540 543
 			} else {
541  
-				$truncate = mb_substr($text, 0, $length - mb_strlen($ending));
  544
+				$truncate = mb_substr($text, 0, $length - mb_strlen($ellipsis));
542 545
 			}
543 546
 		}
544 547
 		if (!$exact) {
@@ -570,7 +573,7 @@ class_exists('Multibyte');
570 573
 			}
571 574
 			$truncate = mb_substr($truncate, 0, $spacepos);
572 575
 		}
573  
-		$truncate .= $ending;
  576
+		$truncate .= $ellipsis;
574 577
 
575 578
 		if ($html) {
576 579
 			foreach ($openTags as $tag) {
@@ -588,23 +591,23 @@ class_exists('Multibyte');
588 591
  * @param string $text String to search the phrase in
589 592
  * @param string $phrase Phrase that will be searched for
590 593
  * @param integer $radius The amount of characters that will be returned on each side of the founded phrase
591  
- * @param string $ending Ending that will be appended
  594
+ * @param string $ellipsis Ending that will be appended
592 595
  * @return string Modified string
593 596
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::excerpt
594 597
  */
595  
-	public static function excerpt($text, $phrase, $radius = 100, $ending = '...') {
  598
+	public static function excerpt($text, $phrase, $radius = 100, $ellipsis = '...') {
596 599
 		if (empty($text) || empty($phrase)) {
597  
-			return self::truncate($text, $radius * 2, array('ending' => $ending));
  600
+			return self::truncate($text, $radius * 2, array('ellipsis' => $ellipsis));
598 601
 		}
599 602
 
600  
-		$append = $prepend = $ending;
  603
+		$append = $prepend = $ellipsis;
601 604
 
602 605
 		$phraseLen = mb_strlen($phrase);
603 606
 		$textLen = mb_strlen($text);
604 607
 
605 608
 		$pos = mb_strpos(mb_strtolower($text), mb_strtolower($phrase));
606 609
 		if ($pos === false) {
607  
-			return mb_substr($text, 0, $radius) . $ending;
  610
+			return mb_substr($text, 0, $radius) . $ellipsis;
608 611
 		}
609 612
 
610 613
 		$startPos = $pos - $radius;

0 notes on commit 2a570e6

Please sign in to comment.
Something went wrong with that request. Please try again.