Skip to content

Commit

Permalink
made tag helper a little smarter
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 22, 2011
1 parent 865c4d8 commit b717d5c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -2,6 +2,7 @@ This file documents the revision history for Perl extension Mojolicious.

1.87 2011-08-22 00:00:00
- Added EXPERIMENTAL app method to Mojo::Command.
- Made tag helper a little smarter.
- Improved documentation.

1.86 2011-08-21 00:00:00
Expand Down
9 changes: 6 additions & 3 deletions lib/Mojolicious/Plugin/TagHelpers.pm
Expand Up @@ -304,7 +304,8 @@ sub _tag {

# Callback
my $cb = defined $_[-1] && ref($_[-1]) eq 'CODE' ? pop @_ : undef;
pop if @_ % 2;
my $content = pop if @_ % 2;
xml_escape $content if defined $content;

# Tag
my $tag = "<$name";
Expand All @@ -319,9 +320,9 @@ sub _tag {
}

# Block
if ($cb) {
if ($cb || defined($content)) {
$tag .= '>';
$tag .= $cb->();
$tag .= $cb ? $cb->() : $content;
$tag .= "<\/$name>";
}

Expand Down Expand Up @@ -587,12 +588,14 @@ Generate submit input element.
<%= tag 'div' %>
<%= tag 'div', id => 'foo' %>
<%= tag div => begin %>Content<% end %>
<%= tag div => 'Content' %>
HTML5 tag generator.
<div />
<div id="foo" />
<div>Content</div>
<div>Content</div>
Very useful for reuse in more specific tag helpers.
Expand Down
19 changes: 18 additions & 1 deletion t/mojolicious/tag_helper_lite_app.t
Expand Up @@ -7,7 +7,7 @@ BEGIN {
$ENV{MOJO_IOWATCHER} = 'Mojo::IOWatcher';
}

use Test::More tests => 45;
use Test::More tests => 48;

# "Hey! Bite my glorious golden ass!"
use Mojolicious::Lite;
Expand All @@ -16,6 +16,9 @@ use Test::Mojo;
# GET /tags
get 'tags';

# GET /more_tags
get 'more_tags';

# GET /links
get 'links';

Expand Down Expand Up @@ -46,6 +49,14 @@ $t->get_ok('/tags')->status_is(200)->content_is(<<EOF);
<foo one="t&lt;wo" three="four">Hello</foo>
EOF

# GET /more_tags
$t->get_ok('/more_tags')->status_is(200)->content_is(<<EOF);
<bar>b&lt;a&gt;z</bar>
<bar>0</bar>
<bar class="test">0</bar>
<bar class="test"></bar>
EOF

# GET /links
$t->get_ok('/links')->status_is(200)->content_is(<<EOF);
<a href="/path">Pa&lt;th</a>
Expand Down Expand Up @@ -278,6 +289,12 @@ __DATA__
<%= tag 'foo', bar => 'baz' %>
<%= tag 'foo', one => 't<wo', three => 'four' => begin %>Hello<% end %>
@@ more_tags.html.ep
%= tag bar => 'b<a>z'
%= tag bar => 0
%= tag 'bar', class => 'test', 0
%= tag 'bar', class => 'test', ''
@@ links.html.ep
<%= link_to 'Pa<th' => '/path' %>
<%= link_to 'http://example.com/', title => 'Foo', sub { 'Foo' } %>
Expand Down

0 comments on commit b717d5c

Please sign in to comment.