| 1 | package Mozilla::Mechanize::Image; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | # $Id: Image.pm,v 1.3 2005/10/07 12:17:24 slanning Exp $ | ||
| 6 | |||
| 7 | =head1 NAME Mozilla::Mechanize::Image | ||
| 8 | |||
| 9 | Mozilla::Mechanize::Image - Mimic L<WWW::Mechanize::Image> | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | sorry, read the code for now | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | The C<Mozilla::Mechanize::Image> object is a thin wrapper around | ||
| 18 | an image element. | ||
| 19 | |||
| 20 | =head1 METHODS | ||
| 21 | |||
| 22 | =head2 Mozilla::Mechanize::Image->new($image_node, $moz) | ||
| 23 | |||
| 24 | Initialize a new object. $image_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 HTMLImageElement or an HTMLInputElement | ||
| 28 | whose type="image". | ||
| 29 | |||
| 30 | $moz is a L<Mozilla::Mechanize|Mozilla::Mechanize> object. | ||
| 31 | This is optional and currently unused. | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | sub new { | ||
| 36 | my $class = shift; | ||
| 37 | my $node = shift; | ||
| 38 | my $moz = shift; | ||
| 39 | |||
| 40 | my $iid = 0; | ||
| 41 | if (lc $node->GetNodeName eq 'img') { | ||
| 42 | $iid = Mozilla::DOM::HTMLImageElement->GetIID; | ||
| 43 | } elsif (lc $node->GetNodeName eq 'input') { | ||
| 44 | $iid = Mozilla::DOM::HTMLInputElement->GetIID; # type="image" | ||
| 45 | } else { | ||
| 46 | my $errstr = "Invalid Image node"; | ||
| 47 | defined($moz) ? $moz->die($errstr) : die($errstr); | ||
| 48 | } | ||
| 49 | my $image = $node->QueryInterface($iid); | ||
| 50 | |||
| 51 | my $self = { image => $image }; | ||
| 52 | $self->{moz} = $moz if defined $moz; | ||
| 53 | bless($self, $class); | ||
| 54 | } | ||
| 55 | |||
| 56 | =head2 $image->url | ||
| 57 | |||
| 58 | Return the SRC attribute from the IMG tag. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | sub url { | ||
| 63 | my $self = shift; | ||
| 64 | my $img = $self->{image}; | ||
| 65 | return $img->GetSrc; | ||
| 66 | } | ||
| 67 | |||
| 68 | =head2 $image->tag | ||
| 69 | |||
| 70 | Return 'IMG' for images. | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | sub tag { | ||
| 75 | my $self = shift; | ||
| 76 | my $img = $self->{image}; | ||
| 77 | return $img->GetTagName; | ||
| 78 | } | ||
| 79 | |||
| 80 | =head2 $image->width | ||
| 81 | |||
| 82 | Return the value of the C<width> attribute. Only works for <img>. | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | sub width { | ||
| 87 | my $self = shift; | ||
| 88 | my $img = $self->{image}; | ||
| 89 | |||
| 90 | if (lc($self->tag) eq 'img') { | ||
| 91 | # xxx: no! | ||
| 92 | #return $img->GetWidth; | ||
| 93 | |||
| 94 | return $img->GetAttribute('width'); | ||
| 95 | } | ||
| 96 | else { | ||
| 97 | return ''; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | =head2 $image->height | ||
| 102 | |||
| 103 | Return the value of the C<height> attribute. Only works for <img>. | ||
| 104 | |||
| 105 | =cut | ||
| 106 | |||
| 107 | sub height { | ||
| 108 | my $self = shift; | ||
| 109 | my $img = $self->{image}; | ||
| 110 | if (lc($self->tag) eq 'img') { | ||
| 111 | # xxx: no! | ||
| 112 | #return $img->GetHeight; | ||
| 113 | |||
| 114 | return $img->GetAttribute('height'); | ||
| 115 | } | ||
| 116 | else { | ||
| 117 | return ''; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | =head2 $image->alt | ||
| 122 | |||
| 123 | Return the value C<alt> attrubite. | ||
| 124 | |||
| 125 | =cut | ||
| 126 | |||
| 127 | sub alt { | ||
| 128 | my $self = shift; | ||
| 129 | my $img = $self->{image}; | ||
| 130 | return $img->GetAlt; | ||
| 131 | } | ||
| 132 | |||
| 133 | |||
| 134 | 1; | ||
| 135 | |||
| 136 | __END__ |