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

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