| 1 | package Mozilla::Mechanize::Input; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | # $Id: Input.pm,v 1.4 2005/10/07 12:17:24 slanning Exp $ | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Mozilla::Mechanize::Input - A small class to interface with the Input objects | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | sorry, read the source for now | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | The C<Mozilla::Mechanize::Input> object is a thin wrapper around | ||
| 18 | HTML input elements. | ||
| 19 | |||
| 20 | =head1 METHODS | ||
| 21 | |||
| 22 | =head2 Mozilla::Mechanize::Input->new($input_node, $moz) | ||
| 23 | |||
| 24 | Initialize a new object. $input_node is a | ||
| 25 | L<Mozilla::DOM::HTMLElement|Mozilla::DOM::HTMLElement> | ||
| 26 | (or a node that can be QueryInterfaced to one); specifically, | ||
| 27 | it must be an HTMLInputElement, an HTMLButtonElement, an HTMLSelectElement, | ||
| 28 | or an HTMLTextAreaElement. | ||
| 29 | $moz is a L<Mozilla::Mechanize|Mozilla::Mechanize> object. | ||
| 30 | (This latter is a hack for `click', so that new pages can load | ||
| 31 | in the browser. The GUI has to be able to enter its main loop. | ||
| 32 | If you don't plan to use that method, you don't have to pass it in.) | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | sub new { | ||
| 37 | my $class = shift; | ||
| 38 | my $node = shift; | ||
| 39 | my $moz = shift; | ||
| 40 | |||
| 41 | my $iid; | ||
| 42 | |||
| 43 | # turn the Node into the appropriate HTMLElement | ||
| 44 | if (lc $node->GetNodeName eq 'input') { | ||
| 45 | $iid = Mozilla::DOM::HTMLInputElement->GetIID; | ||
| 46 | } elsif (lc $node->GetNodeName eq 'button') { | ||
| 47 | $iid = Mozilla::DOM::HTMLButtonElement->GetIID; | ||
| 48 | } elsif (lc $node->GetNodeName eq 'select') { | ||
| 49 | $iid = Mozilla::DOM::HTMLSelectElement->GetIID; | ||
| 50 | } elsif (lc $node->GetNodeName eq 'textarea') { | ||
| 51 | $iid = Mozilla::DOM::HTMLTextAreaElement->GetIID; | ||
| 52 | } else { | ||
| 53 | my $errstr = "Invalid Input node"; | ||
| 54 | defined($moz) ? $moz->die($errstr) : die($errstr); | ||
| 55 | } | ||
| 56 | my $input = $node->QueryInterface($iid); | ||
| 57 | |||
| 58 | my $self = { input => $input }; | ||
| 59 | $self->{moz} = $moz if defined $moz; | ||
| 60 | bless($self, $class); | ||
| 61 | } | ||
| 62 | |||
| 63 | =head2 $input->name | ||
| 64 | |||
| 65 | Return the input-control name. | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | sub name { | ||
| 70 | my $self = shift; | ||
| 71 | my $input = $self->{input}; | ||
| 72 | return $input->GetAttribute('name'); | ||
| 73 | } | ||
| 74 | |||
| 75 | =head2 $input->type | ||
| 76 | |||
| 77 | Return the type of the input control. | ||
| 78 | Note: for <select>, this returns 'select-one' for single select, | ||
| 79 | and 'select-multiple' for multiple. (I don't know why.) | ||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | sub type { | ||
| 84 | my $self = shift; | ||
| 85 | my $input = $self->{input}; | ||
| 86 | my $tagname = lc $input->GetNodeName; | ||
| 87 | if ($tagname eq 'select') { | ||
| 88 | if ($input->GetMultiple) { | ||
| 89 | return 'select-multiple'; | ||
| 90 | } else { | ||
| 91 | # I don't know what this is about, | ||
| 92 | # but it works like Win32::IE::Mechanize | ||
| 93 | return 'select-one'; | ||
| 94 | } | ||
| 95 | } else { | ||
| 96 | return $input->GetAttribute('type'); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | =head2 $input->value( [$value] ) | ||
| 101 | |||
| 102 | Get/Set the value of the input control. | ||
| 103 | |||
| 104 | =cut | ||
| 105 | |||
| 106 | sub value { | ||
| 107 | my $self = shift; | ||
| 108 | my $input = $self->{input}; | ||
| 109 | |||
| 110 | my $type = $self->type || ''; | ||
| 111 | $type =~ /^select/i and return $self->select_value( @_ ); | ||
| 112 | $type =~ /^radio/i and return $self->radio_value( @_ ); | ||
| 113 | |||
| 114 | if (@_ && defined $_[0]) { | ||
| 115 | my $value = shift; | ||
| 116 | $input->SetValue($value); | ||
| 117 | } | ||
| 118 | return $input->GetValue; | ||
| 119 | } | ||
| 120 | |||
| 121 | =head2 $input->select_value( [$value] ) | ||
| 122 | |||
| 123 | Mark all options with C<$value> as selected and unselect all other options. | ||
| 124 | |||
| 125 | =cut | ||
| 126 | |||
| 127 | sub select_value { | ||
| 128 | my $self = shift; | ||
| 129 | my $input = $self->{input}; | ||
| 130 | |||
| 131 | # XXX: I think this could be done better, but I just ported it straight | ||
| 132 | |||
| 133 | my %vals; | ||
| 134 | my @options = $input->GetOptions; | ||
| 135 | |||
| 136 | if ( @_ ) { | ||
| 137 | my @values = @_; | ||
| 138 | if ( @values == 1 && ref $values[0] eq 'HASH' ) { | ||
| 139 | my @ords = ref $values[0]->{n} | ||
| 140 | ? @{ $values[0]->{n} } : $values[0]->{n}; | ||
| 141 | @values = (); | ||
| 142 | foreach my $i ( @ords ) { | ||
| 143 | ($i > 0) && ($i <= @options) and | ||
| 144 | push @values, $options[$i - 1]->GetValue; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | @values = @{ $values[0] } if @values == 1 && ref $values[0]; | ||
| 148 | |||
| 149 | # Make sure only the last value is set for: | ||
| 150 | # select-one type with multiple values; | ||
| 151 | # XXX: not sure if same type in Mozilla | ||
| 152 | @values = ( $values[-1] ) if lc($self->type) eq 'select-one'; | ||
| 153 | |||
| 154 | %vals = map { ( $_ => undef ) } @values; | ||
| 155 | |||
| 156 | for ( my $i = 0; $i < @options; $i++ ) { | ||
| 157 | $options[$i]->SetSelected(exists $vals{ $options[$i]->GetValue }); | ||
| 158 | } | ||
| 159 | } else { | ||
| 160 | for ( my $i = 0; $i < @options; $i++ ) { | ||
| 161 | $options[$i]->GetSelected and | ||
| 162 | $vals{ $options[$i]->GetValue } = 1; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | return keys %vals; | ||
| 167 | } | ||
| 168 | |||
| 169 | =head2 $input->radio_value( [$value] ) | ||
| 170 | |||
| 171 | Locate all radio-buttons with the same name within this form. Now | ||
| 172 | uncheck all values that are not equal to C<$value>. | ||
| 173 | |||
| 174 | =cut | ||
| 175 | |||
| 176 | sub radio_value { | ||
| 177 | my $self = shift; | ||
| 178 | my $input = $self->{input}; | ||
| 179 | |||
| 180 | return unless $self->type =~ /^radio/i; | ||
| 181 | |||
| 182 | my $form = Mozilla::Mechanize::Form->new($input->GetForm, $self->{moz}); | ||
| 183 | my @radios = $form->_radio_group($self->name); | ||
| 184 | |||
| 185 | if (@_) { | ||
| 186 | my $value = shift; | ||
| 187 | for (@radios) { | ||
| 188 | $_->SetChecked(($_->GetValue eq $value) || 0); | ||
| 189 | } | ||
| 190 | } | ||
| 191 | my ($value) = map($_->GetValue, grep($_->GetChecked, @radios)); | ||
| 192 | return $value; | ||
| 193 | } | ||
| 194 | |||
| 195 | =head2 $input->click | ||
| 196 | |||
| 197 | Calls the C<click()> method on the actual object. | ||
| 198 | |||
| 199 | =cut | ||
| 200 | |||
| 201 | sub click { | ||
| 202 | my $self = shift; | ||
| 203 | my $input = $self->{input}; | ||
| 204 | $input->Click(); | ||
| 205 | |||
| 206 | # XXX: if they didn't pass $moz to `new', they're stuck.. | ||
| 207 | my $moz = $self->{moz} || return; | ||
| 208 | $moz->_wait_while_busy(); | ||
| 209 | } | ||
| 210 | |||
| 211 | |||
| 212 | 1; | ||
| 213 | |||
| 214 | __END__ |