line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::RelativeUrlFor; |
2
|
3
|
|
|
3
|
|
2503
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
3
|
|
|
|
|
7625
|
|
|
3
|
|
|
|
|
18
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
our $VERSION = '0.051'; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# function version of the deleted Mojo::URL::to_rel method |
7
|
|
|
|
|
|
|
# from Mojolicious repository revision 551a31 |
8
|
|
|
|
|
|
|
sub url_to_rel { |
9
|
11
|
|
|
11
|
0
|
22
|
my $self = shift; |
10
|
|
|
|
|
|
|
|
11
|
11
|
|
|
|
|
29
|
my $rel = $self->clone; |
12
|
11
|
50
|
|
|
|
790
|
return $rel unless $rel->is_abs; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# Scheme and authority |
15
|
11
|
|
33
|
|
|
270
|
my $base = shift || $rel->base; |
16
|
11
|
|
|
|
|
193
|
$rel->base($base)->scheme(undef); |
17
|
11
|
50
|
|
|
|
265
|
$rel->userinfo(undef)->host(undef)->port(undef) if $base->authority; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# Path |
20
|
11
|
|
|
|
|
1379
|
my @parts = @{$rel->path->parts}; |
|
11
|
|
|
|
|
30
|
|
21
|
11
|
|
|
|
|
158
|
my $base_path = $base->path; |
22
|
11
|
|
|
|
|
65
|
my @base_parts = @{$base_path->parts}; |
|
11
|
|
|
|
|
25
|
|
23
|
11
|
50
|
|
|
|
74
|
pop @base_parts unless $base_path->trailing_slash; |
24
|
11
|
|
100
|
|
|
139
|
while (@parts && @base_parts && $parts[0] eq $base_parts[0]) { |
|
|
|
100
|
|
|
|
|
25
|
11
|
|
|
|
|
85
|
shift @$_ for \@parts, \@base_parts; |
26
|
|
|
|
|
|
|
} |
27
|
11
|
|
|
|
|
34
|
my $path = $rel->path(Mojo::Path->new)->path; |
28
|
11
|
50
|
|
|
|
202
|
$path->leading_slash(1) if $rel->authority; |
29
|
11
|
|
|
|
|
388
|
$path->parts([('..') x @base_parts, @parts]); |
30
|
11
|
50
|
|
|
|
585
|
$path->trailing_slash(1) if $self->path->trailing_slash; |
31
|
|
|
|
|
|
|
|
32
|
11
|
|
|
|
|
146
|
return $rel; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub register { |
36
|
2
|
|
|
2
|
1
|
149
|
my ($self, $app, $conf) = @_; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# url_for helper backup |
39
|
2
|
|
|
|
|
5
|
my $url_for = *Mojolicious::Controller::url_for{CODE}; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# helper sub ref |
42
|
|
|
|
|
|
|
my $rel_url_for = sub { |
43
|
11
|
|
|
11
|
|
96834
|
my $c = shift; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# create urls |
46
|
11
|
|
|
|
|
42
|
my $url = $url_for->($c, @_)->to_abs; |
47
|
11
|
|
|
|
|
8938
|
my $req_url = $c->req->url->to_abs; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# return relative version if request url exists |
50
|
11
|
50
|
|
|
|
3928
|
if ($req_url->to_string) { |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# repair if empty |
53
|
11
|
|
|
|
|
3260
|
my $rel_url = url_to_rel($url, $req_url); |
54
|
11
|
100
|
|
|
|
23
|
return Mojo::URL->new('./') unless $rel_url->to_string; |
55
|
10
|
|
|
|
|
2034
|
return $rel_url; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# change nothing without request url |
59
|
0
|
|
|
|
|
0
|
return $url; |
60
|
2
|
|
|
|
|
8
|
}; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# register rel(ative)_url_for helpers |
63
|
2
|
|
|
|
|
12
|
$app->helper(relative_url_for => $rel_url_for); |
64
|
2
|
|
|
|
|
183
|
$app->helper(rel_url_for => $rel_url_for); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# replace url_for helper |
67
|
2
|
100
|
|
|
|
143
|
if ($conf->{replace_url_for}) { |
68
|
3
|
|
|
3
|
|
2970
|
no strict 'refs'; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
78
|
|
69
|
3
|
|
|
3
|
|
20
|
no warnings 'redefine'; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
202
|
|
70
|
1
|
|
|
|
|
6
|
*Mojolicious::Controller::url_for = $rel_url_for; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
1; |
75
|
|
|
|
|
|
|
__END__ |