~ubuntu-branches/ubuntu/maverick/libwww-perl/maverick

« back to all changes in this revision

Viewing changes to lib/HTML/Form.pm

  • Committer: Bazaar Package Importer
  • Author(s): Nathan Handler
  • Date: 2009-07-09 17:44:48 UTC
  • mfrom: (1.3.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090709174448-bstu374e2ijr8mpd
Tags: 5.829-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
use Carp ();
6
6
 
7
7
use vars qw($VERSION $Encode_available);
8
 
$VERSION = "5.827";
 
8
$VERSION = "5.829";
9
9
 
10
10
eval { require Encode };
11
11
$Encode_available = !$@;
242
242
                            if exists $attr->{$_};
243
243
                    }
244
244
                    # count this new select option separately
245
 
                    $openselect{$attr->{name}}++;
 
245
                    my $name = $attr->{name};
 
246
                    $name = "" unless defined $name;
 
247
                    $openselect{$name}++;
246
248
 
247
249
                    while ($t = $p->get_tag) {
248
250
                        my $tag = shift @$t;
262
264
                            $a{value_name} = $p->get_trimmed_text;
263
265
                            $a{value} = delete $a{value_name}
264
266
                                unless defined $a{value};
265
 
                            $a{idx} = $openselect{$attr->{name}};
 
267
                            $a{idx} = $openselect{$name};
266
268
                            $f->push_input("option", \%a, $verbose);
267
269
                        }
268
270
                        else {
452
454
}
453
455
 
454
456
 
455
 
=item $input = $form->find_input( $name )
456
 
 
457
 
=item $input = $form->find_input( $name, $type )
458
 
 
459
 
=item $input = $form->find_input( $name, $type, $index )
 
457
=item $input = $form->find_input( $selector )
 
458
 
 
459
=item $input = $form->find_input( $selector, $type )
 
460
 
 
461
=item $input = $form->find_input( $selector, $type, $index )
460
462
 
461
463
This method is used to locate specific inputs within the form.  All
462
464
inputs that match the arguments given are returned.  In scalar context
463
465
only the first is returned, or C<undef> if none match.
464
466
 
465
 
If $name is specified, then the input must have the indicated name.
 
467
If $selector is specified, then the input's name, id, class attribute must
 
468
match.  A selector prefixed with '#' must match the id attribute of the input.
 
469
A selector prefixed with '.' matches the class attribute.  A selector prefixed
 
470
with '^' or with no prefix matches the name attribute.
466
471
 
467
472
If $type is specified, then the input must have the specified type.
468
473
The following type names are used: "text", "password", "hidden",
481
486
        my @res;
482
487
        my $c;
483
488
        for (@{$self->{'inputs'}}) {
484
 
            if (defined $name) {
485
 
                next unless exists $_->{name};
486
 
                next if $name ne $_->{name};
487
 
            }
 
489
            next if defined($name) && !$_->selected($name);
488
490
            next if $type && $type ne $_->{type};
489
491
            $c++;
490
492
            next if $no && $no != $c;
496
498
    else {
497
499
        $no ||= 1;
498
500
        for (@{$self->{'inputs'}}) {
499
 
            if (defined $name) {
500
 
                next unless exists $_->{name};
501
 
                next if $name ne $_->{name};
502
 
            }
 
501
            next if defined($name) && !$_->selected($name);
503
502
            next if $type && $type ne $_->{type};
504
503
            next if --$no;
505
504
            return $_;
517
516
}
518
517
 
519
518
 
520
 
=item $value = $form->value( $name )
 
519
=item $value = $form->value( $selector )
521
520
 
522
 
=item $form->value( $name, $new_value )
 
521
=item $form->value( $selector, $new_value )
523
522
 
524
523
The value() method can be used to get/set the value of some input.  If
525
524
strict is enabled and no input has the indicated name, then this method will croak.
720
719
 
721
720
=item $request = $form->click
722
721
 
723
 
=item $request = $form->click( $name )
 
722
=item $request = $form->click( $selector )
724
723
 
725
724
=item $request = $form->click( $x, $y )
726
725
 
727
 
=item $request = $form->click( $name, $x, $y )
 
726
=item $request = $form->click( $selector, $x, $y )
728
727
 
729
728
Will "click" on the first clickable input (which will be of type
730
729
C<submit> or C<image>).  The result of clicking is an C<HTTP::Request>
731
730
object that can then be passed to C<LWP::UserAgent> if you want to
732
731
obtain the server response.
733
732
 
734
 
If a $name is specified, we will click on the first clickable input
735
 
with the given name, and the method will croak if no clickable input
736
 
with the given name is found.  If $name is I<not> specified, then it
 
733
If a $selector is specified, we will click on the first clickable input
 
734
matching the selector, and the method will croak if no matching clickable
 
735
input is found.  If $selector is I<not> specified, then it
737
736
is ok if the form contains no clickable inputs.  In this case the
738
737
click() method returns the same request as the make_request() method
739
 
would do.
 
738
would do.  See description of the find_input() method above for how
 
739
the $selector is specified.
740
740
 
741
741
If there are multiple clickable inputs with the same name, then there
742
742
is no way to get the click() method of the C<HTML::Form> to click on
761
761
    # try to find first submit button to activate
762
762
    for (@{$self->{'inputs'}}) {
763
763
        next unless $_->can("click");
764
 
        next if $name && $_->name ne $name;
 
764
        next if $name && !$_->selected($name);
765
765
        next if $_->disabled;
766
766
        return $_->click($self, @_);
767
767
    }
896
896
 
897
897
This method can be used to get/set the current name of the input.
898
898
 
 
899
=item $input->id
 
900
 
 
901
=item $input->class
 
902
 
 
903
These methods can be used to get/set the current id or class attribute for the input.
 
904
 
 
905
=item $input->selected( $selector )
 
906
 
 
907
Returns TRUE if the given selector matched the input.  See the description of
 
908
the find_input() method above for a description of the selector syntax.
 
909
 
899
910
=item $value = $input->value
900
911
 
901
912
=item $input->value( $new_value )
920
931
    $old;
921
932
}
922
933
 
 
934
sub id
 
935
{
 
936
    my $self = shift;
 
937
    my $old = $self->{id};
 
938
    $self->{id} = shift if @_;
 
939
    $old;
 
940
}
 
941
 
 
942
sub class
 
943
{
 
944
    my $self = shift;
 
945
    my $old = $self->{class};
 
946
    $self->{class} = shift if @_;
 
947
    $old;
 
948
}
 
949
 
 
950
sub selected {
 
951
    my($self, $sel) = @_;
 
952
    return undef unless defined $sel;
 
953
    my $attr =
 
954
        $sel =~ s/^\^// ? "name"  :
 
955
        $sel =~ s/^#//  ? "id"    :
 
956
        $sel =~ s/^\.// ? "class" :
 
957
                          "name";
 
958
    return 0 unless defined $self->{$attr};
 
959
    return $self->{$attr} eq $sel;
 
960
}
 
961
 
923
962
sub value
924
963
{
925
964
    my $self = shift;