/usr/share/perl5/HTML/Widget/Element.pm is in libhtml-widget-perl 1.11-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | package HTML::Widget::Element;
use warnings;
use strict;
use base qw/HTML::Widget::Accessor Class::Data::Accessor/;
use HTML::Element;
use HTML::Widget::Container;
use NEXT;
use Carp qw/croak/;
__PACKAGE__->mk_classaccessor( container_class => 'HTML::Widget::Container' );
__PACKAGE__->mk_accessors(qw/name passive allow_filter allow_constraint/);
__PACKAGE__->mk_attr_accessors(qw/class/);
=head1 NAME
HTML::Widget::Element - Element Base Class
=head1 SYNOPSIS
my $e = $widget->element( $type, $name, {disabled => 'disabled'} );
$e->name('bar');
$e->class('foo');
=head1 DESCRIPTION
Element Base Class.
=head1 METHODS
=head2 new
=head2 attributes
=head2 attrs
Arguments: %attributes
Arguments: \%attributes
Return Value: $element
Arguments: none
Return Value: \%attributes
Accepts either a list of key/value pairs, or a hash-ref.
$e->attributes( $key => $value );
$e->attributes( { $key => $value } );
Returns the C<$element> object, to allow method chaining.
As of v1.10, passing a hash-ref no longer deletes current
attributes, instead the attributes are added to the current attributes
hash.
This means the attributes hash-ref can no longer be emptied using
C<< $e->attributes( { } ); >>. Instead, you may use
C<< %{ $e->attributes } = (); >>.
As a special case, if no arguments are passed, the return value is a
hash-ref of attributes instead of the object reference. This provides
backwards compatibility to support:
$e->attributes->{key} = $value;
L</attrs> is an alias for L</attributes>.
=head2 container
Arguments: \%attributes
Return Value: $container
Creates a new $container_class. Defaults to L<HTML::Widget::Container>.
=cut
sub container {
my ( $self, $attributes ) = @_;
my $class = $self->container_class || 'HTML::Widget::Container';
my $file = $class . ".pm";
$file =~ s{::}{/}g;
eval { require $file };
croak "Unable to load element container class $class: $@" if $@;
return $class->new( { passive => $self->passive, %$attributes } );
}
=head2 id
Arguments: $widget
Return Value: $id
Creates a element id.
=cut
sub id {
my ( $self, $w, $id ) = @_;
my $name = $self->name;
return unless defined($name) || defined($id);
return $w->name . '_' . ( $id || $self->name );
}
=head2 init
Arguments: $widget
Called once when process() gets called for the first time.
=cut
sub init { }
=head2 mk_error
Arguments: $widget, \@errors
Return Value: $error
Creates a new L<HTML::Widget::Error>.
=cut
sub mk_error {
my ( $self, $w, $errors ) = @_;
return
if ( !$w->{empty_errors}
&& ( !defined($errors) || !scalar(@$errors) ) );
my $no_render_count = 0;
$no_render_count += $_->no_render ? 1 : 0 for @$errors;
return if !$w->{empty_errors} && $no_render_count == scalar @$errors;
my $id = $self->attributes->{id} || $self->id($w);
my $cont_id = $id . '_errors';
my $container = HTML::Element->new(
'span',
id => $cont_id,
class => 'error_messages'
);
for my $error (@$errors) {
next if !$w->{empty_errors} && $error->no_render;
my $e_id = $id . '_error_' . lc( $error->{type} );
my $e_class = lc( $error->{type} . '_errors' );
my $e = HTML::Element->new( 'span', id => $e_id, class => $e_class );
$e->push_content( $error->{message} );
$container->push_content($e);
}
return $container;
}
=head2 mk_input
Arguments: $widget, \%attributes, \@errors
Return Value: $input_tag
Creates a new input tag.
=cut
sub mk_input {
my ( $self, $w, $attrs, $errors ) = @_;
return $self->mk_tag( $w, 'input', $attrs, $errors );
}
=head2 mk_tag
Arguments: $widget, $tagtype, \%attributes, \@errors
Return Value: $element_tag
Creates a new tag.
=cut
sub mk_tag {
my ( $self, $w, $tag, $attrs, $errors ) = @_;
my $e = HTML::Element->new($tag);
my $id = $self->attributes->{id} || $self->id($w);
my $type = ref $self;
$type =~ s/^HTML::Widget::Element:://;
$type =~ s/::/_/g;
$self->attributes->{class} ||= lc($type);
$e->attr( id => $id ) unless $self->attributes->{id} || $w->{explicit_ids};
$e->attr( name => $self->name );
for my $key ( keys %$attrs ) {
my $value = $attrs->{$key};
$e->attr( $key, $value ) if defined $value;
}
$e->attr( $_ => ${ $self->attributes }{$_} )
for ( keys %{ $self->attributes } );
return $e;
}
=head2 mk_label
Arguments: $widget, $name, $comment, \@errors
Return Value: $label_tag
Creates a new label tag.
=cut
sub mk_label {
my ( $self, $w, $name, $comment, $errors ) = @_;
return unless defined $name;
my $for = $self->attributes->{id} || $self->id($w);
my $id = $for . '_label';
my $e = HTML::Element->new( 'label', for => $for, id => $id );
if ($errors) {
$e->attr( 'class' => 'labels_with_errors' );
}
$e->push_content($name);
if ($comment) {
my $c = HTML::Element->new(
'span',
id => "$for\_comment",
class => 'label_comments'
);
$c->push_content($comment);
$e->push_content($c);
}
return $e;
}
=head2 name
Arguments: $name
Return Value: $name
Contains the element name.
=head2 passive
Arguments: $bool
Return Value: $bool
Defines if element gets automatically rendered.
=head2 prepare
Arguments: $widget
Called whenever C<< $widget->process >> gets called, before
C<< $element->process >>.
=cut
sub prepare { }
=head2 process
Arguments: \%params, \@uploads
Return Value: \@errors
Called whenever C<< $widget->process >> is called.
Returns an arrayref of L<HTML::Widget::Error> objects.
=cut
sub process { }
=head2 containerize
Arguments: $widget, $value, \@errors
Return Value: $container_tag
Containerize the element, label and error for later rendering.
Uses L<HTML::Widget::Container> by default, but this can be over-ridden on
a class or instance basis via L</container_class>.
=cut
sub containerize { }
=head2 container_class
Arguments: $class
Return Value: $class
Contains the class to use for contain the element which then get rendered. Defaults to L<HTML::Widget::Container>. C<container_class> can be set at a class or instance level:
HTML::Widget::Element->container_class('My::Container');
# Override default to custom class
HTML::Widget::Element::Password->container_class(undef);
# Passwords use the default class
$w->element('Textfield')->name('foo')->container_class->('My::Other::Container');
# This element only will use My::Other::Container to render
=cut
sub container_class {
my ($self) = shift;
if ( not $_[0] and @_ >= 1 ) {
delete $self->{container_class};
}
return $self->_container_class_accessor(@_);
}
=head2 find_elements
Return Value: \@elements
For non-block-container elements, simply returns a one-element list
containing this element.
=cut
sub find_elements { return (shift); }
=head2 new
=cut
sub new {
return shift->NEXT::new(@_)->allow_filter(1)->allow_constraint(1);
}
=head2 allow_filter
Used by C<< $widget->filter_all >>. If false, the filter won't be added.
Default true.
=head2 allow_constraint
Used by C<< $widget->constraint_all >>. If false, the filter won't be added.
Default true.
=head1 AUTHOR
Sebastian Riedel, C<sri@oook.de>
=head1 LICENSE
This library is free software, you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut
1;
|