Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added charset attribute to Mojo::Path
  • Loading branch information
kraih committed Nov 24, 2012
1 parent a11f1dc commit e7e9f10
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,6 @@

3.61 2012-11-24
3.61 2012-11-25
- Added charset attribute to Mojo::Path.
- Improved support for relative redirects in Mojo::UserAgent::Transactor.
- Improved documentation.
- Improved tests.
Expand Down
26 changes: 16 additions & 10 deletions lib/Mojo/Path.pm
Expand Up @@ -5,8 +5,9 @@ use overload
'""' => sub { shift->to_string },
fallback => 1;

use Mojo::Util qw(encode url_escape url_unescape);
use Mojo::Util qw(decode encode url_escape url_unescape);

has charset => 'UTF-8';
has [qw(leading_slash trailing_slash)];
has parts => sub { [] };

Expand Down Expand Up @@ -42,7 +43,7 @@ sub clone {
my $clone = Mojo::Path->new;
$clone->leading_slash($self->leading_slash);
$clone->trailing_slash($self->trailing_slash);
return $clone->parts([@{$self->parts}]);
return $clone->charset($self->charset)->parts([@{$self->parts}]);
}

sub contains {
Expand Down Expand Up @@ -74,9 +75,9 @@ sub parse {
my ($self, $path) = @_;

$path = url_unescape $path // '';
utf8::decode $path;
$path =~ s!^/!! ? $self->leading_slash(1) : $self->leading_slash(undef);
$path =~ s!/$!! ? $self->trailing_slash(1) : $self->trailing_slash(undef);
$path = decode($self->charset, $path) // $path;
$self->leading_slash($path =~ s!^/!! ? 1 : undef);
$self->trailing_slash($path =~ s!/$!! ? 1 : undef);

return $self->parts([split '/', $path, -1]);
}
Expand All @@ -89,11 +90,9 @@ sub to_abs_string {
sub to_string {
my $self = shift;

# Escape
my $chars = '^A-Za-z0-9\-._~!$&\'()*+,;=:@';
my @parts = map { url_escape(encode('UTF-8', $_), $chars) } @{$self->parts};

# Format
my $chars = '^A-Za-z0-9\-._~!$&\'()*+,;=:@';
my $charset = $self->charset;
my @parts = map { url_escape(encode($charset, $_), $chars) } @{$self->parts};
my $path = join '/', @parts;
$path = "/$path" if $self->leading_slash;
$path = "$path/" if $self->trailing_slash;
Expand Down Expand Up @@ -123,6 +122,13 @@ L<Mojo::Path> is a container for URL paths.
L<Mojo::Path> implements the following attributes.
=head2 C<charset>
my $charset = $path->charset;
$path = $path->charset('UTF-8');
Charset used for encoding and decoding.
=head2 C<leading_slash>
my $leading_slash = $path->leading_slash;
Expand Down
9 changes: 9 additions & 0 deletions t/mojo/path.t
Expand Up @@ -274,4 +274,13 @@ is "$path", 'foo%2Fbar', 'right result';
is $path->to_string, 'foo%2Fbar', 'right result';
is $path->to_abs_string, '/foo%2Fbar', 'right result';

# Latin-1
$path = Mojo::Path->new->charset('Latin-1')->parse('/foob%E4r');
is $path->parts->[0], 'foobär', 'right part';
is $path->parts->[1], undef, 'no part';
is "$path", '/foob%E4r', 'right result';
is $path->to_string, '/foob%E4r', 'right result';
is $path->to_abs_string, '/foob%E4r', 'right result';
is $path->clone->to_string, '/foob%E4r', 'right result';

done_testing();

0 comments on commit e7e9f10

Please sign in to comment.