| 1 | package HTML::Zoom::Parser::BuiltIn; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use strictures 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use base qw(HTML::Zoom::SubObject); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 89 | ▶ | sub html_to_events { | ||||||||||||||||||||||||||
|
Subroutine
html_to_events: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 7 | 89 | my ($self, $text) = @_; | |||||||||||||||||||||||||||
| 8 | 89 | my @events; | |||||||||||||||||||||||||||
| 9 | 89 | ▶ | _hacky_tag_parser($text => sub { push @events, $_[0] }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 89 | return \@events; | |||||||||||||||||||||||||||
| 11 | } | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 29 | ▶ | sub html_to_stream { | ||||||||||||||||||||||||||
|
Subroutine
html_to_stream: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 14 | 29 | my ($self, $text) = @_; | |||||||||||||||||||||||||||
| 15 | return $self->_zconfig->stream_utils | ||||||||||||||||||||||||||||
| 16 | 29 | ->stream_from_array(@{$self->html_to_events($text)}); | |||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # DO NOT BE AFRAID. | ||||||||||||||||||||||||||||
| 20 | # | ||||||||||||||||||||||||||||
| 21 | # Well, ok. Be afraid. A little. But this is lexing HTML with a regexp, | ||||||||||||||||||||||||||||
| 22 | # not really parsing (since the structure nesting isn't handled here) so | ||||||||||||||||||||||||||||
| 23 | # it's relatively not dangerous. | ||||||||||||||||||||||||||||
| 24 | # | ||||||||||||||||||||||||||||
| 25 | # Certainly it's not really any more or any less heinous than anything else | ||||||||||||||||||||||||||||
| 26 | # I could do in a handful of lines of pure perl. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 89 | ▶ | sub _hacky_tag_parser { | ||||||||||||||||||||||||||
|
Subroutine
_hacky_tag_parser: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||
| 29 | 89 | my ($text, $handler) = @_; | |||||||||||||||||||||||||||
| 30 | 89 | $text =~ m{^([^<]*)}g; | |||||||||||||||||||||||||||
| 31 | 89 | ▶ | if ( length $1 ) { # leading PCDATA | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | $handler->({ type => 'TEXT', raw => $1 }); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | 89 | ▶ | while ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | $text =~ m{ | ||||||||||||||||||||||||||||
| 36 | ( | ||||||||||||||||||||||||||||
| 37 | (?:[^<]*) < (?: | ||||||||||||||||||||||||||||
| 38 | ( / )? ( [^/!<>\s"'=]+ ) | ||||||||||||||||||||||||||||
| 39 | ( (?:"[^"]*"|'[^']*'|[^/"'<>])+? )? | ||||||||||||||||||||||||||||
| 40 | | | ||||||||||||||||||||||||||||
| 41 | (!-- .*? -- | ![^\-] .*? ) | ||||||||||||||||||||||||||||
| 42 | ) (\s*/\s*)? > | ||||||||||||||||||||||||||||
| 43 | ) | ||||||||||||||||||||||||||||
| 44 | ([^<]*) | ||||||||||||||||||||||||||||
| 45 | }sxg | ||||||||||||||||||||||||||||
| 46 | ) { | ||||||||||||||||||||||||||||
| 47 | 673 | my ($whole, $is_close, $tag_name, $attributes, $is_special, | |||||||||||||||||||||||||||
| 48 | $in_place_close, $content) | ||||||||||||||||||||||||||||
| 49 | = ($1, $2, $3, $4, $5, $6, $7, $8); | ||||||||||||||||||||||||||||
| 50 | 673 | ▶ | if ($is_special) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 1 | $handler->({ type => 'SPECIAL', raw => $whole }); | |||||||||||||||||||||||||||
| 52 | } else { | ||||||||||||||||||||||||||||
| 53 | 672 | $tag_name =~ tr/A-Z/a-z/; | |||||||||||||||||||||||||||
| 54 | 672 | ▶ | if ($is_close) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 317 | $handler->({ type => 'CLOSE', name => $tag_name, raw => $whole }); | |||||||||||||||||||||||||||
| 56 | } else { | ||||||||||||||||||||||||||||
| 57 | 355 | ▶ | $attributes = '' if !defined($attributes) or $attributes =~ /^ +$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $attributes or $attributes =~ /^ +$/
MC/DC100%2/2
not defined $attributes or $attributes =~ /^ +$/
not defined $attributes
$attributes =~ /^ +$/
Truth table100%3/3
not(A) or B
A= not defined $attributesB= $attributes =~ /^ +$/
| |||||||||||||||||||||||||||||
| 58 | 355 | ▶ | $handler->({ | ||||||||||||||||||||||||||
|
Condition100%2/2
$attributes || ''
MC/DC100%1/1
$attributes || ''
$attributes
Truth table100%2/2
A
A= $attributes
| |||||||||||||||||||||||||||||
| 59 | type => 'OPEN', | ||||||||||||||||||||||||||||
| 60 | name => $tag_name, | ||||||||||||||||||||||||||||
| 61 | is_in_place_close => $in_place_close, | ||||||||||||||||||||||||||||
| 62 | _hacky_attribute_parser($attributes), | ||||||||||||||||||||||||||||
| 63 | raw_attrs => $attributes||'', | ||||||||||||||||||||||||||||
| 64 | raw => $whole, | ||||||||||||||||||||||||||||
| 65 | }); | ||||||||||||||||||||||||||||
| 66 | 355 | ▶ | if ($in_place_close) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 38 | $handler->({ | |||||||||||||||||||||||||||
| 68 | type => 'CLOSE', name => $tag_name, raw => '', | ||||||||||||||||||||||||||||
| 69 | is_in_place_close => 1 | ||||||||||||||||||||||||||||
| 70 | }); | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 673 | ▶ | if (length $content) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 497 | $handler->({ type => 'TEXT', raw => $content }); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 355 | ▶ | sub _hacky_attribute_parser { | ||||||||||||||||||||||||||
|
Subroutine
_hacky_attribute_parser: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||
| 81 | 355 | my ($attr_text) = @_; | |||||||||||||||||||||||||||
| 82 | 355 | my (%attrs, @attr_names); | |||||||||||||||||||||||||||
| 83 | 355 | ▶ | while ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | $attr_text =~ m{ | ||||||||||||||||||||||||||||
| 85 | ([^\s\=\"\']+)(\s*=\s*(?:(")(.*?)"|(')(.*?)'|([^'"\s=]+)['"]*))? | ||||||||||||||||||||||||||||
| 86 | }sgx | ||||||||||||||||||||||||||||
| 87 | ) { | ||||||||||||||||||||||||||||
| 88 | 251 | my $key = $1; | |||||||||||||||||||||||||||
| 89 | 251 | my $test = $2; | |||||||||||||||||||||||||||
| 90 | 251 | ▶ | my $val = ( $3 ? $4 : ( $5 ? $6 : $7 )); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 91 | 251 | my $lckey = lc($key); | |||||||||||||||||||||||||||
| 92 | 251 | ▶ | if ($test) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 251 | $attrs{$lckey} = _simple_unescape($val); | |||||||||||||||||||||||||||
| 94 | } else { | ||||||||||||||||||||||||||||
| 95 | 0 | $attrs{$lckey} = $lckey; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 251 | push(@attr_names, $lckey); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 355 | (attrs => \%attrs, attr_names => \@attr_names); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 251 | ▶ | sub _simple_unescape { | ||||||||||||||||||||||||||
|
Subroutine
_simple_unescape: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 103 | 251 | my $str = shift; | |||||||||||||||||||||||||||
| 104 | 251 | $str =~ s/"/"/g; | |||||||||||||||||||||||||||
| 105 | 251 | $str =~ s/</</g; | |||||||||||||||||||||||||||
| 106 | 251 | $str =~ s/>/>/g; | |||||||||||||||||||||||||||
| 107 | 251 | $str =~ s/&/&/g; | |||||||||||||||||||||||||||
| 108 | 251 | $str; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 130 | ▶ | sub _simple_escape { | ||||||||||||||||||||||||||
|
Subroutine
_simple_escape: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 112 | 130 | my $str = shift; | |||||||||||||||||||||||||||
| 113 | 130 | $str =~ s/&/&/g; | |||||||||||||||||||||||||||
| 114 | 130 | $str =~ s/"/"/g; | |||||||||||||||||||||||||||
| 115 | 130 | $str =~ s/</</g; | |||||||||||||||||||||||||||
| 116 | 130 | $str =~ s/>/>/g; | |||||||||||||||||||||||||||
| 117 | 130 | $str; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 130 | ▶ | sub html_escape { _simple_escape($_[1]) } | ||||||||||||||||||||||||||
|
Subroutine
html_escape: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | sub html_unescape { _simple_unescape($_[1]) } | ||||||||||||||||||||||||||
|
Subroutine
html_unescape: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 13 | 1; | |||||||||||||||||||||||||||