Skip to content

Commit ce58ec9

Browse files
dsymondsgitster
authored andcommitted
gitweb: Refactor abbreviation-with-title-attribute code.
Signed-off-by: David Symonds <dsymonds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent e076a0e commit ce58ec9

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

gitweb/gitweb.perl

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,23 @@ sub chop_str {
846846
return "$body$tail";
847847
}
848848

849+
# takes the same arguments as chop_str, but also wraps a <span> around the
850+
# result with a title attribute if it does get chopped. Additionally, the
851+
# string is HTML-escaped.
852+
sub chop_and_escape_str {
853+
my $str = shift;
854+
my $len = shift;
855+
my $add_len = shift || 10;
856+
857+
my $chopped = chop_str($str, $len, $add_len);
858+
if ($chopped eq $str) {
859+
return esc_html($chopped);
860+
} else {
861+
return qq{<span title="} . esc_html($str) . qq{">} .
862+
esc_html($chopped) . qq{</span>};
863+
}
864+
}
865+
849866
## ----------------------------------------------------------------------
850867
## functions returning short strings
851868

@@ -3437,12 +3454,7 @@ sub git_shortlog_body {
34373454
print "<tr class=\"light\">\n";
34383455
}
34393456
$alternate ^= 1;
3440-
my $author = chop_str($co{'author_name'}, 10);
3441-
if ($author ne $co{'author_name'}) {
3442-
$author = "<span title=\"" . esc_html($co{'author_name'}) . "\">" . esc_html($author) . "</span>";
3443-
} else {
3444-
$author = esc_html($author);
3445-
}
3457+
my $author = chop_and_escape_str($co{'author_name'}, 10);
34463458
# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
34473459
print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
34483460
"<td><i>" . $author . "</i></td>\n" .
@@ -3494,12 +3506,7 @@ sub git_history_body {
34943506
}
34953507
$alternate ^= 1;
34963508
# shortlog uses chop_str($co{'author_name'}, 10)
3497-
my $author = chop_str($co{'author_name'}, 15, 3);
3498-
if ($author ne $co{'author_name'}) {
3499-
"<span title=\"" . esc_html($co{'author_name'}) . "\">" . esc_html($author) . "</span>";
3500-
} else {
3501-
$author = esc_html($author);
3502-
}
3509+
my $author = chop_and_escape_str($co{'author_name'}, 15, 3);
35033510
print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
35043511
"<td><i>" . $author . "</i></td>\n" .
35053512
"<td>";
@@ -3655,12 +3662,7 @@ sub git_search_grep_body {
36553662
print "<tr class=\"light\">\n";
36563663
}
36573664
$alternate ^= 1;
3658-
my $author = chop_str($co{'author_name'}, 15, 5);
3659-
if ($author ne $co{'author_name'}) {
3660-
$author = "<span title=\"" . esc_html($co{'author_name'}) . "\">" . esc_html($author) . "</span>";
3661-
} else {
3662-
$author = esc_html($author);
3663-
}
3665+
my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
36643666
print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
36653667
"<td><i>" . $author . "</i></td>\n" .
36663668
"<td>" .
@@ -5175,12 +5177,7 @@ sub git_search {
51755177
print "<tr class=\"light\">\n";
51765178
}
51775179
$alternate ^= 1;
5178-
my $author = chop_str($co{'author_name'}, 15, 5);
5179-
if ($author ne $co{'author_name'}) {
5180-
$author = "<span title=\"" . esc_html($co{'author_name'}) . "\">" . esc_html($author) . "</span>";
5181-
} else {
5182-
$author = esc_html($author);
5183-
}
5180+
my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
51845181
print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
51855182
"<td><i>" . $author . "</i></td>\n" .
51865183
"<td>" .

0 commit comments

Comments
 (0)