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