blib/lib/HTML/Strip.pm

filter: Statementstmt 96.5% 56 / 58 Branchbran 78.5% 11 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 100% 7 / 7 totaltotal 94.5% 87 / 92 CC 7 SCAR 6.2
CC
7
Cov
93%
new
3.0
_do_filter
3.0
filter_entities
2.1
CRAP 1.9
↑ Summary strip_html.c »
1 package HTML::Strip;
2  
3 13 require DynaLoader;
4 13 our @ISA = qw(DynaLoader);
5 13 our $VERSION = '2.10';
6 13 bootstrap HTML::Strip $VERSION;
7  
8 13 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 13 my $_html_entities_p = eval { require HTML::Entities; 1 };
15  
16 13 ▶my %defaults = (
Branch: 1 branch
BranchTrueFalseTotal
$_html_entities_p ? : 13 0 13
17 striptags => [qw( title
18 style
19 script
20 applet )],
21 emit_spaces => 1,
22 decode_entities => 1,
23 filter => $_html_entities_p ? 'filter_entities' : undef,
24 auto_reset => 0,
25 debug => 0,
26 );
27  
28 sub new {
29 17 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.1
Pod documented
30 17 my $obj = _create();
31 17 bless $obj, $class;
32  
33 17 my %args = (%defaults, @_);
34 17 ▶ while( my ($key, $value) = each %args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 102 17 119
35 102 my $method = "set_${key}";
36 102 ▶ if( $obj->can($method) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->can($method)) { } 102 0 102
37 102 $obj->$method($value);
38 } else {
39 0 Carp::carp "Invalid setting '$key'";
40 }
41 }
42 17 return $obj;
43 }
44  
45 sub set_striptags {
46 19 ▶ my ($self, @tags) = @_;
Subroutine set_striptags: called CC=2 SCAR=6.9
Pod documented
47 19 ▶ if( ref($tags[0]) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tags[0] eq 'ARRAY') { } 18 1 19
48 18 $self->_set_striptags_ref( $tags[0] );
49 } else {
50 1 $self->_set_striptags_ref( \@tags );
51 }
52 }
53  
54 {
55 # an inside-out object approach
56 # for the 'filter' attribute
57 13 my %filter_of;
58  
59 sub set_filter {
60 17 ▶ my ($self, $filter) = @_;
Subroutine set_filter: called CC=1 SCAR=0.0
Pod documented
61 17 $filter_of{0+$self} = $filter;
62 }
63  
64 sub filter {
65 40 ▶ my $self = shift;
Subroutine filter: called CC=1 SCAR=0.0
Pod documented
66 40 return $filter_of{0+$self}
67 }
68  
69 # XXX rename _xs_destroy() to DESTROY() in Strip.xs if removing this code
70 sub DESTROY {
71 17 ▶ my $self = shift;
Subroutine DESTROY: called CC=1 SCAR=0.0
72 17 delete $filter_of{0+$self};
73 17 $self->_xs_destroy;
74 }
75 }
76  
77 # $decoded_string = $self->filter_entities( $string )
78 13 sub filter_entities {
79 38 ▶ my $self = shift;
Subroutine filter_entities: called CC=2 SCAR=7.6
Pod documented
80 38 ▶ if( $self->decode_entities ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->decode_entities) 38 0 38
81 38 return HTML::Entities::decode($_[0]);
82 }
83 0 return $_[0];
84 }
85  
86 sub _do_filter {
87 40 ▶ my $self = shift;
Subroutine _do_filter: called CC=3 SCAR=11.0
88 40 my $filter = $self->filter;
89 # no filter: return immediately
90 40 ▶ return $_[0] unless defined $filter;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filter 1 39 40
91  
92 39 ▶ if ( !ref $filter ) { # method name
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $filter) { } 38 1 39
93 38 return $self->$filter( @_ );
94 } else { # code ref
95 1 return $filter->( @_ );
96 }
97 }
98  
99 sub parse {
100 40 ▶ my ($self, $text) = @_;
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
101 40 my $stripped = $self->_strip_html( $text );
102 40 return $self->_do_filter( $stripped );
103 }
104  
105 sub eof {
106 24 ▶ my $self = shift;
Subroutine eof: called CC=1 SCAR=0.0
Pod documented
107 24 $self->_reset();
108 }
109  
110 13 1;
111 __END__
↑ Summary strip_html.c »