[core] test truncate() properly, and then fix it, and then use it

This commit is contained in:
Shish 2024-02-20 11:21:24 +00:00 committed by Shish
parent a21f6ebf6b
commit 2136be80ab
7 changed files with 23 additions and 27 deletions

View file

@ -431,25 +431,26 @@ function clamp(int $val, ?int $min = null, ?int $max = null): int
return $val; return $val;
} }
/**
* Original PHP code by Chirp Internet: www.chirp.com.au
* Please acknowledge use of this code by including this header.
*/
function truncate(string $string, int $limit, string $break = " ", string $pad = "..."): string function truncate(string $string, int $limit, string $break = " ", string $pad = "..."): string
{ {
// return with no change if string is shorter than $limit $e = "UTF-8";
if (strlen($string) <= $limit) { $strlen = mb_strlen($string, $e);
$padlen = mb_strlen($pad, $e);
assert($limit > $padlen, "Can't truncate to a length less than the padding length");
// if string is shorter or equal to limit, leave it alone
if($strlen <= $limit) {
return $string; return $string;
} }
// is $break present between $limit and the end of the string? // if there is a break point between 0 and $limit, truncate to that
if (false !== ($breakpoint = strpos($string, $break, $limit))) { $breakpoint = mb_strrpos($string, $break, -($strlen - $limit + $padlen), $e);
if ($breakpoint < strlen($string) - 1) { if ($breakpoint !== false) {
$string = substr($string, 0, $breakpoint) . $pad; return mb_substr($string, 0, $breakpoint, $e) . $pad;
}
} }
return $string; // if there is no break point, cut mid-word
return mb_substr($string, 0, $limit - $padlen, $e) . $pad;
} }
/** /**

View file

@ -85,10 +85,11 @@ class PolyfillsTest extends TestCase
public function test_truncate(): void public function test_truncate(): void
{ {
$this->assertEquals("test words", truncate("test words", 10)); $this->assertEquals("test words", truncate("test words", 10), "No truncation if string is short enough");
$this->assertEquals("test...", truncate("test...", 9)); $this->assertEquals("test...", truncate("test words", 9), "Truncate when string is too long");
$this->assertEquals("test...", truncate("test...", 6)); $this->assertEquals("test...", truncate("test words", 7), "Truncate to the same breakpoint");
$this->assertEquals("te...", truncate("te...", 2)); $this->assertEquals("te...", truncate("test words", 5), "Breakpoints past the limit don't matter");
$this->assertEquals("o...", truncate("oneVeryLongWord", 4), "Hard-break if there are no breakpoints");
} }
public function test_to_shorthand_int(): void public function test_to_shorthand_int(): void

View file

@ -210,7 +210,7 @@ class CommentListTheme extends Themelet
$h_name = html_escape($comment->owner_name); $h_name = html_escape($comment->owner_name);
$h_timestamp = autodate($comment->posted); $h_timestamp = autodate($comment->posted);
if ($trim) { if ($trim) {
$h_comment = strlen($tfe->stripped) > 52 ? substr($tfe->stripped, 0, 50)."..." : $tfe->stripped; $h_comment = truncate($tfe->stripped, 50);
} else { } else {
$h_comment = $tfe->formatted; $h_comment = $tfe->formatted;
} }

View file

@ -226,13 +226,7 @@ class ForumTheme extends Themelet
foreach ($threads as $thread) { foreach ($threads as $thread) {
$titleSubString = $config->get_int('forumTitleSubString'); $titleSubString = $config->get_int('forumTitleSubString');
$title = truncate($thread["title"], $titleSubString);
if ($titleSubString < strlen($thread["title"])) {
$title = substr($thread["title"], 0, $titleSubString);
$title = $title."...";
} else {
$title = $thread["title"];
}
$tbody->appendChild( $tbody->appendChild(
TR( TR(

View file

@ -104,7 +104,7 @@ class CustomCommentListTheme extends CommentListTheme
$h_name = html_escape($comment->owner_name); $h_name = html_escape($comment->owner_name);
//$h_poster_ip = html_escape($comment->poster_ip); //$h_poster_ip = html_escape($comment->poster_ip);
if ($trim) { if ($trim) {
$h_comment = strlen($tfe->stripped) > 52 ? substr($tfe->stripped, 0, 50)."..." : $tfe->stripped; $h_comment = truncate($tfe->stripped, 50);
} else { } else {
$h_comment = $tfe->formatted; $h_comment = $tfe->formatted;
} }

View file

@ -104,7 +104,7 @@ class CustomCommentListTheme extends CommentListTheme
$h_name = html_escape($comment->owner_name); $h_name = html_escape($comment->owner_name);
//$h_poster_ip = html_escape($comment->poster_ip); //$h_poster_ip = html_escape($comment->poster_ip);
if ($trim) { if ($trim) {
$h_comment = strlen($tfe->stripped) > 52 ? substr($tfe->stripped, 0, 50)."..." : $tfe->stripped; $h_comment = truncate($tfe->stripped, 50);
} else { } else {
$h_comment = $tfe->formatted; $h_comment = $tfe->formatted;
} }

View file

@ -78,7 +78,7 @@ class CustomCommentListTheme extends CommentListTheme
$h_name = html_escape($comment->owner_name); $h_name = html_escape($comment->owner_name);
//$h_poster_ip = html_escape($comment->poster_ip); //$h_poster_ip = html_escape($comment->poster_ip);
if ($trim) { if ($trim) {
$h_comment = strlen($tfe->stripped) > 52 ? substr($tfe->stripped, 0, 50)."..." : $tfe->stripped; $h_comment = truncate($tfe->stripped, 50);
} else { } else {
$h_comment = $tfe->formatted; $h_comment = $tfe->formatted;
} }