Skip to content

Commit ab56749

Browse files
authored
Merge pull request #191 from gRegorLove/issue190
Fix implied photo parsing
2 parents 35fee8b + 04aadf2 commit ab56749

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

Mf2/Parser.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,7 @@ public function parseH(\DOMElement $e, $is_backcompat = false, $has_nested_mf =
10781078
$photo = $this->parseImpliedPhoto($e);
10791079

10801080
if ($photo !== false) {
1081-
$return['photo'][] = $this->resolveUrl($photo);
1081+
$return['photo'][] = $photo;
10821082
}
10831083

10841084
}
@@ -1155,38 +1155,37 @@ public function parseH(\DOMElement $e, $is_backcompat = false, $has_nested_mf =
11551155
*/
11561156
public function parseImpliedPhoto(\DOMElement $e) {
11571157

1158+
// img.h-x[src]
11581159
if ($e->tagName == 'img') {
1159-
return $e->getAttribute('src');
1160+
return $this->resolveUrl($e->getAttribute('src'));
11601161
}
11611162

1163+
// object.h-x[data]
11621164
if ($e->tagName == 'object' && $e->hasAttribute('data')) {
1163-
return $e->getAttribute('data');
1165+
return $this->resolveUrl($e->getAttribute('data'));
11641166
}
11651167

11661168
$xpaths = array(
1167-
'./img',
1168-
'./object',
1169-
'./*[not(contains(concat(" ", @class), " h-"))]/img[count(preceding-sibling::img)+count(following-sibling::img)=0]',
1170-
'./*[not(contains(concat(" ", @class), " h-"))]/object[count(preceding-sibling::object)+count(following-sibling::object)=0]',
1169+
// .h-x>img[src]:only-of-type:not[.h-*]
1170+
'./img[not(contains(concat(" ", @class), " h-")) and count(../img) = 1 and @src]',
1171+
// .h-x>object[data]:only-of-type:not[.h-*]
1172+
'./object[not(contains(concat(" ", @class), " h-")) and count(../object) = 1 and @data]',
1173+
// .h-x>:only-child:not[.h-*]>img[src]:only-of-type:not[.h-*]
1174+
'./*[not(contains(concat(" ", @class), " h-")) and count(../*) = 1 and count(img) = 1]/img[not(contains(concat(" ", @class), " h-")) and @src]',
1175+
// .h-x>:only-child:not[.h-*]>object[data]:only-of-type:not[.h-*]
1176+
'./*[not(contains(concat(" ", @class), " h-")) and count(../*) = 1 and count(object) = 1]/object[not(contains(concat(" ", @class), " h-")) and @data]',
11711177
);
11721178

11731179
foreach ($xpaths as $path) {
11741180
$els = $this->xpath->query($path, $e);
11751181

1176-
if ($els->length == 1) {
1182+
if ($els !== false && $els->length === 1) {
11771183
$el = $els->item(0);
1178-
$hClasses = mfNamesFromElement($el, 'h-');
1179-
1180-
// no nested h-
1181-
if (empty($hClasses)) {
1182-
1183-
if ($el->tagName == 'img') {
1184-
return $el->getAttribute('src');
1185-
} else if ($el->tagName == 'object' && $el->hasAttribute('data')) {
1186-
return $el->getAttribute('data');
1187-
}
1188-
1189-
} // no nested h-
1184+
if ($el->tagName == 'img') {
1185+
return $this->resolveUrl($el->getAttribute('src'));
1186+
} else if ($el->tagName == 'object') {
1187+
return $this->resolveUrl($el->getAttribute('data'));
1188+
}
11901189
}
11911190
}
11921191

tests/Mf2/ParseImpliedTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,26 @@ public function testIgnoredPhotoObjectInNestedH() {
293293
$this->assertArrayNotHasKey('photo', $result['items'][0]['properties']);
294294
}
295295

296+
/**
297+
* @see https://github.com/indieweb/php-mf2/issues/190
298+
*/
299+
public function testIgnoredMultiChildrenWithNestedPhotoImg() {
300+
$input = '<div class="h-card"> <a href="https://example.com"><img src="https://example.com/photo.jpg"></a> <span class="p-name"><a href="/User:Example.com">Max Mustermann</a></span> </div>';
301+
$result = Mf2\parse($input);
302+
303+
$this->assertArrayNotHasKey('photo', $result['items'][0]['properties']);
304+
}
305+
306+
/**
307+
* @see https://github.com/indieweb/php-mf2/issues/190
308+
*/
309+
public function testIgnoredMultiChildrenWithNestedPhotoObject() {
310+
$input = '<div class="h-card"> <a href="https://example.com"><object data="https://example.com/photo.jpg"></object></a> <span class="p-name"><a href="/User:Example.com">Max Mustermann</a></span> </div>';
311+
$result = Mf2\parse($input);
312+
313+
$this->assertArrayNotHasKey('photo', $result['items'][0]['properties']);
314+
}
315+
296316
/**
297317
* Imply properties only on explicit h-x class name root microformat element (no backcompat roots)
298318
* @see http://microformats.org/wiki/microformats2-parsing#parsing_for_implied_properties

0 commit comments

Comments
 (0)