blib/lib/Mail/Pyzor/Digest/StripHtml.pm

filter: Statementstmt 24.3% 10 / 41 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 42.8% 3 / 7 Podpod 100% 1 / 1 totaltotal 23.7% 14 / 59 CC 6 SCAR 23.3
CC
6
Cov
20%
__ANON__
20.0
__ANON__
12.0
strip
2.0
CRAP 10.2
« blib/lib/Mail/Pyzor/Client.pm ↑ Summary lib/Mail/Pyzor/Client.pm »
1 package Mail::Pyzor::Digest::StripHtml;
2  
3 # Copyright 2018 cPanel, LLC.
4 # All rights reserved.
5 # http://cpanel.net
6 #
7 # This is free software; you can redistribute it and/or modify it under the
8 # same terms as Perl itself. See L<perlartistic>.
9  
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =encoding utf-8
14  
15 =head1 NAME
16  
17 Mail::Pyzor::Digest::StripHtml
18  
19 =head1 SYNOPSIS
20  
21 my $stripped = Mail::Pyzor::Digest::StripHtml::strip($html);
22  
23 =head1 DESCRIPTION
24  
25 This module attempts to duplicate pyzor’s HTML-stripping logic.
26  
27 =head1 ACCURACY
28  
29 This library cannot achieve 100%, bug-for-bug parity with pyzor
30 because to do so would require duplicating Python’s own HTML parsing
31 library. Since that library’s output has changed over time, and those
32 changes in turn affect pyzor, it’s literally impossible to arrive at
33 a single, fully-compatible reimplementation.
34  
35 That said, all known divergences between pyzor and this library involve
36 invalid HTML as input.
37  
38 Please open bug reports for any divergences you identify, particularly
39 if the input is valid HTML.
40  
41 =cut
42  
43 #----------------------------------------------------------------------
44  
45 1 ▶use HTML::Parser ();
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 #----------------------------------------------------------------------
48  
49 =head1 FUNCTIONS
50  
51 =head2 $stripped = strip( $HTML )
52  
53 Give it some HTML, and it’ll give back the stripped text.
54  
55 In B<general>, the stripping consists of removing tags as well as
56 C<E<lt>scriptE<gt>> and C<E<lt>styleE<gt>> elements; however, it also
57 removes HTML entities.
58  
59 This tries very hard to duplicate pyzor’s behavior with invalid HTML.
60  
61 =cut
62  
63 sub strip {
64 0 ▶ my ($html) = @_;
Subroutine strip: not called CC=1 SCAR=6.9
Pod documented
65  
66 0 $html =~ s<\A\s+><>;
67 0 $html =~ s<\s+\z><>;
68  
69 0 my $p = HTML::Parser->new( api_version => 3 );
70  
71 0 my @pieces;
72  
73 0 my $accumulate = 1;
74  
75 $p->handler(
76 start => sub {
77 0 ▶ my ($tagname) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
78  
79 0 ▶ $accumulate = 0 if $tagname eq 'script';
Branch: 1 branch
BranchTrueFalseTotal
if $tagname eq "script" 0 0 0
80 0 ▶ $accumulate = 0 if $tagname eq 'style';
Branch: 1 branch
BranchTrueFalseTotal
if $tagname eq "style" 0 0 0
81  
82 0 return;
83 },
84 0 'tagname',
85 );
86  
87 $p->handler(
88 end => sub {
89 0 ▶ $accumulate = 1;
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 0 return;
91 }
92 0 );
93  
94 $p->handler(
95 text => sub {
96 0 ▶ my ($copy) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
97  
98 0 ▶ return if !$accumulate;
Branch: 1 branch
BranchTrueFalseTotal
unless $accumulate 0 0 0
99  
100 # pyzor’s HTML parser discards HTML entities. On top of that,
101 # we need to match, as closely as possible, pyzor’s handling of
102 # invalid HTML entities … which is a function of Python’s
103 # standard HTML parsing library. This will probably never be
104 # fully compatible with the pyzor, but we can get it close.
105  
106 # The original is:
107 #
108 # re.compile('&#(?:[0-9]+|[xX][0-9a-fA-F]+)[^0-9a-fA-F]')
109 #
110 # The parsing loop then “backs up” one byte if the last
111 # character isn’t a “;”. We use a look-ahead assertion to
112 # mimic that behavior.
113 0 $copy =~ s<\&\# (?:[0-9]+ | [xX][0-9a-fA-F]+) (?: ; | \z | (?=[^0-9a-fA-F]) )>< >gx;
114  
115 # The original is:
116 #
117 # re.compile('&([a-zA-Z][-.a-zA-Z0-9]*)[^a-zA-Z0-9]')
118 #
119 # We again use a look-ahead assertion to mimic Python.
120 0 $copy =~ s<\& [a-zA-Z] [-.a-zA-Z0-9]* (?: ; | \z | (?=[^a-zA-Z0-9]) )>< >gx;
121  
122 # Python’s HTMLParser aborts its parsing loop when it encounters
123 # an invalid numeric reference.
124 0 $copy =~ s<\&\#
125 (?:
126 [^0-9xX] # anything but the expected first char
127 |
128 [0-9]+[a-fA-F] # hex within decimal
129 |
130 [xX][^0-9a-fA-F]
131 )
132 (.*)
133 ><
134 0 ▶ ( -1 == index($1, ';') ) ? q<> : '&#'
Branch: 1 branch
BranchTrueFalseTotal
(index($1, ';') == -1) ? : 0 0 0
135 >exs;
136  
137 # Python’s HTMLParser treats invalid entities as incomplete
138 0 $copy =~ s<(\&\#?)><$1 >gx;
139  
140 0 $copy =~ s<\A\s+><>;
141 0 $copy =~ s<\s+\z><>;
142  
143 0 ▶ push @pieces, \$copy if length $copy;
Branch: 1 branch
BranchTrueFalseTotal
if length $copy 0 0 0
144 },
145 0 'text,tagname',
146 );
147  
148 0 $p->parse($html);
149 0 $p->eof();
150  
151 0 my $payload = join( q< >, map { $$_ } @pieces );
152  
153 # Convert all sequences of whitespace OTHER THAN non-breaking spaces to
154 # plain spaces.
155 0 $payload =~ s<[^\S\x{a0}]+>< >g;
156  
157 0 return $payload;
158 }
159  
160 1 1;
« blib/lib/Mail/Pyzor/Client.pm ↑ Summary lib/Mail/Pyzor/Client.pm »