Skip to content

Commit

Permalink
improved url_for to accept absolute URLs without scheme (closes #502)
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 8, 2013
1 parent 87cf8e1 commit e40e314
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

4.13 2013-06-08
- Improved url_for to accept absolute URLs without scheme.

4.12 2013-06-07
- Improved Mojo::Message::Request to allow a few more ASCII characters in
Expand Down
3 changes: 2 additions & 1 deletion lib/Mojolicious/Controller.pm
Expand Up @@ -382,7 +382,7 @@ sub url_for {

# Absolute URL
return $target if Scalar::Util::blessed $target && $target->isa('Mojo::URL');
return Mojo::URL->new($target) if $target =~ m!^\w+://!;
return Mojo::URL->new($target) if $target =~ m!^(?:\w+:)?//!;

# Base
my $url = Mojo::URL->new;
Expand Down Expand Up @@ -901,6 +901,7 @@ Get L<Mojo::UserAgent> object from L<Mojo/"ua">.
my $url = $c->url_for('test', {name => 'sebastian'});
my $url = $c->url_for('/perldoc');
my $url = $c->url_for('http://mojolicio.us/perldoc');
my $url = $c->url_for('//mojolicio.us/perldoc');
Generate a portable L<Mojo::URL> object with base for a route, path or URL.
Expand Down
3 changes: 3 additions & 0 deletions t/mojolicious/rebased_lite_app.t
Expand Up @@ -47,6 +47,7 @@ $t->get_ok('/')->status_is(200)->header_is('X-Route' => 'root')
http://example.com/rebased/
<script src="/rebased/mojo/jquery/jquery.js"></script>
<img src="/rebased/images/test.png" />
<link href="//example.com/base.css" media="screen" rel="stylesheet" />
http://example.com/rebased
http://example.com/rebased/foo
/rebased/foo
Expand Down Expand Up @@ -93,6 +94,7 @@ $t->get_ok('/baz')->status_is(200)->header_is('X-Route' => 'baz')
http://example.com/rebased/
<script src="/rebased/mojo/jquery/jquery.js"></script>
<img src="/rebased/images/test.png" />
<link href="//example.com/base.css" media="screen" rel="stylesheet" />
http://example.com/rebased/baz
http://example.com/rebased/foo
/rebased/foo
Expand All @@ -110,6 +112,7 @@ __DATA__
%= $self->req->url->base
%= javascript '/mojo/jquery/jquery.js'
%= image '/images/test.png'
%= stylesheet '//example.com/base.css'
%= $self->req->url->to_abs
%= url_for('foo')->to_abs
%= url_for 'foo'
Expand Down
6 changes: 3 additions & 3 deletions t/mojolicious/tag_helper_lite_app.t
Expand Up @@ -64,7 +64,7 @@ EOF
$t->get_ok('/links')->status_is(200)->content_is(<<'EOF');
<a href="/path">Pa&lt;th</a>
<a href="http://example.com/" title="Foo">Foo</a>
<a href="http://example.com/"><foo>Example</foo></a>
<a href="//example.com/"><foo>Example</foo></a>
<a href="mailto:sri@example.com">Contact</a>
<a href="/links">Home</a>
<a href="/form/23" title="Foo">Foo</a>
Expand All @@ -73,7 +73,7 @@ EOF
$t->post_ok('/links')->status_is(200)->content_is(<<'EOF');
<a href="/path">Pa&lt;th</a>
<a href="http://example.com/" title="Foo">Foo</a>
<a href="http://example.com/"><foo>Example</foo></a>
<a href="//example.com/"><foo>Example</foo></a>
<a href="mailto:sri@example.com">Contact</a>
<a href="/links">Home</a>
<a href="/form/23" title="Foo">Foo</a>
Expand Down Expand Up @@ -439,7 +439,7 @@ __DATA__
@@ links.html.ep
<%= link_to 'Pa<th' => '/path' %>
<%= link_to 'http://example.com/', title => 'Foo', sub { 'Foo' } %>
<%= link_to 'http://example.com/' => begin %><foo>Example</foo><% end %>
<%= link_to '//example.com/' => begin %><foo>Example</foo><% end %>
<%= link_to Contact => Mojo::URL->new('mailto:sri@example.com') %>
<%= link_to Home => 'links' %>
<%= link_to Foo => 'form', {test => 23}, title => 'Foo' %>
Expand Down

0 comments on commit e40e314

Please sign in to comment.