Firefox/Application/API35.pm untested

filter: Statementstmt 0.0% 0 / 42 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 99 CC 2 SCAR 9.6
CC
2
Cov
0%
addTab
6.0
updateitems
2.0
addons
2.0
CRAP 2.6
1 package Firefox::Application::API35;
2 use strict;
3 use parent 'Firefox::Application';
4 our $VERSION = '0.80';
5  
6 =head1 NAME
7  
8 Firefox::Application::API35 - API wrapper for Firefox 3.5+
9  
10 =head1 SYNOPSIS
11  
12 use Firefox::Application;
13 my $ff = Firefox::Application->new(
14 # Force the Firefox 3.5 API
15 api => 'Firefox::Application::API35',
16 );
17  
18 =head1 METHODS
19  
20 =head2 C<< $api->updateitems( %args ) >>
21  
22 for my $item ($api->updateitems) {
23 print sprintf "Name: %s\n", $item->{name};
24 print sprintf "Version: %s\n", $item->{version};
25 print sprintf "GUID: %s\n", $item->{id};
26 };
27  
28 Returns the list of updateable items. Under Firefox 4,
29 can be restricted by the C<type> option.
30  
31 =over 4
32  
33 =item * C<type> - type of items to fetch
34  
35 C<ANY> - fetch any item
36  
37 C<ADDON> - fetch add-ons
38  
39 C<LOCALE> - fetch locales
40  
41 C<THEME> - fetch themes
42  
43 =back
44  
45 =cut
46  
47 sub updateitems {
48 my ($self, %options) = @_;
49 my $repl = delete $options{ repl } || $self->repl;
50 my $type = $options{type} || 'ANY';
51
52 my $addons_js = $repl->declare(sprintf( <<'JS', $type), 'list');
53 function () {
54 var em = Components.classes["@mozilla.org/extensions/manager;1"]
55 .getService(Components.interfaces.nsIExtensionManager);
56 var type = Components.interfaces.nsIUpdateItem.TYPE_%s;
57 var count = {};
58 var list = em.getItemList(type, count);
59 return list
60 };
61 JS
62 $addons_js->()
63 };
64  
65 sub addons {
66 my $self = shift;
67 $self->updateitems(type => 'ADDON', @_);
68 };
69  
70 sub themes {
71 my $self = shift;
72 $self->updateitems(type => 'THEME', @_);
73 };
74  
75 sub locales {
76 my $self = shift;
77 $self->updateitems(type => 'LOCALE', @_);
78 };
79  
80 sub selectedTab {
81 my ($self,%options) = @_;
82 my $repl = delete $options{ repl } || $self->repl;
83 return $self->browser( $repl )->{tabContainer}->{selectedItem};
84 }
85  
86 sub addTab {
87 my ($self, %options) = @_;
88 my $repl = $options{ repl } || $self->repl;
89  
90 my $tab = $self->browser( $repl )->addTab();
91  
92 if (not exists $options{ autoclose } or $options{ autoclose }) {
93 $self->autoclose_tab($tab)
94 };
95
96 $tab
97 };
98  
99 sub autoclose_tab {
100 my ($self,$tab) = @_;
101 my $release = join "",
102 q<var p=self.parentNode;>,
103 q<while(p && p.tagName != "tabbrowser") {>,
104 q<p = p.parentNode>,
105 q<};>,
106 q<if(p){p.removeTab(self)};>,
107 ;
108 $tab->__release_action($release);
109 };
110  
111 =head2 C<< $ff->closeTab( $tab [,$repl] ) >>
112  
113 $ff->closeTab( $tab );
114  
115 Close the given tab.
116  
117 =cut
118  
119 sub closeTab {
120 my ($self,$tab,$repl) = @_;
121 $repl ||= $self->repl;
122 my $close_tab = $repl->declare(<<'JS');
123 function(tab) {
124 // find containing browser
125 var p = tab.parentNode;
126 while (p.tagName != "tabbrowser") {
127 p = p.parentNode;
128 };
129 if(p){p.removeTab(tab)};
130 }
131 JS
132 return $close_tab->($tab);
133 }
134  
135  
136 sub openTabs {
137 my ($self,$repl) = @_;
138 $repl ||= $self->repl;
139 my $open_tabs = $repl->declare(<<'JS', 'list');
140 function() {
141 var idx = 0;
142 var tabs = [];
143
144 var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
145 .getService(Components.interfaces.nsIWindowMediator);
146 var en = wm.getEnumerator('navigator:browser');
147 while( en.hasMoreElements() ) {
148 var win= en.getNext();
149 var browser = win.getBrowser();
150 Array.prototype.forEach.call(
151 browser.tabContainer.childNodes,
152 function(tab) {
153 var d = tab.linkedBrowser.contentWindow.document;
154 tabs.push({
155 location: d.location.href,
156 document: d,
157 title: d.title,
158 "id": d.id,
159 index: idx++,
160 panel: tab.linkedPanel,
161 tab: tab,
162 });
163 });
164 };
165  
166 return tabs;
167 }
168 JS
169 $open_tabs->();
170 }
171  
172 =head2 C<< $api->element_query( \@elements, \%attributes ) >>
173  
174 my $query = $element_query(['input', 'select', 'textarea'],
175 { name => 'foo' });
176  
177 Returns the XPath query that searches for all elements with C<tagName>s
178 in C<@elements> having the attributes C<%attributes>. The C<@elements>
179 will form an C<or> condition, while the attributes will form an C<and>
180 condition.
181  
182 =cut
183  
184 sub element_query {
185 my ($self, $elements, $attributes) = @_;
186 my $query =
187 './/*[(' .
188 join( ' or ',
189 map {
190 sprintf qq{local-name(.)="%s"}, uc $_
191 } @$elements
192 )
193 . ') and '
194 . join( " and ",
195 map { sprintf q{@%s="%s"}, $_, $attributes->{$_} }
196 sort keys(%$attributes)
197 )
198 . ']';
199 };
200  
201 1;
202  
203 =head1 AUTHOR
204  
205 Max Maischein C<corion@cpan.org>
206  
207 =head1 COPYRIGHT (c)
208  
209 Copyright 2009-2014 by Max Maischein C<corion@cpan.org>.
210  
211 =head1 LICENSE
212  
213 This module is released under the same terms as Perl itself.
214  
215 =cut