mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
ellipsis instead of ending/beginning for core wide consistency
This commit is contained in:
parent
853fa7d95b
commit
2a570e639c
2 changed files with 43 additions and 21 deletions
|
@ -379,7 +379,7 @@ TEXT;
|
|||
$this->assertSame($this->Text->truncate($text2, 10, array('exact' => false)), '...');
|
||||
$this->assertSame($this->Text->truncate($text3, 20), '<b>© 2005-20...');
|
||||
$this->assertSame($this->Text->truncate($text4, 15), '<img src="my...');
|
||||
$this->assertSame($this->Text->truncate($text5, 6, array('ending' => '')), '0<b>1<');
|
||||
$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ölr...');
|
||||
|
@ -388,8 +388,8 @@ TEXT;
|
|||
$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($text5, 6, array('ending' => '', '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('ending' => '', 'html' => true)), $text5);
|
||||
$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($text7, 255), $text7);
|
||||
$this->assertSame($this->Text->truncate($text7, 15), 'El moño está...');
|
||||
|
@ -399,7 +399,7 @@ TEXT;
|
|||
|
||||
$text = '<p><span style="font-size: medium;"><a>Iamatestwithnospacesandhtml</a></span></p>';
|
||||
$result = $this->Text->truncate($text, 10, array(
|
||||
'ending' => '...',
|
||||
'ellipsis' => '...',
|
||||
'exact' => false,
|
||||
'html' => true
|
||||
));
|
||||
|
@ -422,7 +422,7 @@ podeís adquirirla.</span></p>
|
|||
<p><span style="font-size: medium;"><a>http://www.amazon.com/Steve-
|
||||
Jobs-Walter-Isaacson/dp/1451648537</a></span></p>';
|
||||
$result = $this->Text->truncate($text, 500, array(
|
||||
'ending' => '... ',
|
||||
'ellipsis' => '... ',
|
||||
'exact' => false,
|
||||
'html' => true
|
||||
));
|
||||
|
@ -441,6 +441,22 @@ Isaacson</strong>", aquí os dejamos la dirección de amazon donde
|
|||
podeís adquirirla.</span></p>
|
||||
<p><span style="font-size: medium;"><a>... </a></span></p>';
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
// test deprecated `ending` (`ellipsis` taking precedence if both are defined)
|
||||
$result = $this->Text->truncate($text1, 31, array(
|
||||
'ending' => '.',
|
||||
'exact' => false,
|
||||
));
|
||||
$expected = 'The quick brown fox jumps.';
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$result = $this->Text->truncate($text1, 31, array(
|
||||
'ellipsis' => '..',
|
||||
'ending' => '.',
|
||||
'exact' => false,
|
||||
));
|
||||
$expected = 'The quick brown fox jumps..';
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -481,6 +497,9 @@ podeís adquirirla.</span></p>
|
|||
|
||||
$result = $this->Text->tail($text5, 10);
|
||||
$this->assertEquals('...цчшщъыь', $result);
|
||||
|
||||
$result = $this->Text->tail($text5, 6, array('ellipsis' => ''));
|
||||
$this->assertEquals('чшщъыь', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -422,11 +422,11 @@ class String {
|
|||
* Truncates text starting from the end.
|
||||
*
|
||||
* Cuts a string to the length of $length and replaces the first characters
|
||||
* with the beginning if the text is longer than length.
|
||||
* with the ellipsis if the text is longer than length.
|
||||
*
|
||||
* ### Options:
|
||||
*
|
||||
* - `beginning` Will be used as Beginning and prepended to the trimmed string
|
||||
* - `ellipsis` Will be used as Beginning and prepended to the trimmed string
|
||||
* - `exact` If false, $text will not be cut mid-word
|
||||
*
|
||||
* @param string $text String to truncate.
|
||||
|
@ -436,7 +436,7 @@ class String {
|
|||
*/
|
||||
public static function tail($text, $length = 100, $options = array()) {
|
||||
$default = array(
|
||||
'beginning' => '...', 'exact' => true
|
||||
'ellipsis' => '...', 'exact' => true
|
||||
);
|
||||
$options = array_merge($default, $options);
|
||||
extract($options);
|
||||
|
@ -448,25 +448,25 @@ class String {
|
|||
if (mb_strlen($text) <= $length) {
|
||||
return $text;
|
||||
} else {
|
||||
$truncate = mb_substr($text, mb_strlen($text) - $length + mb_strlen($beginning));
|
||||
$truncate = mb_substr($text, mb_strlen($text) - $length + mb_strlen($ellipsis));
|
||||
}
|
||||
if (!$exact) {
|
||||
$spacepos = mb_strpos($truncate, ' ');
|
||||
$truncate = $spacepos === false ? '' : trim(mb_substr($truncate, $spacepos));
|
||||
}
|
||||
|
||||
return $beginning . $truncate;
|
||||
return $ellipsis . $truncate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncates text.
|
||||
*
|
||||
* Cuts a string to the length of $length and replaces the last characters
|
||||
* with the ending if the text is longer than length.
|
||||
* with the ellipsis if the text is longer than length.
|
||||
*
|
||||
* ### Options:
|
||||
*
|
||||
* - `ending` Will be used as Ending and appended to the trimmed string
|
||||
* - `ellipsis` Will be used as Ending and appended to the trimmed string (`ending` is deprecated)
|
||||
* - `exact` If false, $text will not be cut mid-word
|
||||
* - `html` If true, HTML tags would be handled correctly
|
||||
*
|
||||
|
@ -478,8 +478,11 @@ class String {
|
|||
*/
|
||||
public static function truncate($text, $length = 100, $options = array()) {
|
||||
$default = array(
|
||||
'ending' => '...', 'exact' => true, 'html' => false
|
||||
'ellipsis' => '...', 'exact' => true, 'html' => false
|
||||
);
|
||||
if (isset($options['ending'])) {
|
||||
$default['ellipsis'] = $options['ending'];
|
||||
}
|
||||
$options = array_merge($default, $options);
|
||||
extract($options);
|
||||
|
||||
|
@ -491,7 +494,7 @@ class String {
|
|||
if (mb_strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
|
||||
return $text;
|
||||
}
|
||||
$totalLength = mb_strlen(strip_tags($ending));
|
||||
$totalLength = mb_strlen(strip_tags($ellipsis));
|
||||
$openTags = array();
|
||||
$truncate = '';
|
||||
|
||||
|
@ -538,7 +541,7 @@ class String {
|
|||
if (mb_strlen($text) <= $length) {
|
||||
return $text;
|
||||
} else {
|
||||
$truncate = mb_substr($text, 0, $length - mb_strlen($ending));
|
||||
$truncate = mb_substr($text, 0, $length - mb_strlen($ellipsis));
|
||||
}
|
||||
}
|
||||
if (!$exact) {
|
||||
|
@ -570,7 +573,7 @@ class String {
|
|||
}
|
||||
$truncate = mb_substr($truncate, 0, $spacepos);
|
||||
}
|
||||
$truncate .= $ending;
|
||||
$truncate .= $ellipsis;
|
||||
|
||||
if ($html) {
|
||||
foreach ($openTags as $tag) {
|
||||
|
@ -588,23 +591,23 @@ class String {
|
|||
* @param string $text String to search the phrase in
|
||||
* @param string $phrase Phrase that will be searched for
|
||||
* @param integer $radius The amount of characters that will be returned on each side of the founded phrase
|
||||
* @param string $ending Ending that will be appended
|
||||
* @param string $ellipsis Ending that will be appended
|
||||
* @return string Modified string
|
||||
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::excerpt
|
||||
*/
|
||||
public static function excerpt($text, $phrase, $radius = 100, $ending = '...') {
|
||||
public static function excerpt($text, $phrase, $radius = 100, $ellipsis = '...') {
|
||||
if (empty($text) || empty($phrase)) {
|
||||
return self::truncate($text, $radius * 2, array('ending' => $ending));
|
||||
return self::truncate($text, $radius * 2, array('ellipsis' => $ellipsis));
|
||||
}
|
||||
|
||||
$append = $prepend = $ending;
|
||||
$append = $prepend = $ellipsis;
|
||||
|
||||
$phraseLen = mb_strlen($phrase);
|
||||
$textLen = mb_strlen($text);
|
||||
|
||||
$pos = mb_strpos(mb_strtolower($text), mb_strtolower($phrase));
|
||||
if ($pos === false) {
|
||||
return mb_substr($text, 0, $radius) . $ending;
|
||||
return mb_substr($text, 0, $radius) . $ellipsis;
|
||||
}
|
||||
|
||||
$startPos = $pos - $radius;
|
||||
|
|
Loading…
Reference in a new issue