Skip to content

Commit 3b9e49f

Browse files
authored
Merge pull request #192 from Zegnat/implied-url
Fix implied URL parsing
2 parents ab56749 + 223f2be commit 3b9e49f

File tree

3 files changed

+37
-28
lines changed

3 files changed

+37
-28
lines changed

Mf2/Parser.php

+23-27
Original file line numberDiff line numberDiff line change
@@ -1083,35 +1083,31 @@ public function parseH(\DOMElement $e, $is_backcompat = false, $has_nested_mf =
10831083

10841084
}
10851085

1086-
// Check for u-url
1087-
if (!array_key_exists('url', $return) && !$is_backcompat) {
1088-
$url = null;
1089-
// Look for img @src
1090-
if ($e->tagName == 'a' or $e->tagName == 'area') {
1091-
$url = $e->getAttribute('href');
1092-
}
1093-
1094-
// Look for nested a @href
1095-
foreach ($this->xpath->query('./a[count(preceding-sibling::a)+count(following-sibling::a)=0]', $e) as $em) {
1096-
$emNames = mfNamesFromElement($em, 'h-');
1097-
if (empty($emNames)) {
1098-
$url = $em->getAttribute('href');
1099-
break;
1100-
}
1101-
}
1102-
1103-
// Look for nested area @src
1104-
foreach ($this->xpath->query('./area[count(preceding-sibling::area)+count(following-sibling::area)=0]', $e) as $em) {
1105-
$emNames = mfNamesFromElement($em, 'h-');
1106-
if (empty($emNames)) {
1107-
$url = $em->getAttribute('href');
1108-
break;
1086+
// Do we need to imply a url property?
1087+
// if no explicit "url" property, and no other explicit u-* properties, and no nested microformats
1088+
if (!array_key_exists('url', $return) && !in_array('u-', $prefixes) && !$has_nested_mf && !$is_backcompat) {
1089+
// a.h-x[href] or area.h-x[href]
1090+
if (($e->tagName === 'a' || $e->tagName === 'area') && $e->hasAttribute('href')) {
1091+
$return['url'][] = $this->resolveUrl($e->getAttribute('href'));
1092+
} else {
1093+
$xpaths = array(
1094+
// .h-x>a[href]:only-of-type:not[.h-*]
1095+
'./a[not(contains(concat(" ", @class), " h-")) and count(../a) = 1 and @href]',
1096+
// .h-x>area[href]:only-of-type:not[.h-*]
1097+
'./area[not(contains(concat(" ", @class), " h-")) and count(../area) = 1 and @href]',
1098+
// .h-x>:only-child:not[.h-*]>a[href]:only-of-type:not[.h-*]
1099+
'./*[not(contains(concat(" ", @class), " h-")) and count(../*) = 1 and count(a) = 1]/a[not(contains(concat(" ", @class), " h-")) and @href]',
1100+
// .h-x>:only-child:not[.h-*]>area[href]:only-of-type:not[.h-*]
1101+
'./*[not(contains(concat(" ", @class), " h-")) and count(../*) = 1 and count(area) = 1]/area[not(contains(concat(" ", @class), " h-")) and @href]'
1102+
);
1103+
foreach ($xpaths as $xpath) {
1104+
$url = $this->xpath->query($xpath, $e);
1105+
if ($url !== false && $url->length === 1) {
1106+
$return['url'][] = $this->resolveUrl($url->item(0)->getAttribute('href'));
1107+
break;
1108+
}
11091109
}
11101110
}
1111-
1112-
if (!is_null($url)) {
1113-
$return['url'][] = $this->resolveUrl($url);
1114-
}
11151111
}
11161112

11171113
// Make sure things are unique and in alphabetical order

tests/Mf2/ClassicMicroformatsTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ public function testMixedMf2andMf1Case3() {
704704
$parser = new Parser($input);
705705
$result = $parser->parse();
706706

707-
$this->assertCount(3, $result['items'][0]['properties']);
707+
$this->assertCount(2, $result['items'][0]['properties']);
708708
$this->assertArrayNotHasKey('street-address', $result['items'][0]['properties']);
709709
$this->assertArrayNotHasKey('locality', $result['items'][0]['properties']);
710710
$this->assertArrayNotHasKey('country-name', $result['items'][0]['properties']);

tests/Mf2/ParseImpliedTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -351,5 +351,18 @@ public function testBackcompatNoImpliedUrl() {
351351
$this->assertArrayHasKey('content', $result['items'][0]['properties']);
352352
}
353353

354+
355+
/**
356+
* Don't imply u-url if there are other u-*
357+
* @see http://microformats.org/wiki/microformats2-parsing#parsing_for_implied_properties
358+
* @see https://github.com/microformats/php-mf2/issues/183
359+
*/
360+
public function testNoImpliedUrl() {
361+
$input = '<div class="h-entry"> <h1 class="p-name"><a href="https://example.com/this-post">Title</a></h1> <div class="e-content"> <p> blah blah blah </p> </div> <a href="https://example.org/syndicate" class="u-syndication"></a> </div>';
362+
$result = Mf2\parse($input);
363+
364+
$this->assertArrayNotHasKey('url', $result['items'][0]['properties']);
365+
}
366+
354367
}
355368

0 commit comments

Comments
 (0)