XXX: row method master
authorMischa POSLAWSKY <perl@shiar.org>
Wed, 22 Oct 2008 19:27:35 +0000 (19:27 +0000)
committerMischa POSLAWSKY <perl@shiar.org>
Wed, 22 Oct 2008 19:27:35 +0000 (19:27 +0000)
lib/HTML/Form/Simple.pm
t/html.t

index 7d023146eef48d65c69bb858dff5fe50b5c317bf..a72215122ed92c349ac329f539932cdf2b157136 100644 (file)
@@ -33,7 +33,7 @@ sub tag {
 
        # strip empty if it shouldn't be
        defined $attr->{$_} and $attr->{$_} eq '' and delete $attr->{$_}
-               for qw(id type class style);
+               for qw(id for type class style);
 
        my $return = '<' . $tag;
 
@@ -248,6 +248,24 @@ sub check {
        $self->select($name, $rows, {%$attr, type => 'checkbox'});
 }
 
+sub row {
+       my $self = shift;
+       my ($name, $contents, $attr) = $self->_attr(2, @_);
+
+       $contents = defined $contents && ref $contents ne 'HASH'
+               ? $self->quote($contents) : $self->text($name, $contents);
+       my $label = defined $attr->{label}
+               ? $self->quote(delete $attr->{label})
+               : defined $name ? $self->quote($name) : '';
+
+       return $self->tag(label => {for => $name, %$attr})
+               . $label
+               . '</label>'
+               . (defined $, ? $, : ' ')
+               . $contents
+               ;
+}
+
 1;
 
 __END__
index a82c916d0b5fbecf87531d0f5538bf539b4a2c8a..08f0e6ba8d9e9c4320366ff3ced51cfd86995317 100644 (file)
--- a/t/html.t
+++ b/t/html.t
@@ -5,7 +5,7 @@ use warnings;
 
 use Test::More;
 
-plan tests => 76;
+plan tests => 80;
 
 use_ok('HTML::Form::Simple');
 
@@ -543,6 +543,32 @@ is_deeply(
        'various checkboxes'
 );
 
+# row
+
+is(
+       $form->row(undef),
+       '<label></label> <input type="text">',
+       'empty row'
+);
+
+is(
+       $form->row('test'),
+       '<label for="test">test</label> <input id="test" name="test" type="text">',
+       'standard row'
+);
+
+is(
+       $form->row('test', {type => 'password'}, {for => '', label => '"blah"'}),
+       '<label>&quot;blah&quot;</label> <input id="test" name="test" type="password">',
+       'row options'
+);
+
+is(
+       $form->row('test', '"scalar"', {for => ''}),
+       '<label>test</label> &quot;scalar&quot;',
+       'row scalar'
+);
+
 # defaults
 
 my $defform = HTML::Form::Simple->new({foo => '<">', '' => 'empty', 0 => 0});