XUL/State.pm

filter: Statementstmt 86.1% 124 / 144 Branchbran 87.5% 28 / 32 Conditioncond 68.1% 15 / 22 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 71.1% 32 / 45 Podpod 0.0% 0 / 40 totaltotal 69.4% 205 / 295 CC 26 SCAR 10.8
CC
26
Cov
84%
as_command
7.0
new
6.4
set_attribute
5.2
CRAP 2.9
« XUL/Event.pm ↑ Summary XUL/TWindow.pm »
1 package POE::XUL::State;
2 # $Id: State.pm 1566 2010-11-03 03:13:32Z fil $
3 # Copyright Philip Gwyn 2007-2010. All rights reserved.
4 # Based on code Copyright 2003-2004 Ran Eilam. All rights reserved.
5  
6 #
7 # ROLE: Track all the changes to a node so they may be sent to the browser.
8 # All normal responses are generated here, either by ->flush. Or in
9 # some cases calling the make_command directly.
10 #
11 # {buffer} is list of attribute key/value pairs set on state since last flush
12 # {is_new} is true if we have never been flushed before
13 # {is_destroyed} true after node has been destroyed
14 #
15  
16 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 19 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 19 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 19 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 19 our $VERSION = '0.0600';
25 19 our $ID = 0;
26  
27  
28 ##############################################################
29 sub new
30 {
31 55 ▶ my( $package, $node ) = @_;
Subroutine new: called CC=6 SCAR=18.5
Pod undocumented
32 55 my $self = bless {
33 buffer => [],
34 deferred_buffer => [],
35 is_new => 1,
36 is_destroyed => 0,
37 is_textnode => 0
38 }, $package;
39  
40 55 my $id;
41 55 ▶ if( blessed $node and $node->can( 'getAttribute' ) and
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($node) and $node->can('getAttribute') and $node->getAttribute('id')) { } 38 17 55
Condition66%4/6
builtin::blessed($node) and $node->can("getAttribute") and $node->getAttribute("id")
!ll&&!rl&&r
17038
builtin::blessed($node) and $node->can("getAttribute")
!ll&&!rl&&r
01738
MC/DC33%1/3
builtin::blessed($node) and $node->can("getAttribute") and $node->getAttribute("id")
builtin::blessed($node) $node->can("getAttribute") $node->getAttribute("id")
Truth table50%2/4
A and B and C
A= builtin::blessed($node)B= $node->can("getAttribute")C= $node->getAttribute("id")
ABCresult
0XX0
10X0
1100
1111
42 $node->getAttribute( 'id' ) ) {
43 38 $id = $node->getAttribute( 'id' );
44 }
45 else {
46 17 $id = 'PX' . $ID++;
47 17 ▶ if( $node ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) 17 0 17
48 # set the nodes attribute to the generated ID
49 # 2008/10 do NOT use setAttribute, it will call the CM which will
50 # try to build a new State. Infinite recursion.
51 17 ▶ $node->{attributes}{id} ||= $id;
Condition33%1/3
$node->{"attributes"}{"id"} ||= $id
l!l&&r!l&&!r
0170
MC/DC0%0/2
$node->{"attributes"}{"id"} ||= $id
$node->{"attributes"}{"id"} $id
Truth table33%1/3
A ||= B
A= $node->{"attributes"}{"id"}B= $id
ABresult
1X1
011
000
52 }
53 }
54 55 $self->{orig_id} = $self->{id} = $id;
55  
56 55 return $self;
57 }
58  
59 ##############################################################
60 sub flush
61 {
62 208 ▶ my( $self ) = @_;
Subroutine flush: called CC=2 SCAR=7.0
Pod undocumented
63 208 my @out = $self->as_command;
64 208 $self->{is_new} = 0;
65 208 ▶ $self->{index} = delete $self->{trueindex} if defined $self->{trueindex};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"trueindex"} 0 208 208
66 208 $self->clear_buffer;
67 208 return @out;
68 }
69  
70 # command building ------------------------------------------------------------
71  
72 sub as_command {
73 208 ▶ my $self = shift;
Subroutine as_command: called CC=7 SCAR=19.5
Pod undocumented
74  
75 208 my $is_new = $self->{is_new};
76 208 my $is_destroyed = $self->{is_destroyed};
77  
78 # TODO: this is probably a bad idea
79 208 ▶ return if $is_new && $is_destroyed;
Branch: 1 branch
BranchTrueFalseTotal
if $is_new and $is_destroyed 4 204 208
Condition100%3/3
$is_new and $is_destroyed
!ll&&!rl&&r
156484
MC/DC100%2/2
$is_new and $is_destroyed
$is_new $is_destroyed
Truth table100%3/3
A and B
A= $is_newB= $is_destroyed
ABresult
0X0
100
111
80  
81 204 ▶ if( $is_destroyed ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($is_destroyed) { } 5 199 204
elsif ($self->is_textnode) { } 43 156 199
elsif ($self->{'cdata'}) { } 8 148 156
82 5 return $self->get_buffer_as_commands;
83 }
84 elsif( $self->is_textnode ) {
85 43 return $self->make_command_textnode;
86 }
87 elsif( $self->{cdata} ) {
88 8 ▶ return unless $self->{is_new};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"is_new"} 3 5 8
89 5 return $self->make_command_cdata;
90 }
91 else {
92 148 return $self->make_command_new, $self->get_buffer_as_commands;
93 }
94 }
95  
96 sub as_deferred_command {
97 199 ▶ my $self = shift;
Subroutine as_deferred_command: called CC=2 SCAR=7.0
Pod undocumented
98  
99 199 my $is_new = $self->{is_new};
100 199 my $is_destroyed = $self->{is_destroyed};
101  
102 # TODO: this is probably a bad idea
103 199 ▶ return if $is_destroyed;
Branch: 1 branch
BranchTrueFalseTotal
if $is_destroyed 0 199 199
104 199 return $self->get_buffer_as_deferred_commands;
105 }
106  
107 ##############################################################
108 sub make_command_new
109 {
110 148 ▶ my( $self ) = @_;
Subroutine make_command_new: called CC=4 SCAR=13.9
Pod undocumented
111 148 ▶ return unless $self->{is_new};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"is_new"} 119 29 148
112 # return unless $self->get_tag;
113
114 my @cmd = ( 'new',
115 $self->{orig_id},
116 29 ▶ $self->get_tag,
Condition100%2/2
$self->get_parent_id || ''
l!l
236
MC/DC100%1/1
$self->get_parent_id || ''
$self->get_parent_id
Truth table100%2/2
A
A= $self->get_parent_id
Aresult
11
00
117 ( $self->get_parent_id || '' )
118 );
119 29 ▶ if( exists $self->{index} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"index"}) 23 6 29
120 23 push @cmd, $self->{index};
121 }
122  
123 29 delete $self->{orig_id};
124  
125 29 return \@cmd;
126 }
127  
128 ##############################################################
129 sub make_command_bye
130 {
131 7 ▶ my( $self, $parent_id, $index ) = @_;
Subroutine make_command_bye: called CC=1 SCAR=0.0
Pod undocumented
132 7 return [ bye => $self->{id} ] #, $parent_id, $index ];
133 }
134  
135 ##############################################################
136 sub make_command_textnode
137 {
138 43 ▶ my( $self ) = @_;
Subroutine make_command_textnode: called CC=3 SCAR=11.0
Pod undocumented
139 43 ▶ return unless $self->{buffer} and $self->{buffer}[-1];
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"buffer"} and $self->{"buffer"}[-1] 28 15 43
Condition66%2/3
$self->{"buffer"} and $self->{"buffer"}[-1]
!ll&&!rl&&r
02815
MC/DC50%1/2
$self->{"buffer"} and $self->{"buffer"}[-1]
$self->{"buffer"} $self->{"buffer"}[-1]
Truth table66%2/3
A and B
A= $self->{"buffer"}B= $self->{"buffer"}[-1]
ABresult
0X0
100
111
140 my $ret = [ 'textnode',
141 $self->get_parent_id,
142 $self->{index},
143 15 $self->{buffer}[-1][-1]
144 ];
145 15 return $ret;
146 }
147  
148 ##############################################################
149 sub make_command_textnode_bye
150 {
151 2 ▶ my( $self, $parent_id, $index ) = @_;
Subroutine make_command_textnode_bye: called CC=1 SCAR=0.0
Pod undocumented
152 2 return [ 'bye-textnode', $parent_id, $index ];
153 }
154  
155 ##############################################################
156 sub make_command_cdata
157 {
158 5 ▶ my( $self ) = @_;
Subroutine make_command_cdata: called CC=1 SCAR=0.0
Pod undocumented
159 # use Data::Dumper;
160 # warn Dumper $self->{buffer};
161 my $ret = [ 'cdata',
162 $self->get_parent_id,
163 $self->{index},
164 $self->{cdata}
165 5 ];
166 5 return $ret;
167 }
168  
169 ##############################################################
170 sub make_command_cdata_bye
171 {
172 0 ▶ my( $self, $parent_id, $index ) = @_;
Subroutine make_command_cdata_bye: not called CC=1 SCAR=6.9
Pod undocumented
173 0 return [ 'bye-cdata', $parent_id, $index ];
174 }
175  
176  
177 ##############################################################
178 sub make_command_SID
179 {
180 0 ▶ my( $package, $SID ) = @_;
Subroutine make_command_SID: not called CC=1 SCAR=6.9
Pod undocumented
181 0 return [ 'SID', $SID ];
182 }
183  
184 ##############################################################
185 sub make_command_boot
186 {
187 0 ▶ my( $package, $msg ) = @_;
Subroutine make_command_boot: not called CC=1 SCAR=6.9
Pod undocumented
188 0 return [ 'boot', $msg ];
189 }
190  
191 #############################################################
192 sub make_command_set
193 {
194 48 ▶ my($self, $key, $value) = @_;
Subroutine make_command_set: called CC=1 SCAR=0.0
Pod undocumented
195  
196 48 return [ 'set', $self->{id}, $key, $value ];
197 }
198  
199 #############################################################
200 sub make_command_method
201 {
202 1 ▶ my($self, $key, $args) = @_;
Subroutine make_command_method: called CC=1 SCAR=0.0
Pod undocumented
203  
204 1 return [ 'method', $self->{id}, $key, $args ];
205 }
206  
207 #############################################################
208 sub make_command_style
209 {
210 8 ▶ my($self, $property, $value) = @_;
Subroutine make_command_style: called CC=1 SCAR=0.0
Pod undocumented
211  
212 8 $property =~ s/-([a-z])/\U$1/g;
213 8 return [ 'style', $self->{id}, $property, $value ];
214 }
215  
216 #############################################################
217 sub make_command_remove
218 {
219 4 ▶ my($self, $key) = @_;
Subroutine make_command_remove: called CC=1 SCAR=0.0
Pod undocumented
220 4 return [ 'remove', $self->{id}, $key ];
221 }
222  
223  
224  
225 #############################################################
226 sub get_buffer_as_commands
227 {
228 153 ▶ my( $self ) = @_;
Subroutine get_buffer_as_commands: called CC=1 SCAR=0.0
Pod undocumented
229 153 return $self->get_buffer;
230 }
231  
232 #############################################################
233 sub get_buffer_as_deferred_commands
234 {
235 199 ▶ my( $self ) = @_;
Subroutine get_buffer_as_deferred_commands: called CC=2 SCAR=6.9
Pod undocumented
236  
237 # Just in case the ID changed since the command was added
238 # to the deferred buffer
239 199 foreach my $cmd ( $self->get_deferred_buffer ) {
240 1 $cmd->[1] = $self->{id};
241 }
242 199 return $self->get_deferred_buffer;
243 }
244  
245 sub set_trueindex
246 {
247 0 ▶ my( $self, $index ) = @_;
Subroutine set_trueindex: not called CC=1 SCAR=6.9
Pod undocumented
248  
249 0 $self->{trueindex} = $index;
250 }
251  
252  
253 #############################################################
254 sub set_attribute
255 {
256 48 ▶ my( $self, $key, $value ) = @_;
Subroutine set_attribute: called CC=5 SCAR=16.6
Pod undocumented
257 48 ▶ if( $key eq 'id' and ($self->{orig_id}||'' ) eq $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "id" and ($self->{"orig_id"} || "") eq $value) 0 48 48
Condition60%3/5
$key eq "id" and ($self->{"orig_id"} || "") eq $value
!ll&&!rl&&r
4710
$self->{'orig_id'} || ''
l!l
10
MC/DC50%1/2
$key eq "id" and ($self->{"orig_id"} || "") eq $value
$key eq "id" $self->{'orig_id'}
Truth table66%2/3
A and B
A= $key eq "id"B= $self->{'orig_id'}
ABresult
0X0
100
111
258 0 return;
259 }
260 48 my $cmd = $self->make_command_set( $key, $value );
261 48 ▶ if( $key eq 'selectedIndex' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'selectedIndex') { } 1 47 48
262 1 push @{ $self->{deferred_buffer} }, $cmd;
263 }
264 else {
265 47 push @{$self->{buffer}}, $cmd;
266 }
267 48 return;
268 }
269  
270 #############################################################
271 sub remove_attribute
272 {
273 4 ▶ my( $self, $key ) = @_;
Subroutine remove_attribute: called CC=1 SCAR=0.0
Pod undocumented
274  
275 4 push @{$self->{buffer}}, $self->make_command_remove( $key );
276 4 return;
277 }
278  
279 #############################################################
280 sub method_call
281 {
282 1 ▶ my( $self, $key, $args ) = @_;
Subroutine method_call: called CC=1 SCAR=0.0
Pod undocumented
283 1 push @{$self->{buffer}}, $self->make_command_method( $key, $args );
284 1 return;
285 }
286  
287 #############################################################
288 sub style_change
289 {
290 8 ▶ my( $self, $property, $value ) = @_;
Subroutine style_change: called CC=1 SCAR=0.0
Pod undocumented
291 8 push @{$self->{buffer}}, $self->make_command_style( $property, $value );
292 8 return;
293 }
294  
295 #############################################################
296 sub is_destroyed
297 {
298 9 ▶ my( $self, $parent, $index ) = @_;
Subroutine is_destroyed: called CC=2 SCAR=6.9
Pod undocumented
299 9 $self->{is_destroyed} = 1;
300  
301 9 my $cmd;
302 9 ▶ if( $self->{is_textnode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'is_textnode'}) { } 2 7 9
303 2 $cmd = $self->make_command_textnode_bye( $parent->id, $index );
304 }
305 else {
306 7 $cmd = $self->make_command_bye( $parent->id, $index );
307 }
308 # 2007/05 -- If the node disapears, we want to skip all other commands
309 # that might be sent. However, there might be a case were a commands
310 # side effects are desired, so we are pushing. However that breaks when
311 # something is a "late" command.
312 9 push @{ $self->{buffer} }, $cmd;
313 9 return;
314 }
315  
316 #############################################################
317 sub dispose
318 {
319 0 ▶ my( $self ) = @_;
Subroutine dispose: not called CC=1 SCAR=6.9
Pod undocumented
320 0 $self->clear_buffer;
321 }
322  
323 # accessors -------------------------------------------------------------------
324  
325 0 ▶sub get_id { $_[0]->{id} }
Subroutine get_id: not called CC=1 SCAR=6.9
Pod undocumented
326 98 ▶sub id { $_[0]->{id} }
Subroutine id: called CC=1 SCAR=0.0
Pod undocumented
327 29 ▶sub get_tag { $_[0]->{tag} }
Subroutine get_tag: called CC=1 SCAR=0.0
Pod undocumented
328 0 ▶sub is_new { $_[0]->{is_new} }
Subroutine is_new: not called CC=1 SCAR=6.9
Pod undocumented
329 153 ▶sub get_buffer { @{$_[0]->{buffer}} }
Subroutine get_buffer: called CC=1 SCAR=0.0
Pod undocumented
330 398 ▶sub get_deferred_buffer { @{ $_[0]->{deferred_buffer} } }
Subroutine get_deferred_buffer: called CC=1 SCAR=0.0
Pod undocumented
331 199 ▶sub is_textnode { $_[0]->{is_textnode} }
Subroutine is_textnode: called CC=1 SCAR=0.0
Pod undocumented
332 sub get_parent_id {
333 49 ▶ my( $self ) = @_;
Subroutine get_parent_id: called CC=2 SCAR=6.9
Pod undocumented
334 49 ▶ return unless $self->{parent};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"parent"} 6 43 49
335 43 $self->{parent}->id;
336 }
337  
338 # modifiers -------------------------------------------------------------------
339  
340 0 ▶sub set_id { delete $_[0]->{default_id}; $_[0]->{id} = $_[1] }
Subroutine set_id: not called CC=1 SCAR=6.9
Pod undocumented
341 0 ▶sub set_tag { $_[0]->{tag} = lc $_[1] }
Subroutine set_tag: not called CC=1 SCAR=6.9
Pod undocumented
342 0 ▶sub set_old { $_[0]->{is_new} = 0 }
Subroutine set_old: not called CC=1 SCAR=6.9
Pod undocumented
343 0 ▶sub set_index { $_[0]->{index} = $_[1] }
Subroutine set_index: not called CC=1 SCAR=6.9
Pod undocumented
344 208 ▶sub clear_buffer { $_[0]->{buffer} = [];
Subroutine clear_buffer: called CC=1 SCAR=0.0
Pod undocumented
345 208 $_[0]->{deferred_buffer} = []; }
346 0 ▶sub set_destroyed { $_[0]->{is_destroyed} = 1 }
Subroutine set_destroyed: not called CC=1 SCAR=6.9
Pod undocumented
347 0 ▶sub set_textnode { $_[0]->{is_textnode} = 1 }
Subroutine set_textnode: not called CC=1 SCAR=6.9
Pod undocumented
348 # sub set_parent_id { $_[0]->{parent_id} = $_[1] }
349  
350  
351 19 1;
« XUL/Event.pm ↑ Summary XUL/TWindow.pm »