Turndown/Node.pm

filter: Statementstmt 96.0% 73 / 76 Branchbran 73.6% 28 / 38 Conditioncond 80.0% 24 / 30 MC/DCmcdc 47.0% 8 / 17 Subroutinesub 100% 18 / 18 Podpod 0.0% 0 / 6 totaltotal 81.6% 151 / 185 CC 31 SCAR 16.9
CC
31
Cov
87%
flankingWhitespace
8.8
isFlankedByWhitespace
8.6
__ANON__
7.6
CRAP 5.4
« CollapseWhitespace.pm ↑ Summary Turndown/Tables.pm »
1 package Text::HTML::Turndown::Node 0.12;
2 6 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use stable 'postderef';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 our @blockElements = (
8 'ADDRESS', 'ARTICLE', 'ASIDE', 'AUDIO', 'BLOCKQUOTE', 'BODY', 'CANVAS',
9 'CENTER', 'DD', 'DIR', 'DIV', 'DL', 'DT', 'FIELDSET', 'FIGCAPTION', 'FIGURE',
10 'FOOTER', 'FORM', 'FRAMESET', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'HEADER',
11 'HGROUP', 'HR', 'HTML', 'ISINDEX', 'LI', 'MAIN', 'MENU', 'NAV', 'NOFRAMES',
12 'NOSCRIPT', 'OL', 'OUTPUT', 'P', 'PRE', 'SECTION', 'TABLE', 'TBODY', 'TD',
13 'TFOOT', 'TH', 'THEAD', 'TR', 'UL'
14 );
15 6 our %blockElements = map { $_ => 1, lc $_ => 1 } @blockElements;
16  
17 3246 ▶sub _isBlock ($self) {
Subroutine _isBlock: called CC=1 SCAR=0.0
18 3246 $blockElements{ $self->nodeName }
19 };
20  
21 6 our @voidElements = (
22 'AREA', 'BASE', 'BR', 'COL', 'COMMAND', 'EMBED', 'HR', 'IMG', 'INPUT',
23 'KEYGEN', 'LINK', 'META', 'PARAM', 'SOURCE', 'TRACK', 'WBR'
24 );
25 6 our %voidElements = map { $_ => 1, lc $_ => 1 } @voidElements;
26  
27  
28 6 has '_node' => (
29 is => 'ro',
30 required => 1,
31 handles => [qw[
32 parentNode
33 firstChild
34 previousSibling
35 nextSibling
36 childNodes
37 lastChild
38 nodeName
39 nodeValue
40 nodeType
41 textContent
42 getAttribute
43 isEqual
44  
45 toString
46  
47 find
48 ]],
49 );
50  
51 1221 ▶sub _isVoid( $self ) {
Subroutine _isVoid: called CC=1 SCAR=0.0
52 1221 $voidElements{ $self->nodeName }
53 }
54  
55 1078 ▶sub _hasVoid( $self ) {
Subroutine _hasVoid: called CC=1 SCAR=0.0
56 1078 return _has($self, \%voidElements)
57 }
58  
59 6 has ['isVoid', 'hasVoid', 'isBlock', 'isMeaningfulWhenBlank', 'hasMeaningfulWhenBlank',
60 'isCode'] => (
61 is => 'ro',
62 required => 1,
63 );
64  
65 4 ▶sub className( $self ) {
Subroutine className: called CC=1 SCAR=0.0
Pod undocumented
66 4 $self->getAttribute('class');
67 }
68  
69 4715 ▶sub _isCode( $self ) {
Subroutine _isCode: called CC=4 SCAR=13.9
70 4715 ▶ return 1 if uc $self->nodeName eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if uc $self->nodeName eq "CODE" 71 4644 4715
71 4644 my $p = $self->parentNode;
72 4644 ▶ if( $p and $p->can('nodeName')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p and $p->can("nodeName")) 3637 1007 4644
Condition66%2/3
$p and $p->can("nodeName")
!ll&&!rl&&r
100703637
MC/DC50%1/2
$p and $p->can("nodeName")
$p $p->can("nodeName")
Truth table66%2/3
A and B
A= $pB= $p->can("nodeName")
ABresult
0X0
100
111
73 3637 return _isCode($self->parentNode)
74 };
75 }
76  
77 6 our @meaningfulWhenBlankElements = (
78 'A', 'TABLE', 'THEAD', 'TBODY', 'TFOOT', 'TH', 'TD', 'IFRAME', 'SCRIPT',
79 'AUDIO', 'VIDEO'
80 );
81 6 our %meaningfulWhenBlankElements = map { $_ => 1, lc $_ => 1 } @meaningfulWhenBlankElements;
82  
83 1078 ▶sub _isMeaningfulWhenBlank( $self ) {
Subroutine _isMeaningfulWhenBlank: called CC=1 SCAR=0.0
84 1078 $meaningfulWhenBlankElements{ $self->nodeName }
85 }
86  
87 1078 ▶sub _hasMeaningfulWhenBlank( $self ) {
Subroutine _hasMeaningfulWhenBlank: called CC=1 SCAR=0.0
88 1078 _has( $self, \%meaningfulWhenBlankElements )
89 }
90  
91 2156 ▶sub _has( $self, $nodeNames ) {
Subroutine _has: called CC=4 SCAR=13.9
92 2156 ▶ return if ! $self->can('getElementsByTagName');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("getElementsByTagName") 642 1514 2156
93 1514 for my $tag (sort keys $nodeNames->%*) {
94 39451 ▶ return 1 if $self->getElementsByTagName($tag)
Branch: 1 branch
BranchTrueFalseTotal
if $self->getElementsByTagName($tag) 197 39254 39451
95 }
96 }
97  
98 54 ▶sub firstNonBlankChild( $self ) {
Subroutine firstNonBlankChild: called CC=1 SCAR=0.0
Pod undocumented
99 54 return ([$self->_node->nonBlankChildNodes]->[0]);
100 }
101  
102 757 ▶sub isBlank( $self ) {
Subroutine isBlank: called CC=5 SCAR=16.1
Pod undocumented
103 757 ▶ !$self->isVoid
Branch: 1 branch
BranchTrueFalseTotal
not $self->isVoid || $self->isMeaningfulWhenBlank and $self->textContent =~ /^\s*$/u and not $self->hasVoid and not $self->hasMeaningfulWhenBlank 11 746 757
Condition100%9/9
not $self->isVoid || $self->isMeaningfulWhenBlank and $self->textContent =~ /^\s*$/u and not $self->hasVoid
!ll&&!rl&&r
7143211
not $self->isVoid || $self->isMeaningfulWhenBlank and $self->textContent =~ /^\s*$/u
!ll&&!rl&&r
15456043
$self->isVoid || $self->isMeaningfulWhenBlank
l!l&&r!l&&!r
27127603
MC/DC100%4/4
not $self->isVoid || $self->isMeaningfulWhenBlank and $self->textContent =~ /^\s*$/u and not $self->hasVoid
$self->isVoid $self->isMeaningfulWhenBlank $self->textContent =~ /^\s*$/u not $self->hasVoid
Truth table100%5/5
not(A || B) and C and not(D)
A= $self->isVoidB= $self->isMeaningfulWhenBlankC= $self->textContent =~ /^\s*$/uD= not $self->hasVoid
ABCDresult
1XXX0
01XX0
000X0
00100
00111
104 && !$self->isMeaningfulWhenBlank
105 && $self->textContent =~ /^\s*$/
106 && !$self->hasVoid
107 && !$self->hasMeaningfulWhenBlank()
108 }
109  
110 1078 ▶around BUILDARGS => sub( $orig, $class, %args ) {
Subroutine __ANON__: called CC=7 SCAR=20.3
111 1078 my $node = $args{ _node };
112 1078 ▶ if( ! exists $args{ isVoid } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"isVoid"}) 1078 0 1078
113 1078 $args{ isVoid } = _isVoid( $node );
114 };
115 1078 ▶ if( ! exists $args{ hasVoid } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"hasVoid"}) 1078 0 1078
116 1078 $args{ hasVoid } = _hasVoid( $node );
117 };
118 1078 ▶ if( ! exists $args{ isMeaningfulWhenBlank } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"isMeaningfulWhenBlank"}) 1078 0 1078
119 1078 $args{ isMeaningfulWhenBlank } = _isMeaningfulWhenBlank( $node );
120 };
121 1078 ▶ if( ! exists $args{ hasMeaningfulWhenBlank } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"hasMeaningfulWhenBlank"}) 1078 0 1078
122 1078 $args{ hasMeaningfulWhenBlank } = _hasMeaningfulWhenBlank( $node );
123 };
124 1078 ▶ if( ! exists $args{ isCode } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"isCode"}) 1078 0 1078
125 1078 $args{ isCode } = _isCode( $node );
126 };
127 1078 ▶ if( ! exists $args{ isBlock } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"isBlock"}) 1078 0 1078
128 1078 $args{ isBlock } = _isBlock( $node );
129 };
130  
131 1078 return $class->$orig(\%args);
132 6 };
133  
134  
135 757 ▶sub flankingWhitespace( $node, $options ) {
Subroutine flankingWhitespace: called CC=8 SCAR=21.7
Pod undocumented
136 757 ▶ if( _isBlock($node) || ($options->{ preformattedCode } && isCode($node) )) {
Branch: 1 branch
BranchTrueFalseTotal
if (_isBlock($node) or $options->{"preformattedCode"} and isCode($node)) 651 106 757
Condition100%6/6
_isBlock($node) or $options->{"preformattedCode"} and isCode($node)
l!l&&r!l&&!r
6465106
$options->{"preformattedCode"} and isCode($node)
!ll&&!rl&&r
10155
MC/DC100%3/3
_isBlock($node) or $options->{"preformattedCode"} and isCode($node)
_isBlock($node) $options->{"preformattedCode"} isCode($node)
Truth table100%4/4
A or B and C
A= _isBlock($node)B= $options->{"preformattedCode"}C= isCode($node)
ABCresult
1XX1
0111
00X0
0100
137 651 return { leading => '', trailing => '' };
138 }
139  
140 106 my $edges = edgeWhitespace( $node->textContent );
141  
142 # abandon leading ASCII WS if left-flanked by ASCII WS
143 106 ▶ if ($edges->{leadingAscii} && isFlankedByWhitespace('left', $node, $options)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($edges->{"leadingAscii"} and isFlankedByWhitespace("left", $node, $options)) 0 106 106
Condition66%2/3
$edges->{"leadingAscii"} and isFlankedByWhitespace("left", $node, $options)
!ll&&!rl&&r
9970
MC/DC0%0/2
$edges->{"leadingAscii"} and isFlankedByWhitespace("left", $node, $options)
$edges->{"leadingAscii"} isFlankedByWhitespace("left", $node, $options)
Truth table66%2/3
A and B
A= $edges->{"leadingAscii"}B= isFlankedByWhitespace("left", $node, $options)
ABresult
0X0
100
111
144 0 $edges->{leading} = $edges->{leadingNonAscii};
145 }
146  
147 # abandon trailing ASCII WS if right-flanked by ASCII WS
148 106 ▶ if ($edges->{trailingAscii} && isFlankedByWhitespace('right', $node, $options)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($edges->{"trailingAscii"} and isFlankedByWhitespace("right", $node, $options)) 0 106 106
Condition66%2/3
$edges->{"trailingAscii"} and isFlankedByWhitespace("right", $node, $options)
!ll&&!rl&&r
9790
MC/DC0%0/2
$edges->{"trailingAscii"} and isFlankedByWhitespace("right", $node, $options)
$edges->{"trailingAscii"} isFlankedByWhitespace("right", $node, $options)
Truth table66%2/3
A and B
A= $edges->{"trailingAscii"}B= isFlankedByWhitespace("right", $node, $options)
ABresult
0X0
100
111
149 $edges->{trailing} = $edges->{trailingNonAscii}
150 0 }
151  
152 return { leading => $edges->{leading}, trailing => $edges->{trailing}, }
153 106 }
154  
155  
156 106 ▶sub edgeWhitespace ($string) {
Subroutine edgeWhitespace: called CC=1 SCAR=0.0
Pod undocumented
157 106 $string =~ /^(([ \t\r\n]*)(\s*))(?:(?=\S)[\s\S]*\S)?((\s*?)([ \t\r\n]*))$/;
158 return {
159 106 leading => $1, # whole string for whitespace-only strings
160 leadingAscii => $2,
161 leadingNonAscii => $3,
162 trailing => $4, # empty for whitespace-only strings
163 trailingNonAscii => $5,
164 trailingAscii => $6,
165 }
166 }
167  
168 16 ▶sub isFlankedByWhitespace( $side, $node, $options) {
Subroutine isFlankedByWhitespace: called CC=8 SCAR=21.5
Pod undocumented
169 16 my $sibling;
170 my $regExp;
171 16 my $isFlanked;
172  
173 16 ▶ if ($side eq 'left') {
Branch: 1 branch
BranchTrueFalseTotal
if ($side eq 'left') { } 7 9 16
174 7 $sibling = $node->previousSibling;
175 7 $regExp = qr/ $/;
176 } else {
177 9 $sibling = $node->nextSibling;
178 9 $regExp = qr/^ /;
179 }
180  
181 16 ▶ if ($sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sibling) 10 6 16
182 10 ▶ if ($sibling->nodeType == 3) {
Branch: 3 branches
BranchTrueFalseTotal
if ($sibling->nodeType == 3) { } 6 4 10
elsif ($options->{'preformattedCode'} and $sibling->nodeName eq 'CODE') { } 0 4 4
elsif ($sibling->nodeType == 1 and not _isBlock($sibling)) { } 4 0 4
183 6 $isFlanked = $sibling->nodeValue =~ /$regExp/;
184 ▶ } elsif ($options->{preformattedCode} && $sibling->nodeName eq 'CODE') {
Condition66%2/3
$options->{"preformattedCode"} and $sibling->nodeName eq "CODE"
!ll&&!rl&&r
220
MC/DC0%0/2
$options->{"preformattedCode"} and $sibling->nodeName eq "CODE"
$options->{"preformattedCode"} $sibling->nodeName eq "CODE"
Truth table66%2/3
A and B
A= $options->{"preformattedCode"}B= $sibling->nodeName eq "CODE"
ABresult
0X0
100
111
185 0 $isFlanked = undef;
186 ▶ } elsif ($sibling->nodeType == 1 && !_isBlock($sibling)) {
Condition33%1/3
$sibling->nodeType == 1 and not _isBlock($sibling)
!ll&&!rl&&r
004
MC/DC0%0/2
$sibling->nodeType == 1 and not _isBlock($sibling)
$sibling->nodeType == 1 not _isBlock($sibling)
Truth table33%1/3
A and not(B)
A= $sibling->nodeType == 1B= not _isBlock($sibling)
ABresult
0X0
100
111
187 4 $isFlanked = $sibling->textContent =~ /$regExp/;
188 }
189 }
190 16 return $isFlanked
191 }
192  
193 6 1;
194 =head1 REPOSITORY
195  
196 The public repository of this module is
197 L<https://github.com/Corion/Text-HTML-Turndown>.
198  
199 =head1 SUPPORT
200  
201 The public support forum of this module is L<https://perlmonks.org/>.
202  
203 =head1 BUG TRACKER
204  
205 Please report bugs in this module via the Github bug queue at
206 L<https://github.com/Corion/Text-HTML-Turndown/issues>
207  
208 =head1 AUTHOR
209  
210 Max Maischein C<corion@cpan.org>
211  
212 =head1 COPYRIGHT (c)
213  
214 Copyright 2025- by Max Maischein C<corion@cpan.org>.
215  
216 =head1 LICENSE
217  
218 This module is released under the Artistic License 2.0.
219  
220 =cut
« CollapseWhitespace.pm ↑ Summary Turndown/Tables.pm »