blib/lib/XML/Minifier.pm

filter: Statementstmt 98.6% 149 / 151 Branchbran 91.6% 110 / 120 Conditioncond 94.7% 18 / 19 MC/DCmcdc 91.6% 11 / 12 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 2 totaltotal 94.8% 295 / 311 CC 73 SCAR 35.6
CC
73
Cov
96%
traverse
40.0
minify
34.9
BEGIN
1.0
CRAP 35.3
↑ Summary
1 package XML::Minifier;
2 25 ▶use 5.010000;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 25 our $VERSION = "1.03";
7  
8 25 ▶use XML::LibXML; # To be installed from CPAN : sudo cpanm XML::LibXML
Subroutine BEGIN: called CC=1 SCAR=0.0
9 # CPAN rules !
10  
11 25 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 25 our @EXPORT = qw(minify);
13 25 our @EXPORT_OK = qw(minify);
14  
15  
16 25 my %do_not_remove_blanks;
17 my $we_have_infos_from_dtd;
18 25 my %opt = ();
19 25 my $doc;
20 my $tree;
21 25 my $root;
22 25 my $parser;
23 25 my $output;
24  
25 sub traverse($$);
26  
27 145 ▶sub minify($%) {
Subroutine minify: called CC=34 SCAR=35.5
Pod undocumented
28 145 my $string = shift;
29  
30 145 %do_not_remove_blanks = ();
31 145 $we_have_infos_from_dtd = 0;
32  
33  
34 145 ▶ if(not defined $string) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $string) 4 141 145
35 4 return undef;
36 }
37  
38 141 ▶ if($string eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($string eq "") 2 139 141
39 2 return "";
40 }
41  
42 139 %opt = @_;
43  
44 # Reinit output
45 139 $output = "";
46  
47 # remove_indent is an alias
48 139 ▶ if($opt{remove_indent}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"remove_indent"}) 2 137 139
49 2 $opt{remove_spaces_line_start} = 1;
50 }
51  
52 # Accept "aggressive" and "agressive" (for people making typos... like me :D)
53 139 ▶ if($opt{agressive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"agressive"}) 1 138 139
54 1 $opt{aggressive} = 1;
55 }
56  
57 # Insane is more than destructive (and aggressive)
58 139 ▶ if($opt{insane}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"insane"}) 17 122 139
59 17 $opt{destructive} = 1;
60 }
61  
62 # Destructive is more than aggressive
63 139 ▶ if($opt{destructive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"destructive"}) 28 111 139
64 28 $opt{aggressive} = 1;
65 }
66  
67 # Aggressive but relatively soft
68 139 ▶ if($opt{aggressive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"aggressive"}) 36 103 139
69 36 ▶ (defined $opt{remove_empty_text}) or $opt{remove_empty_text} = 1; # a bit aggressive
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{"remove_empty_text"} or $opt{"remove_empty_text"} = 1 0 36 36
70 36 ▶ (defined $opt{remove_blanks_start}) or $opt{remove_blanks_start} = 1; # aggressive
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{"remove_blanks_start"} or $opt{"remove_blanks_start"} = 1 0 36 36
71 36 ▶ (defined $opt{remove_blanks_end}) or $opt{remove_blanks_end} = 1; # aggressive
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{"remove_blanks_end"} or $opt{"remove_blanks_end"} = 1 2 34 36
72 }
73  
74 # Remove indent and pseudo invisible characters
75 139 ▶ if($opt{destructive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"destructive"}) 28 111 139
76 28 ▶ (defined $opt{remove_spaces_line_start}) or $opt{remove_spaces_line_start} = 1; # very aggressive
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{"remove_spaces_line_start"} or $opt{"remove_spaces_line_start"} = 1 1 27 28
77 28 ▶ (defined $opt{remove_spaces_line_end}) or $opt{remove_spaces_line_end} = 1; # very aggressive
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{"remove_spaces_line_end"} or $opt{"remove_spaces_line_end"} = 1 1 27 28
78 }
79  
80 # Densify text nodes but clearly change your data
81 139 ▶ if($opt{insane}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"insane"}) 17 122 139
82 17 ▶ (defined $opt{remove_spaces_everywhere}) or $opt{remove_spaces_everywhere} = 1; # very very aggressive
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{"remove_spaces_everywhere"} or $opt{"remove_spaces_everywhere"} = 1 0 17 17
83 17 ▶ (defined $opt{remove_cr_lf_everywhere}) or $opt{remove_cr_lf_everywhere} = 1; # very very aggressive
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{"remove_cr_lf_everywhere"} or $opt{"remove_cr_lf_everywhere"} = 1 0 17 17
84 }
85
86 # Configurable with expand_entities
87 139 $parser = XML::LibXML->new(expand_entities => $opt{expand_entities});
88 139 $tree = $parser->parse_string($string);
89 139 ▶ if($opt{process_xincludes}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"process_xincludes"}) 1 138 139
90 1 $parser->process_xincludes($tree);
91 }
92  
93 139 $root = $tree->getDocumentElement;
94  
95 # I disable automatic xml declaration as :
96 # - It would be printed too late (after pi and subset) and produce broken output
97 # - I want to have full control on it
98 139 $XML::LibXML::skipXMLDeclaration = 1;
99 139 $doc = XML::LibXML::Document->new();
100  
101 # Configurable with no_prolog : do not put prolog (a bit aggressive for readers)
102 # version=1.0 encoding=UTF-8 : choose values
103 # TODO : standalone ?
104 139 ▶ my $version = $opt{version} // "1.0";
Condition100%2/2
$opt{"version"} // "1.0"
l!l
10129
MC/DC100%1/1
$opt{"version"} // "1.0"
$opt{"version"}
Truth table100%2/2
A
A= $opt{"version"}
Aresult
11
00
105 139 ▶ my $encoding = $opt{encoding} // "UTF-8";
Condition100%2/2
$opt{"encoding"} // "UTF-8"
l!l
7132
MC/DC100%1/1
$opt{"encoding"} // "UTF-8"
$opt{"encoding"}
Truth table100%2/2
A
A= $opt{"encoding"}
Aresult
11
00
106 139 ▶ $opt{no_prolog} or $output .= "<?xml version=\"$version\" encoding=\"$encoding\"?>";
Branch: 1 branch
BranchTrueFalseTotal
$opt{"no_prolog"} or $output .= qq[<?xml version="$version" encoding="$encoding"?>] 77 62 139
107  
108 139 my $rootnode;
109  
110  
111 # Parsing first level
112 139 foreach my $flc ($tree->childNodes()) {
113  
114 162 ▶ if(($flc->nodeType eq XML_DTD_NODE) or ($flc->nodeType eq XML_DOCUMENT_TYPE_NODE)) { # second is synonym but deprecated
Branch: 4 branches
BranchTrueFalseTotal
if ($flc->nodeType eq 14 or $flc->nodeType eq 10) { } 15 147 162
elsif ($flc->nodeType eq 7) { } 8 139 147
elsif ($flc->nodeType eq 8) { } 0 139 139
elsif ($flc->nodeType eq 1) { } 139 0 139
Condition66%2/3
$flc->nodeType eq 14 or $flc->nodeType eq 10
l!l&&r!l&&!r
150147
MC/DC50%1/2
$flc->nodeType eq 14 or $flc->nodeType eq 10
$flc->nodeType eq 14 $flc->nodeType eq 10
Truth table66%2/3
A or B
A= $flc->nodeType eq 14B= $flc->nodeType eq 10
ABresult
1X1
011
000
115 # Configurable with keep_dtd
116 15 my $str = $flc->toString();
117 # alternative : my $internaldtd = $tree->internalSubset(); my $str = $internaldtd->toString();
118 15 $str =~ s/\R*//g;
119 15 ▶ $opt{keep_dtd} and $output .= $str;
Branch: 1 branch
BranchTrueFalseTotal
$opt{"keep_dtd"} and $output .= $str 2 13 15
120
121 # XML_ELEMENT_DECL
122 # XML_ATTRIBUTE_DECL
123 # XML_ENTITY_DECL
124 # XML_NOTATION_DECL
125  
126 # I need to manually (yuck) parse the node as XML::LibXML does not provide (wrap) such function
127 15 ▶ if($opt{ignore_dtd}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'ignore_dtd'}) { } 9 6 15
128 # Do not try to get infos from DTD
129 } else {
130 6 foreach my $dc ($flc->childNodes()) {
131 14 ▶ if($dc->nodeType == XML_ELEMENT_DECL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dc->nodeType == 15) 10 4 14
132 10 $we_have_infos_from_dtd = "We can remove empty text in leafs if not protected by DTD";
133 # The .* could appear weak (and match towards other ELEMENTS), but we are working on ONLY ONE <!ELEMENT ... >
134 # (because we are in one child node)
135 10 ▶ if($dc->toString() =~ /<!ELEMENT\s*(\w+)\s*\(.*#PCDATA.*\).*>/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dc->toString =~ /<!ELEMENT\s*(\w+)\s*\(.*#PCDATA.*\).*>/) 5 5 10
136 5 $do_not_remove_blanks{$1} = "Not ignorable due to DTD declaration !";
137 }
138 }
139 }
140 }
141  
142 # Some notes :
143 # If I iterate over attributes of the childs of DTD (so ELEMENT, ATTLIST etc..) I get a segfault
144 # Probable bug from XML::LibXML similar to https://rt.cpan.org/Public/Bug/Display.html?id=71076
145  
146 # If I try to access the content of XML_ENTITY_REF_DECL with getValue I get correct result, but on XML_ELEMENT_DECL I get empty string
147 # Seems like there's no function to play with DTD
148 # I guess we need to write the perl binding for xmlElementPtr xmlGetDtdElementDesc (xmlDtdPtr dtd, const xmlChar * name)
149  
150 # If I try to iterate over childNodes, I never see XML_NOTATION_DECL (why?!)
151  
152 # One word about DTD and XML::LibXML :
153 # DTD validation works like a charm of course...
154 # But reading from one xml and set to another with experimental function seems just broken or works very weirdly
155 # Segfault when reading big external subset, weird message "can't import dtd" when trying to add DTD...
156  
157 } elsif($flc->nodeType eq XML_PI_NODE) {
158 # Configurable with keep_pi
159 8 my $str = $flc->toString();
160 8 ▶ $opt{keep_pi} and $output .= $str;
Branch: 1 branch
BranchTrueFalseTotal
$opt{"keep_pi"} and $output .= $str 2 6 8
161 } elsif($flc->nodeType eq XML_COMMENT_NODE) {
162 # Configurable with keep_comments
163 0 my $str = $flc->toString();
164 0 ▶ $opt{keep_comments} and $output .= $str;
Branch: 1 branch
BranchTrueFalseTotal
$opt{"keep_comments"} and $output .= $str 0 0 0
165 } elsif($flc->nodeType eq XML_ELEMENT_NODE) { # Actually document node as if we do getDocumentNode
166 # "main" tree, only one (parser is protecting us)
167 139 $rootnode = traverse($root, $doc);
168 # XML_ATTRIBUTE_NODE
169 # XML_TEXT_NODE
170 # XML_ENTITY_REF_NODE
171 # XML_COMMENT_NODE
172 # XML_CDATA_SECTION_NODE
173  
174 # Ignore
175 # XML_XINCLUDE_START
176 # XML_XINCLUDE_END
177
178 # Will stay hidden in any case
179 # XML_NAMESPACE_DECL
180  
181 # Not Applicable
182 # XML_DOCUMENT_NODE
183 # XML_DOCUMENT_FRAG_NODE
184 # XML_HTML_DOCUMENT_NODE
185
186 # What is it ?
187 # XML_ENTITY_NODE
188
189 } else {
190 # Should I print these unattended things ?
191 # Should it be configurable ?
192 }
193
194 }
195
196 # XML_ELEMENT_NODE => 1
197 # E.G. : <tag></tag> or <tag/>
198  
199 # XML_ATTRIBUTE_NODE => 2
200 # E.G. : <tag attribute="value">
201  
202 # XML_TEXT_NODE => 3
203 # E.G. : This is a piece of text
204  
205 # XML_CDATA_SECTION_NODE => 4
206 # E.G. : <![CDATA[<sender>John Smith</sender>]]>
207 # CDATA section (not for parsers)
208  
209 # XML_ENTITY_REF_NODE => 5
210 # Entities like &entity;
211  
212 # XML_ENTITY_NODE => 6
213 # XML_PI_NODE => 7
214 # Processing Instructions like <?xml-stylesheet href="style.css">
215  
216 # XML_COMMENT_NODE => 8
217 # Comments like <!-- comment -->
218  
219 # XML_DOCUMENT_NODE => 9
220 # The document itself
221  
222 # XML_DOCUMENT_TYPE_NODE => 10
223 # E.G. : Deprecated, use XML_DOCUMENT_TYPE_NODE
224  
225 # XML_DOCUMENT_FRAG_NODE => 11
226 # E.G. : Never read, for use, should be created as element node
227  
228 # XML_NOTATION_NODE => 12
229 # E.G. : <!NOTATION GIF SYSTEM "GIF"> seems not working
230  
231 # XML_HTML_DOCUMENT_NODE => 13
232 # E.G. : <catalog></catalog>
233 # In HTML context, for us, should be treated as a document node
234  
235 # XML_DTD_NODE => 14
236 # E.G. : <!DOCTYPE book PUBLIC "blahblah" "http://www.example.com/docbookx.dtd" [
237  
238 # XML_ELEMENT_DECL => 15
239 # E.G. : <!ELEMENT element-name EMPTY>
240  
241 # XML_ATTRIBUTE_DECL => 16
242 # E.G. : <!ATTLIST image height CDATA #REQUIRED>
243  
244 # XML_ENTITY_DECL => 17
245 # E.G. : <!ENTITY Entity2 "<strong>Entity</strong>">
246  
247 # XML_NAMESPACE_DECL => 18
248 # E.G. : <catalog xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
249  
250 # XML_XINCLUDE_START => 19
251 # <xi:include href="inc.xml"/> if we process includes
252  
253 # XML_XINCLUDE_END => 20
254 # <xi:include href="inc.xml"/> if we process includes
255  
256 139 $doc->setDocumentElement($rootnode);
257  
258 139 $output .= $doc->toString();
259  
260 139 return $output;
261 }
262  
263 # Traverse the document
264 111424 ▶sub traverse($$) {
Subroutine traverse: called CC=40 SCAR=36.9
Pod undocumented
265 111424 my $node = shift;
266 111424 my $outnode = shift;
267  
268  
269 111424 my $name = $node->getName();
270 111424 my $newnode = $doc->createElement($name);
271  
272 111424 ▶ if($outnode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outnode) 111424 0 111424
273 111424 $outnode->addChild($newnode);
274 }
275  
276 111424 $outnode = $newnode;
277  
278 111424 my @as = $node->attributes ;
279 111424 foreach my $a (@as) {
280 67 $outnode->setAttribute($a->nodeName, $a->value);
281 }
282  
283 111424 foreach my $child ($node->childNodes) {
284 111826 ▶ if($child->nodeType eq XML_TEXT_NODE) {
Branch: 6 branches
BranchTrueFalseTotal
if ($child->nodeType eq 3) { } 477 111349 111826
elsif ($child->nodeType eq 5) { } 2 111347 111349
elsif ($child->nodeType eq 8) { } 32 111315 111347
elsif ($child->nodeType eq 4) { } 20 111295 111315
elsif ($child->nodeType eq 7) { } 8 111287 111295
elsif ($child->nodeType eq 1) { } 111285 2 111287
285 477 my $str = $child->data;
286  
287
288 477 ▶ if($do_not_remove_blanks{$child->parentNode->getName()}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_not_remove_blanks{$child->parentNode->getName}) { } 6 471 477
289 # DO NOT REMOVE, PROTECTED BY DTD ELEMENT DECL
290 } else {
291 # All these substitutions aim to remove indentation that people tend to put in xml files...
292 # ...Or just clean on demand (default behavior keeps these blanks)
293  
294  
295 # Blanks are several things like spaces, tabs, lf, cr, vertical space...
296  
297 # Configurable with remove_blanks_start : remove extra space/lf/cr at the start of the string
298 471 ▶ $opt{remove_blanks_start} and $str =~ s/\A\s*//g;
Branch: 1 branch
BranchTrueFalseTotal
$opt{"remove_blanks_start"} and $str =~ s/\A\s*//g 135 336 471
299 # Configurable with remove_blanks_end : remove extra space/lf/cr at the end of the string
300 471 ▶ $opt{remove_blanks_end} and $str =~ s/\s*\Z//g;
Branch: 1 branch
BranchTrueFalseTotal
$opt{"remove_blanks_end"} and $str =~ s/\s*\Z//g 125 346 471
301  
302  
303 # Only CR and LF
304  
305 # Configurable with remove_cr_lf_everywhere : remove extra lf/cr everywhere
306 471 ▶ $opt{remove_cr_lf_everywhere} and $str =~ s/\R*//g;
Branch: 1 branch
BranchTrueFalseTotal
$opt{"remove_cr_lf_everywhere"} and $str =~ s/\R*//g 53 418 471
307  
308  
309 # Spaces are 2 things : space and tabs
310  
311 # Configurable with remove_spaces_line_start : remove extra spaces or tabs at the start of each line
312 471 ▶ $opt{remove_spaces_line_start} and $str =~ s/^( |\t)*//mg;
Branch: 1 branch
BranchTrueFalseTotal
$opt{"remove_spaces_line_start"} and $str =~ s/^( |\t)*//gm 122 349 471
313 # Configurable with remove_spaces_line_end : remove extra spaces or tabs at the end of each line
314 471 ▶ $opt{remove_spaces_line_end} and $str =~ s/( |\t)*$//mg;
Branch: 1 branch
BranchTrueFalseTotal
$opt{"remove_spaces_line_end"} and $str =~ s/( |\t)*$//gm 102 369 471
315 # Configurable with remove_spaces_everywhere : remove extra spaces everywhere
316 471 ▶ $opt{remove_spaces_everywhere} and $str =~ s/( |\t)*//g;
Branch: 1 branch
BranchTrueFalseTotal
$opt{"remove_spaces_everywhere"} and $str =~ s/( |\t)*//g 58 413 471
317  
318 # Configurable with remove_empty_text : remove text nodes that contains only space/lf/cr
319 471 ▶ $opt{remove_empty_text} and $str =~ s/\A\s*\Z//g;
Branch: 1 branch
BranchTrueFalseTotal
$opt{"remove_empty_text"} and $str =~ s/\A\s*\Z//g 126 345 471
320 }
321
322 # Let me explain, we could have text nodes basically everywhere, and we don't know if whitespaces are ignorable or not.
323 # As we want to minify the xml, we can't just keep all blanks, because it is generally indentation or spaces that could be ignored.
324 # Here is the strategy :
325 # A. If we have <name> </name> we should keep it anyway (unless forced with argument)
326 # B. If we have </name> </person> we should *maybe* remove (in this case parent node contains more than one child node : text node + element node)
327 # C. If we have <person> <name> we should *maybe* remove it (in this case parent node contains more than one child node : text node + element node)
328 # D. If we have </person> <person> we should *maybe* remove it (in this case parent node contains more than one child node : text node + element node)
329 # B, C, D : remove... unless explicitely declared in DTD as potential #PCDATA container OR unless it contains something...
330 # *something* is a comment (not removed), some other text not empty, some cdata.
331 # Imagine </name> <!-- comment --> some text </person> then we don't want to remove spaces in the first text node
332 # Same with </name> <!-- comment --> </person>
333 # But if comments are removed then the latter piece of code will become </name></person>
334  
335 477 my $empty = 1;
336
337 477 my $childbak = $child;
338 477 my @siblings = ();
339 # We want to inspect siblings to the right until we reach an element
340 477 ▶ while($child = $child->nextSibling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child = $child->nextSibling) 398 276 674
341 398 ▶ if($child->nodeType eq XML_ELEMENT_NODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->nodeType eq 1) 201 197 398
342 201 last;
343 }
344 197 push @siblings, $child;
345 }
346 477 $child = $childbak;
347 # We inspect to the left also
348 477 ▶ while($child = $child->previousSibling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child = $child->previousSibling) 402 272 674
349 402 ▶ if($child->nodeType eq XML_ELEMENT_NODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->nodeType eq 1) 205 197 402
350 205 last;
351 }
352 197 push @siblings, $child;
353 }
354  
355 # Then we will look at each siling to check
356 # If it is an empty text node or not
357 # If it is something that will be removed or not
358 477 foreach my $child (@siblings) {
359 245 ▶ if($child->nodeType eq XML_TEXT_NODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->nodeType eq 3) 90 155 245
360 90 ▶ if($child->data =~ m/[^ \t\r\n]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->data =~ /[^ \t\r\n]/) 4 86 90
361 # Not empty
362 4 $empty = 0;
363 4 last;
364 }
365 }
366 241 ▶ if($child->nodeType eq XML_COMMENT_NODE and $opt{keep_comments}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->nodeType eq 8 and $opt{"keep_comments"}) 27 214 241
Condition100%3/3
$child->nodeType eq 8 and $opt{"keep_comments"}
!ll&&!rl&&r
1724227
MC/DC100%2/2
$child->nodeType eq 8 and $opt{"keep_comments"}
$child->nodeType eq 8 $opt{"keep_comments"}
Truth table100%3/3
A and B
A= $child->nodeType eq 8B= $opt{"keep_comments"}
ABresult
0X0
100
111
367 27 $empty = 0;
368 27 last;
369 }
370 214 ▶ if($child->nodeType eq XML_CDATA_SECTION_NODE and $opt{keep_cdata}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->nodeType eq 4 and $opt{"keep_cdata"}) 20 194 214
Condition100%3/3
$child->nodeType eq 4 and $opt{"keep_cdata"}
!ll&&!rl&&r
1643020
MC/DC100%2/2
$child->nodeType eq 4 and $opt{"keep_cdata"}
$child->nodeType eq 4 $opt{"keep_cdata"}
Truth table100%3/3
A and B
A= $child->nodeType eq 4B= $opt{"keep_cdata"}
ABresult
0X0
100
111
371 20 $empty = 0;
372 20 last;
373 }
374 194 ▶ if($child->nodeType eq XML_PI_NODE and $opt{keep_pi}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->nodeType eq 7 and $opt{"keep_pi"}) 8 186 194
Condition100%3/3
$child->nodeType eq 7 and $opt{"keep_pi"}
!ll&&!rl&&r
168188
MC/DC100%2/2
$child->nodeType eq 7 and $opt{"keep_pi"}
$child->nodeType eq 7 $opt{"keep_pi"}
Truth table100%3/3
A and B
A= $child->nodeType eq 7B= $opt{"keep_pi"}
ABresult
0X0
100
111
375 8 $empty = 0;
376 8 last;
377 }
378 # Entity refs : we can choose to expand or not... but not to drop them
379 186 ▶ if($child->nodeType eq XML_ENTITY_REF_NODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->nodeType eq 5) 8 178 186
380 8 $empty = 0;
381 8 last;
382 }
383 }
384  
385  
386 477 $child = $childbak;
387  
388 # Were all siblings empty ?
389 # Are we alone ? (count child nodes from parent instead of filtered siblings)
390 # If there is a DTD, probably we can remove even in the leafs (I'm not doing this at the moment)
391 477 ▶ if($we_have_infos_from_dtd) {
Branch: 2 branches
BranchTrueFalseTotal
if ($we_have_infos_from_dtd) { } 26 451 477
elsif ($empty and @{$child->parentNode->childNodes;} > 1) { } 260 258 518
392 # Only trust DTD, no need to consider if we are in a leaf or node
393 26 ▶ if($do_not_remove_blanks{$child->parentNode->getName()}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_not_remove_blanks{$child->parentNode->getName}) { } 6 20 26
394 # DO NOT REMOVE, PROTECTED BY DTD ELEMENT DECL
395 } else {
396 20 $str =~ s/\A\R*\Z//mg;
397 20 $str =~ s/\A\s*\Z//mg;
398 }
399 384 ▶ } elsif($empty and @{$child->parentNode->childNodes()} > 1) {
Condition100%3/3
$empty and @{$child->parentNode->childNodes;} > 1
!ll&&!rl&&r
67124260
MC/DC100%2/2
$empty and @{$child->parentNode->childNodes;} > 1
$empty @{$child->parentNode->childNodes;} > 1
Truth table100%3/3
A and B
A= $emptyB= @{$child->parentNode->childNodes;} > 1
ABresult
0X0
100
111
400 # Should it be configurable ?
401 260 ▶ if($do_not_remove_blanks{$child->parentNode->getName()}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_not_remove_blanks{$child->parentNode->getName}) { } 0 260 260
402 # DO NOT REMOVE, PROTECTED BY DTD ELEMENT DECL
403 } else {
404 260 $str =~ s/\A\R*\Z//mg;
405 260 $str =~ s/\A\s*\Z//mg;
406 }
407 }
408 477 $outnode->appendText($str);
409 } elsif($child->nodeType eq XML_ENTITY_REF_NODE) {
410 # Configuration will be done above when creating document
411 2 my $er = $doc->createEntityReference($child->getName());
412 2 $outnode->addChild($er);
413 } elsif($child->nodeType eq XML_COMMENT_NODE) {
414 # Configurable with keep_comments
415 32 my $com = $doc->createComment($child->getData());
416 32 ▶ $opt{keep_comments} and $outnode->addChild($com);
Branch: 1 branch
BranchTrueFalseTotal
$opt{"keep_comments"} and $outnode->addChild($com) 13 19 32
417 } elsif($child->nodeType eq XML_CDATA_SECTION_NODE) {
418 # Configurable with keep_cdata
419 #my $cdata = $child->cloneNode(1);
420 20 my $cdata = $doc->createCDATASection($child->getData());
421 20 ▶ $opt{keep_cdata} and $outnode->addChild($cdata);
Branch: 1 branch
BranchTrueFalseTotal
$opt{"keep_cdata"} and $outnode->addChild($cdata) 8 12 20
422 } elsif($child->nodeType eq XML_PI_NODE) {
423 # Configurable with keep_pi
424 #my $pi = $child->cloneNode(1);
425 8 my $pi = $doc->createPI($child->nodeName, $child->getData());
426 8 ▶ $opt{keep_pi} and $outnode->addChild($pi);
Branch: 1 branch
BranchTrueFalseTotal
$opt{"keep_pi"} and $outnode->addChild($pi) 2 6 8
427 } elsif($child->nodeType eq XML_ELEMENT_NODE) {
428 111285 $outnode->addChild(traverse($child, $outnode));
429 }
430 }
431 111424 return $outnode;
432 }
433  
434  
435 25 1;
436  
437 __END__
↑ Summary