Skip to content

Commit

Permalink
deprecated use of hash references for optgroup generation with select…
Browse files Browse the repository at this point in the history
…_field helper in favor of Mojo::Collection objects
  • Loading branch information
kraih committed Jan 2, 2014
1 parent e6dc209 commit 684e637
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 45 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,5 +1,8 @@

4.65 2014-01-02
- Deprecated use of hash references for optgroup geneneration with
select_field helper in favor of Mojo::Collection objects.
- Added b and c helpers to Mojolicious::Plugin::DefaultHelpers.

4.64 2014-01-01
- Fixed helper export bug in Mojolicious::Plugin::EPRenderer.
Expand Down
1 change: 0 additions & 1 deletion lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -114,7 +114,6 @@ which is the default to prevent XSS attacks against your application.

Only L<Mojo::ByteStream> objects are excluded from automatic escaping.

% use Mojo::ByteStream 'b';
<%= b('<p>test</p>') %>

Newline characters can be escaped with a backslash.
Expand Down
15 changes: 15 additions & 0 deletions lib/Mojolicious/Plugin/DefaultHelpers.pm
Expand Up @@ -2,6 +2,7 @@ package Mojolicious::Plugin::DefaultHelpers;
use Mojo::Base 'Mojolicious::Plugin';

use Mojo::ByteStream;
use Mojo::Collection;
use Mojo::Util qw(dumper sha1_sum steady_time);

sub register {
Expand All @@ -25,6 +26,8 @@ sub register {
);
}

$app->helper(b => sub { shift; Mojo::ByteStream->new(@_) });
$app->helper(c => sub { shift; Mojo::Collection->new(@_) });
$app->helper(config => sub { shift->app->config(@_) });
$app->helper(content => \&_content);
$app->helper(content_for => \&_content_for);
Expand Down Expand Up @@ -123,6 +126,18 @@ L<Mojolicious::Plugin::DefaultHelpers> implements the following helpers.
Alias for L<Mojolicious::Controller/"app">.
=head2 b
%= b('test 123')->b64_encode
Turn string into a L<Mojo::ByteStream> object.
=head2 c
%= c(qw(a b c))->shuffle->join
Turn list into a L<Mojo::Collection> object.
=head2 config
%= config 'something'
Expand Down
36 changes: 22 additions & 14 deletions lib/Mojolicious/Plugin/TagHelpers.pm
Expand Up @@ -2,7 +2,8 @@ package Mojolicious::Plugin::TagHelpers;
use Mojo::Base 'Mojolicious::Plugin';

use Mojo::ByteStream;
use Mojo::Util 'xml_escape';
use Mojo::Util qw(deprecated xml_escape);
use Scalar::Util 'blessed';

sub register {
my ($self, $app) = @_;
Expand Down Expand Up @@ -157,11 +158,18 @@ sub _select_field {
my $groups = '';
for my $group (@$options) {

# "optgroup" tag
# DEPRECATED in Top Hat!
if (ref $group eq 'HASH') {
my ($label, $values) = each %$group;
deprecated
'hash references are DEPRECATED in favor of Mojo::Collection objects';
$group = Mojo::Collection->new(each %$group);
}

# "optgroup" tag
if (blessed $group && $group->isa('Mojo::Collection')) {
my ($label, $values) = splice @$group, 0, 2;
my $content = join '', map { _option(\%values, $_) } @$values;
$groups .= _tag('optgroup', label => $label, sub {$content});
$groups .= _tag('optgroup', label => $label, @$group, sub {$content});
}

# "option" tag
Expand Down Expand Up @@ -579,25 +587,25 @@ picked up and shown as default.
=head2 select_field
%= select_field language => [qw(de en)]
%= select_field language => [qw(de en)], id => 'lang'
%= select_field language => [{Europe => [qw(de en)]}, {Asia => [qw(cn jp)]}]
%= select_field country => [qw(de en)]
%= select_field country => [qw(de en)], id => 'eu'
%= select_field country => [c(Europe => [qw(de en)]), c(Asia => [qw(cn jp)])]
%= select_field country => [[Germany => 'de'], 'en']
%= select_field country => [{Europe => [[Germany => 'de'], 'en']}]
%= select_field country => [[Germany => 'de', class => 'europe'], 'en']
%= select_field country => [c(EU => [[Germany => 'de'], 'en'], id => 'eu')]
%= select_field country => [[Germany => 'de', class => 'eu'], 'en']
Generate select, option and optgroup elements. Previous input values will
automatically get picked up and shown as default.
<select name="language">
<select name="country">
<option value="de">de</option>
<option value="en">en</option>
</select>
<select id="lang" name="language">
<select id="eu" name="country">
<option value="de">de</option>
<option value="en">en</option>
</select>
<select name="language">
<select name="country">
<optgroup label="Europe">
<option value="de">de</option>
<option value="en">en</option>
Expand All @@ -612,13 +620,13 @@ automatically get picked up and shown as default.
<option value="en">en</option>
</select>
<select name="country">
<optgroup label="Europe">
<optgroup id="eu" label="EU">
<option value="de">Germany</option>
<option value="en">en</option>
</optgroup>
</select>
<select name="country">
<option class="europe" value="de">Germany</option>
<option class="eu" value="de">Germany</option>
<option value="en">en</option>
</select>
Expand Down
1 change: 0 additions & 1 deletion t/mojolicious/layouted_lite_app.t
Expand Up @@ -283,7 +283,6 @@ Exception happened!
Not found happened!
@@ template_inheritance.html.ep
% use Mojo::ByteStream 'b';
% layout 'template_inheritance';
% title 'Works!';
<% content header => begin =%>
Expand Down
4 changes: 2 additions & 2 deletions t/mojolicious/lite_app.t
Expand Up @@ -896,7 +896,7 @@ $t->get_ok('/helper' => {'User-Agent' => 'Explorer'})->status_is(200)

# Exception in EP template
$t->get_ok('/eperror')->status_is(500)
->header_is(Server => 'Mojolicious (Perl)')->content_like(qr/\$c/);
->header_is(Server => 'Mojolicious (Perl)')->content_like(qr/\$unknown/);

# Subrequest
$t->get_ok('/subrequest')->status_is(200)
Expand Down Expand Up @@ -1108,7 +1108,7 @@ app layout <%= content %><%= app->mode %>
(<%= agent %>)\
@@ eperror.html.ep
%= $c->foo('bar');
%= $unknown->foo('bar');
@@ favicon.ico
Not a favicon!
Expand Down
76 changes: 49 additions & 27 deletions t/mojolicious/tag_helper_lite_app.t
Expand Up @@ -315,21 +315,26 @@ $t->put_ok('/selection')->status_is(200)
. '<option value="f">f</option>'
. '</optgroup>'
. '<option value="g">g</option>'
. '</select>' . "\n "
. "</select>\n "
. '<select multiple="multiple" name="foo">'
. '<option value="bar">bar</option>'
. '<option value="baz">baz</option>'
. '</select>' . "\n "
. "</select>\n "
. '<select name="bar">'
. '<option disabled="disabled" value="d">D</option>'
. '<option value="baz">baz</option>'
. '</select>' . "\n "
. '<input type="submit" value="Ok" />' . "\n"
. '</form>'
. "\n");
. "</select>\n "
. '<select name="yada">'
. '<optgroup class="x" label="test">'
. '<option value="a">a</option>'
. '<option value="b">b</option>'
. '</optgroup>'
. "</select>\n "
. '<input type="submit" value="Ok" />'
. "\n</form>\n");

# Selection with values
$t->put_ok('/selection?a=e&foo=bar&bar=baz')->status_is(200)
$t->put_ok('/selection?a=e&foo=bar&bar=baz&yada=b')->status_is(200)
->content_is("<form action=\"/selection\">\n "
. '<select name="a">'
. '<option value="b">b</option>'
Expand All @@ -339,21 +344,27 @@ $t->put_ok('/selection?a=e&foo=bar&bar=baz')->status_is(200)
. '<option value="f">f</option>'
. '</optgroup>'
. '<option value="g">g</option>'
. '</select>' . "\n "
. "</select>\n "
. '<select multiple="multiple" name="foo">'
. '<option selected="selected" value="bar">bar</option>'
. '<option value="baz">baz</option>'
. '</select>' . "\n "
. "</select>\n "
. '<select name="bar">'
. '<option disabled="disabled" value="d">D</option>'
. '<option selected="selected" value="baz">baz</option>'
. '</select>' . "\n "
. '<input type="submit" value="Ok" />' . "\n"
. '</form>'
. "\n");
. "</select>\n "
. '<select name="yada">'
. '<optgroup class="x" label="test">'
. '<option value="a">a</option>'
. '<option selected="selected" value="b">b</option>'
. '</optgroup>'
. "</select>\n "
. '<input type="submit" value="Ok" />'
. "\n</form>\n");

# Selection with multiple values
$t->put_ok('/selection?foo=bar&a=e&foo=baz&bar=d')->status_is(200)
$t->put_ok('/selection?foo=bar&a=e&foo=baz&bar=d&yada=a&yada=b')
->status_is(200)
->content_is("<form action=\"/selection\">\n "
. '<select name="a">'
. '<option value="b">b</option>'
Expand All @@ -363,18 +374,23 @@ $t->put_ok('/selection?foo=bar&a=e&foo=baz&bar=d')->status_is(200)
. '<option value="f">f</option>'
. '</optgroup>'
. '<option value="g">g</option>'
. '</select>' . "\n "
. "</select>\n "
. '<select multiple="multiple" name="foo">'
. '<option selected="selected" value="bar">bar</option>'
. '<option selected="selected" value="baz">baz</option>'
. '</select>' . "\n "
. "</select>\n "
. '<select name="bar">'
. '<option disabled="disabled" selected="selected" value="d">D</option>'
. '<option value="baz">baz</option>'
. '</select>' . "\n "
. '<input type="submit" value="Ok" />' . "\n"
. '</form>'
. "\n");
. "</select>\n "
. '<select name="yada">'
. '<optgroup class="x" label="test">'
. '<option selected="selected" value="a">a</option>'
. '<option selected="selected" value="b">b</option>'
. '</optgroup>'
. "</select>\n "
. '<input type="submit" value="Ok" />'
. "\n</form>\n");

# Selection with multiple values preselected
$t->put_ok('/selection?preselect=1')->status_is(200)
Expand All @@ -387,18 +403,23 @@ $t->put_ok('/selection?preselect=1')->status_is(200)
. '<option value="f">f</option>'
. '</optgroup>'
. '<option selected="selected" value="g">g</option>'
. '</select>' . "\n "
. "</select>\n "
. '<select multiple="multiple" name="foo">'
. '<option value="bar">bar</option>'
. '<option value="baz">baz</option>'
. '</select>' . "\n "
. "</select>\n "
. '<select name="bar">'
. '<option disabled="disabled" value="d">D</option>'
. '<option value="baz">baz</option>'
. '</select>' . "\n "
. '<input type="submit" value="Ok" />' . "\n"
. '</form>'
. "\n");
. "</select>\n "
. '<select name="yada">'
. '<optgroup class="x" label="test">'
. '<option value="a">a</option>'
. '<option value="b">b</option>'
. '</optgroup>'
. "</select>\n "
. '<input type="submit" value="Ok" />'
. "\n</form>\n");

# Snowman form
$t->post_ok('/☃')->status_is(200)->content_is(<<'EOF');
Expand Down Expand Up @@ -573,9 +594,10 @@ __DATA__
@@ selection.html.ep
% param a => qw(b g) if param 'preselect';
%= form_for selection => begin
%= select_field a => ['b', {c => ['<d', [ E => 'e'], 'f']}, 'g']
%= select_field a => ['b', c(c => ['<d', [ E => 'e'], 'f']), 'g']
%= select_field foo => [qw(bar baz)], multiple => 'multiple'
%= select_field bar => [['D' => 'd', disabled => 'disabled'], 'baz']
%= select_field yada => [c(test => [qw(a b)], class => 'x')];
%= submit_button
%= end
Expand Down

0 comments on commit 684e637

Please sign in to comment.