| 1 | package HTML::Microdata; | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use HTML::TreeBuilder::LibXML; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Hash::MultiValue; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Scalar::Util qw(refaddr); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use JSON; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use URI; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||
| 12 | 2 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||
| 14 | sub new { | ||||||||||||||||||||||||||||||||||
| 15 | 0 | ▶ | my ($class, %args) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 16 | bless { | ||||||||||||||||||||||||||||||||||
| 17 | items => [], | ||||||||||||||||||||||||||||||||||
| 18 | 0 | ▶ | base => $args{base} ? URI->new($args{base}) : undef, | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 19 | }, $class; | ||||||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | sub extract { | ||||||||||||||||||||||||||||||||||
| 23 | 0 | ▶ | my ($class, $content, %opts) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
extract: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 24 | 0 | my $self = $class->new(%opts); | |||||||||||||||||||||||||||||||||
| 25 | 0 | $self->_parse($content); | |||||||||||||||||||||||||||||||||
| 26 | 0 | $self | |||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||
| 29 | sub as_json { | ||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
as_json: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 31 | encode_json +{ | ||||||||||||||||||||||||||||||||||
| 32 | items => $self->{items}, | ||||||||||||||||||||||||||||||||||
| 33 | 0 | }; | |||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||
| 36 | sub items { | ||||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
items: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 38 | 0 | $self->{items}; | |||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||
| 41 | sub _parse { | ||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | my ($self, $content) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_parse: not called CC=13 SCAR=52.0
| |||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||
| 44 | 0 | my $items = {}; | |||||||||||||||||||||||||||||||||
| 45 | 0 | my $tree = HTML::TreeBuilder::LibXML->new_from_content($content); | |||||||||||||||||||||||||||||||||
| 46 | 0 | my $scopes = $tree->findnodes('//*[@itemscope]'); | |||||||||||||||||||||||||||||||||
| 47 | 0 | my $number = 0; | |||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||
| 49 | 0 | for my $scope (@$scopes) { | |||||||||||||||||||||||||||||||||
| 50 | 0 | my $type = $scope->attr('itemtype'); | |||||||||||||||||||||||||||||||||
| 51 | 0 | my $id = $scope->attr('itemid'); | |||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | unless ($scope->id) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 54 | 0 | $scope->id($number++); | |||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | my $item = { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 58 | ($id ? (id => $id) : ()), | ||||||||||||||||||||||||||||||||||
| 59 | ($type ? (type => $type) : ()), | ||||||||||||||||||||||||||||||||||
| 60 | properties => Hash::MultiValue->new, | ||||||||||||||||||||||||||||||||||
| 61 | }; | ||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||
| 63 | 0 | $items->{ $scope->id } = $item; | |||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | unless ($scope->attr('itemprop')) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 66 | # This is top level item | ||||||||||||||||||||||||||||||||||
| 67 | 0 | push @{ $self->{items} }, $item; | |||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||
| 71 | 0 | for my $scope (@$scopes) { | |||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if (my $refs = $scope->attr('itemref')) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 73 | 0 | my $ids = [ split /\s+/, $refs ]; | |||||||||||||||||||||||||||||||||
| 74 | 0 | for my $id (@$ids) { | |||||||||||||||||||||||||||||||||
| 75 | 0 | my $props = $tree->findnodes('//*[@id="' . $id . '"]/descendant-or-self::*[@itemprop]'); | |||||||||||||||||||||||||||||||||
| 76 | 0 | for my $prop (@$props) { | |||||||||||||||||||||||||||||||||
| 77 | 0 | my $name = $prop->attr('itemprop'); | |||||||||||||||||||||||||||||||||
| 78 | 0 | my $value = $self->extract_value($prop, items => $items); | |||||||||||||||||||||||||||||||||
| 79 | 0 | $items->{ $scope->id }->{properties}->add($name => $value); | |||||||||||||||||||||||||||||||||
| 80 | 0 | $prop->delete; | |||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||
| 86 | 0 | my $props = $tree->findnodes('//*[@itemscope]/descendant-or-self::*[@itemprop]'); | |||||||||||||||||||||||||||||||||
| 87 | 0 | for my $prop (@$props) { | |||||||||||||||||||||||||||||||||
| 88 | 0 | my $value = $self->extract_value($prop, items => $items); | |||||||||||||||||||||||||||||||||
| 89 | 0 | my $scope = $prop->findnodes('./ancestor::*[@itemscope]')->[-1]; | |||||||||||||||||||||||||||||||||
| 90 | 0 | for my $name (split /\s+/, $prop->attr('itemprop')) { | |||||||||||||||||||||||||||||||||
| 91 | 0 | $items->{ $scope->id }->{properties}->add($name => $value); | |||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||
| 95 | 0 | for my $key (keys %$items) { | |||||||||||||||||||||||||||||||||
| 96 | 0 | my $item = $items->{$key}; | |||||||||||||||||||||||||||||||||
| 97 | 0 | $item->{properties} = $item->{properties}->multi; | |||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||
| 102 | sub absolute { | ||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | my ($self, $uri) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
absolute: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | if (defined $uri) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | if ($self->{base}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 106 | 0 | URI->new_abs($uri, $self->{base}).q(); | |||||||||||||||||||||||||||||||||
| 107 | } else { | ||||||||||||||||||||||||||||||||||
| 108 | 0 | $uri; | |||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||
| 110 | } else { | ||||||||||||||||||||||||||||||||||
| 111 | 0 | ""; | |||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||
| 115 | sub extract_value { | ||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | my ($self, $prop, %opts) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
extract_value: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||
| 118 | 0 | my $value; | |||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | if (defined $prop->attr('itemscope')) { | ||||||||||||||||||||||||||||||||
|
Branch: 7 branches
| |||||||||||||||||||||||||||||||||||
| 120 | # XXX : inifinite loop | ||||||||||||||||||||||||||||||||||
| 121 | 0 | $value = $opts{items}->{ $prop->id }; | |||||||||||||||||||||||||||||||||
| 122 | } elsif ($prop->tag eq 'meta') { | ||||||||||||||||||||||||||||||||||
| 123 | 0 | $value = $prop->attr('content'); | |||||||||||||||||||||||||||||||||
| 124 | } elsif ($prop->tag =~ m{^audio|embed|iframe|img|source|video|track$}) { | ||||||||||||||||||||||||||||||||||
| 125 | 0 | $value = $self->absolute($prop->attr('src')); | |||||||||||||||||||||||||||||||||
| 126 | } elsif ($prop->tag =~ m{^a|area|link$}) { | ||||||||||||||||||||||||||||||||||
| 127 | 0 | $value = $self->absolute($prop->attr('href')); | |||||||||||||||||||||||||||||||||
| 128 | } elsif ($prop->tag eq 'object') { | ||||||||||||||||||||||||||||||||||
| 129 | 0 | $value = $self->absolute($prop->attr('data')); | |||||||||||||||||||||||||||||||||
| 130 | } elsif ($prop->tag eq 'data') { | ||||||||||||||||||||||||||||||||||
| 131 | 0 | $value = $prop->attr('value'); | |||||||||||||||||||||||||||||||||
| 132 | ▶ | } elsif ($prop->tag eq 'time' && $prop->attr('datetime')) { | |||||||||||||||||||||||||||||||||
|
Condition0%0/3
$prop->tag eq "time" and $prop->attr("datetime")
MC/DC0%0/2
$prop->tag eq "time" and $prop->attr("datetime")
$prop->tag eq "time"
$prop->attr("datetime")
Truth table0%0/3
A and B
A= $prop->tag eq "time"B= $prop->attr("datetime")
| |||||||||||||||||||||||||||||||||||
| 133 | 0 | $value = $prop->attr('datetime'); | |||||||||||||||||||||||||||||||||
| 134 | } else { | ||||||||||||||||||||||||||||||||||
| 135 | 0 | $value = $prop->findvalue('normalize-space(.)'); | |||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||
| 138 | 0 | $value; | |||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||
| 141 | 2 | 1; | |||||||||||||||||||||||||||||||||
| 142 | __END__ | ||||||||||||||||||||||||||||||||||