line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::RelativeUrlFor; |
2
|
3
|
|
|
3
|
|
2215
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
3
|
|
|
|
|
6798
|
|
|
3
|
|
|
|
|
17
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
our $VERSION = '0.052'; |
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
|
16
|
my $self = shift; |
10
|
|
|
|
|
|
|
|
11
|
11
|
|
|
|
|
25
|
my $rel = $self->clone; |
12
|
11
|
50
|
|
|
|
659
|
return $rel unless $rel->is_abs; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# Scheme and host |
15
|
11
|
|
33
|
|
|
80
|
my $base = shift || $rel->base; |
16
|
11
|
|
|
|
|
55
|
$rel->base($base)->scheme(undef); |
17
|
11
|
50
|
|
|
|
92
|
$rel->userinfo(undef)->host(undef)->port(undef) if $base->host; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# Path |
20
|
11
|
|
|
|
|
124
|
my @parts = @{$rel->path->parts}; |
|
11
|
|
|
|
|
23
|
|
21
|
11
|
|
|
|
|
128
|
my $base_path = $base->path; |
22
|
11
|
|
|
|
|
59
|
my @base_parts = @{$base_path->parts}; |
|
11
|
|
|
|
|
18
|
|
23
|
11
|
50
|
|
|
|
72
|
pop @base_parts unless $base_path->trailing_slash; |
24
|
11
|
|
100
|
|
|
138
|
while (@parts && @base_parts && $parts[0] eq $base_parts[0]) { |
|
|
|
100
|
|
|
|
|
25
|
11
|
|
|
|
|
71
|
shift @$_ for \@parts, \@base_parts; |
26
|
|
|
|
|
|
|
} |
27
|
11
|
|
|
|
|
34
|
my $path = $rel->path(Mojo::Path->new)->path; |
28
|
11
|
50
|
|
|
|
193
|
$path->leading_slash(1) if $rel->host; |
29
|
11
|
|
|
|
|
69
|
$path->parts([('..') x @base_parts, @parts]); |
30
|
11
|
50
|
|
|
|
339
|
$path->trailing_slash(1) if $self->path->trailing_slash; |
31
|
|
|
|
|
|
|
|
32
|
11
|
|
|
|
|
122
|
return $rel; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub register { |
36
|
2
|
|
|
2
|
1
|
59
|
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
|
|
54999
|
my $c = shift; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# create urls |
46
|
11
|
|
|
|
|
39
|
my $url = $url_for->($c, @_)->to_abs; |
47
|
11
|
|
|
|
|
3773
|
my $req_url = $c->req->url->to_abs; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# return relative version if request url exists |
50
|
11
|
50
|
|
|
|
1533
|
if ($req_url->to_string) { |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# repair if empty |
53
|
11
|
|
|
|
|
1440
|
my $rel_url = url_to_rel($url, $req_url); |
54
|
11
|
100
|
|
|
|
23
|
return Mojo::URL->new('./') unless $rel_url->to_string; |
55
|
10
|
|
|
|
|
1038
|
return $rel_url; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# change nothing without request url |
59
|
0
|
|
|
|
|
0
|
return $url; |
60
|
2
|
|
|
|
|
6
|
}; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# register rel(ative)_url_for helpers |
63
|
2
|
|
|
|
|
11
|
$app->helper(relative_url_for => $rel_url_for); |
64
|
2
|
|
|
|
|
48
|
$app->helper(rel_url_for => $rel_url_for); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# replace url_for helper |
67
|
2
|
100
|
|
|
|
37
|
if ($conf->{replace_url_for}) { |
68
|
3
|
|
|
3
|
|
2084
|
no strict 'refs'; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
83
|
|
69
|
3
|
|
|
3
|
|
19
|
no warnings 'redefine'; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
216
|
|
70
|
1
|
|
|
|
|
4
|
*Mojolicious::Controller::url_for = $rel_url_for; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
1; |
75
|
|
|
|
|
|
|
__END__ |