do not modify parameters in radio/check
authorMischa Poslawsky <mischa@mediadesign.nl>
Fri, 1 Aug 2008 11:25:22 +0000 (13:25 +0200)
committerMischa Poslawsky <mischa@mediadesign.nl>
Fri, 1 Aug 2008 11:25:22 +0000 (13:25 +0200)
lib/HTML/Form/Simple.pm
t/html.t

index 3a58c799761a27e880891e321a7ec375166b75c4..911b864c2eba3a9c38897bcc7964ea8fe1d62a2e 100644 (file)
@@ -164,38 +164,42 @@ sub select {
 
 sub radio {
        my $self = shift;
-       my ($name, $label, $value, $attr) = $self->_attr(3, @_);
+       my ($name, $label, $rows, $attr) = $self->_attr(3, @_);
 
-       if (not defined $value) {
+       if (not defined $rows) {
                if (defined $label) {
-                       $value = ref $label eq 'ARRAY' ? [1 .. $#$label+1] : [1];
+                       $rows = ref $label eq 'ARRAY' ? [1 .. $#$label+1] : [1];
                }
                else {
-                       $value = [{}];
+                       $rows = [{}];
                }
        }
-       elsif (ref $value ne 'ARRAY') {
-               $value = [$value];
+       elsif (ref $rows ne 'ARRAY') {
+               $rows = [$rows];
        }
 
        if (defined $label) {
-               $_ = ref $_ eq 'HASH' ? {%$_} : {value => $_} for @$value;
-               $_->{label} = ref $label eq 'ARRAY' ? shift @$label : $label for @$value;
+               $rows = [ map { ref $_ eq 'HASH' ? {%$_} : {value => $_} } @$rows ];
+               if (ref $label eq 'ARRAY') {
+                       $rows->[$_]->{label} = $label->[$_] for 0 .. $#$rows;
+               } else {
+                       $_->{label} = $label for @$rows;
+               }
        }
 
-       $self->select($name, $value, {%$attr, type => 'radio'});
+       $self->select($name, $rows, {%$attr, type => 'radio'});
 }
 
 sub check {
        my $self = shift;
        my ($name, $label, $checked, $attr) = $self->_attr(3, @_);
 
-       my $rows = defined $label ? ref $label eq 'ARRAY' ? $label : [$label] : [{}];
+       my $rows = defined $label ? ref $label eq 'ARRAY' ? [@$label] : [$label] : [{}];
        ref $_ eq 'HASH' or $_ = {label => $_} for @$rows;
        if (defined $checked) {
                if (ref $checked eq 'ARRAY') {
-                       $_->{checked} = shift @$checked for @$rows;
-                       push @$rows, map { {checked => $_} } @$checked;
+                       $rows->[$_]->{checked} = $checked->[$_] for 0 .. $#$rows;
+                       push @$rows, map { {checked => $_} } @$checked[@$rows .. $#$checked];
                }
                else {
                        $_->{checked} = $checked for @$rows;
index 2fea804b2353d89b0eeb9d07dc09b83047150aef..b4420e1829588f788f4b6d6e1cfa4ccdf552258e 100644 (file)
--- a/t/html.t
+++ b/t/html.t
@@ -5,7 +5,7 @@ use warnings;
 
 use Test::More;
 
-plan tests => 55;
+plan tests => 59;
 
 use_ok('HTML::Form::Simple');
 
@@ -356,6 +356,39 @@ is_deeply(
        'multiple radios with custom values'
 );
 
+{
+       # make sure arguments aren't modified
+       my @args = (foo => [0, 1], [0, {value => 1}], {name => 0, value => 1});
+       my @orgs = (foo => [0, 1], [0, {value => 1}], {name => 0, value => 1});
+       my @output = (
+               '<label><input id="foo_0" name="foo" type="radio" value="0"> 0</label>',
+               '<label><input checked id="foo_1" name="foo" type="radio" value="1"> 1</label>',
+       );
+
+       is_deeply(
+               [ $form->radio(@args) ],
+               \@output,
+               'options var to radio'
+       );
+
+       is_deeply(
+               [ $form->check(@args) ],
+               [
+                       '<label><input checked id="foo_1" name="foo" type="checkbox" value="1"> 0</label>',
+                       '<label><input checked id="foo_2" name="foo" type="checkbox" value="2"> 1</label>',
+               ],
+               'options var to check'
+       );
+
+       is(
+               scalar $form->radio(@args),
+               join('', @output),
+               'options var again to radio'
+       );
+
+       is_deeply(\@args, \@orgs, 'options var unmodified');
+}
+
 # check
 
 is(