| 1 | package HTML::Inject; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | BEGIN { | ||||||||||||||||||||||||||||
| 9 | 2 | ▶ | $HTML::Inject::AUTHORITY = 'cpan:TOBYINK'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | $HTML::Inject::VERSION = '0.003'; | |||||||||||||||||||||||||||
| 11 | } | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | use constant { | ||||||||||||||||||||||||||||
| 14 | 2 | true => !!1, | |||||||||||||||||||||||||||
| 15 | false => !!0, | ||||||||||||||||||||||||||||
| 16 | read_only => 'ro', | ||||||||||||||||||||||||||||
| 17 | read_write => 'rw', | ||||||||||||||||||||||||||||
| 18 | 2 | ▶ | }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use HTML::HTML5::Parser; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use IO::Detect qw( is_filehandle is_filename ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use Scalar::Does 0.002 qw( blessed reftype -constants ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 2 | ▶ | use XML::LibXML 1.94; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | sub must_do { | ||||||||||||||||||||||||||||
| 27 | my $role = shift; | ||||||||||||||||||||||||||||
| 28 | return sub { does($_[0], $role) or confess "Does not do role $_[0]" }; | ||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 2 | use namespace::sweep -also => [qw( | |||||||||||||||||||||||||||
| 33 | must_do | ||||||||||||||||||||||||||||
| 34 | true false read_only read_write | ||||||||||||||||||||||||||||
| 35 | 2 | ▶ | )]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 2 | has target => ( | |||||||||||||||||||||||||||
| 38 | is => read_only, | ||||||||||||||||||||||||||||
| 39 | isa => must_do( 'XML::LibXML::Document' ), | ||||||||||||||||||||||||||||
| 40 | coerce => \&_coerce_dom, | ||||||||||||||||||||||||||||
| 41 | required => true, | ||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | has missing_nodes => ( | ||||||||||||||||||||||||||||
| 45 | is => read_write, | ||||||||||||||||||||||||||||
| 46 | isa => must_do( ARRAY ), | ||||||||||||||||||||||||||||
| 47 | 1 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | 2 | ); | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | has head_element_test => ( | ||||||||||||||||||||||||||||
| 51 | is => read_only, | ||||||||||||||||||||||||||||
| 52 | isa => must_do( CODE ), | ||||||||||||||||||||||||||||
| 53 | 2 | ▶ | default => sub { sub { no warnings; $_[0]->nodeName ~~ [qw(title link meta style)] } }, | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | 2 | ); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | has body_element_test => ( | ||||||||||||||||||||||||||||
| 57 | is => read_only, | ||||||||||||||||||||||||||||
| 58 | isa => must_do( CODE ), | ||||||||||||||||||||||||||||
| 59 | 2 | ▶ | default => sub { sub { no warnings; $_[0]->nodeName ~~ [qw(script map)] } }, | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.1
Subroutine
__ANON__: called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||
| 60 | 2 | ); | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | sub inject | ||||||||||||||||||||||||||||
| 63 | { | ||||||||||||||||||||||||||||
| 64 | 1 | ▶ | my ($self, $content) = @_; | ||||||||||||||||||||||||||
|
Subroutine
inject: called CC=6 SCAR=19.3
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 1 | my %content = $self->_find_content($content); | |||||||||||||||||||||||||||
| 66 | 1 | my $dom = $self->target->cloneNode(true); | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 1 | @{ $self->missing_nodes } = (); | |||||||||||||||||||||||||||
| 69 | 1 | ▶ | while (my ($id, $el) = each %content) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | { | ||||||||||||||||||||||||||||
| 71 | 2 | ▶ | if ($id =~ /^-inject/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | { | ||||||||||||||||||||||||||||
| 73 | 1 | ▶ | if ($self->head_element_test->($el)) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 74 | 1 | { $dom->getElementsByTagName('head')->get_node(1)->appendChild($el) } | |||||||||||||||||||||||||||
| 75 | elsif ($self->body_element_test->($el)) | ||||||||||||||||||||||||||||
| 76 | 0 | { $dom->getElementsByTagName('body')->get_node(1)->appendChild($el) } | |||||||||||||||||||||||||||
| 77 | else | ||||||||||||||||||||||||||||
| 78 | 0 | { push @{ $self->missing_nodes }, $el } | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | else | ||||||||||||||||||||||||||||
| 81 | { | ||||||||||||||||||||||||||||
| 82 | 1 | my $target = $dom->findnodes(qq{//*[\@id="$id"]}); | |||||||||||||||||||||||||||
| 83 | 1 | ▶ | if ($target->size) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | { | ||||||||||||||||||||||||||||
| 85 | $target->foreach(sub { | ||||||||||||||||||||||||||||
| 86 | 1 | ▶ | my $t = $_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 87 | 1 | $t->{$_} = $el->{$_} for keys %$el; | |||||||||||||||||||||||||||
| 88 | 1 | $t->appendChild($_->cloneNode(1)) for $el->childNodes; | |||||||||||||||||||||||||||
| 89 | 1 | }); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | else | ||||||||||||||||||||||||||||
| 92 | { | ||||||||||||||||||||||||||||
| 93 | 0 | push @{ $self->missing_nodes }, $el; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 1 | return $dom; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | sub inject_and_new | ||||||||||||||||||||||||||||
| 102 | { | ||||||||||||||||||||||||||||
| 103 | 0 | ▶ | my ($self, $content) = @_; | ||||||||||||||||||||||||||
|
Subroutine
inject_and_new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | 0 | my $class = ref $self; | |||||||||||||||||||||||||||
| 105 | 0 | $class->new( target => $self->inject($content) ); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | sub _find_content | ||||||||||||||||||||||||||||
| 109 | { | ||||||||||||||||||||||||||||
| 110 | 1 | ▶ | my ($self, $content) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_find_content: called CC=5 SCAR=19.7
| |||||||||||||||||||||||||||||
| 111 | 1 | my %rv; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 1 | ▶ | if (does($content, ARRAY)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | { | ||||||||||||||||||||||||||||
| 115 | 0 | for my $c (@$content) | |||||||||||||||||||||||||||
| 116 | { | ||||||||||||||||||||||||||||
| 117 | 0 | my %tmp = $self->_find_content($c); | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | $rv{$_} //= $tmp{$_} for keys %tmp; | ||||||||||||||||||||||||||
|
Condition0%0/3
$rv{$_} //= $tmp{$_}
MC/DC0%0/2
$rv{$_} //= $tmp{$_}
$rv{$_}
$tmp{$_}
Truth table0%0/3
A //= B
A= $rv{$_}B= $tmp{$_}
| |||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 0 | return %rv; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 1 | my $i; | |||||||||||||||||||||||||||
| 124 | _coerce_dom($content) | ||||||||||||||||||||||||||||
| 125 | -> findnodes('/*/*/*') | ||||||||||||||||||||||||||||
| 126 | -> foreach(sub { | ||||||||||||||||||||||||||||
| 127 | 2 | ▶ | $rv{ $_->{id} ? $_->{id} : sprintf('-inject_%d', ++$i) } = $_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 1 | }); | |||||||||||||||||||||||||||
| 129 | 1 | return %rv; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | sub _coerce_dom | ||||||||||||||||||||||||||||
| 133 | { | ||||||||||||||||||||||||||||
| 134 | 2 | ▶ | my ($it) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_coerce_dom: called CC=8 SCAR=24.7
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 2 | ▶ | return $it | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | if does($it, 'XML::LibXML::Document'); | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 2 | ▶ | return HTML::HTML5::Parser::->load_html(IO => $it) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | if is_filehandle $it; | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 2 | ▶ | return HTML::HTML5::Parser::->load_html(location => $it) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | if is_filename $it; | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 2 | ▶ | return HTML::HTML5::Parser::->load_html(location => "$it") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($it) and $it->isa("URI")
MC/DC0%0/2
builtin::blessed($it) and $it->isa("URI")
builtin::blessed($it)
$it->isa("URI")
Truth table33%1/3
A and B
A= builtin::blessed($it)B= $it->isa("URI")
| |||||||||||||||||||||||||||||
| 146 | if blessed $it && $it->isa('URI'); | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 2 | ▶ | return HTML::HTML5::Parser::->load_html(location => "$it") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not builtin::blessed($it) and $it =~ /^(https?|file):\S+$/i
MC/DC0%0/2
not builtin::blessed($it) and $it =~ /^(https?|file):\S+$/i
not builtin::blessed($it)
$it =~ /^(https?|file):\S+$/i
Truth table33%1/3
not(A) and B
A= not builtin::blessed($it)B= $it =~ /^(https?|file):\S+$/i
| |||||||||||||||||||||||||||||
| 149 | if !blessed $it && $it =~ /^(https?|file):\S+$/i; | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 2 | return HTML::HTML5::Parser::->load_html(string => $it) | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 2 | 1; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | __END__ | ||||||||||||||||||||||||||||