Tags/Output/Raw.pm

filter: Statementstmt 99.4% 186 / 187 Branchbran 98.2% 55 / 56 Conditioncond 85.0% 17 / 20 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 100% 24 / 24 Podpod 100% 2 / 2 totaltotal 97.0% 292 / 301 CC 36 SCAR 15.1
CC
36
Cov
98%
_put_end_of_tag
11.0
_put_attribute
6.0
_flush_tmp
5.0
CRAP 4.5
« Tags/Output.pm ↑ Summary Tags/Utils.pm »
1 package Tags::Output::Raw;
2  
3 22 ▶use base qw(Tags::Output);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 22 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 22 ▶use List::Util 1.33 qw(none);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 22 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 22 ▶use Tags::Utils::Preserve;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 22 ▶use Tags::Utils qw(encode_attr_entities encode_char_entities);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Constants.
14 22 Readonly::Scalar my $EMPTY_STR => q{};
15 22 Readonly::Scalar my $LAST_INDEX => -1;
16 22 Readonly::Scalar my $SPACE => q{ };
17  
18 22 our $VERSION = 0.16;
19  
20 # Finalize Tags output.
21 5 ▶sub finalize {
Subroutine finalize: called CC=4 SCAR=13.9
Pod documented
22 5 my $self = shift;
23  
24 # XML mode.
25 5 ▶ if ($self->{'xml'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'xml'}) { } 1 4 5
26  
27 # Add ending of all opened tags.
28 1 ▶ while (@{$self->{'printed_tags'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"printed_tags"};}) 1 1 2
29 1 $self->put(['e', $self->{'printed_tags'}->[0]]);
30 }
31  
32 # SGML mode.
33 } else {
34  
35 # Flush tmp code.
36 4 ▶ if (scalar @{$self->{'tmp_code'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"tmp_code"};}) 3 1 4
37 3 $self->_flush_tmp('>');
38 }
39 4 $self->{'printed_tags'} = [];
40 }
41 5 return;
42 }
43  
44 # Resets internal variables.
45 77 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
46 77 my $self = shift;
47  
48 # Comment flag.
49 77 $self->{'comment_flag'} = 0;
50  
51 # Flush code.
52 77 $self->_reset_flush;
53  
54 # Tmp code.
55 77 $self->{'tmp_code'} = [];
56 77 $self->{'tmp_comment_code'} = [];
57  
58 # Printed tags.
59 77 $self->{'printed_tags'} = [];
60  
61 # Preserved object.
62 $self->{'preserve_obj'} = Tags::Utils::Preserve->new(
63 77 'preserved' => $self->{'preserved'},
64 );
65  
66 77 return;
67 }
68  
69 # Check parameters to rigth values.
70 51 ▶sub _check_params {
Subroutine _check_params: called CC=3 SCAR=11.0
71 51 my $self = shift;
72  
73 # Check params from SUPER.
74 51 $self->SUPER::_check_params();
75  
76 # Check 'attr_delimeter'.
77 49 ▶ if ($self->{'attr_delimeter'} ne q{"}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"attr_delimeter"} ne "\"" and $self->{"attr_delimeter"} ne "'") 1 48 49
Condition100%3/3
$self->{"attr_delimeter"} ne "\"" and $self->{"attr_delimeter"} ne "'"
!ll&&!rl&&r
4441
MC/DC100%2/2
$self->{"attr_delimeter"} ne "\"" and $self->{"attr_delimeter"} ne "'"
$self->{"attr_delimeter"} ne "\"" $self->{"attr_delimeter"} ne "'"
Truth table100%3/3
A and B
A= $self->{"attr_delimeter"} ne "\""B= $self->{"attr_delimeter"} ne "'"
ABresult
0X0
100
111
78 && $self->{'attr_delimeter'} ne q{'}) {
79  
80 1 err "Bad attribute delimeter '$self->{'attr_delimeter'}'.";
81 }
82  
83 48 return;
84 }
85  
86 # Default parameters.
87 53 ▶sub _default_parameters {
Subroutine _default_parameters: called CC=1 SCAR=0.0
88 53 my $self = shift;
89  
90 # Default parameters from SUPER.
91 53 $self->SUPER::_default_parameters();
92  
93 # Attribute callback.
94 53 $self->{'attr_callback'} = \&encode_attr_entities;
95  
96 # Attribute delimeter.
97 53 $self->{'attr_delimeter'} = q{"};
98  
99 # CDATA callback.
100 53 $self->{'cdata_callback'} = undef;
101  
102 # Data callback.
103 53 $self->{'data_callback'} = \&encode_char_entities;
104  
105 # No data callback.
106 53 $self->{'no_data_callback'} = ['script', 'style'];
107  
108 # No simple tags.
109 53 $self->{'no_simple'} = [];
110  
111 # Preserved tags.
112 53 $self->{'preserved'} = [];
113  
114 # Raw data callback.
115 53 $self->{'raw_callback'} = undef;
116  
117 # XML output.
118 53 $self->{'xml'} = 0;
119  
120 53 return;
121 }
122  
123 # Flush $self->{'tmp_code'}.
124 83 ▶sub _flush_tmp {
Subroutine _flush_tmp: called CC=5 SCAR=16.1
125 83 my ($self, $string) = @_;
126  
127 # Added string.
128 83 push @{$self->{'tmp_code'}}, $string;
129  
130 # Detect preserve mode.
131 83 my ($pre, $pre_pre) = $self->{'preserve_obj'}->get;
132 83 ▶ if ($pre && ! $pre_pre) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pre and not $pre_pre) 4 79 83
Condition66%2/3
$pre and not $pre_pre
!ll&&!rl&&r
7904
MC/DC50%1/2
$pre and not $pre_pre
$pre not $pre_pre
Truth table66%2/3
A and not(B)
A= $preB= not $pre_pre
ABresult
0X0
100
111
133 4 push @{$self->{'tmp_code'}}, "\n";
134 }
135  
136 # Flush comment code before tag.
137 83 ▶ if ($self->{'comment_flag'} == 0
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'comment_flag'} == 0 and scalar @{$self->{'tmp_comment_code'};}) { } 3 80 83
Condition100%2/2
$self->{"comment_flag"} == 0 and scalar @{$self->{"tmp_comment_code"};}
!ll
281
MC/DC100%1/1
$self->{"comment_flag"} == 0 and scalar @{$self->{"tmp_comment_code"};}
$self->{"comment_flag"} == 0
Truth table100%2/2
A
A= $self->{"comment_flag"} == 0
Aresult
00
11
138 81 && scalar @{$self->{'tmp_comment_code'}}) {
139  
140 $self->{'flush_code'} .= join $EMPTY_STR,
141 3 @{$self->{'tmp_comment_code'}},
142 3 @{$self->{'tmp_code'}};
143  
144 # After tag.
145 } else {
146 $self->{'flush_code'} .= join $EMPTY_STR,
147 80 @{$self->{'tmp_code'}},
148 80 @{$self->{'tmp_comment_code'}};
149 }
150  
151 # Resets tmp_codes.
152 83 $self->{'tmp_code'} = [];
153 83 $self->{'tmp_comment_code'} = [];
154  
155 83 return;
156 }
157  
158 # Attributes.
159 29 ▶sub _put_attribute {
Subroutine _put_attribute: called CC=6 SCAR=18.0
160 29 my ($self, $attr, $value) = @_;
161  
162 # Check to 'tmp_code'.
163 29 ▶ if (! @{$self->{'tmp_code'}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"tmp_code"};}) 1 28 29
164 1 err 'Bad tag type \'a\'.';
165 }
166  
167 # Check to pairs in XML mode.
168 28 ▶ if ($self->{'xml'} && ! defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"xml"} and not defined $value) 0 28 28
Condition66%2/3
$self->{"xml"} and not defined $value
!ll&&!rl&&r
11170
MC/DC0%0/2
$self->{"xml"} and not defined $value
$self->{"xml"} not defined $value
Truth table66%2/3
A and not(B)
A= $self->{"xml"}B= not defined $value
ABresult
0X0
100
111
169 0 err "In XML mode must be a attribute '$attr' value.";
170 }
171  
172 # Process data callback.
173 28 my @attr = ($attr);
174 28 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 27 1 28
175 27 push @attr, $value;
176 }
177 28 $self->_process_callback(\@attr, 'attr_callback');
178  
179 # Process attribute.
180 28 my $full_attr = $attr[0];
181 28 ▶ if (defined $attr[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attr[1]) 27 1 28
182 $full_attr .= q{=}.$self->{'attr_delimeter'}.
183 27 $attr[1].$self->{'attr_delimeter'};
184 }
185 28 push @{$self->{'tmp_code'}}, $SPACE, $full_attr;
186  
187 # Reset comment flag.
188 28 $self->{'comment_flag'} = 0;
189  
190 28 return;
191 }
192  
193 # Begin of tag.
194 84 ▶sub _put_begin_of_tag {
Subroutine _put_begin_of_tag: called CC=2 SCAR=6.9
195 84 my ($self, $tag) = @_;
196  
197 # Flush tmp code.
198 84 ▶ if (scalar @{$self->{'tmp_code'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"tmp_code"};}) 15 69 84
199 15 $self->_flush_tmp('>');
200 }
201  
202 # TODO Add checking of XML element name.
203 # if ($self->{'xml'} && _check(element_name)) {
204 # err 'This is not XML format.';
205 # }
206  
207 # Push begin of tag to tmp code.
208 84 push @{$self->{'tmp_code'}}, "<$tag";
209  
210 # Added tag to printed tags.
211 84 unshift @{$self->{'printed_tags'}}, $tag;
212  
213 84 $self->{'preserve_obj'}->begin($tag);
214  
215 84 return;
216 }
217  
218 # CData.
219 4 ▶sub _put_cdata {
Subroutine _put_cdata: called CC=3 SCAR=11.0
220 4 my ($self, @cdata) = @_;
221  
222 # Flush tmp code.
223 4 ▶ if (scalar @{$self->{'tmp_code'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"tmp_code"};}) 3 1 4
224 3 $self->_flush_tmp('>');
225 }
226  
227 # Added begin of cdata section.
228 4 unshift @cdata, '<![CDATA[';
229  
230 # Check to bad cdata.
231 4 ▶ if (join($EMPTY_STR, @cdata) =~ /]]>$/ms) {
Branch: 1 branch
BranchTrueFalseTotal
if (join($EMPTY_STR, @cdata) =~ /]]>$/ms) 1 3 4
232 1 err 'Bad CDATA data.'
233 }
234  
235 # Added end of cdata section.
236 3 push @cdata, ']]>';
237  
238 # Process data callback.
239 3 $self->_process_callback(\@cdata, 'cdata_callback');
240  
241 # To flush code.
242 3 $self->{'flush_code'} .= join $EMPTY_STR, @cdata;
243  
244 3 return;
245 }
246  
247 # Comment.
248 9 ▶sub _put_comment {
Subroutine _put_comment: called CC=3 SCAR=11.0
249 9 my ($self, @comments) = @_;
250  
251 # Comment string.
252 9 unshift @comments, '<!--';
253 9 ▶ if (substr($comments[$LAST_INDEX], $LAST_INDEX) eq q{-}) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($comments[$LAST_INDEX], $LAST_INDEX) eq '-') { } 1 8 9
254 1 push @comments, ' -->';
255 } else {
256 8 push @comments, '-->';
257 }
258  
259 # Process comment.
260 9 my $comment = join $EMPTY_STR, @comments;
261 9 ▶ if (scalar @{$self->{'tmp_code'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{'tmp_code'};}) { } 5 4 9
262 5 push @{$self->{'tmp_comment_code'}}, $comment;
263  
264 # Flag, that means comment is last.
265 5 $self->{'comment_flag'} = 1;
266 } else {
267 4 $self->{'flush_code'} .= $comment;
268 }
269  
270 9 return;
271 }
272  
273 # Data.
274 35 ▶sub _put_data {
Subroutine _put_data: called CC=3 SCAR=11.0
275 35 my ($self, @character_data) = @_;
276  
277 # Flush tmp code.
278 35 ▶ if (scalar @{$self->{'tmp_code'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"tmp_code"};}) 32 3 35
279 32 $self->_flush_tmp('>');
280 }
281  
282 # Process data callback.
283 35 ▶ if (none { defined $self->{'printed_tags'}->[0] && $_ eq $self->{'printed_tags'}->[0] }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
if (&none(sub { defined $self->{"printed_tags"}[0] and $_ eq $self->{"printed_tags"}[0]; } , @{$$self{"no_data_callback"};})) 34 1 35
defined $self->{"printed_tags"}[0] and $_ eq $self->{"printed_tags"}[0] 67 1 68
284 35 @{$self->{'no_data_callback'}}) {
285  
286 34 $self->_process_callback(\@character_data, 'data_callback');
287 }
288  
289 # To flush code.
290 35 $self->{'flush_code'} .= join $EMPTY_STR, @character_data;
291  
292 35 return;
293 }
294  
295 # End of tag.
296 78 ▶sub _put_end_of_tag {
Subroutine _put_end_of_tag: called CC=11 SCAR=24.0
297 78 my ($self, $tag) = @_;
298  
299 78 ▶ if ($self->{'xml'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"xml"}) 48 30 78
300 48 my $printed = shift @{$self->{'printed_tags'}};
301 48 ▶ if (! defined $printed) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $printed) { } 1 47 48
elsif ($printed ne $tag) { } 1 46 47
302 1 err "Ending bad tag: '$tag' doesn't begin.";
303 } elsif ($printed ne $tag) {
304 1 err "Ending bad tag: '$tag' in block of ".
305 "tag '$printed'.";
306 }
307 }
308  
309 # Tag can be simple.
310 76 ▶ if ($self->{'xml'} && (! scalar @{$self->{'no_simple'}}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'xml'} and !scalar(@{$self->{'no_simple'};}) || &none(sub { $_ eq $tag; } , @{$$self{'no_simple'};})) { } 45 31 76
Condition83%5/6
$self->{"xml"} and !scalar(@{$self->{"no_simple"};}) || &none(sub { $_ eq $tag; } , @{$$self{"no_simple"};})
!ll&&!rl&&r
30145
not scalar @{$self->{"no_simple"};} or &none(sub { $_ eq $tag; } , @{$$self{"no_simple"};})
l!l&&r!l&&!r
4501
MC/DC66%2/3
$self->{"xml"} and !scalar(@{$self->{"no_simple"};}) || &none(sub { $_ eq $tag; } , @{$$self{"no_simple"};})
$self->{"xml"} not scalar @{$self->{"no_simple"};} &none(sub { $_ eq $tag; } , @{$$self{"no_simple"};})
Truth table75%3/4
A and not(B) or C
A= $self->{"xml"}B= not scalar @{$self->{"no_simple"};}C= &none(sub { $_ eq $tag; } , @{$$self{"no_simple"};})
ABCresult
0XX0
1000
11X1
1011
311 1 ▶ || none { $_ eq $tag } @{$self->{'no_simple'}})) {
Subroutine __ANON__: called CC=1 SCAR=0.0
312  
313 45 ▶ if (scalar @{$self->{'tmp_code'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{'tmp_code'};}) { } 13 32 45
314 13 ▶ if (scalar @{$self->{'tmp_comment_code'}}
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{'tmp_comment_code'};} and $self->{'comment_flag'} == 1) { } 2 11 13
Condition100%3/3
scalar @{$self->{"tmp_comment_code"};} and $self->{"comment_flag"} == 1
!ll&&!rl&&r
1012
MC/DC100%2/2
scalar @{$self->{"tmp_comment_code"};} and $self->{"comment_flag"} == 1
scalar @{$self->{"tmp_comment_code"};} $self->{"comment_flag"} == 1
Truth table100%3/3
A and B
A= scalar @{$self->{"tmp_comment_code"};}B= $self->{"comment_flag"} == 1
ABresult
0X0
100
111
315 && $self->{'comment_flag'} == 1) {
316  
317 2 $self->_flush_tmp('>');
318 2 $self->{'flush_code'} .= "</$tag>";
319 } else {
320 11 $self->_flush_tmp(' />');
321 }
322 } else {
323 32 $self->{'flush_code'} .= "</$tag>";
324 }
325  
326 # Tag cannot be simple.
327 } else {
328 31 ▶ if (scalar @{$self->{'tmp_code'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"tmp_code"};}) 10 21 31
329 10 $self->_flush_tmp('>');
330 }
331 31 $self->{'flush_code'} .= "</$tag>";
332 }
333 76 $self->{'preserve_obj'}->end($tag);
334  
335 76 return;
336 }
337  
338 # Instruction.
339 3 ▶sub _put_instruction {
Subroutine _put_instruction: called CC=3 SCAR=11.0
340 3 my ($self, $target, $code) = @_;
341  
342 # Flush tmp code.
343 3 ▶ if (scalar @{$self->{'tmp_code'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"tmp_code"};}) 1 2 3
344 1 $self->_flush_tmp('>');
345 }
346  
347 # To flush code.
348 3 $self->{'flush_code'} .= '<?'.$target;
349 3 ▶ if ($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) 2 1 3
350 2 $self->{'flush_code'} .= $SPACE.$code;
351 }
352 3 $self->{'flush_code'} .= '?>';
353  
354 3 return;
355 }
356  
357 # Raw data.
358 11 ▶sub _put_raw {
Subroutine _put_raw: called CC=2 SCAR=6.9
359 11 my ($self, @raw_data) = @_;
360  
361 # Flush tmp code.
362 11 ▶ if (scalar @{$self->{'tmp_code'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"tmp_code"};}) 6 5 11
363 6 $self->_flush_tmp('>');
364 }
365  
366 # Process data callback.
367 11 $self->_process_callback(\@raw_data, 'raw_callback');
368  
369 # To flush code.
370 11 $self->{'flush_code'} .= join $EMPTY_STR, @raw_data;
371  
372 11 return;
373 }
374  
375 # Reset flush code.
376 89 ▶sub _reset_flush {
Subroutine _reset_flush: called CC=1 SCAR=0.0
377 89 my $self = shift;
378  
379 89 $self->{'flush_code'} = $EMPTY_STR;
380  
381 89 return;
382 }
383  
384 22 1;
385  
386 __END__
« Tags/Output.pm ↑ Summary Tags/Utils.pm »