Author: kake Date: 2012-05-02 14:14:07 +0100 (Wed, 02 May 2012) New Revision: 1373
Modified: trunk/lib/OpenGuides/Template.pm trunk/t/68_bug_website_displayed.t Log: Also strip the http:// and any leading www. from the truncated website display text.
Modified: trunk/lib/OpenGuides/Template.pm =================================================================== --- trunk/lib/OpenGuides/Template.pm 2012-05-02 11:18:32 UTC (rev 1372) +++ trunk/lib/OpenGuides/Template.pm 2012-05-02 13:14:07 UTC (rev 1373) @@ -370,8 +370,9 @@ if ( $website && $website ne "http://" && is_web_uri( $website ) ) { my $maxlen = $config->website_link_max_chars; my $trunc_website = $website; - if ( length( $website ) > $maxlen ) { - $trunc_website = substr( $website, 0, $maxlen - 3 ) . "..."; + $trunc_website =~ s|http://(www.)?%7C%7C; + if ( length( $trunc_website ) > $maxlen ) { + $trunc_website = substr( $trunc_website, 0, $maxlen - 3 ) . "..."; } $formatted_website_text = '<a href="' . $website . '">' . $trunc_website . '</a>';
Modified: trunk/t/68_bug_website_displayed.t =================================================================== --- trunk/t/68_bug_website_displayed.t 2012-05-02 11:18:32 UTC (rev 1372) +++ trunk/t/68_bug_website_displayed.t 2012-05-02 13:14:07 UTC (rev 1373) @@ -11,51 +11,59 @@ plan skip_all => "DBD::SQLite could not be used - no database to test with ($error)"; }
-plan tests => 5; +plan tests => 6;
OpenGuides::Test::refresh_db();
my $config = OpenGuides::Test->make_basic_config; my $guide = OpenGuides->new( config => $config ); -$config->website_link_max_chars( 23 );
$guide->wiki->write_node( "South Croydon Station", "A sleepy main-line station in what is arguably the nicest part of Croydon.", undef, { website => "http://example.com/" } ) or die "Couldn't write node"; $guide->wiki->write_node( "North Croydon Station", "A busy main-line station in what is arguably the furthest North part of Croydon.", undef, { website => "http://longer.example.com/asdfasdf" } ) or die "Couldn't write node"; +$guide->wiki->write_node( "East Croydon Station", + "A busy main-line station that actually exists.", undef, + { website => "http://www.example.com/foo" } ) + or die "Couldn't write node";
-my $output = $guide->display_node( - id => "South Croydon Station", - return_output => 1, - ); -like( $output, qr#Website:</span> <span class="url"><a href="http://example.com/">http://example.com/</a>#, "website correctly displayed" ); +$config->website_link_max_chars( 20 ); +my %tt_vars = $guide->display_node( id => "South Croydon Station", + return_tt_vars => 1 ); +is( $tt_vars{formatted_website_text}, + '<a href="http://example.com/">example.com/</a>', + "Website correctly displayed when no need for truncation," );
-$output = $guide->display_node( - id => "North Croydon Station", - return_output => 1, - ); +%tt_vars = $guide->display_node( id => "East Croydon Station", + return_tt_vars => 1 ); +is( $tt_vars{formatted_website_text}, + '<a href="http://www.example.com/foo">example.com/foo</a>', + "Website correctly truncated when there's a leading www" );
-like( $output, qr#Website:</span> <span class="url"><a href="http://longer.example.com/asdfasdf">http://longer.exampl...</a>#, "website correctly truncated" ); +%tt_vars = $guide->display_node( id => "North Croydon Station", + return_tt_vars => 1 ); +is( $tt_vars{formatted_website_text}, + '<a href="http://longer.example.com/asdfasdf">longer.example.co...</a>', + "Website correctly truncated when much too long." );
# Make sure website isn't truncated unnecessarily, e.g. that we don't end up # just replacing the final three characters with the ellipsis. Our full URL -# has 34 characters. -$config->website_link_max_chars( 33 ); -my %tt_vars = $guide->display_node( id => "North Croydon Station", - return_tt_vars => 1 ); +# has 27 characters (not counting the http://). +$config->website_link_max_chars( 26 ); +%tt_vars = $guide->display_node( id => "North Croydon Station", + return_tt_vars => 1 ); is( $tt_vars{formatted_website_text}, - '<a href="http://longer.example.com/asdfasdf">http://longer.example.com/asdf...</a>', + '<a href="http://longer.example.com/asdfasdf">longer.example.com/asdf...</a>', "Website truncated correctly when 1 character longer than allowed." );
-$config->website_link_max_chars( 34 ); +$config->website_link_max_chars( 27 ); %tt_vars = $guide->display_node( id => "North Croydon Station", return_tt_vars => 1 ); is( $tt_vars{formatted_website_text}, - '<a href="http://longer.example.com/asdfasdf">http://longer.example.com/asdfasdf</a>', +'<a href="http://longer.example.com/asdfasdf">longer.example.com/asdfasdf</a>', "Website not truncated when exact length allowed." );
-$config->website_link_max_chars( 35 ); +$config->website_link_max_chars( 28 ); %tt_vars = $guide->display_node( id => "North Croydon Station", return_tt_vars => 1 ); is( $tt_vars{formatted_website_text}, - '<a href="http://longer.example.com/asdfasdf">http://longer.example.com/asdfasdf</a>', +'<a href="http://longer.example.com/asdfasdf">longer.example.com/asdfasdf</a>', "Website not truncated when 1 character shorter than allowed." ); -