File Coverage

blib/lib/HTML/Prototype/Helper/Tag.pm
Criterion Covered Total %
statement 9 74 12.1
branch 0 28 0.0
condition 0 38 0.0
subroutine 3 16 18.7
pod 6 6 100.0
total 18 162 11.1


line stmt bran cond sub pod time code
1             package HTML::Prototype::Helper::Tag;
2            
3 1     1   22291 use strict;
  1         2  
  1         38  
4 1     1   5 use base qw/Class::Accessor::Fast/;
  1         2  
  1         3545  
5             __PACKAGE__->mk_accessors(
6             qw/object object_name method_name template_object local_binding auto_index/
7             );
8 1     1   3858 use vars qw/$USE_ASXML_FOR_TAG/;
  1         9  
  1         1269  
9             $USE_ASXML_FOR_TAG = 0;
10            
11             =head1 NAME
12            
13             HTML::Prototype::Helper::Tag - Defines a tag object needed by HTML::Prototype
14            
15             =head1 SYNOPSIS
16            
17             use HTML::Prototype::Helper;
18            
19             =head1 DESCRIPTION
20            
21             Defines a tag object needed by HTML::Prototype
22            
23             =head2 REMARKS
24            
25             Until version 1.43, the internal function I<$self->_tag> used I<$tag->as_XML>
26             as its return value. By now, it will use I<$tag->as_HTML( $entities )> to
27             invokee I. This behaviour can be overridden
28             by setting I<$HTML::Prototype::Helper::Tag::USE_ASXML_FOR_TAG> to 1.
29            
30             =head2 METHODS
31            
32             =over 4
33            
34             =item HTML::Prototype::Helper::Tag->new( $object_name, $method_name, $template_object, $local_binding, $object )
35            
36             =cut
37            
38             sub new {
39 0     0 1   my ( $class, $object_name, $method_name, $template_object, $local_binding,
40             $object )
41             = @_;
42            
43 0           my $self = $class->SUPER::new();
44            
45 0           $self->object($object);
46 0           $self->object_name($object_name);
47 0           $self->method_name($method_name);
48 0           $self->template_object($template_object);
49 0           $self->local_binding($local_binding);
50            
51 0 0         if ( $object_name =~ s/\[\]$// ) {
52 0           $self->auto_index( $self->template_object->instance_variable_get($`) );
53 0           $self->object_name($object_name);
54             }
55            
56 0           return $self;
57             }
58            
59             =item $tag->object_name( [$object_name] )
60            
61             =item $tag->method_name( [$method_name] )
62            
63             =item $tag->template_object( [$template_object] )
64            
65             =item $tag->local_binding( [$local_binding] )
66            
67             =item $tag->object( [$object] )
68            
69             =cut
70            
71             sub object {
72 0     0 1   my $self = shift;
73 0 0         @_ = ( $self->template_object->instance_variable_get( $self->object_name ) )
74             unless @_;
75 0           return $self->_object_accessor(@_);
76             }
77            
78             =item $tag->value( )
79            
80             =cut
81            
82             sub value {
83 0     0 1   my $self = shift;
84 0 0         my $coderef =
85             $self->object ? $self->object->can( $self->method_name ) : undef;
86 0 0         return $coderef ? $self->object->$coderef() : '';
87             }
88            
89             =item $tag->value_before_type_cast( )
90            
91             =cut
92            
93             sub value_before_type_cast {
94 0     0 1   my $self = shift;
95            
96 0           my $value = '';
97 0 0         if ( defined $self->object ) {
98 0   0       my $coderef =
99             $self->object->can( $self->method_name . '_before_type_cast' )
100             || $self->object->can( $self->method_name );
101 0 0         $value = $self->object->$coderef() if $coderef;
102             }
103            
104 0           return $value;
105             }
106            
107             =item $tag->to_input_field_tag( $field_type, \%options )
108            
109             =cut
110            
111             sub to_input_field_tag {
112 0     0 1   my ( $self, $field_type, $options ) = @_;
113            
114 0   0       $options ||= {};
115 0   0       $options->{size} ||= $options->{maxlength} || 30;
      0        
116 0 0         delete $options->{size} if 'hidden' eq lc $field_type;
117 0           $options->{type} = $field_type;
118 0 0 0       $options->{value} ||= $self->value_before_type_cast()
119             unless 'file' eq lc $field_type;
120            
121 0           $self->_add_default_name_and_id($options);
122 0           return $self->_tag( "input", $options );
123             }
124            
125             =item $tag->to_content_tag( $tag_name, $value, \%options )
126            
127             =cut
128            
129             sub to_content_tag {
130 0     0 1   my ( $self, $tag_name, $options ) = @_;
131            
132 0   0       return $self->_content_tag( $tag_name, $self->value(), $options || {} );
133             }
134            
135             sub _add_default_name_and_id {
136 0     0     my ( $self, $options ) = @_;
137            
138 0   0       $options ||= {};
139            
140 0           my $index;
141 0 0 0       if ( ( $index = delete $options->{index} )
142             || ( $index = $self->auto_index ) )
143             {
144 0   0       $options->{name} ||= $self->_tag_name_with_index($index);
145 0   0       $options->{id} ||= $self->_tag_id_with_index($index);
146             }
147             else {
148 0   0       $options->{name} ||= $self->_tag_name;
149 0   0       $options->{id} ||= $self->_tag_id;
150             }
151             }
152            
153             sub _tag_name {
154 0     0     my $self = shift;
155            
156 0           return $self->object_name . '[' . $self->method_name . ']';
157             }
158            
159             sub _tag_name_with_index {
160 0     0     my ( $self, $index ) = @_;
161            
162 0           return $self->object_name . '[' . $index . '][' . $self->method_name . ']';
163             }
164            
165             sub _tag_id {
166 0     0     my $self = shift;
167            
168 0           return $self->object_name . '_' . $self->method_name;
169             }
170            
171             sub _tag_id_with_index {
172 0     0     my ( $self, $index ) = @_;
173            
174 0           return $self->object_name . '_' . $index . '_' . $self->method_name;
175             }
176            
177             sub _tag {
178 0     0     my ( $self, $name, $options, $starttag ) = @_;
179 0   0       $starttag ||= 0;
180 0   0       $options ||= {};
181 0 0         my $entities =
182             defined $options->{entities}
183             ? delete $options->{entities}
184             : '<>&';
185 0           my $tag = HTML::Element->new( $name, %$options );
186 0 0         if ($starttag) {
    0          
187 0           return $tag->starttag($entities);
188             }
189             elsif ($USE_ASXML_FOR_TAG) {
190 0           return $tag->as_XML;
191             }
192             else {
193 0           $tag->as_HTML($entities);
194             }
195             }
196            
197             sub _content_tag {
198 0     0     my ( $self, $name, $content, $html_options ) = @_;
199 0   0       $html_options ||= {};
200 0 0         my $entities =
201             defined $html_options->{entities}
202             ? delete $html_options->{entities}
203             : '<>&';
204 0           my $tag = HTML::Element->new( $name, %$html_options );
205 0 0         $tag->push_content( ref $content eq 'ARRAY' ? @{$content} : $content );
  0            
206 0           return $tag->as_HTML($entities);
207             }
208            
209             =back
210            
211             =head1 SEE ALSO
212            
213             L, L
214            
215             =head1 AUTHOR
216            
217             Sascha Kiefer, C
218            
219             Built around Prototype by Sam Stephenson.
220             Much code is ported from Ruby on Rails javascript helpers.
221            
222             =head1 LICENSE
223            
224             This library is free software. You can redistribute it and/or modify it under
225             the same terms as perl itself.
226            
227             =cut
228            
229             1;