Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
normalize data attributes
  • Loading branch information
kraih committed Jan 24, 2014
1 parent c29d234 commit 9d0bede
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Changes
Expand Up @@ -2,9 +2,9 @@
4.70 2014-01-25
- Updated jQuery to version 2.1.
- Improved tag helpers to make data attributes more convenient. (ravengerUA)
%= tag 'div', data => {id => 1, name => 'test'} => 'some content'
%= tag 'div', data => {my_id => 1, Name => 'test'} => 'some content'
is equivalent to
%= tag 'div', data-id => 1, data-name => 'test' => 'some content'
%= tag 'div', data-my-id => 1, data-name => 'test' => 'some content'

4.69 2014-01-24
- Improved router to allow format detection for bridges.
Expand Down
32 changes: 18 additions & 14 deletions lib/Mojolicious/Plugin/TagHelpers.pm
Expand Up @@ -213,8 +213,12 @@ sub _tag {

# Attributes
my %attrs = @_;
if ((my $data = $attrs{data}) && ref $attrs{data} eq 'HASH') {
$attrs{"data-$_"} = $data->{$_} for keys %{delete $attrs{data}};
if ($attrs{data} && ref $attrs{data} eq 'HASH') {
while (my ($key, $value) = each %{$attrs{data}}) {
$key =~ tr/_/-/;
$attrs{lc("data-$key")} = $value;
}
delete $attrs{data};
}
$tag .= qq{ $_="} . xml_escape($attrs{$_} // '') . '"' for sort keys %attrs;

Expand Down Expand Up @@ -655,35 +659,35 @@ Generate submit input element.
=head2 t
%=t div => 'some & content'
%=t div => 'test & 123'
Alias for L</"tag">.
<div>some &amp; content</div>
<div>test &amp; 123</div>
=head2 tag
%= tag 'div'
%= tag 'div', id => 'foo'
%= tag div => 'some & content'
%= tag div => (id => 'foo') => 'some & content'
%= tag div => (data => {id => 1, name => 'test'}) => 'some & content'
%= tag div => 'test & 123'
%= tag div => (id => 'foo') => 'test & 123'
%= tag div => (data => {my_id => 1, Name => 'test'}) => 'test & 123'
%= tag div => begin
some & content
test & 123
% end
<%= tag div => (id => 'foo') => begin %>some & content<% end %>
<%= tag div => (id => 'foo') => begin %>test & 123<% end %>
HTML/XML tag generator.
<div />
<div id="foo" />
<div>some &amp; content</div>
<div id="foo">some &amp; content</div>
<div data-id="1" data-name="test">some &amp; content</div>
<div>test &amp; 123</div>
<div id="foo">test &amp; 123</div>
<div data-my-id="1" data-name="test">test &amp; 123</div>
<div>
some & content
test & 123
</div>
<div id="foo">some & content</div>
<div id="foo">test & 123</div>
Very useful for reuse in more specific tag helpers.
Expand Down
8 changes: 4 additions & 4 deletions t/mojolicious/tag_helper_lite_app.t
Expand Up @@ -59,7 +59,7 @@ $t->options_ok('/tags')->status_is(200)->content_is(<<EOF);
<foo />
<foo bar="baz" />
<foo one="t&lt;wo" three="four">Hello</foo>
<div data-id="1" data-name="test">some content</div>
<div data-my-test-id="1" data-name="test">some content</div>
<div data="bar">some content</div>
EOF
$t->patch_ok('/more_tags')->status_is(200)->content_is(<<EOF);
Expand All @@ -71,7 +71,7 @@ EOF

# Shortcut
$t->get_ok('/small_tags')->status_is(200)->content_is(<<EOF);
<div>some &amp; content</div>
<div>test &amp; 123</div>
<div>
<p id="0">just</p>
<p>0</p>
Expand Down Expand Up @@ -487,7 +487,7 @@ __DATA__
<%= tag 'foo' %>
<%= tag 'foo', bar => 'baz' %>
<%= tag 'foo', one => 't<wo', three => 'four' => begin %>Hello<% end %>
<%= tag 'div', data => {id => 1, name => 'test'} => 'some content' %>
<%= tag 'div', data => {my_test_ID => 1, naMe => 'test'} => 'some content' %>
<%= tag 'div', data => 'bar' => 'some content' %>
@@ more_tags.html.ep
Expand All @@ -497,7 +497,7 @@ __DATA__
%= tag 'bar', class => 'test', ''
@@ small_tags.html.ep
%=t div => 'some & content'
%=t div => 'test & 123'
%=t div => begin
%=t p => (id => 0) => 'just'
%=t p => 0
Expand Down

0 comments on commit 9d0bede

Please sign in to comment.