CollapseWhitespace.pm

filter: Statementstmt 94.7% 54 / 57 Branchbran 84.6% 22 / 26 Conditioncond 80.4% 37 / 46 MC/DCmcdc 46.1% 12 / 26 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 81.8% 135 / 165 CC 30 SCAR 28.3
CC
30
Cov
88%
collapseWhitespace
24.6
_next
7.0
remove
2.0
CRAP 16.9
↑ Summary Turndown/Node.pm »
1 package Text::HTML::CollapseWhitespace 0.12;
2 6 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use stable 'postderef';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 require Text::HTML::Turndown::Node;
8  
9 6 our @EXPORT_OK = ('collapseWhitespace');
10  
11 =head1 NAME
12  
13 Text::HTML::CollapseWhitespace - remove extraneous whitespace from a fragment
14  
15 =head1 SYNOPSIS
16  
17 my $tree = XML::LibXML->new->parse_html_string(
18 $input,
19 { recover => 2, encoding => 'UTF-8' }
20 );
21 $tree = collapseWhitespace($tree);
22  
23 =cut
24  
25 =head1 FUNCTIONS
26  
27 =head2 C<< collapseWhitespace (%options) >>
28  
29 collapseWhitespace( element => $tree,
30 isVoid => \&_isVoid,
31 )
32  
33 our @voidElements = (
34 'AREA', 'BASE', 'BR', 'COL', 'COMMAND', 'EMBED', 'HR', 'IMG', 'INPUT',
35 'KEYGEN', 'LINK', 'META', 'PARAM', 'SOURCE', 'TRACK', 'WBR'
36 );
37 our %voidElements = map { $_ => 1, lc $_ => 1 } @voidElements;
38 sub _isVoid( $element ) {
39 $voidElements{ $element->nodeName }
40 }
41  
42 This function modifies the tree in-place and removes extraneous whitespace
43 from the elements. The C<isPre>, C<isVoid> and C<isBlock> predicates allow you
44 to customize what elements are recognized as pre , void or block HTML elements
45 if needed.
46  
47 =cut
48  
49 167 ▶sub collapseWhitespace (%options) {
Subroutine collapseWhitespace: called CC=23 SCAR=32.0
Pod documented
50 167 my $element = $options{ element }; # should be XML::LibXML
51 167 ▶ my $isBlock = $options{ isBlock } // \&Text::HTML::Turndown::Node::_isBlock;
Condition50%1/2
$options{"isBlock"} // \&Text::HTML::Turndown::Node::_isBlock
l!l
1670
MC/DC0%0/1
$options{"isBlock"} // \&Text::HTML::Turndown::Node::_isBlock
$options{"isBlock"}
Truth table50%1/2
A
A= $options{"isBlock"}
Aresult
11
00
52 167 ▶ my $isVoid = $options{ isVoid } // \&Text::HTML::Turndown::Node::_isVoid;
Condition50%1/2
$options{"isVoid"} // \&Text::HTML::Turndown::Node::_isVoid
l!l
1670
MC/DC0%0/1
$options{"isVoid"} // \&Text::HTML::Turndown::Node::_isVoid
$options{"isVoid"}
Truth table50%1/2
A
A= $options{"isVoid"}
Aresult
11
00
53 1429 ▶ my $isPre = $options{ isPre } || sub ($node) {
Subroutine __ANON__: called CC=1 SCAR=0.0
54 1429 return uc($node->nodeName) eq 'PRE'
55 167 ▶ };
Condition66%2/3
$options{'isPre'} || sub ($node) { return uc $node->nodeName eq 'PRE'; }
l!l&&r!l&&!r
51620
MC/DC0%0/2
$options{'isPre'} || sub ($node) { return uc $node->nodeName eq 'PRE'; }
$options{'isPre'} sub ($node) { return uc $node->nodeName eq 'PRE'; }
Truth table66%2/3
A || B
A= $options{'isPre'}B= sub ($node) { return uc $node->nodeName eq 'PRE'; }
ABresult
1X1
011
000
56  
57 return
58 167 ▶ if (!$element->firstChild || $isPre->($element));
Branch: 1 branch
BranchTrueFalseTotal
if not $element->firstChild or &$isPre($element) 0 167 167
Condition33%1/3
not $element->firstChild or &$isPre($element)
l!l&&r!l&&!r
00167
MC/DC0%0/2
not $element->firstChild or &$isPre($element)
not $element->firstChild &$isPre($element)
Truth table33%1/3
not(A) or B
A= not $element->firstChildB= &$isPre($element)
ABresult
1X1
011
000
59  
60 167 my $prevText;
61 my $keepLeadingWs;
62  
63 167 my $prev;
64 167 my $node = _next($prev, $element, $isPre);
65  
66 167 ▶ while (! $node->isEqual($element)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->isEqual($element)) 2222 167 2389
67 2222 ▶ if ($node->nodeType == 3 || $node->nodeType == 4) { # Node.TEXT_NODE or Node.CDATA_SECTION_NODE
Branch: 2 branches
BranchTrueFalseTotal
if ($node->nodeType == 3 or $node->nodeType == 4) { } 647 1575 2222
elsif ($node->nodeType == 1) { } 1407 168 1575
Condition66%2/3
$node->nodeType == 3 or $node->nodeType == 4
l!l&&r!l&&!r
64701575
MC/DC50%1/2
$node->nodeType == 3 or $node->nodeType == 4
$node->nodeType == 3 $node->nodeType == 4
Truth table66%2/3
A or B
A= $node->nodeType == 3B= $node->nodeType == 4
ABresult
1X1
011
000
68 647 my $text = $node->data =~ s/[ \r\n\t]+/ /gr; # we only want to fold ASCII whitespace here
69  
70 647 ▶ if ((!$prevText || $prevText->data =~ / $/) &&
Branch: 1 branch
BranchTrueFalseTotal
if (!$prevText || $prevText->data =~ / $/u and not $keepLeadingWs and substr($text, 0, 1) eq " ") 369 278 647
Condition100%9/9
!$prevText || $prevText->data =~ / $/u and not $keepLeadingWs and substr($text, 0, 1) eq " "
!ll&&!rl&&r
23255369
!$prevText || $prevText->data =~ / $/u and not $keepLeadingWs
!ll&&!rl&&r
158624
not $prevText or $prevText->data =~ / $/u
l!l&&r!l&&!r
6161615
MC/DC100%4/4
!$prevText || $prevText->data =~ / $/u and not $keepLeadingWs and substr($text, 0, 1) eq " "
not $prevText $prevText->data =~ / $/u not $keepLeadingWs substr($text, 0, 1) eq " "
Truth table85%6/7
not(A) or B and not(C) and D
A= not $prevTextB= $prevText->data =~ / $/uC= not $keepLeadingWsD= substr($text, 0, 1) eq " "
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
71 !$keepLeadingWs && substr($text,0,1) eq ' ') {
72 369 $text = substr($text, 1);
73 }
74  
75 # `text` might be empty at this point.
76 647 ▶ if (!$text) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($text) 362 285 647
77 362 $node = remove($node);
78 362 next;
79 }
80  
81 285 $node->setData( $text );
82  
83 285 $prevText = $node
84 } elsif ($node->nodeType == 1) { # Node.ELEMENT_NODE
85 1407 ▶ if ($isBlock->($node) || uc $node->nodeName eq 'BR') {
Branch: 3 branches
BranchTrueFalseTotal
if (&$isBlock($node) or uc $node->nodeName eq 'BR') { } 1264 143 1407
elsif (&$isVoid($node) or &$isPre($node)) { } 19 124 143
elsif ($prevText) { } 78 46 124
Condition100%3/3
&$isBlock($node) or uc $node->nodeName eq "BR"
l!l&&r!l&&!r
12586143
MC/DC100%2/2
&$isBlock($node) or uc $node->nodeName eq "BR"
&$isBlock($node) uc $node->nodeName eq "BR"
Truth table100%3/3
A or B
A= &$isBlock($node)B= uc $node->nodeName eq "BR"
ABresult
1X1
011
000
86 1264 ▶ if ($prevText) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prevText) 252 1012 1264
87 252 $prevText->setData( $prevText->data =~ s/ $//r );
88 }
89 1264 undef $prevText;
90 1264 undef $keepLeadingWs;
91 ▶ } elsif ($isVoid->($node) || $isPre->($node)) {
Condition100%3/3
&$isVoid($node) or &$isPre($node)
l!l&&r!l&&!r
145124
MC/DC100%2/2
&$isVoid($node) or &$isPre($node)
&$isVoid($node) &$isPre($node)
Truth table100%3/3
A or B
A= &$isVoid($node)B= &$isPre($node)
ABresult
1X1
011
000
92 # Avoid trimming space around non-block, non-BR void elements and inline PRE.
93 19 undef $prevText;
94 19 $keepLeadingWs = 1;
95 } elsif ($prevText) {
96 # Drop protection if set previously.
97 78 undef $keepLeadingWs;
98 }
99 } else {
100 168 $node = remove($node);
101 168 next;
102 }
103  
104 1692 my $nextNode = _next($prev, $node, $isPre);
105 1692 $prev = $node;
106 1692 $node = $nextNode;
107 }
108  
109 167 ▶ if ($prevText) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prevText) 0 167 167
110 0 $prevText->{data} =~ s/ $//;
111 0 ▶ if (!$prevText->{data}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prevText->{"data"}) 0 0 0
112 0 remove($prevText)
113 }
114 }
115 167 return $element;
116 }
117  
118 =head2 remove($note)
119  
120 my $next = remove( $node );
121  
122 remove(node) removes the given node from the DOM and returns the
123 next node in the sequence.
124  
125 =cut
126  
127 530 ▶sub remove ($node) {
Subroutine remove: called CC=2 SCAR=7.0
Pod documented
128 530 ▶ my $next = $node->nextSibling || $node->parentNode;
Condition66%2/3
$node->nextSibling || $node->parentNode
l!l&&r!l&&!r
3831470
MC/DC0%0/2
$node->nextSibling || $node->parentNode
$node->nextSibling $node->parentNode
Truth table66%2/3
A || B
A= $node->nextSiblingB= $node->parentNode
ABresult
1X1
011
000
129  
130 530 $node->parentNode->removeChild($node);
131  
132 530 return $next
133 }
134  
135 =head2 _next(prev, current, isPre)
136  
137 my $next = _next( $node );
138  
139 returns the next node in the sequence, given the
140 current and previous nodes.
141  
142 @param {Node} prev
143 @param {Node} current
144 @param {Function} isPre
145 @return {Node}
146  
147 =cut
148  
149 1859 ▶sub _next($prev, $current, $isPre) {
Subroutine _next: called CC=7 SCAR=19.5
150 1859 ▶ if (($prev && $prev->parentNode->isEqual($current)) || $isPre->($current)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev and $prev->parentNode->isEqual($current) or &$isPre($current)) 698 1161 1859
Condition100%6/6
$prev and $prev->parentNode->isEqual($current) or &$isPre($current)
l!l&&r!l&&!r
673251161
$prev and $prev->parentNode->isEqual($current)
!ll&&!rl&&r
333853673
MC/DC100%3/3
$prev and $prev->parentNode->isEqual($current) or &$isPre($current)
$prev $prev->parentNode->isEqual($current) &$isPre($current)
Truth table80%4/5
A and B or C
A= $prevB= $prev->parentNode->isEqual($current)C= &$isPre($current)
ABCresult
11X1
0X11
1011
0X00
1000
151 698 ▶ return $current->nextSibling || $current->parentNode
Condition66%2/3
$current->nextSibling || $current->parentNode
l!l&&r!l&&!r
2804180
MC/DC0%0/2
$current->nextSibling || $current->parentNode
$current->nextSibling $current->parentNode
Truth table66%2/3
A || B
A= $current->nextSiblingB= $current->parentNode
ABresult
1X1
011
000
152 }
153  
154 1161 ▶ return $current->firstChild || $current->nextSibling || $current->parentNode
Condition83%5/6
$current->firstChild || $current->nextSibling || $current->parentNode
l!l&&r!l&&!r
8862750
$current->firstChild or $current->nextSibling
l!l&&r!l&&!r
84046275
MC/DC0%0/3
$current->firstChild || $current->nextSibling || $current->parentNode
$current->firstChild $current->nextSibling $current->parentNode
Truth table75%3/4
A or B || C
A= $current->firstChildB= $current->nextSiblingC= $current->parentNode
ABCresult
1XX1
01X1
0011
0000
155 }
156  
157 6 1;
158  
159 =head1 LICENSE
160  
161 The collapseWhitespace function is adapted from collapse-whitespace
162 by Luc Thevenard.
163  
164 The MIT License (MIT)
165  
166 Copyright (c) 2014 Luc Thevenard <lucthevenard@gmail.com>
167  
168 Permission is hereby granted, free of charge, to any person obtaining a copy
169 of this software and associated documentation files (the "Software"), to deal
170 in the Software without restriction, including without limitation the rights
171 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
172 copies of the Software, and to permit persons to whom the Software is
173 furnished to do so, subject to the following conditions:
174  
175 The above copyright notice and this permission notice shall be included in
176 all copies or substantial portions of the Software.
177  
178 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
179 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
180 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
181 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
182 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
183 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
184 THE SOFTWARE.
185  
186 =head1 REPOSITORY
187  
188 The public repository of this module is
189 L<https://github.com/Corion/Text-HTML-Turndown>.
190  
191 =head1 SUPPORT
192  
193 The public support forum of this module is L<https://perlmonks.org/>.
194  
195 =head1 BUG TRACKER
196  
197 Please report bugs in this module via the Github bug queue at
198 L<https://github.com/Corion/Text-HTML-Turndown/issues>
199  
200 =head1 AUTHOR
201  
202 Max Maischein C<corion@cpan.org>
203  
204 =head1 COPYRIGHT (c)
205  
206 Copyright 2025- by Max Maischein C<corion@cpan.org>.
207  
208 =head1 LICENSE
209  
210 This module is released under the Artistic License 2.0
211 as far as permitted by the license of the original code.
212  
213 =cut
↑ Summary Turndown/Node.pm »