| 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
| |||||||||||||||||||||||||||||
| 33 | 10 | ▶ | my $type = (ref($_[0]) eq "SCALAR") ? "doc" : "file"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 10 | %cnf = ($type => $_[0]); | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | else { | ||||||||||||||||||||||||||||
| 37 | 0 | ▶ | unshift @_, (ref($_[0]) eq "SCALAR") ? "doc" : "file" if(scalar(@_) % 2 == 1); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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'}
MC/DC0%0/1
delete $cnf{'textify'} || {'img', 'alt', 'applet', 'alt'}
delete $cnf{'textify'}
Truth table50%1/2
A
A= delete $cnf{'textify'}
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 10 | ▶ | my $self = $class->SUPER::new(%ARGS, %cnf) || return undef; | ||||||||||||||||||||||||||
|
Condition100%2/2
$class->SUPER::new(%ARGS, %cnf) || (return undef)
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)
| |||||||||||||||||||||||||||||
| 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)
MC/DC100%1/1
$self->get_token || (return undef)
$self->get_token
Truth table100%2/2
A
A= $self->get_token
| |||||||||||||||||||||||||||||
| 56 | 115 | my $type = shift @$token; | |||||||||||||||||||||||||||
| 57 | 115 | ▶ | next unless $type eq "S" || $type eq "E"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$type eq "S" or $type eq "E"
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"
| |||||||||||||||||||||||||||||
| 58 | 57 | ▶ | substr($token->[0], 0, 0) = "/" if $type eq "E"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 57 | ▶ | return $token unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 21 | for (@_) { | |||||||||||||||||||||||||||
| 61 | 24 | ▶ | return $token if $token->[0] eq $_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 1 | my $alt = $self->{textify}{$tag}; | |||||||||||||||||||||||||||
| 73 | 1 | my $text; | |||||||||||||||||||||||||||
| 74 | 1 | ▶ | if (ref($alt)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | $text = &$alt(@$token); | |||||||||||||||||||||||||||
| 76 | } else { | ||||||||||||||||||||||||||||
| 77 | 1 | ▶ | $text = $token->[2]{$alt || "alt"}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$alt || 'alt'
MC/DC0%0/1
$alt || 'alt'
$alt
Truth table50%1/2
A
A= $alt
| |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | $text = "[\U$tag]" unless defined $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 89 | 19 | my $type = $token->[0]; | |||||||||||||||||||||||||||
| 90 | 19 | ▶ | if ($type eq "T") { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 91 | 9 | my $text = $token->[1]; | |||||||||||||||||||||||||||
| 92 | 9 | ▶ | decode_entities($text) unless $token->[2]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 9 | push(@text, $text); | |||||||||||||||||||||||||||
| 94 | } elsif ($type =~ /^[SE]$/) { | ||||||||||||||||||||||||||||
| 95 | 9 | my $tag = $token->[1]; | |||||||||||||||||||||||||||
| 96 | 9 | ▶ | if ($type eq "S") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 5 | ▶ | if (defined(my $text = _textify($self, $token))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
not @_ or grep(($_ eq $tag), @_)
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), @_)
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$tag eq "br" or not $HTML::Tagset::isPhraseMarkup{$token->[1]}
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]}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 128 | 19 | my $type = $token->[0]; | |||||||||||||||||||||||||||
| 129 | 19 | ▶ | if ($type eq "T") { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 130 | 9 | my $text = $token->[1]; | |||||||||||||||||||||||||||
| 131 | 9 | ▶ | decode_entities($text) unless $token->[2]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 9 | push(@text, $text); | |||||||||||||||||||||||||||
| 133 | } elsif ($type =~ /^[SE]$/) { | ||||||||||||||||||||||||||||
| 134 | 10 | my $tag = $token->[1]; | |||||||||||||||||||||||||||
| 135 | 10 | ▶ | if ($type eq "S") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 5 | ▶ | if (defined(my $text = _textify($self, $token))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 0 | push(@text, $text); | |||||||||||||||||||||||||||
| 138 | 0 | next; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | 10 | ▶ | if (!$HTML::Tagset::isPhraseMarkup{$tag}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 3 | $self->unget_token($token); | |||||||||||||||||||||||||||
| 143 | 3 | last; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | 7 | ▶ | push(@text, " ") if $tag eq "br"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||