diff --git a/lib/Mojo/Path.pm b/lib/Mojo/Path.pm index 2f5310f6f6..486a4a0a28 100644 --- a/lib/Mojo/Path.pm +++ b/lib/Mojo/Path.pm @@ -41,11 +41,18 @@ sub merge { my ($self, $path) = @_; # Replace - return $self->parse($path) if $path =~ m!^/!; + if (ref $path) { + if ($path->leading_slash) { + @{$self->parts} = @{$path->parts}; + $self->trailing_slash($path->trailing_slash); + return $self->leading_slash($path->leading_slash); + } + } + elsif ($path =~ m!^/!) { return $self->parse($path) } # Merge - pop @{$self->parts} unless $self->trailing_slash; - $path = $self->new($path); + pop @{$self->parts} unless $self->trailing_slash; + $path = $self->new($path) unless ref $path; push @{$self->parts}, @{$path->parts}; return $self->trailing_slash($path->trailing_slash); } diff --git a/t/mojo/path.t b/t/mojo/path.t index 642a07bb88..631f8a43d0 100644 --- a/t/mojo/path.t +++ b/t/mojo/path.t @@ -2,6 +2,7 @@ use Mojo::Base -strict; use Test::More; use Mojo::Path; +use Mojo::Util qw(encode url_escape); subtest 'Basic functionality' => sub { my $path = Mojo::Path->new; @@ -170,6 +171,31 @@ subtest 'Merge' => sub { is $path->to_route, '/foo/baz/yada', 'right route'; }; +subtest 'Merge path object' => sub { + my $charset = 'ISO-8859-15'; + my $part = 'b€r'; + my $part_enc = url_escape(encode($charset, $part)); + my $parse_path = sub { Mojo::Path->new->charset($charset)->parse(@_) }; + + for my $has_trailing_slash (!!0, !!1) { + my $trailing_slash = $has_trailing_slash ? '/' : ''; + my $trailing_slash_diag = 'has'.($has_trailing_slash ? '' : ' no').' trailing slash'; + my $path = $parse_path->("/$part_enc/"); + $path->merge($parse_path->($part_enc.$trailing_slash)); + is_deeply $path->parts, [($part) x 2], 'right structure'; + is "$path", "/$part_enc/$part_enc".$trailing_slash, 'right path'; + ok $path->leading_slash, 'has leading slash'; + is $path->trailing_slash, $has_trailing_slash, $trailing_slash_diag; + is $path->to_route, "/$part/$part".$trailing_slash, 'right route'; + $path = $parse_path->("/foo/")->merge($parse_path->("/$part_enc".$trailing_slash)); + is_deeply $path->parts, [$part], 'right structure'; + is "$path", "/$part_enc".$trailing_slash, 'right path'; + ok $path->leading_slash, 'has leading slash'; + is $path->trailing_slash, $has_trailing_slash, $trailing_slash_diag; + is $path->to_route, "/$part".$trailing_slash, 'right route'; + } +}; + subtest 'Empty path elements' => sub { my $path = Mojo::Path->new('//'); is "$path", '//', 'right path';