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

Commits

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

Nav-Menu:

Added the TagGen module to generate tags, and the accompanying test suite.

  • Participants
  • Parent commits fc939c5
  • Branches default

Comments (0)

Files changed (4)

File module/MANIFEST

View file
  • Ignore whitespace
 t/11predicate.t
 t/12x-host-rel-url.t
 t/13escape-html.t
+t/14tag-gen.t
 lib/HTML/Widgets/NavMenu/EscapeHtml.pm
 lib/HTML/Widgets/NavMenu/HeaderRole.pm
 lib/HTML/Widgets/NavMenu/Iterator/Base.pm
 lib/HTML/Widgets/NavMenu/Object.pm
 lib/HTML/Widgets/NavMenu.pm
 lib/HTML/Widgets/NavMenu/Predicate.pm
+lib/HTML/Widgets/NavMenu/TagGen.pm
 lib/HTML/Widgets/NavMenu/Tree/Iterator/Item.pm
 lib/HTML/Widgets/NavMenu/Tree/Iterator.pm
 lib/HTML/Widgets/NavMenu/Tree/Iterator/Stack.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.5.0_00
+version:      0.6.0
 version_from: lib/HTML/Widgets/NavMenu.pm
 installdirs:  site
 requires:

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

View file
  • Ignore whitespace
+package HTML::Widgets::NavMenu::TagGen;
+
+use base 'HTML::Widgets::NavMenu::Object';
+use base 'Class::Accessor';
+
+use HTML::Widgets::NavMenu::EscapeHtml;
+
+__PACKAGE__->mk_accessors(
+    qw(name attributes)
+);
+
+sub initialize
+{
+    my $self = shift;
+
+    my (%args) = (@_);
+
+    $self->name($args{'name'});
+    $self->attributes($args{'attributes'});
+
+    return 0;
+}
+
+sub gen
+{
+    my $self = shift;
+
+    my $attr_values = shift;
+
+    my $is_standalone = shift || 0;
+
+    my @tag_list = keys(%$attr_values);
+
+    @tag_list = (grep { defined($attr_values->{$_}) } @tag_list);
+
+    @tag_list = (sort { $a cmp $b } @tag_list);
+
+    my $attr_spec = $self->attributes();
+
+    return "<" . $self->name() . 
+        join("", map { " $_=\"" .
+            ($attr_spec->{$_}->{'escape'} ? 
+                escape_html($attr_values->{$_}) 
+                : $attr_values->{$_}
+            ) . "\""
+            } @tag_list) .
+        ($is_standalone ? " /" : "") . ">";
+}
+
+1;
+

File module/t/14tag-gen.t

View file
  • Ignore whitespace
+#!/usr/bin/perl -w
+
+use Test::More tests => 12;
+
+use strict;
+
+BEGIN {
+use_ok ('HTML::Widgets::NavMenu::TagGen'); # TEST
+}
+
+{
+    my $test_tag = 
+        HTML::Widgets::NavMenu::TagGen->new(
+            'name' => "a",
+            'attributes' =>
+            {
+                'href' => { 'escape' => 1, },
+                'title' => { 'escape' => 0, },
+                'id' => { 'escape' => 1, },
+                'iname' => { 'escape' => 0, },
+            },
+        );
+
+    # TEST
+    is ($test_tag->gen({'href' => "http://www.mysite.com/",}), 
+        q{<a href="http://www.mysite.com/">}, 
+        "Simple Tag Test");
+    # TEST
+    is ($test_tag->gen({'href' => "/hello&you<yes>",}),
+        q{<a href="/hello&amp;you&lt;yes&gt;">},
+        "Escaping");
+    # TEST
+    is ($test_tag->gen({'href' => "http://www.mysite.com/",}, 1), 
+        q{<a href="http://www.mysite.com/" />}, 
+        "Standalone Tag");
+    # TEST
+    is ($test_tag->gen({'href' => "/hello&you<yes>",}, 1),
+        q{<a href="/hello&amp;you&lt;yes&gt;" />},
+        "Standalone Tag with Escaping");
+    # TEST
+    is ($test_tag->gen({}), q{<a>}, "Empty Tag");
+    # TEST
+    is ($test_tag->gen({}, 1), q{<a />}, "Empty Standalone Tag");
+    # TEST
+    is ($test_tag->gen({'title' => "This is me&amp;yours title"}),
+        q{<a title="This is me&amp;yours title">},
+        "Non-escaping for unescaped attribute");
+    # TEST
+    is ($test_tag->gen({'title' => "Hello", 'href' => "/hi/", 'id' => "myid"}),
+        q{<a href="/hi/" id="myid" title="Hello">}, 
+        "Multiple Attributes");
+    # TEST
+    is ($test_tag->gen({'title' => "Hello", 
+        'href' => "/hi/", 'id' => "myid"}, 1),
+        q{<a href="/hi/" id="myid" title="Hello" />}, 
+        "Multiple Attributes Standalone");
+    my $string = "&lt;Hello&amp;";
+    # TEST
+    is ($test_tag->gen({map { $_ => $string } (qw(href title id iname))}),
+        q{<a href="&amp;lt;Hello&amp;amp;" id="&amp;lt;Hello&amp;amp;" iname="&lt;Hello&amp;" title="&lt;Hello&amp;">},
+        "Selective Escaping");
+    # TEST
+    is ($test_tag->gen({map { $_ => $string } (qw(href title id iname))}, 1),
+        q{<a href="&amp;lt;Hello&amp;amp;" id="&amp;lt;Hello&amp;amp;" iname="&lt;Hello&amp;" title="&lt;Hello&amp;" />},
+        "Selective Escaping Standalone");
+}
+