Skip to content

Commit

Permalink
improved Mojolicious::Routes::Pattern to normalize more route variations
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 15, 2014
1 parent b946ff6 commit b131e30
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@

4.81 2014-02-14
- Added direct array access for child nodes to Mojo::DOM.
- Improved Mojolicious::Routes::Pattern to normalize more route variations.
- Improved routes command to show which routes are using certain features
with flags.

Expand Down
12 changes: 8 additions & 4 deletions lib/Mojolicious/Routes/Pattern.pm
Expand Up @@ -49,10 +49,14 @@ sub new { @_ > 1 ? shift->SUPER::new->parse(@_) : shift->SUPER::new }

sub parse {
my $self = shift;

# Normalize pattern
my $pattern = @_ % 2 ? (shift || '/') : '/';
$self->_tokenize($pattern =~ m!^/! ? $pattern : "/$pattern")
if $pattern ne '/';
return $self->constraints({@_});
$pattern =~ s!/+!/!g;
$pattern ne '/' ? $self->constraints({@_}) : return $self->constraints({@_});
$pattern =~ s!/$!!;

return $self->_tokenize($pattern =~ m!^/! ? $pattern : "/$pattern");
}

sub render {
Expand Down Expand Up @@ -230,7 +234,7 @@ sub _tokenize {
}
}

$self->pattern($pattern)->tree(\@tree);
return $self->pattern($pattern)->tree(\@tree);
}

1;
Expand Down
10 changes: 10 additions & 0 deletions t/mojolicious/pattern.t
Expand Up @@ -234,6 +234,16 @@ $result = $pattern->match('/footest', 1);
is_deeply $result, {'' => 'foo'}, 'right structure';
is $pattern->render($result, 1), '/footest', 'right result';

# Normalize slashes
$pattern = Mojolicious::Routes::Pattern->new(':foo/');
$result = $pattern->match('/bar', 1);
is_deeply $result, {'foo' => 'bar'}, 'right structure';
is $pattern->render($result, 1), '/bar', 'right result';
$pattern = Mojolicious::Routes::Pattern->new('//:foo//bar//');
$result = $pattern->match('/foo/bar', 1);
is_deeply $result, {'foo' => 'foo'}, 'right structure';
is $pattern->render($result, 1), '/foo/bar', 'right result';

# Unicode
$pattern = Mojolicious::Routes::Pattern->new('/(one)♥(two)');
$result = $pattern->match('/i♥mojolicious');
Expand Down

0 comments on commit b131e30

Please sign in to comment.