Skip to content

Allow parsing of Open Graph metas without og:type present. #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: topic/pr-1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 42 additions & 24 deletions lib/Data/OpenGraph/Parser.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package Data::OpenGraph::Parser;
use strict;
use Scalar::Util qw(reftype);
use Scalar::Util qw(reftype blessed);


sub new {
Expand Down Expand Up @@ -29,44 +29,36 @@ sub parse_string {
my %properties = ();

my $og_type = $tree->findnodes('//meta[@property="og:type" and @content]');
if (! defined($og_type)) {
$tree->delete;
return \%properties;
if (defined($og_type)) {
$og_type = _unwrap_one_node($og_type);
}

# There appears to be a slight difference in return value depending on
# whether HTML::TreeBuilder::LibXML or HTML::TreeBuilder::XPath is used.
if (reftype($og_type) eq 'HASH' && scalar keys %$og_type) {
$og_type = $og_type->get_nodelist->get_node(0);
}
elsif (reftype($og_type) eq 'ARRAY' && scalar @$og_type) {
$og_type = $og_type->[0];
}
else {
$tree->delete;
return \%properties;
}

my $content = $og_type->attr('content');
if ($content =~ /^([^.]+)\..+$/) {
$properties{'_basictype'} = $1;
} else {
$properties{'_basictype'} = $content;
my $content;
if (defined($og_type)
&& ($content = $og_type->attr('content'))) {
$properties{'type'} = $content;
if ($content =~ /^([^.]+)\..+$/) {
$properties{'_basictype'} = $1;
} else {
$properties{'_basictype'} = $content;
}
}
my $basictype = $properties{'_basictype'};

my $metas = $tree->findnodes(
'//meta['
.'((starts-with(@property, "og:") and @property != "og:type")'
.' or starts-with(@property, "'.$basictype.':"))'
.(defined($basictype)
? ' or starts-with(@property, "'.$basictype.':")' : '')
.')'
.'and @content'
.']');
for my $meta (@$metas) {
my $prop = $meta->attr('property');
$content = $meta->attr('content');
next unless $prop && $content;
$prop =~ s/^og://;
if ($prop =~ /^$basictype:.+$/) {
if (defined($basictype) && $prop =~ /^$basictype:.+$/) {
if (exists $properties{$prop}) {
if ((reftype($properties{$prop}) // '') eq 'ARRAY') {
push @{$properties{$prop}}, $content;
Expand All @@ -88,6 +80,32 @@ sub parse_string {
return \%properties;
}

sub _unwrap_one_node {
# There appears to be a slight difference in return value depending on
# whether HTML::TreeBuilder::LibXML or HTML::TreeBuilder::XPath is used.

my ($nodes_or_node,) = @_;
if (blessed($nodes_or_node)
&& $nodes_or_node->isa('XML::XPathEngine::NodeSet')) {
if ($nodes_or_node->size()) {
return $nodes_or_node->get_node(0);
}
else {
return;
}
}
elsif (reftype($nodes_or_node) eq 'ARRAY') {
if (scalar @$nodes_or_node) {
return $nodes_or_node->[0];
}
else {
return;
}
}

return $nodes_or_node;
}

1;


Expand Down
12 changes: 11 additions & 1 deletion t/001_basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,14 @@ EOM
}
};

done_testing;
subtest 'nested-without-prefix' => sub {
my $og = Data::OpenGraph->parse_string(<<EOM);
<meta property="og:type" content="audio">
<meta property="audio:title" content="foo bar baz">
EOM
if (not is $og->property("audio:title"), "foo bar baz") {
diag(Data::Dumper::Dumper($og));
}
};

done_testing;