File Coverage

blib/lib/Rose/HTML/Form/Field/Option.pm
Criterion Covered Total %
statement 30 30 100.0
branch 4 4 100.0
condition 2 3 66.6
subroutine 13 13 100.0
pod 7 9 77.7
total 56 59 94.9


line stmt bran cond sub pod time code
1             package Rose::HTML::Form::Field::Option;
2              
3 12     12   219935 use strict;
  12         51  
  12         365  
4              
5 12     12   1007 use Rose::HTML::Util();
  12         32  
  12         203  
6 12     12   927 use Rose::HTML::Text;
  12         35  
  12         88  
7              
8 12         6191 use base qw(Rose::HTML::Object::WithWrapAroundChildren
9 12     12   66 Rose::HTML::Form::Field::OnOff::Selectable);
  12         23  
10              
11             our $VERSION = '0.606';
12              
13             __PACKAGE__->add_valid_html_attrs
14             (
15             'label',
16             'selected',
17             );
18              
19             __PACKAGE__->add_boolean_html_attrs
20             (
21             'selected',
22             );
23              
24             __PACKAGE__->delete_valid_html_attrs(qw(name type checked));
25             __PACKAGE__->delete_required_html_attr('type');
26              
27 1484     1484 1 5631 sub html_element { 'option' }
28 292     292 1 1407 sub xhtml_element { 'option' }
29              
30             sub html_field
31             {
32 445     445 1 1481 my($self) = shift;
33 445         1056 $self->html_attr(selected => $self->selected);
34 445         1298 return $self->html_tag(@_);
35             }
36              
37             sub xhtml_field
38             {
39 145     145 1 271 my($self) = shift;
40 145         372 $self->html_attr(selected => $self->selected);
41 145         459 return $self->xhtml_tag(@_);
42             }
43              
44 1184     1184 0 2345 sub immutable_children { shift->label_text_object }
45              
46             sub label
47             {
48 391     391 1 809 my($self) = shift;
49 391 100       1020 $self->label_text_object->text(@_) if(@_);
50 391         1404 return $self->SUPER::label(@_);
51             }
52              
53             sub label_text_object
54             {
55 1397     1397 0 2207 my($self) = shift;
56 1397   66     4922 return $self->{'label_text_object'} ||=
57             ref($self)->object_type_class_loaded('literal text')->new(text => $self->label);
58             }
59              
60 7     7 1 31 sub short_label { shift->html_attr('label', @_) }
61              
62             sub name
63             {
64 1064     1064 1 1855 my($self) = shift;
65 1064 100       3340 $self->local_name(shift) if(@_);
66 1064         2580 return $self->fq_name;
67             }
68              
69             1;
70              
71             __END__
72              
73             =head1 NAME
74              
75             Rose::HTML::Form::Field::Option - Object representation of the "option" HTML tag.
76              
77             =head1 SYNOPSIS
78              
79             $field =
80             Rose::HTML::Form::Field::Option->new(
81             value => 'apple',
82             label => 'Apple');
83              
84             $field->selected(1);
85              
86             print $field->html;
87              
88             ...
89              
90             =head1 DESCRIPTION
91              
92             L<Rose::HTML::Form::Field::Option> is an object representation of a single option in a pop-up menu or select box in an HTML form.
93              
94             This class inherits from, and follows the conventions of, L<Rose::HTML::Form::Field>. Inherited methods that are not overridden will not be documented a second time here. See the L<Rose::HTML::Form::Field> documentation for more information.
95              
96             =head1 HTML ATTRIBUTES
97              
98             Valid attributes:
99              
100             accept
101             accesskey
102             alt
103             checked
104             class
105             dir
106             disabled
107             id
108             ismap
109             label
110             lang
111             maxlength
112             name
113             onblur
114             onchange
115             onclick
116             ondblclick
117             onfocus
118             onkeydown
119             onkeypress
120             onkeyup
121             onmousedown
122             onmousemove
123             onmouseout
124             onmouseover
125             onmouseup
126             onselect
127             readonly
128             selected
129             size
130             src
131             style
132             tabindex
133             title
134             type
135             usemap
136             value
137             xml:lang
138              
139             Required attributes:
140              
141             value
142              
143             Boolean attributes:
144              
145             checked
146             disabled
147             ismap
148             readonly
149             selected
150              
151             =head1 CONSTRUCTOR
152              
153             =over 4
154              
155             =item B<new PARAMS>
156              
157             Constructs a new L<Rose::HTML::Form::Field::Option> object based on PARAMS, where PARAMS are name/value pairs. Any object method is a valid parameter name.
158              
159             =back
160              
161             =head1 OBJECT METHODS
162              
163             =over 4
164              
165             =item B<hidden [BOOL]>
166              
167             Get or set a boolean value that indicates whether or not this radio button will be shown in its parent L<select box|Rose::HTML::Form::Field::SelectBox>, L<pop-up menu|Rose::HTML::Form::Field::PopUpMenu>, or L<option group|Rose::HTML::Form::Field::OptionGroup>. Setting it to true also sets L<selected|/selected> to false.
168              
169             =item B<hide>
170              
171             Calls L<hidden|/hidden>, passing a true value.
172              
173             =item B<selected [BOOL]>
174              
175             Select or unselect the option by passing a boolean value. If BOOL is true, the option will be selected. If it's false, it will be unselected. Returns true if the option is selected, false otherwise.
176              
177             =item B<short_label [TEXT]>
178              
179             Get or set the value of the "label" HTML attribute. When present, user agents are supposed to use this value instead of the contents of the option tag as the label for the option. Example:
180              
181             $field =
182             Rose::HTML::Form::Field::Option->new(
183             value => 'apple',
184             label => 'Shiny Apple');
185              
186             print $field->html;
187              
188             # The HTML:
189             #
190             # <option value="apple">Shiny Apple</option>
191             #
192             # Label shown in web browser: "Shiny Apple"
193              
194             $field->short_label("Apple");
195             print $field->html;
196              
197             # The HTML:
198             #
199             # <option label="Apple" value="apple">Shiny Apple</option>
200             #
201             # Label shown in web browser: "Apple"
202              
203             (Hey, don't look at me, I didn't write the HTML specs...)
204              
205             =item B<show>
206              
207             Calls L<hidden|/hidden>, passing a false value.
208              
209             =back
210              
211             =head1 AUTHOR
212              
213             John C. Siracusa (siracusa@gmail.com)
214              
215             =head1 LICENSE
216              
217             Copyright (c) 2010 by John C. Siracusa. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.