blib/lib/Web/Microformats2/Parser.pm

filter: Statementstmt 49.2% 171 / 347 Branchbran 29.2% 55 / 188 Conditioncond 18.7% 9 / 48 MC/DCmcdc 6.9% 2 / 29 Subroutinesub 90.3% 28 / 31 Podpod 50.0% 1 / 2 totaltotal 41.2% 266 / 645 CC 130 SCAR 51.7
CC
130
Cov
40%
analyze_element
378.1
_set_implied_name
342.0
_format_datetime
240.0
CRAP 175.3
↑ Summary blib/lib/Web/Microformats2/Document.pm »
1 package Web::Microformats2::Parser;
2  
3 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Types::Standard qw(InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use HTML::TreeBuilder::XPath;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use DateTime::Format::ISO8601;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Web::Microformats2::Item;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Web::Microformats2::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 has 'url_context' => (
20 is => 'rw',
21 isa => InstanceOf['URI'],
22 1 ▶ coerce => sub { URI->new( $_[0] ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 lazy => 1,
24 clearer => '_clear_url_context',
25 1 ▶ default => sub { URI->new( 'http://example.com/' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 2 );
27  
28 sub parse {
29 2 ▶ my $self = shift;
Subroutine parse: called CC=4 SCAR=14.1
Pod documented
30 2 my ( $html, %args ) = @_;
31  
32 2 $self->_clear;
33 2 ▶ if ( $args{ url_context } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"url_context"}) 0 2 2
34 0 $self->url_context( $args{url_context} );
35 }
36  
37 2 my $tree = HTML::TreeBuilder::XPath->new;
38 2 $tree->ignore_unknown( 0 );
39 2 $tree->no_space_compacting( 1 );
40 2 $tree->ignore_ignorable_whitespace( 0 );
41 2 $tree->no_expand_entities( 1 );
42  
43 # Adding HTML5 elements because it's 2018.
44 2 foreach (qw(article aside details figcaption figure footer header main mark nav section summary time)) {
45 26 $HTML::TreeBuilder::isBodyElement{$_}=1;
46 }
47  
48 2 $tree->parse( $html );
49  
50 2 ▶ if ( my $base_url = $tree->findvalue( './/base/@href' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $base_url = $tree->findvalue(".//base/\@href")) 0 2 2
51 0 $self->url_context( $base_url );
52 }
53  
54 2 my $document = Web::Microformats2::Document->new;
55 2 $self->analyze_element( $document, $tree );
56 1 return $document;
57 }
58  
59 # analyze_element: Recursive method that scans an element for new microformat
60 # definitions (h-*) or properties (u|dt|e|p-*) and then does the right thing.
61 # It also builds up the MF2 document's rels and rel-urls as it goes.
62 sub analyze_element {
63 32 ▶ my $self = shift;
Subroutine analyze_element: called CC=45 SCAR=59.4
Pod undocumented
64 32 my ( $document, $element, $current_item ) = @_;
65  
66 32 ▶ return unless blessed( $element) && $element->isa( 'HTML::Element' );
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($element) and $element->isa("HTML::Element") 16 16 32
Condition66%2/3
builtin::blessed($element) and $element->isa("HTML::Element")
!ll&&!rl&&r
16016
MC/DC50%1/2
builtin::blessed($element) and $element->isa("HTML::Element")
builtin::blessed($element) $element->isa("HTML::Element")
Truth table66%2/3
A and B
A= builtin::blessed($element)B= $element->isa("HTML::Element")
ABresult
0X0
100
111
67  
68 16 $self->_add_element_rels_to_mf2_document( $element, $document );
69  
70 16 my $mf2_attrs = $self->_tease_out_mf2_attrs( $element );
71  
72 16 my $h_attrs = delete $mf2_attrs->{h};
73 16 my $new_item;
74 16 ▶ if ( $h_attrs->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h_attrs->[0]) 2 14 16
75 2 $new_item = Web::Microformats2::Item->new( {
76 types => $h_attrs,
77 parent => $current_item,
78 } );
79 2 $document->add_item( $new_item );
80 2 ▶ unless ( $current_item ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($current_item) 2 0 2
81 2 $document->add_top_level_item( $new_item );
82 }
83 }
84  
85 16 ▶ while (my ($mf2_type, $properties_ref ) = each( %$mf2_attrs ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$mf2_attrs) 62 15 77
86 62 ▶ next unless $current_item;
Branch: 1 branch
BranchTrueFalseTotal
unless $current_item 32 30 62
87 30 ▶ next unless @{ $properties_ref };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$properties_ref;} 22 8 30
88 8 ▶ if ( $mf2_type eq 'p' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($mf2_type eq 'p') { } 7 1 8
elsif ($mf2_type eq 'u') { } 1 0 1
elsif ($mf2_type eq 'e') { } 0 0 0
elsif ($mf2_type eq 'dt') { } 0 0 0
89 # p-property:
90 # A catch-all generic property to store on the current
91 # MF2 item being defined.
92 # (If this same element begins an h-* microformat, we don't parse
93 # this p-* any further; instead we'll store the new item under
94 # this property name.)
95 7 ▶ unless ( $new_item ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($new_item) 7 0 7
96 7 for my $property ( @$properties_ref ) {
97 7 my $value = $self->_parse_property_value( $element );
98 7 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 7 0 7
99 7 $current_item->add_property(
100 "p-$property",
101 $value,
102 );
103 }
104 }
105 }
106 }
107 elsif ( $mf2_type eq 'u' ) {
108 # u-property:
109 # Look for a URL in child attributes, and store it as a property.
110  
111 # (But not if a new h-format has been defined, in which case we'll
112 # just use the u-property's name to store it. Why would you do that
113 # instead of using a p-property? I don't know, but the tests demand
114 # it.)
115 1 ▶ unless ( $new_item ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($new_item) 1 0 1
116 1 for my $property ( @$properties_ref ) {
117 1 my $vcp_fragments_ref =
118 $self->_seek_value_class_pattern( $element );
119 1 ▶ if ( my $url = $self->_tease_out_url( $element ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (my $url = $self->_tease_out_url($element)) { } 0 0 0
elsif (@$vcp_fragments_ref) { } 0 0 0
elsif ($url = $self->_tease_out_unlikely_url($element)) { } 0 0 0
120 0 $current_item->add_property( "u-$property", $url );
121 }
122 elsif ( @$vcp_fragments_ref ) {
123 0 $current_item->add_property(
124 "u-$property",
125 join q{}, @$vcp_fragments_ref,
126 )
127 }
128 elsif ( $url = $self->_tease_out_unlikely_url($element)) {
129 0 $current_item->add_property( "u-$property", $url );
130 }
131 else {
132 0 $current_item->add_property(
133 "u-$property",
134 _trim( $element->as_text ),
135 );
136 }
137 }
138 }
139 }
140 elsif ( $mf2_type eq 'e' ) {
141 # e-property:
142 # Create a struct with keys "html" and "value", and then
143 # store this in a new property.
144 0 for my $property ( @$properties_ref ) {
145 0 my %e_data;
146 0 for my $content_piece ( $element->content_list ) {
147  
148 # Make sure all URLs found in certain HTML attrs are
149 # absolute.
150 0 ▶ if ( ref $content_piece ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content_piece) { } 0 0 0
151 # XXX This is probably a bit too loose about what tags
152 # these attrs can appear on.
153 0 for my $href_element ( $content_piece, $content_piece->findnodes('.//*[@href|@src]') ) {
154 0 foreach ( qw( href src ) ) {
155 0 my $url = $href_element->attr($_);
156 0 ▶ if ( $url ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url) 0 0 0
157 0 my $abs_url = URI->new_abs( $url, $self->url_context)->as_string;
158 0 $href_element->attr( $_=> $abs_url );
159 }
160 }
161 }
162 0 $e_data{html} .= $content_piece->as_HTML( '<>&', undef, {} );
163  
164 }
165 else {
166  
167 0 $e_data{html} .= $content_piece;
168 }
169 }
170 0 $e_data{ value } = _trim (decode_entities( $element->as_text) );
171  
172 # The official tests specifically trim space-glyphs per se;
173 # all other trailing whitespace stays. Shrug.
174 0 $e_data{ html } =~ s/ +$//;
175  
176 0 $current_item->add_property( "e-$property", \%e_data );
177 }
178 }
179 elsif ( $mf2_type eq 'dt' ) {
180 # dt-property:
181 # Read a child attribute as an ISO-8601 date-time string.
182 # Store it as a property in the MF2 date-time representation format.
183 0 for my $property ( @$properties_ref ) {
184 0 my $dt_string;
185 0 my $vcp_fragments_ref =
186 $self->_seek_value_class_pattern( $element );
187 0 ▶ if ( @$vcp_fragments_ref ) {
Branch: 3 branches
BranchTrueFalseTotal
if (@$vcp_fragments_ref) { } 0 0 0
elsif (my $alt = $element->findvalue('./@datetime|@title|@value')) { } 0 0 0
elsif (my $text = $element->as_trimmed_text) { } 0 0 0
188 0 $dt_string = $self->_format_datetime(join (q{T}, @$vcp_fragments_ref), $current_item);
189 }
190 elsif ( my $alt = $element->findvalue( './@datetime|@title|@value' ) ) {
191 0 $dt_string = $alt;
192 }
193 elsif ( my $text = $element->as_trimmed_text ) {
194 0 $dt_string = $text;
195 }
196 0 ▶ if ( defined $dt_string ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dt_string) 0 0 0
197 0 $current_item->add_property(
198 "dt-$property",
199 $dt_string,
200 );
201 }
202 }
203 }
204 }
205  
206 15 ▶ if ( $new_item ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_item) { } 2 13 15
207 2 for my $child_element ( $element->content_list ) {
208 9 $self->analyze_element( $document, $child_element, $new_item );
209 }
210  
211 # Now that the new item's been recursively scanned, perform
212 # some post-processing.
213 # First, add any implied properties.
214 1 for my $impliable_property (qw(name photo url)) {
215 3 ▶ unless ( $new_item->has_property( $impliable_property ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($new_item->has_property($impliable_property)) 2 1 3
216 2 my $method = "_set_implied_$impliable_property";
217 2 $self->$method( $new_item, $element );
218 }
219 }
220  
221 # Put this onto the parent item's property-list, or its children-list,
222 # depending on context.
223 1 my @item_properties;
224 1 for my $prefix (qw( u p ) ) {
225 2 push @item_properties, map { "$prefix-$_" } @{ $mf2_attrs->{$prefix} };
226 }
227 1 ▶ if ( $current_item && @item_properties ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($current_item and @item_properties) { } 0 1 1
elsif ($current_item) { } 0 1 1
Condition33%1/3
$current_item and @item_properties
!ll&&!rl&&r
100
MC/DC0%0/2
$current_item and @item_properties
$current_item @item_properties
Truth table33%1/3
A and B
A= $current_itemB= @item_properties
ABresult
0X0
100
111
228 0 for my $item_property ( @item_properties ) {
229 # We place a clone of the new item into the current item's
230 # property list, rather than the item itself. This allows for
231 # edge cases where the same item needs to go under multiple
232 # properties, but carry different 'value' attributes.
233 0 my $cloned_new_item =
234 bless { %$new_item }, ref $new_item;
235  
236 0 $current_item
237 ->add_property( "$item_property", $cloned_new_item );
238  
239 # Now add a "value" attribute to this new item, if appropriate,
240 # according to the MF2 spec.
241 0 my $value_attribute;
242 0 ▶ if ( $item_property =~ /^p-/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($item_property =~ /^p-/) { } 0 0 0
elsif ($item_property =~ /^u-/) { } 0 0 0
243 0 ▶ if ( my $name = $new_item->get_properties('name')->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = $new_item->get_properties('name')->[0]) { } 0 0 0
244 0 $value_attribute = $name;
245 }
246 else {
247 0 $value_attribute =
248 $self->_parse_property_value( $element );
249 }
250 }
251 elsif ( $item_property =~ /^u-/ ) {
252 0 $value_attribute = $new_item->get_properties('url')->[0];
253 }
254  
255 0 ▶ $cloned_new_item->value( $value_attribute ) if defined ($value_attribute);
Branch: 1 branch
BranchTrueFalseTotal
if defined $value_attribute 0 0 0
256 }
257 }
258 elsif ($current_item) {
259 0 $current_item->add_child ( $new_item );
260 }
261  
262 }
263 else {
264 13 for my $child_element ( $element->content_list ) {
265 21 $self->analyze_element( $document, $child_element, $current_item );
266 }
267 }
268 }
269  
270 sub _tease_out_mf2_attrs {
271 16 ▶ my $self = shift;
Subroutine _tease_out_mf2_attrs: called CC=4 SCAR=13.9
272 16 my ( $element ) = @_;
273  
274 16 my %mf2_attrs;
275 16 foreach ( qw( h e u dt p ) ) {
276 80 $mf2_attrs{ $_ } = [];
277 }
278  
279 16 my $class_attr = $element->attr('class');
280 16 ▶ if ( $class_attr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class_attr) 10 6 16
281 10 ▶ while ($class_attr =~ /\b(h|e|u|dt|p)-([a-z]+(\-[a-z]+)*)($|\s)/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class_attr =~ /\b(h|e|u|dt|p)-([a-z]+(\-[a-z]+)*)($|\s)/g) 11 10 21
282 11 my $mf2_type = $1;
283 11 my $mf2_attr = $2;
284  
285 11 push @{ $mf2_attrs{ $mf2_type } }, $mf2_attr;
286 }
287 }
288  
289 16 return \%mf2_attrs;
290 }
291  
292 sub _tease_out_url {
293 1 ▶ my $self = shift;
Subroutine _tease_out_url: called CC=7 SCAR=26.3
294 1 my ( $element ) = @_;
295  
296 1 my $xpath;
297 my $url;
298 1 ▶ if ( $element->tag =~ /^(a|area|link)$/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($element->tag =~ /^(a|area|link)$/) { } 1 0 1
elsif ($element->tag =~ /^(img|audio)$/) { } 0 0 0
elsif ($element->tag eq 'video') { } 0 0 0
elsif ($element->tag eq 'object') { } 0 0 0
299 1 $xpath = './@href';
300 }
301 elsif ( $element->tag =~ /^(img|audio)$/ ) {
302 0 $xpath = './@src';
303 }
304 elsif ( $element->tag eq 'video' ) {
305 0 $xpath = './@src|@poster';
306 }
307 elsif ( $element->tag eq 'object' ) {
308 0 $xpath = './@data';
309 }
310  
311 1 ▶ if ( $xpath ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xpath) 1 0 1
312 1 $url = $element->findvalue( $xpath );
313 }
314  
315 1 ▶ if ( defined $url ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $url) 1 0 1
316 1 $url = URI->new_abs( $url, $self->url_context )->as_string;
317 }
318  
319 0 return $url;
320 }
321  
322 sub _tease_out_unlikely_url {
323 0 ▶ my $self = shift;
Subroutine _tease_out_unlikely_url: not called CC=4 SCAR=30.0
324 0 my ( $element ) = @_;
325  
326 0 my $xpath;
327 my $url;
328 0 ▶ if ( $element->tag eq 'abbr' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($element->tag eq 'abbr') { } 0 0 0
elsif ($element->tag =~ /^(data|input)$/) { } 0 0 0
329 0 $xpath = './@title';
330 }
331 elsif ( $element->tag =~ /^(data|input)$/ ) {
332 0 $xpath = './@value';
333 }
334  
335 0 ▶ if ( $xpath ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xpath) 0 0 0
336 0 $url = $element->findvalue( $xpath );
337 }
338  
339 0 return $url;
340 }
341  
342 sub _set_implied_name {
343 0 ▶ my $self = shift;
Subroutine _set_implied_name: not called CC=18 SCAR=58.3
344 0 my ( $item, $element ) = @_;
345  
346 0 my $types = $item->types;
347  
348 0 ▶ return if $item->has_properties || $item->has_children;
Branch: 1 branch
BranchTrueFalseTotal
if $item->has_properties or $item->has_children 0 0 0
Condition0%0/3
$item->has_properties or $item->has_children
l!l&&r!l&&!r
000
MC/DC0%0/2
$item->has_properties or $item->has_children
$item->has_properties $item->has_children
Truth table0%0/3
A or B
A= $item->has_propertiesB= $item->has_children
ABresult
1X1
011
000
349  
350 0 my $xpath;
351 my $name;
352 0 my $kid;
353 0 my $accept_if_empty = 1; # If true, then null-string names are okay.
354 0 ▶ if ( $element->tag =~ /^(img|area)$/ ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($element->tag =~ /^(img|area)$/) { } 0 0 0
elsif ($element->tag eq 'abbr') { } 0 0 0
elsif ($kid = $self->_non_h_unique_child($element, 'img') or $kid = $self->_non_h_unique_child($element, 'area')) { } 0 0 0
elsif ($kid = $self->_non_h_unique_child($element, 'abbr')) { } 0 0 0
elsif ($kid = $self->_non_h_unique_grandchild($element, 'img') or $kid = $self->_non_h_unique_grandchild($element, 'area')) { } 0 0 0
elsif ($kid = $self->_non_h_unique_grandchild($element, 'abbr')) { } 0 0 0
355 0 $xpath = './@alt';
356 }
357 elsif ( $element->tag eq 'abbr' ) {
358 0 $xpath = './@title';
359 }
360 ▶ elsif (
Condition0%0/3
$kid = $self->_non_h_unique_child($element, "img") or $kid = $self->_non_h_unique_child($element, "area")
l!l&&r!l&&!r
000
MC/DC0%0/2
$kid = $self->_non_h_unique_child($element, "img") or $kid = $self->_non_h_unique_child($element, "area")
$kid = $self->_non_h_unique_child($element, "img") $kid = $self->_non_h_unique_child($element, "area")
Truth table0%0/3
A or B
A= $kid = $self->_non_h_unique_child($element, "img")B= $kid = $self->_non_h_unique_child($element, "area")
ABresult
1X1
011
000
361 ( $kid = $self->_non_h_unique_child( $element, 'img' ) )
362 || ( $kid = $self->_non_h_unique_child( $element, 'area' ) )
363 ) {
364 0 $xpath = './@alt';
365 0 $accept_if_empty = 0;
366 }
367 elsif ( $kid = $self->_non_h_unique_child( $element, 'abbr' ) ) {
368 0 $xpath = './@title';
369 0 $accept_if_empty = 0;
370 }
371 ▶ elsif (
Condition0%0/3
$kid = $self->_non_h_unique_grandchild($element, "img") or $kid = $self->_non_h_unique_grandchild($element, "area")
l!l&&r!l&&!r
000
MC/DC0%0/2
$kid = $self->_non_h_unique_grandchild($element, "img") or $kid = $self->_non_h_unique_grandchild($element, "area")
$kid = $self->_non_h_unique_grandchild($element, "img") $kid = $self->_non_h_unique_grandchild($element, "area")
Truth table0%0/3
A or B
A= $kid = $self->_non_h_unique_grandchild($element, "img")B= $kid = $self->_non_h_unique_grandchild($element, "area")
ABresult
1X1
011
000
372 ( $kid = $self->_non_h_unique_grandchild( $element, 'img' ) )
373 || ( $kid = $self->_non_h_unique_grandchild( $element, 'area' ) )
374 ) {
375 0 $xpath = './@alt';
376 0 $accept_if_empty = 0;
377 }
378 elsif ( $kid = $self->_non_h_unique_grandchild( $element, 'abbr' ) ) {
379 0 $xpath = './@title';
380 0 $accept_if_empty = 0;
381 }
382  
383 0 ▶ my $foo = $kid || $element;
Condition0%0/3
$kid || $element
l!l&&r!l&&!r
000
MC/DC0%0/2
$kid || $element
$kid $element
Truth table0%0/3
A || B
A= $kidB= $element
ABresult
1X1
011
000
384  
385 0 ▶ if ( $xpath ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xpath) 0 0 0
386 0 ▶ my $element_to_check = $kid || $element;
Condition0%0/3
$kid || $element
l!l&&r!l&&!r
000
MC/DC0%0/2
$kid || $element
$kid $element
Truth table0%0/3
A || B
A= $kidB= $element
ABresult
1X1
011
000
387 0 my $value = $element_to_check->findvalue( $xpath );
388 0 ▶ if ( ( $value ne q{} ) || $accept_if_empty ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value ne "" or $accept_if_empty) 0 0 0
Condition0%0/3
$value ne "" or $accept_if_empty
l!l&&r!l&&!r
000
MC/DC0%0/2
$value ne "" or $accept_if_empty
$value ne "" $accept_if_empty
Truth table0%0/3
A or B
A= $value ne ""B= $accept_if_empty
ABresult
1X1
011
000
389 0 $name = $value;
390 }
391 }
392  
393 0 ▶ unless ( defined $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 0 0
394 0 $name = _trim( $element->as_text );
395 }
396  
397 0 ▶ if ( length $name > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $name > 0) 0 0 0
398 0 $item->add_property( 'p-name', $name );
399 }
400  
401 }
402  
403 sub _set_implied_photo {
404 1 ▶ my $self = shift;
Subroutine _set_implied_photo: called CC=9 SCAR=32.2
405 1 my ( $item, $element ) = @_;
406  
407 1 my $xpath;
408 my $url;
409 1 my $kid;
410  
411 1 ▶ if ( $element->tag eq 'img' ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($element->tag eq 'img') { } 0 1 1
elsif ($element->tag eq 'object') { } 0 1 1
elsif ($kid = $self->_non_h_unique_child($element, 'img')) { } 0 1 1
elsif ($kid = $self->_non_h_unique_child($element, 'object')) { } 0 1 1
elsif ($kid = $self->_non_h_unique_grandchild($element, 'img')) { } 0 1 1
elsif ($kid = $self->_non_h_unique_grandchild($element, 'object')) { } 0 1 1
412 0 $xpath = './@src';
413 }
414 elsif ( $element->tag eq 'object' ) {
415 0 $xpath = './@data';
416 }
417 elsif ( $kid = $self->_non_h_unique_child( $element, 'img' ) ) {
418 0 $xpath = './@src';
419 0 $element = $kid;
420 }
421 elsif ( $kid = $self->_non_h_unique_child( $element, 'object' ) ) {
422 0 $xpath = './@data';
423 0 $element = $kid;
424 }
425 elsif ( $kid = $self->_non_h_unique_grandchild( $element, 'img' ) ) {
426 0 $xpath = './@src';
427 0 $element = $kid;
428 }
429 elsif ( $kid = $self->_non_h_unique_grandchild( $element, 'object' ) ) {
430 0 $xpath = './@data';
431 0 $element = $kid;
432 }
433  
434 1 ▶ if ( $xpath ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xpath) 0 1 1
435 0 $url = $element->findvalue( $xpath );
436 }
437  
438 1 ▶ if ( defined $url ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $url) 0 1 1
439 0 $url = URI->new_abs( $url, $self->url_context )->as_string;
440 0 $item->add_property( 'u-photo', $url );
441 }
442  
443 }
444  
445 sub _set_implied_url {
446 1 ▶ my $self = shift;
Subroutine _set_implied_url: called CC=8 SCAR=28.7
447 1 my ( $item, $element ) = @_;
448  
449 1 my $xpath;
450 my $url;
451  
452 1 my $kid;
453 1 ▶ if ( $element->tag =~ /^(a|area)$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($element->tag =~ /^(a|area)$/) { } 0 1 1
elsif ($kid = $self->_non_h_unique_child($element, 'a') or $kid = $self->_non_h_unique_child($element, 'area') or $kid = $self->_non_h_unique_grandchild($element, 'a') or $kid = $self->_non_h_unique_grandchild($element, 'area')) { } 0 1 1
454 0 $xpath = './@href';
455 }
456 ▶ elsif (
Condition33%3/9
$kid = $self->_non_h_unique_child($element, "a") or $kid = $self->_non_h_unique_child($element, "area") or $kid = $self->_non_h_unique_grandchild($element, "a") or $kid = $self->_non_h_unique_grandchild($element, "area")
l!l&&r!l&&!r
001
$kid = $self->_non_h_unique_child($element, "a") or $kid = $self->_non_h_unique_child($element, "area") or $kid = $self->_non_h_unique_grandchild($element, "a")
l!l&&r!l&&!r
001
$kid = $self->_non_h_unique_child($element, "a") or $kid = $self->_non_h_unique_child($element, "area")
l!l&&r!l&&!r
001
MC/DC0%0/4
$kid = $self->_non_h_unique_child($element, "a") or $kid = $self->_non_h_unique_child($element, "area") or $kid = $self->_non_h_unique_grandchild($element, "a") or $kid = $self->_non_h_unique_grandchild($element, "area")
$kid = $self->_non_h_unique_child($element, "a") $kid = $self->_non_h_unique_child($element, "area") $kid = $self->_non_h_unique_grandchild($element, "a") $kid = $self->_non_h_unique_grandchild($element, "area")
Truth table20%1/5
A or B or C or D
A= $kid = $self->_non_h_unique_child($element, "a")B= $kid = $self->_non_h_unique_child($element, "area")C= $kid = $self->_non_h_unique_grandchild($element, "a")D= $kid = $self->_non_h_unique_grandchild($element, "area")
ABCDresult
1XXX1
01XX1
001X1
00011
00000
457 ( $kid = $self->_non_h_unique_child( $element, 'a' ) )
458 || ( $kid = $self->_non_h_unique_child( $element, 'area' ) )
459 || ( $kid = $self->_non_h_unique_grandchild( $element, 'a' ) )
460 || ( $kid = $self->_non_h_unique_grandchild( $element, 'area' ) )
461 ) {
462 0 $xpath = './@href';
463 0 $element = $kid;
464 }
465  
466 1 ▶ if ( $xpath ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xpath) 0 1 1
467 0 $url = $element->findvalue( $xpath );
468 }
469  
470 1 ▶ if ( defined $url ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $url) 0 1 1
471 0 $url = URI->new_abs( $url, $self->url_context )->as_string;
472 0 $item->add_property( 'u-url', $url );
473 }
474  
475 }
476  
477 sub _non_h_unique_child {
478 4 ▶ my $self = shift;
Subroutine _non_h_unique_child: called CC=4 SCAR=16.1
479 4 my ( $element, $tag ) = @_;
480  
481 4 ▶ my @children = grep { (ref $_) && $_->tag eq $tag } $element->content_list;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ and $_->tag eq $tag 12 16 28
482  
483 4 ▶ if ( @children == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@children == 1) 0 4 4
484 0 my $mf2_attrs = $self->_tease_out_mf2_attrs( $children[0] );
485 0 ▶ if (not ( $mf2_attrs->{h}->[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mf2_attrs->{"h"}[0]) 0 0 0
486 0 return $children[0];
487 }
488 }
489  
490 4 return;
491 }
492  
493 sub _non_h_unique_grandchild {
494 4 ▶ my $self = shift;
Subroutine _non_h_unique_grandchild: called CC=3 SCAR=13.6
495 4 my ( $element, $tag ) = @_;
496  
497 4 my @children = grep { ref $_ } $element->content_list;
498  
499 4 ▶ if ( @children == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@children == 1) 0 4 4
500 0 my $mf2_attrs = $self->_tease_out_mf2_attrs( $children[0] );
501 0 ▶ if (not ( $mf2_attrs->{h}->[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mf2_attrs->{"h"}[0]) 0 0 0
502 0 return $self->_non_h_unique_child( $children[0], $tag );
503 }
504 }
505  
506 4 return;
507 }
508  
509 sub _clear {
510 2 ▶ my $self = shift;
Subroutine _clear: called CC=1 SCAR=0.0
511  
512 2 $self->_clear_url_context;
513 }
514  
515 sub _seek_value_class_pattern {
516 12 ▶ my $self = shift;
Subroutine _seek_value_class_pattern: called CC=10 SCAR=35.1
517  
518 12 my ( $element, $vcp_fragments_ref ) = @_;
519  
520 12 ▶ $vcp_fragments_ref ||= [];
Condition100%2/2
$vcp_fragments_ref ||= []
l!l
48
MC/DC100%1/1
$vcp_fragments_ref ||= []
$vcp_fragments_ref
Truth table100%2/2
A
A= $vcp_fragments_ref
Aresult
11
00
521  
522 12 my $class = $element->attr( 'class' );
523 12 ▶ if ( $class && $class =~ /\bvalue(-title)?\b/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class and $class =~ /\bvalue(-title)?\b/) { } 0 12 12
Condition33%1/3
$class and $class =~ /\bvalue(-title)?\b/
!ll&&!rl&&r
0120
MC/DC0%0/2
$class and $class =~ /\bvalue(-title)?\b/
$class $class =~ /\bvalue(-title)?\b/
Truth table33%1/3
A and B
A= $classB= $class =~ /\bvalue(-title)?\b/
ABresult
0X0
100
111
524 0 ▶ if ( $1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($1) { } 0 0 0
elsif ($element->tag =~ /^(del|ins|time)$/ and defined $element->attr('datetime')) { } 0 0 0
525 0 push @$vcp_fragments_ref, $element->attr( 'title' );
526 }
527 ▶ elsif ( ( $element->tag =~ /^(del|ins|time)$/ ) && defined( $element->attr('datetime'))) {
Condition0%0/3
$element->tag =~ /^(del|ins|time)$/ and defined $element->attr("datetime")
!ll&&!rl&&r
000
MC/DC0%0/2
$element->tag =~ /^(del|ins|time)$/ and defined $element->attr("datetime")
$element->tag =~ /^(del|ins|time)$/ defined $element->attr("datetime")
Truth table0%0/3
A and B
A= $element->tag =~ /^(del|ins|time)$/B= defined $element->attr("datetime")
ABresult
0X0
100
111
528 0 push @$vcp_fragments_ref, $element->attr('datetime');
529 }
530 else {
531 0 my $html;
532 0 for my $content_piece ( $element->content_list ) {
533 0 ▶ if ( ref $content_piece ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content_piece) { } 0 0 0
534 0 $html .= $content_piece->as_HTML;
535 }
536 else {
537 0 $html .= $content_piece;
538 }
539 }
540 0 push @$vcp_fragments_ref, $html;
541 }
542 }
543 else {
544 12 for my $child_element ( grep { ref $_ } $element->content_list ) {
545 4 $self->_seek_value_class_pattern(
546 $child_element, $vcp_fragments_ref
547 );
548 }
549 }
550  
551 12 return $vcp_fragments_ref;
552 }
553  
554 sub _trim {
555 7 ▶ my ($string) = @_;
Subroutine _trim: called CC=1 SCAR=0.0
556 7 $string =~ s/^\s+//;
557 7 $string =~ s/\s+$//;
558 7 return $string;
559 }
560  
561 sub _format_datetime {
562 0 ▶ my ($self, $dt_string, $current_item) = @_;
Subroutine _format_datetime: not called CC=15 SCAR=54.8
563  
564 0 my $dt;
565  
566 # Knock off leading/trailing whitespace.
567 0 $dt_string = _trim($dt_string);
568  
569 0 $dt_string =~ s/t/T/;
570  
571 # Note presence of AM/PM, but toss it out of the string.
572 0 $dt_string =~ s/((?:a|p)\.?m\.?)//i;
573 0 ▶ my $am_or_pm = $1 || '';
Condition0%0/2
$1 || ''
l!l
00
MC/DC0%0/1
$1 || ''
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
574  
575 # Store the provided TZ offset.
576 0 my ($provided_offset) = $dt_string =~ /([\-\+Z](?:\d\d:?\d\d)?)$/;
577 0 ▶ $provided_offset ||= '';
Condition0%0/2
$provided_offset ||= ""
l!l
00
MC/DC0%0/1
$provided_offset ||= ""
$provided_offset
Truth table0%0/2
A
A= $provided_offset
Aresult
11
00
578  
579 # Reformat HHMM offset as HH:MM.
580 0 $dt_string =~ s/(-|\+)(\d\d)(\d\d)/$1$2:$3/;
581  
582 # Store the provided seconds.
583 0 my ($seconds) = $dt_string =~ /\d\d:\d\d:(\d\d)/;
584 0 ▶ $seconds = '' unless defined $seconds;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seconds 0 0 0
585  
586 # Insert :00 seconds on time when paired with a TZ offset.
587 0 $dt_string =~ s/T(\d\d:\d\d)([\-\+Z])/T$1:00$2/;
588 0 $dt_string =~ s/^(\d\d:\d\d)([\-\+Z])/$1:00$2/;
589  
590 # Zero-pad hours when only a single-digit hour appears.
591 0 $dt_string =~ s/T(\d)$/T0$1/;
592 0 $dt_string =~ s/T(\d):/T0$1:/;
593  
594 # Insert :00 minutes on time when only an hour is listed.
595 0 $dt_string =~ s/T(\d\d)$/T$1:00/;
596  
597 # Treat a space separator between date & time as a 'T'.
598 0 $dt_string =~ s/ /T/;
599  
600 # If this is a time with no date, try to apply a previously-seen
601 # date to it.
602 0 my $date_is_defined = 1;
603 0 ▶ if ( $dt_string =~ /^\d\d:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dt_string =~ /^\d\d:/) 0 0 0
604 0 ▶ if ( my $previous_dt = $current_item->last_seen_date ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $previous_dt = $current_item->last_seen_date) { } 0 0 0
605 0 $dt_string = $previous_dt->ymd . "T$dt_string";
606 }
607 else {
608 0 $date_is_defined = 0;
609 0 carp "Encountered a value-class datetime with only a time, "
610 . "no date, and no date defined earlier. Results may "
611 . "not be what you expect. (Data: $dt_string)";
612 }
613 }
614  
615 0 eval {
616 0 $dt = DateTime::Format::ISO8601->new
617 ->parse_datetime( $dt_string );
618 };
619  
620 0 ▶ return if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
621  
622 0 ▶ if ($date_is_defined) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date_is_defined) 0 0 0
623 0 $current_item->last_seen_date( $dt );
624 }
625  
626 0 ▶ if ($am_or_pm =~ /^[pP]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($am_or_pm =~ /^[pP]/) 0 0 0
627 # There was a 'pm' specified, so add 12 hours.
628 0 $dt->add( hours => 12 );
629 }
630  
631 0 my $format;
632 0 ▶ if ( ($dt_string =~ /-/) && ($dt_string =~ /[ T]/) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dt_string =~ /-/ and $dt_string =~ /[ T]/) { } 0 0 0
elsif ($dt_string =~ /-/) { } 0 0 0
Condition0%0/3
$dt_string =~ /-/ and $dt_string =~ /[ T]/
!ll&&!rl&&r
000
MC/DC0%0/2
$dt_string =~ /-/ and $dt_string =~ /[ T]/
$dt_string =~ /-/ $dt_string =~ /[ T]/
Truth table0%0/3
A and B
A= $dt_string =~ /-/B= $dt_string =~ /[ T]/
ABresult
0X0
100
111
633 0 my $offset;
634 0 ▶ if ($provided_offset eq 'Z') {
Branch: 2 branches
BranchTrueFalseTotal
if ($provided_offset eq 'Z') { } 0 0 0
elsif ($provided_offset) { } 0 0 0
635 0 $offset = 'Z';
636 }
637 elsif ($provided_offset) {
638 0 $offset = '%z';
639 }
640 else {
641 0 $offset = '';
642 }
643 0 ▶ $seconds = ":$seconds" if length $seconds;
Branch: 1 branch
BranchTrueFalseTotal
if length $seconds 0 0 0
644 0 $format = "%Y-%m-%d %H:%M$seconds$offset";
645 }
646 elsif ( $dt_string =~ /-/ ) {
647 0 $format = '%Y-%m-%d';
648 }
649  
650 0 return $dt->strftime( $format );
651 }
652  
653 sub _parse_property_value {
654 7 ▶ my ( $self, $element ) = @_;
Subroutine _parse_property_value: called CC=4 SCAR=15.5
655  
656 7 my $value;
657  
658 7 my $vcp_fragments_ref =
659 $self->_seek_value_class_pattern( $element );
660 7 ▶ if ( @$vcp_fragments_ref ) {
Branch: 3 branches
BranchTrueFalseTotal
if (@$vcp_fragments_ref) { } 0 7 7
elsif (my $alt = $element->findvalue('./@title|@value|@alt')) { } 0 7 7
elsif (my $text = _trim(decode_entities($element->as_text))) { } 7 0 7
661 0 $value = join q{}, @$vcp_fragments_ref;
662 }
663 elsif ( my $alt = $element->findvalue( './@title|@value|@alt' ) ) {
664 0 $value = $alt;
665 }
666 elsif ( my $text = _trim( decode_entities($element->as_text) ) ) {
667 7 $value = $text;
668 }
669  
670 7 return $value;
671 }
672  
673 sub _add_element_rels_to_mf2_document {
674 16 ▶ my ( $self, $element, $document ) = @_;
Subroutine _add_element_rels_to_mf2_document: called CC=8 SCAR=36.1
675  
676 16 ▶ return unless $element->tag =~ /^(a|link)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $element->tag =~ /^(a|link)$/ 15 1 16
677  
678 1 my $rel = $element->attr( 'rel' );
679 1 ▶ return unless defined $rel;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rel 1 0 1
680  
681 0 my $href = $element->attr( 'href' );
682 0 my $url = URI->new_abs( $href, $self->url_context)->as_string;
683  
684 0 my @rels = split /\s+/, $rel;
685 0 for my $rel ( @rels ) {
686 0 $document->add_rel( $rel, $url );
687 }
688  
689 0 my $rel_url_value = {};
690 0 foreach (qw( hreflang media title type ) ) {
691 0 ▶ next if defined $rel_url_value->{ $_ };
Branch: 1 branch
BranchTrueFalseTotal
if defined $rel_url_value->{$_} 0 0 0
692 0 my $value = $element->attr( $_ );
693 0 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
694 0 $rel_url_value->{ $_ } = $value;
695 }
696 }
697 0 my $text = ($element->as_text);
698 0 ▶ if ( defined $text ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $text) 0 0 0
699 0 $rel_url_value->{ text } = $text;
700 }
701  
702 0 $rel_url_value->{ rels } = \@rels;
703  
704 0 $document->add_rel_url( $url, $rel_url_value );
705  
706 }
707  
708 2 1;
709  
710 =pod
711  
712 =head1 NAME
713  
714 Web::Microformats2::Parser - Read Microformats2 information from HTML
715  
716 =head1 DESCRIPTION
717  
718 An object of this class represents a Microformats2 parser.
719  
720 See L<Web::Microformats2> for further context and purpose.
721  
722 =head1 METHODS
723  
724 =head2 Class Methods
725  
726 =head3 new
727  
728 $parser = Web::Microformats2::Parser->new;
729  
730 Returns a parser object.
731  
732 =head2 Object Methods
733  
734 =head3 parse
735  
736 $doc = $parser->parse( $html, %args );
737  
738 Pass in a string containing HTML which itself contains Microformats2
739 metadata, and receive a L<Web::Microformats2::Document> object in return.
740  
741 The optional args hash recognizes the following keys:
742  
743 =over
744  
745 =item url_context
746  
747 A L<URI> object or URI-shaped string that will be used as a context for
748 transforming all relative URL properties encountered within MF2 tags
749 into absolute URLs.
750  
751 The default value is C<http://example.com>, so you'll probably want to
752 set this to something more interesting, such as the absolute URL of the
753 HTML that we are parsing.
754  
755 =back
756  
757 =head1 AUTHOR
758  
759 Jason McIntosh (jmac@jmac.org)
760  
761 =head1 COPYRIGHT AND LICENSE
762  
763 This software is Copyright (c) 2018 by Jason McIntosh.
764  
765 This is free software, licensed under:
766  
767 The MIT (X11) License
↑ Summary blib/lib/Web/Microformats2/Document.pm »