blib/lib/Web/Microformats2/Parser.pm

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