File Coverage

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


line stmt bran cond sub pod time code
1 15     15   10656 use strict;
  15         28  
  15         389  
2 15     15   74 use warnings;
  15         25  
  15         714  
3             package HTML::Widget::Plugin::Button;
4             # ABSTRACT: a button for clicking
5             $HTML::Widget::Plugin::Button::VERSION = '0.204';
6 15     15   71 use parent 'HTML::Widget::Plugin';
  15         25  
  15         90  
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   863 use HTML::Element;
  15         31  
  15         152  
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 47 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   75 sub _attribute_args { qw(type value) }
71 32     32   103 sub _boolean_args { qw(disabled) }
72              
73             sub button {
74 6     6 1 14 my ($self, $factory, $arg) = @_;
75              
76 6         15 $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   8 my ($self, $type) = @_;
92              
93 6         204 return exists $TYPES{ $type };
94             }
95              
96             sub build {
97 6     6 1 9 my ($self, $factory, $arg) = @_;
98              
99 6 100       18 $arg->{attr}{name} = $arg->{attr}{id} if not defined $arg->{attr}{name};
100 6   100     21 $arg->{attr}{type} ||= 'button';
101              
102             Carp::croak "invalid button type: $arg->{attr}{type}"
103 6 100       21 unless $self->__is_valid_type($arg->{attr}{type});
104              
105             Carp::croak "text and html arguments for link widget are mutually exclusive"
106 5 100 66     127 if $arg->{text} and $arg->{html};
107              
108 4         15 my $widget = HTML::Element->new('button');
109 4         89 $widget->attr($_ => $arg->{attr}{$_}) for keys %{ $arg->{attr} };
  4         28  
110              
111 4         122 my $content;
112 4 100       11 if ($arg->{html}) {
113             $content = ref $arg->{html}
114             ? $arg->{html}
115 2 100       10 : HTML::Element->new('~literal' => text => $arg->{html});
116             } else {
117             $content = defined $arg->{text}
118             ? $arg->{text}
119 2 100       45 : ucfirst lc $arg->{attr}{type};
120             }
121              
122 4         37 $widget->push_content($content);
123              
124 4         68 return $widget->as_XML;
125             }
126              
127             1;
128              
129             __END__