blib/lib/HTML/TokeParser.pm

filter: Statementstmt 94.7% 90 / 95 Branchbran 75.9% 41 / 54 Conditioncond 82.3% 14 / 17 MC/DCmcdc 70.0% 7 / 10 Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 87.4% 167 / 191 CC 36 SCAR 20.9
CC
36
Cov
87%
get_text
11.0
get_phrase
9.3
new
9.1
CRAP 8.1
« blib/lib/HTML/PullParser.pm ↑ Summary blib/lib/HTML/Parser.pm »
1 package HTML::TokeParser;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 require HTML::PullParser;
6 1 our @ISA = qw(HTML::PullParser);
7 1 our $VERSION = '3.87';
8  
9 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use HTML::Entities qw(decode_entities);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use HTML::Tagset ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 my %ARGS =
14 (
15 start => "'S',tagname,attr,attrseq,text",
16 end => "'E',tagname,text",
17 text => "'T',text,is_cdata",
18 process => "'PI',token0,text",
19 comment => "'C',text",
20 declaration => "'D',text",
21  
22 # options that default on
23 unbroken_text => 1,
24 );
25  
26  
27 sub new
28 10 ▶{
Subroutine new: called CC=7 SCAR=22.0
Pod documented
29 10 my $class = shift;
30 10 my %cnf;
31  
32 10 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 10 0 10
33 10 ▶ my $type = (ref($_[0]) eq "SCALAR") ? "doc" : "file";
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'SCALAR' ? : 4 6 10
34 10 %cnf = ($type => $_[0]);
35 }
36 else {
37 0 ▶ unshift @_, (ref($_[0]) eq "SCALAR") ? "doc" : "file" if(scalar(@_) % 2 == 1);
Branch: 2 branches
BranchTrueFalseTotal
if scalar(@_) % 2 == 1 0 0 0
ref $_[0] eq 'SCALAR' ? : 0 0 0
38 0 %cnf = @_;
39 }
40  
41 10 ▶ my $textify = delete $cnf{textify} || {img => "alt", applet => "alt"};
Condition50%1/2
delete $cnf{'textify'} || {'img', 'alt', 'applet', 'alt'}
l!l
010
MC/DC0%0/1
delete $cnf{'textify'} || {'img', 'alt', 'applet', 'alt'}
delete $cnf{'textify'}
Truth table50%1/2
A
A= delete $cnf{'textify'}
Aresult
11
00
42  
43 10 ▶ my $self = $class->SUPER::new(%ARGS, %cnf) || return undef;
Condition100%2/2
$class->SUPER::new(%ARGS, %cnf) || (return undef)
l!l
91
MC/DC100%1/1
$class->SUPER::new(%ARGS, %cnf) || (return undef)
$class->SUPER::new(%ARGS, %cnf)
Truth table100%2/2
A
A= $class->SUPER::new(%ARGS, %cnf)
Aresult
11
00
44  
45 9 $self->{textify} = $textify;
46 9 $self;
47 }
48  
49  
50 sub get_tag
51 43 ▶{
Subroutine get_tag: called CC=8 SCAR=20.8
Pod documented
52 43 my $self = shift;
53 43 my $token;
54 43 while (1) {
55 117 ▶ $token = $self->get_token || return undef;
Condition100%2/2
$self->get_token || (return undef)
l!l
1152
MC/DC100%1/1
$self->get_token || (return undef)
$self->get_token
Truth table100%2/2
A
A= $self->get_token
Aresult
11
00
56 115 my $type = shift @$token;
57 115 ▶ next unless $type eq "S" || $type eq "E";
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "S" or $type eq "E" 58 57 115
Condition100%3/3
$type eq "S" or $type eq "E"
l!l&&r!l&&!r
362158
MC/DC100%2/2
$type eq "S" or $type eq "E"
$type eq "S" $type eq "E"
Truth table100%3/3
A or B
A= $type eq "S"B= $type eq "E"
ABresult
1X1
011
000
58 57 ▶ substr($token->[0], 0, 0) = "/" if $type eq "E";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "E" 21 36 57
59 57 ▶ return $token unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 36 21 57
60 21 for (@_) {
61 24 ▶ return $token if $token->[0] eq $_;
Branch: 1 branch
BranchTrueFalseTotal
if $token->[0] eq $_ 5 19 24
62 }
63 }
64 }
65  
66  
67 10 ▶sub _textify {
Subroutine _textify: called CC=5 SCAR=16.6
68 10 my($self, $token) = @_;
69 10 my $tag = $token->[1];
70 10 ▶ return undef unless exists $self->{textify}{$tag};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"textify"}{$tag} 9 1 10
71  
72 1 my $alt = $self->{textify}{$tag};
73 1 my $text;
74 1 ▶ if (ref($alt)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $alt) { } 0 1 1
75 0 $text = &$alt(@$token);
76 } else {
77 1 ▶ $text = $token->[2]{$alt || "alt"};
Condition50%1/2
$alt || 'alt'
l!l
10
MC/DC0%0/1
$alt || 'alt'
$alt
Truth table50%1/2
A
A= $alt
Aresult
11
00
78 1 ▶ $text = "[\U$tag]" unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 1 1
79 }
80 1 return $text;
81 }
82  
83  
84 sub get_text
85 5 ▶{
Subroutine get_text: called CC=11 SCAR=24.0
Pod documented
86 5 my $self = shift;
87 5 my @text;
88 5 ▶ while (my $token = $self->get_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $self->get_token) 19 0 19
89 19 my $type = $token->[0];
90 19 ▶ if ($type eq "T") {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'T') { } 9 10 19
elsif ($type =~ /^[SE]$/) { } 9 1 10
91 9 my $text = $token->[1];
92 9 ▶ decode_entities($text) unless $token->[2];
Branch: 1 branch
BranchTrueFalseTotal
unless $token->[2] 9 0 9
93 9 push(@text, $text);
94 } elsif ($type =~ /^[SE]$/) {
95 9 my $tag = $token->[1];
96 9 ▶ if ($type eq "S") {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'S') { } 5 4 9
97 5 ▶ if (defined(my $text = _textify($self, $token))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $text = _textify($self, $token))) 1 4 5
98 1 push(@text, $text);
99 1 next;
100 }
101 } else {
102 4 $tag = "/$tag";
103 }
104 8 ▶ if (!@_ || grep $_ eq $tag, @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @_ or grep(($_ eq $tag), @_)) 5 3 8
Condition100%3/3
not @_ or grep(($_ eq $tag), @_)
l!l&&r!l&&!r
323
MC/DC100%2/2
not @_ or grep(($_ eq $tag), @_)
not @_ grep(($_ eq $tag), @_)
Truth table100%3/3
not(A) or B
A= not @_B= grep(($_ eq $tag), @_)
ABresult
1X1
011
000
105 5 $self->unget_token($token);
106 5 last;
107 }
108 push(@text, " ")
109 3 ▶ if $tag eq "br" || !$HTML::Tagset::isPhraseMarkup{$token->[1]};
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "br" or not $HTML::Tagset::isPhraseMarkup{$token->[1]} 1 2 3
Condition66%2/3
$tag eq "br" or not $HTML::Tagset::isPhraseMarkup{$token->[1]}
l!l&&r!l&&!r
012
MC/DC50%1/2
$tag eq "br" or not $HTML::Tagset::isPhraseMarkup{$token->[1]}
$tag eq "br" not $HTML::Tagset::isPhraseMarkup{$token->[1]}
Truth table66%2/3
A or not(B)
A= $tag eq "br"B= not $HTML::Tagset::isPhraseMarkup{$token->[1]}
ABresult
1X1
011
000
110 }
111 }
112 5 join("", @text);
113 }
114  
115  
116 sub get_trimmed_text
117 4 ▶{
Subroutine get_trimmed_text: called CC=1 SCAR=0.0
Pod documented
118 4 my $self = shift;
119 4 my $text = $self->get_text(@_);
120 4 $text =~ s/^\s+//; $text =~ s/\s+$//; $text =~ s/\s+/ /g;
121 4 $text;
122 }
123  
124 3 ▶sub get_phrase {
Subroutine get_phrase: called CC=9 SCAR=22.3
Pod documented
125 3 my $self = shift;
126 3 my @text;
127 3 ▶ while (my $token = $self->get_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $self->get_token) 19 0 19
128 19 my $type = $token->[0];
129 19 ▶ if ($type eq "T") {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'T') { } 9 10 19
elsif ($type =~ /^[SE]$/) { } 10 0 10
130 9 my $text = $token->[1];
131 9 ▶ decode_entities($text) unless $token->[2];
Branch: 1 branch
BranchTrueFalseTotal
unless $token->[2] 9 0 9
132 9 push(@text, $text);
133 } elsif ($type =~ /^[SE]$/) {
134 10 my $tag = $token->[1];
135 10 ▶ if ($type eq "S") {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "S") 5 5 10
136 5 ▶ if (defined(my $text = _textify($self, $token))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $text = _textify($self, $token))) 0 5 5
137 0 push(@text, $text);
138 0 next;
139 }
140 }
141 10 ▶ if (!$HTML::Tagset::isPhraseMarkup{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($HTML::Tagset::isPhraseMarkup{$tag}) 3 7 10
142 3 $self->unget_token($token);
143 3 last;
144 }
145 7 ▶ push(@text, " ") if $tag eq "br";
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "br" 1 6 7
146 }
147 }
148 3 my $text = join("", @text);
149 3 $text =~ s/^\s+//; $text =~ s/\s+$//; $text =~ s/\s+/ /g;
150 3 $text;
151 }
152  
153 1 1;
154  
155  
156 __END__
« blib/lib/HTML/PullParser.pm ↑ Summary blib/lib/HTML/Parser.pm »