File Coverage

blib/lib/HTML/Widget/Plugin/Button.pm
Criterion Covered Total %
statement 33 33 100.0
branch 12 12 100.0
condition 5 5 100.0
subroutine 10 10 100.0
pod 3 3 100.0
total 63 63 100.0


line stmt bran cond sub pod time code
1 15     15   8056 use strict;
  15         23  
  15         513  
2 15     15   62 use warnings;
  15         19  
  15         895  
3             package HTML::Widget::Plugin::Button;
4             # ABSTRACT: a button for clicking
5             $HTML::Widget::Plugin::Button::VERSION = '0.203';
6 15     15   66 use parent 'HTML::Widget::Plugin';
  15         18  
  15         86  
7              
8             #pod =head1 SYNOPSIS
9             #pod
10             #pod $widget_factory->button({
11             #pod text => "submit & continue",
12             #pod type => 'submit',
13             #pod });
14             #pod
15             #pod ...or...
16             #pod
17             #pod $widget_factory->button({
18             #pod html => "reset all content",
19             #pod type => 'reset',
20             #pod });
21             #pod
22             #pod =head1 DESCRIPTION
23             #pod
24             #pod This plugin provides a basic button widget.
25             #pod
26             #pod =cut
27              
28 15     15   940 use HTML::Element;
  15         34  
  15         108  
29              
30             #pod =head1 METHODS
31             #pod
32             #pod =head2 C< provided_widgets >
33             #pod
34             #pod This plugin provides the following widgets: input
35             #pod
36             #pod =cut
37              
38 16     16 1 35 sub provided_widgets { qw(button) }
39              
40             #pod =head2 C< button >
41             #pod
42             #pod This method returns simple button element.
43             #pod
44             #pod In addition to the generic L attributes, the following
45             #pod are valid arguments:
46             #pod
47             #pod =over
48             #pod
49             #pod =item text
50             #pod
51             #pod =item html
52             #pod
53             #pod One of these options may be provided. If text is provided, it is used as the
54             #pod content of the button, after being entity encoded. If html is provided, it is
55             #pod used as the content of the button with no encoding performed.
56             #pod
57             #pod =item type
58             #pod
59             #pod This is the type of input button to be created. Valid types are button,
60             #pod submit, and reset. The default is button.
61             #pod
62             #pod =item value
63             #pod
64             #pod This is the widget's initial value.
65             #pod
66             #pod =back
67             #pod
68             #pod =cut
69              
70 16     16   50 sub _attribute_args { qw(type value) }
71 32     32   69 sub _boolean_args { qw(disabled) }
72              
73             sub button {
74 6     6 1 5 my ($self, $factory, $arg) = @_;
75              
76 6         9 $self->build($factory, $arg);
77             }
78              
79             #pod =head2 C< build >
80             #pod
81             #pod my $widget = $class->build($factory, $arg);
82             #pod
83             #pod This method does the actual construction of the input based on the args
84             #pod collected by the widget-constructing method. It is primarily here for
85             #pod subclasses to exploit.
86             #pod
87             #pod =cut
88              
89             my %TYPES = map { $_ => 1 } qw(button reset submit);
90             sub __is_valid_type {
91 6     6   7 my ($self, $type) = @_;
92              
93 6         173 return exists $TYPES{ $type };
94             }
95              
96             sub build {
97 6     6 1 7 my ($self, $factory, $arg) = @_;
98              
99 6 100       16 $arg->{attr}{name} = $arg->{attr}{id} if not defined $arg->{attr}{name};
100 6   100     18 $arg->{attr}{type} ||= 'button';
101              
102 6 100       13 Carp::croak "invalid button type: $arg->{attr}{type}"
103             unless $self->__is_valid_type($arg->{attr}{type});
104              
105 5 100 100     106 Carp::croak "text and html arguments for link widget are mutually exclusive"
106             if $arg->{text} and $arg->{html};
107              
108 4         13 my $widget = HTML::Element->new('button');
109 4         73 $widget->attr($_ => $arg->{attr}{$_}) for keys %{ $arg->{attr} };
  4         15  
110              
111 4         76 my $content;
112 4 100       7 if ($arg->{html}) {
113 2 100       7 $content = ref $arg->{html}
114             ? $arg->{html}
115             : HTML::Element->new('~literal' => text => $arg->{html});
116             } else {
117 2 100       7 $content = defined $arg->{text}
118             ? $arg->{text}
119             : ucfirst lc $arg->{attr}{type};
120             }
121              
122 4         25 $widget->push_content($content);
123              
124 4         78 return $widget->as_XML;
125             }
126              
127             1;
128              
129             __END__