Mechanize/Link.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 83 CC 9 SCAR 32.8
CC
9
Cov
0%
new
56.0
url
6.0
name
6.0
CRAP 26.6
1 package Mozilla::Mechanize::Link;
2 use strict;
3 use warnings;
4  
5 # $Id: Link.pm,v 1.3 2005/10/06 18:25:18 slanning Exp $
6  
7 =head1 NAME Mozilla::Mechanize::Link
8  
9 Mozilla::Mechanize::Link - Mimic WWW::Mechanize::Link
10  
11 =head1 SYNOPSIS
12  
13 sorry, read the source for now
14  
15 =head1 DESCRIPTION
16  
17 The C<Mozilla::Mechanize::Link> object is a thin wrapper around
18 HTML link elements.
19  
20 =head1 METHODS
21  
22 =head2 Mozilla::Mechanize::Link->new($link_node, $moz)
23  
24 Initialize a new object. $link_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 HTMLAnchorElement, an HTMLFrameElement, an HTMLIFrameElement,
28 or an HTMLAreaElement.
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 B<Note>: Although it supports the same methods as
35 L<WWW::Mechanize::Link|WWW::Mechanize::Link>, it is a
36 completely different implementation.
37  
38 =cut
39  
40 sub new {
41 my $class = shift;
42 my $node = shift;
43 my $moz = shift;
44  
45 my $iid = 0;
46  
47 # turn the Node into the appropriate HTMLElement
48 if (lc $node->GetNodeName eq 'a') {
49 $iid = Mozilla::DOM::HTMLAnchorElement->GetIID;
50 } elsif (lc $node->GetNodeName eq 'frame') {
51 $iid = Mozilla::DOM::HTMLFrameElement->GetIID;
52 } elsif (lc $node->GetNodeName eq 'iframe') {
53 $iid = Mozilla::DOM::HTMLIFrameElement->GetIID;
54 } elsif (lc $node->GetNodeName eq 'area') {
55 $iid = Mozilla::DOM::HTMLAreaElement->GetIID;
56 } else {
57 my $errstr = "Invalid Link node";
58 defined($moz) ? $moz->die($errstr) : die($errstr);
59 }
60 my $link = $node->QueryInterface($iid);
61  
62 my $self = { link => $link };
63 $self->{moz} = $moz if defined $moz;
64 bless($self, $class);
65 }
66  
67 =head2 $link->url
68  
69 Returns the url from the link.
70  
71 =cut
72  
73 sub url {
74 my $self = shift;
75 my $link = $self->{link};
76  
77 if ($link->GetTagName =~ /^i?frame$/i) {
78 return $link->GetSrc;
79 } else {
80 return $link->GetHref;
81 }
82 }
83  
84 =head2 $link->text
85  
86 Text of the link (innerHTML, so includes any HTML markup).
87  
88 =cut
89  
90 sub text {
91 my $self = shift;
92 my $link = $self->{link};
93  
94 my $iid = Mozilla::DOM::NSHTMLElement->GetIID;
95 my $nshtmlelem = $link->QueryInterface($iid);
96  
97 return $nshtmlelem->GetInnerHTML;
98 }
99  
100 =head2 $link->name
101  
102 NAME attribute from the source tag, if any.
103  
104 =cut
105  
106 sub name {
107 my $self = shift;
108 my $link = $self->{link};
109  
110 return $link->HasAttribute('name') ? $link->GetAttribute('name') : '';
111 }
112  
113 =head2 $link->tag
114  
115 Tag name ("A", "AREA", "FRAME" or "IFRAME").
116  
117 =cut
118  
119 sub tag {
120 my $self = shift;
121 my $link = $self->{link};
122  
123 return $link->GetTagName;
124 }
125  
126 =head2 $link->click
127  
128 Click the link (does this fire onClick?).
129  
130 =cut
131  
132 sub click {
133 my $self = shift;
134 my $link = $self->{link};
135  
136 # XXX: maybe this could be done better
137  
138 # Create a click event
139 my $doc = $link->GetOwnerDocument;
140 my $deiid = Mozilla::DOM::DocumentEvent->GetIID();
141 my $docevent = $doc->QueryInterface($deiid);
142 my $event = $docevent->CreateEvent('MouseEvents');
143 $event->InitEvent('click', 1, 1);
144  
145 # Dispatch the click event
146 my $etiid = Mozilla::DOM::EventTarget->GetIID();
147 my $target = $link->QueryInterface($etiid);
148 $target->DispatchEvent($event);
149  
150 # XXX: if they didn't pass $moz to `new', they're stuck..
151 my $moz = $self->{moz} || return;
152 $moz->_wait_while_busy();
153 }
154  
155  
156 1;
157  
158 __END__