Skip to content

Commit

Permalink
improved Mojo::URL to be able to contain scheme data for unknown schemes
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 21, 2013
1 parent ede2cb5 commit 44857dd
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -2,11 +2,13 @@
3.96 2013-04-22
- Updated jQuery to version 2.0.
- Updated prettify.js to version 4-Mar-2013.
- Improved Mojo::URL to be able to contain scheme data for unknown schemes.
- Improved default descriptions in Test::Mojo.
- Improved documentation.
- Improved tests.
- Fixed small html_unescape bug in Mojo::Util.
- Fixed small encoding bug in routes command.
- Fixed a few small clone bugs.

3.95 2013-04-12
- Added finished_ok method to Test::Mojo.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Parameters.pm
Expand Up @@ -34,7 +34,7 @@ sub append {
sub clone {
my $self = shift;

my $clone = Mojo::Parameters->new->charset($self->charset)
my $clone = $self->new->charset($self->charset)
->pair_separator($self->pair_separator);
if (defined $self->{string}) { $clone->{string} = $self->{string} }
else { $clone->params([@{$self->params}]) }
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Path.pm
Expand Up @@ -38,7 +38,7 @@ sub canonicalize {
sub clone {
my $self = shift;

my $clone = Mojo::Path->new->charset($self->charset);
my $clone = $self->new->charset($self->charset);
if (my $parts = $self->{parts}) {
$clone->{$_} = $self->{$_} for qw(leading_slash trailing_slash);
$clone->{parts} = [@$parts];
Expand Down
28 changes: 21 additions & 7 deletions lib/Mojo/URL.pm
Expand Up @@ -44,7 +44,8 @@ sub authority {
sub clone {
my $self = shift;

my $clone = Mojo::URL->new;
my $clone = $self->new;
$clone->{data} = $self->{data};
$clone->$_($self->$_) for qw(scheme userinfo host port fragment);
$clone->path($self->path->clone);
$clone->query($self->query->clone);
Expand Down Expand Up @@ -79,11 +80,17 @@ sub parse {

# Official regex
$url =~ m!(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?!;
$self->scheme($1);
$self->authority($2);
$self->path->parse($3);
$self->query($4);
$self->fragment($5);

# URL
my $proto = $self->scheme($1)->protocol;
unless ($proto && !grep { $proto eq $_ } qw(http https ws wss)) {
$self->authority($2);
$self->path->parse($3);
$self->query($4)->fragment($5);
}

# Scheme and scheme data
else { $self->{data} = $url }

return $self;
}
Expand Down Expand Up @@ -196,6 +203,9 @@ sub to_rel {
sub to_string {
my $self = shift;

# Scheme data
return $self->{data} if defined $self->{data};

# Protocol
my $url = '';
if (my $proto = $self->protocol) { $url .= "$proto://" }
Expand Down Expand Up @@ -355,7 +365,11 @@ Check if URL is absolute.
$url = $url->parse('http://127.0.0.1:3000/foo/bar?fo=o&baz=23#foo');
Parse URL.
Parse relative or absolute URL for the C<http>, C<https>, C<ws> as well as
C<wss> schemes and preserve scheme data for all unknown ones.
# "mailto:sri@example.com"
$url->parse('mailto:sri@example.com')->to_string;
=head2 path
Expand Down
2 changes: 2 additions & 0 deletions lib/Mojolicious/Plugin/TagHelpers.pm
Expand Up @@ -432,6 +432,7 @@ Generate portable script tag for C<Javascript> asset.
%= link_to index => {format => 'txt'} => (class => 'links') => begin
Home
% end
%= link_to Contact => Mojo::URL->new('mailto:sri@example.com')
<%= link_to index => begin %>Home<% end %>
<%= link_to '/path/to/file' => begin %>File<% end %>
<%= link_to 'http://mojolicio.us' => begin %>Mojolicious<% end %>
Expand All @@ -445,6 +446,7 @@ capitalized link target as content.
<a class="links" href="/path/to/index.txt">
Home
</a>
<a href="mailto:sri@example.com">Contact</a>
<a href="/path/to/index">Home</a>
<a href="/path/to/file">File</a>
<a href="http://mojolicio.us">Mojolicious</a>
Expand Down
26 changes: 26 additions & 0 deletions t/mojo/url.t
Expand Up @@ -96,6 +96,32 @@ is $url->fragment, '23', 'right fragment';
is "$url", 'http://sri:foobar@kraih.com:8080?_monkeybiz%3B=&_monkey=&23=#23',
'right format';

# Unknown scheme
$url = Mojo::URL->new('DATA:helloworld123');
is $url->scheme, 'DATA', 'right scheme';
is $url->protocol, 'data', 'right protocol';
is $url->userinfo, undef, 'no userinfo';
is $url->host, undef, 'no host';
is $url->port, undef, 'no port';
is $url->path, '', 'no path';
is $url->query, '', 'no query';
is $url->fragment, undef, 'no fragment';
is "$url", 'DATA:helloworld123', 'right format';
$url = $url->clone;
is $url->scheme, 'DATA', 'right scheme';
is $url->protocol, 'data', 'right protocol';
is $url->userinfo, undef, 'no userinfo';
is $url->host, undef, 'no host';
is $url->port, undef, 'no port';
is $url->path, '', 'no path';
is $url->query, '', 'no query';
is $url->fragment, undef, 'no fragment';
is "$url", 'DATA:helloworld123', 'right format';
$url = Mojo::URL->new->parse('mailto:sri@example.com');
is $url->scheme, 'mailto', 'right scheme';
is $url->protocol, 'mailto', 'right protocol';
is "$url", 'mailto:sri@example.com', 'right format';

# Relative
$url = Mojo::URL->new('foo?foo=bar#23');
ok !$url->is_abs, 'is not absolute';
Expand Down
7 changes: 5 additions & 2 deletions t/mojolicious/tag_helper_lite_app.t
Expand Up @@ -62,18 +62,20 @@ $t->get_ok('/small_tags')->status_is(200)->content_is(<<EOF);
EOF

# Links
$t->get_ok('/links')->status_is(200)->content_is(<<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="mailto:sri@example.com">Contact</a>
<a href="/links">Home</a>
<a href="/form/23" title="Foo">Foo</a>
<a href="/form/23" title="Foo">Foo</a>
EOF
$t->post_ok('/links')->status_is(200)->content_is(<<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="mailto:sri@example.com">Contact</a>
<a href="/links">Home</a>
<a href="/form/23" title="Foo">Foo</a>
<a href="/form/23" title="Foo">Foo</a>
Expand Down Expand Up @@ -439,6 +441,7 @@ __DATA__
<%= 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 Contact => Mojo::URL->new('mailto:sri@example.com') %>
<%= link_to Home => 'links' %>
<%= link_to Foo => 'form', {test => 23}, title => 'Foo' %>
<%= link_to form => {test => 23} => (title => 'Foo') => begin %>Foo<% end %>
Expand Down

0 comments on commit 44857dd

Please sign in to comment.