[Rt-commit] rt branch, 4.0/absolute-menu-urls, created. rt-4.0.8rc2-2-g74816ed

Thomas Sibley trs at bestpractical.com
Wed Oct 24 14:32:44 EDT 2012


The branch, 4.0/absolute-menu-urls has been created
        at  74816edd89c3c79404c0d3f09be65fb39470ae3b (commit)

- Log -----------------------------------------------------------------
commit 93c02a85bf86123121e38f50cc179b3cfd545ece
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Wed Oct 24 11:03:23 2012 -0700

    Failing tests for menu path canonicalization

diff --git a/t/api/menu.t b/t/api/menu.t
new file mode 100644
index 0000000..13472f1
--- /dev/null
+++ b/t/api/menu.t
@@ -0,0 +1,48 @@
+use strict;
+use warnings;
+
+use RT::Test tests => undef;
+
+sub child_path_is($$$) {
+    my ($menu, $child, $expected) = @_;
+    my $c = $menu->child($child->[0], path => $child->[1]);
+    is $c->path, $expected, "'$child->[1]' normalizes to '$expected'";
+    return $c;
+}
+
+{
+    package FakeRequest;
+    sub new { bless {}, shift }
+    sub path_info { "" }
+
+    package FakeInterp;
+    require CGI;
+    sub new { bless {}, shift }
+    sub cgi_object { CGI->new }
+}
+
+local $HTML::Mason::Commands::r = FakeRequest->new;
+local $HTML::Mason::Commands::m = FakeInterp->new;
+
+my $menu = RT::Interface::Web::Menu->new;
+ok $menu, "Created top level menu";
+
+child_path_is $menu, [search    => "Search/Simple.html"],   "/Search/Simple.html";
+child_path_is $menu, [absolute  => "/Prefs/Other.html"],    "/Prefs/Other.html";
+child_path_is $menu, [scheme    => "http://example.com"],   "http://example.com";
+
+my $tools =
+    child_path_is $menu,    [tools      => "/Tools/"],              "/Tools/";
+    child_path_is $tools,   [myday      => "MyDay.html"],           "/Tools/MyDay.html";
+    child_path_is $tools,   [activity   => "/Activity.html"],       "/Activity.html";
+    my $ext =
+        child_path_is $tools,   [external   => "http://example.com"],   "http://example.com";
+        child_path_is $ext,     [wiki       => "wiki/"],                "http://example.com/wiki/";
+
+# Pathological case of multiplying slashes
+my $home =
+    child_path_is $menu, [home  => "/"], "/";
+    child_path_is $home, [slash => "/"], "/";
+    child_path_is $home, [empty => ""],  "/";
+
+done_testing;

commit 74816edd89c3c79404c0d3f09be65fb39470ae3b
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Wed Oct 24 11:07:33 2012 -0700

    Canonicalize menu paths in a less haphazard way
    
    Instead of generating double or triple slashes and then removing them
    with a regex, simply avoid generating the extra slashes in the first
    place by skipping the append if the parent path already has a trailing
    slash.
    
    Switch to always making an absolute (although often schemeless) URI
    object, even if there's no parent or parent path.  If no parent path
    exists, simply use "/".  This ensures the path correctly concatenates
    with WebPath.
    
    Resolves [rt3 #21304] in which overzealous slash smashing in 9b8d142
    broke menu paths with schemes after 4.0.7.

diff --git a/lib/RT/Interface/Web/Menu.pm b/lib/RT/Interface/Web/Menu.pm
index 90821e8..045df1f 100644
--- a/lib/RT/Interface/Web/Menu.pm
+++ b/lib/RT/Interface/Web/Menu.pm
@@ -150,10 +150,12 @@ treated as relative to it's parent's path, and made absolute.
 sub path {
     my $self = shift;
     if (@_) {
-        $self->{path} = shift;
-        $self->{path} = URI->new_abs($self->{path}, $self->parent->path . "/")->as_string
-            if defined $self->{path} and $self->parent and $self->parent->path;
-        $self->{path} =~ s!/+!/!g if $self->{path};
+        if (defined($self->{path} = shift)) {
+            my $base  = ($self->parent and $self->parent->path) ? $self->parent->path : "";
+               $base .= "/" unless $base =~ m{/$};
+            my $uri = URI->new_abs($self->{path}, $base);
+            $self->{path} = $uri->as_string;
+        }
     }
     return $self->{path};
 }

-----------------------------------------------------------------------


More information about the Rt-commit mailing list