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.11';
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 emit_newlines => 0,
23 decode_entities => 1,
24 filter => $_html_entities_p ? 'filter_entities' : undef,
25 auto_reset => 0,
26 debug => 0,
27 );
28  
29 sub new {
30 17 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.1
Pod documented
31 17 my $obj = _create();
32 17 bless $obj, $class;
33  
34 17 my %args = (%defaults, @_);
35 17 ▶ while( my ($key, $value) = each %args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 119 17 136
36 119 my $method = "set_${key}";
37 119 ▶ if( $obj->can($method) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->can($method)) { } 119 0 119
38 119 $obj->$method($value);
39 } else {
40 0 Carp::carp "Invalid setting '$key'";
41 }
42 }
43 17 return $obj;
44 }
45  
46 sub set_striptags {
47 19 ▶ my ($self, @tags) = @_;
Subroutine set_striptags: called CC=2 SCAR=6.9
Pod documented
48 19 ▶ if( ref($tags[0]) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tags[0] eq 'ARRAY') { } 18 1 19
49 18 $self->_set_striptags_ref( $tags[0] );
50 } else {
51 1 $self->_set_striptags_ref( \@tags );
52 }
53 }
54  
55 {
56 # an inside-out object approach
57 # for the 'filter' attribute
58 13 my %filter_of;
59  
60 sub set_filter {
61 17 ▶ my ($self, $filter) = @_;
Subroutine set_filter: called CC=1 SCAR=0.0
Pod documented
62 17 $filter_of{0+$self} = $filter;
63 }
64  
65 sub filter {
66 40 ▶ my $self = shift;
Subroutine filter: called CC=1 SCAR=0.0
Pod documented
67 40 return $filter_of{0+$self}
68 }
69  
70 # XXX rename _xs_destroy() to DESTROY() in Strip.xs if removing this code
71 sub DESTROY {
72 17 ▶ my $self = shift;
Subroutine DESTROY: called CC=1 SCAR=0.0
73 17 delete $filter_of{0+$self};
74 17 $self->_xs_destroy;
75 }
76 }
77  
78 # $decoded_string = $self->filter_entities( $string )
79 13 sub filter_entities {
80 38 ▶ my $self = shift;
Subroutine filter_entities: called CC=2 SCAR=7.6
Pod documented
81 38 ▶ if( $self->decode_entities ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->decode_entities) 38 0 38
82 38 return HTML::Entities::decode($_[0]);
83 }
84 0 return $_[0];
85 }
86  
87 sub _do_filter {
88 40 ▶ my $self = shift;
Subroutine _do_filter: called CC=3 SCAR=11.0
89 40 my $filter = $self->filter;
90 # no filter: return immediately
91 40 ▶ return $_[0] unless defined $filter;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filter 1 39 40
92  
93 39 ▶ if ( !ref $filter ) { # method name
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $filter) { } 38 1 39
94 38 return $self->$filter( @_ );
95 } else { # code ref
96 1 return $filter->( @_ );
97 }
98 }
99  
100 sub parse {
101 40 ▶ my ($self, $text) = @_;
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
102 40 my $stripped = $self->_strip_html( $text );
103 40 return $self->_do_filter( $stripped );
104 }
105  
106 sub eof {
107 24 ▶ my $self = shift;
Subroutine eof: called CC=1 SCAR=0.0
Pod documented
108 24 $self->_reset();
109 }
110  
111 13 1;
112 __END__
↑ Summary strip_html.c »