1. Shlomi Fish
  2. perl-HTML-Widgets-NavMenu

Commits

shl...@52c325ad-5fd0-0310-8a0f-c43feede02cc  committed fe1626f

Nav-Menu: added the ::EscapeHtml module to provide a function for escaping
HTML strings. Converted the code to use it instead of CGI.pm. Also added
tests to test this.

  • Participants
  • Parent commits 00df5d0
  • Branches default

Comments (0)

Files changed (15)

File module/Changes

View file
  • Ignore whitespace
 Revision history for Perl extension Shlomif::NavMenu.
 
+    - no longer making use of the CGI.pm module.
+
 0.4.0     Thu Jan 20 16:49:15 IST 2005
     - Added the url_is_abs option to the nodes.
     - Many code cleanups.

File module/HTML/Widgets/NavMenu/Test/Data.pm

View file
  • Ignore whitespace
     },
 );
 
+my @special_chars_nav_menu =
+(
+    'current_host' => "default",
+    'hosts' => { 'default' => { 'base_url' => "http://www.hello.com/" }, },
+    'tree_contents' =>
+    {
+        'host' => "default",
+        'text' => "Top 1",
+        'title' => "T1 Title",
+        'subs' =>
+        [
+            {
+                'text' => "Home",
+                'url' => "",
+            },
+            {
+                'text' => "Special Chars",
+                'url' => "<hello>&\"you\"/",
+            },
+            {
+                'text' => "Non-special",
+                'url' => "non-special/",
+            },
+        ],
+    },
+);
 
 sub get_test_data
 {
             'rec_url_type_menu' => \@rec_url_type_menu,
             'url_is_abs_menu' => \@url_is_abs_nav_menu,
             'mixed_expand_menu' => \@mixed_expand_nav_menu,
+            'special_chars_menu' => \@special_chars_nav_menu,
         };
 }
 

File module/MANIFEST

View file
  • Ignore whitespace
 t/10ul-classes.t
 t/11predicate.t
 t/12x-host-rel-url.t
+t/13escape-html.t
+lib/HTML/Widgets/NavMenu/EscapeHtml.pm
 lib/HTML/Widgets/NavMenu/HeaderRole.pm
 lib/HTML/Widgets/NavMenu/Iterator/Base.pm
 lib/HTML/Widgets/NavMenu/Iterator/Html.pm

File module/META.yml

View file
  • Ignore whitespace
 # http://module-build.sourceforge.net/META-spec.html
 #XXXXXXX This is a prototype!!!  It will change in the future!!! XXXXX#
 name:         HTML-Widgets-NavMenu
-version:      0.4.0
+version:      0.5.0_00
 version_from: lib/HTML/Widgets/NavMenu.pm
 installdirs:  site
 requires:
-    CGI:                           0
     Class::Accessor:               0
     Error:                         0
     Test::More:                    0

File module/Makefile.PL

View file
  • Ignore whitespace
+use 5.006;
+use ExtUtils::MakeMaker;
+# See lib/ExtUtils/MakeMaker.pm for details of how to influence
+# the contents of the Makefile that is written.
+WriteMakefile(
+    NAME              => 'HTML::Widgets::NavMenu',
+    VERSION_FROM      => 'lib/HTML/Widgets/NavMenu.pm', # finds $VERSION
+    PREREQ_PM         =>
+    {
+        'Test::More' => 0,
+        'Class::Accessor' => 0,
+        'Error' => 0,
+    },
+    ($] >= 5.005 ?     ## Add these new keywords supported since 5.005
+      (AUTHOR         => 'Shlomi Fish <shlomif@iglu.org.il>') : ()),
+);

File module/TODO

View file
  • Ignore whitespace
 * Create a base class for classes with 'subs' functionality - like 
 get_nth_sub, add_sub, num_subs, etc. Both ..::Node and ..::Item share it.
 
-* Eliminate the use of the CGI module, by providing my own alternative for
-CGI::escapeHTML().
+* Give an option to render a sub-set of the navigation-menu, starting
+at a certain coords/url-path.
 

File module/lib/HTML/Widgets/NavMenu.pm

View file
  • Ignore whitespace
 
 package HTML::Widgets::NavMenu;
 
-our $VERSION = '0.4.0';
+our $VERSION = '0.5.0_00';
 
 package HTML::Widgets::NavMenu::Error;
 

File module/lib/HTML/Widgets/NavMenu/EscapeHtml.pm

View file
  • Ignore whitespace
+package HTML::Widgets::NavMenu::EscapeHtml;
+
+use base qw(Exporter);
+
+use vars qw(@EXPORT);
+
+@EXPORT=(qw(escape_html));
+
+sub escape_html
+{
+    my $string = shift;
+    $string =~ s{&}{&amp;}gso;
+    $string =~ s{<}{&lt;}gso;
+    $string =~ s{>}{&gt;}gso;
+    $string =~ s{"}{&quot;}gso;
+    return $string;
+}
+
+1;
+

File module/lib/HTML/Widgets/NavMenu/Iterator/Base.pm

View file
  • Ignore whitespace
 
 use base qw(HTML::Widgets::NavMenu::Tree::Iterator);
 
-use CGI;
-
 sub initialize
 {
     my $self = shift;

File module/lib/HTML/Widgets/NavMenu/Iterator/Html.pm

View file
  • Ignore whitespace
 
 use base qw(HTML::Widgets::NavMenu::Iterator::Base);
 
+use HTML::Widgets::NavMenu::EscapeHtml;
+
 sub construct_new_item
 {
     my $self = shift;
     my $title = $node->title;
 
     $tag .= " href=\"" .
-        CGI::escapeHTML(
+        escape_html(
             $self->nav_menu()->get_url_to_item(
                 'item' => $item,
             )

File module/lib/HTML/Widgets/NavMenu/Iterator/NavMenu.pm

View file
  • Ignore whitespace
 
 use base qw(HTML::Widgets::NavMenu::Iterator::Html);
 
-use CGI;
+use HTML::Widgets::NavMenu::EscapeHtml;
 
 sub initialize
 {
 
     return "<ul" .
         (defined($class) ?
-            (" class=\"" . CGI::escapeHTML($class) . "\"") :
+            (" class=\"" . escape_html($class) . "\"") :
             ""
         ) . ">";
 }

File module/lib/HTML/Widgets/NavMenu/Iterator/SiteMap.pm

View file
  • Ignore whitespace
 
 use base qw(HTML::Widgets::NavMenu::Iterator::Html);
 
-use CGI;
-
 sub start_root
 {
     my $self = shift;

File module/t/04nav-menu.t

View file
  • Ignore whitespace
 
 use strict;
 
-use Test::More tests => 18;
+use Test::More tests => 20;
 
 use HTML::Widgets::NavMenu;
 use HTML::Widgets::NavMenu::HeaderRole;
         "Mixed Expand Nav-Menu #3"); 
 }
 
+{
+    my $nav_menu = HTML::Widgets::NavMenu->new(
+        'path_info' => "/base/",
+        @{$test_data->{'special_chars_menu'}},
+        'current_host' => "default",
+    );
+
+    my $rendered =
+        $nav_menu->render();
+
+    my $expected_string = <<"EOF";
+<ul>
+<li>
+<a href="../">Home</a>
+</li>
+<li>
+<a href="../&lt;hello&gt;&amp;&quot;you&quot;/">Special Chars</a>
+</li>
+<li>
+<a href="../non-special/">Non-special</a>
+</li>
+</ul>
+EOF
+
+    # TEST
+    ok (validate_nav_menu($rendered, $expected_string), 
+        "Special Chars Nav Menu"); 
+}
+
+# Test a special chars-based URL.
+{
+    my $nav_menu = HTML::Widgets::NavMenu->new(
+        'path_info' => "/<hello>&\"you\"/",
+        @{$test_data->{'special_chars_menu'}},
+        'current_host' => "default",
+    );
+
+    my $rendered =
+        $nav_menu->render();
+
+    my $expected_string = <<"EOF";
+<ul>
+<li>
+<a href="../">Home</a>
+</li>
+<li>
+<b>Special Chars</b>
+</li>
+<li>
+<a href="../non-special/">Non-special</a>
+</li>
+</ul>
+EOF
+
+    # TEST
+    ok (validate_nav_menu($rendered, $expected_string), 
+        "Nav Menu with a special chars URL."); 
+}

File module/t/10ul-classes.t

View file
  • Ignore whitespace
 
 use strict;
 
-use Test::More tests => 2;
+use Test::More tests => 3;
 
 use HTML::Widgets::NavMenu;
 
         "Nav Menu with depth classes");
 }
 
+# This test tests the escaping of the class names.
+{
+    my $nav_menu = HTML::Widgets::NavMenu->new(
+        'path_info' => "/me/",
+        @{$test_data->{'show_always'}},
+        'ul_classes' => [ "F&F Class", "sec<h>", "T\"C" ],
+    );
+
+    my $rendered = 
+        $nav_menu->render();
+
+    my $expected_string = <<"EOF";
+<ul class="F&amp;F Class">
+<li>
+<a href="../">Home</a>
+</li>
+<li>
+<b>About Me</b>
+</li>
+<li>
+<a href="../show-always/">Show Always</a>
+<br />
+<ul class="sec&lt;h&gt;">
+<li>
+<a href="../show-always/gandalf/">Gandalf</a>
+</li>
+<li>
+<a href="../robin/">Robin</a>
+<br />
+<ul class="T&quot;C">
+<li>
+<a href="../robin/hood/">Hood</a>
+</li>
+</ul>
+</li>
+<li>
+<a href="../esther/">Queen Esther</a>
+<br />
+<ul class="T&quot;C">
+<li>
+<a href="../haman/">Haman</a>
+</li>
+</ul>
+</li>
+</ul>
+</li>
+</ul>
+EOF
+
+    # TEST
+    ok (validate_nav_menu($rendered, $expected_string),
+        "Nav Menu with depth classes");
+}
+

File module/t/13escape-html.t

View file
  • Ignore whitespace
+#!/usr/bin/perl -w
+
+use Test::More tests => 9;
+
+use strict;
+
+BEGIN {
+use_ok ('HTML::Widgets::NavMenu::EscapeHtml'); # TEST
+}
+
+{
+    # TEST
+    is (escape_html("hello"), "hello", "Simple 1");
+    # TEST
+    is (escape_html("hi\nYou  rule."), "hi\nYou  rule.", "Simple 2 with WS");
+    # TEST
+    is (escape_html("D&D"), "D&amp;D", "Ampersand");
+    # TEST
+    is (escape_html("<b>Hello</b>"), "&lt;b&gt;Hello&lt;/b&gt;", "Tags");
+    # TEST
+    is (escape_html("&amp;"), "&amp;amp;", "Double amp");
+    # TEST
+    is (escape_html("&<hello>"), "&amp;&lt;hello&gt;", "Seq of 2");
+    # TEST
+    is (escape_html(q{Hi "phony"}), q{Hi &quot;phony&quot;}, "Double quotes");
+    # TEST
+    is (escape_html(q{"<&>"}), q{&quot;&lt;&amp;&gt;&quot;}, "All in one");
+}    
+