diff --git a/Mf2/Parser.php b/Mf2/Parser.php index 1ba275f..c9ac910 100644 --- a/Mf2/Parser.php +++ b/Mf2/Parser.php @@ -1078,7 +1078,7 @@ public function parseH(\DOMElement $e, $is_backcompat = false, $has_nested_mf = $photo = $this->parseImpliedPhoto($e); if ($photo !== false) { - $return['photo'][] = $this->resolveUrl($photo); + $return['photo'][] = $photo; } } @@ -1155,38 +1155,37 @@ public function parseH(\DOMElement $e, $is_backcompat = false, $has_nested_mf = */ public function parseImpliedPhoto(\DOMElement $e) { + // img.h-x[src] if ($e->tagName == 'img') { - return $e->getAttribute('src'); + return $this->resolveUrl($e->getAttribute('src')); } + // object.h-x[data] if ($e->tagName == 'object' && $e->hasAttribute('data')) { - return $e->getAttribute('data'); + return $this->resolveUrl($e->getAttribute('data')); } $xpaths = array( - './img', - './object', - './*[not(contains(concat(" ", @class), " h-"))]/img[count(preceding-sibling::img)+count(following-sibling::img)=0]', - './*[not(contains(concat(" ", @class), " h-"))]/object[count(preceding-sibling::object)+count(following-sibling::object)=0]', + // .h-x>img[src]:only-of-type:not[.h-*] + './img[not(contains(concat(" ", @class), " h-")) and count(../img) = 1 and @src]', + // .h-x>object[data]:only-of-type:not[.h-*] + './object[not(contains(concat(" ", @class), " h-")) and count(../object) = 1 and @data]', + // .h-x>:only-child:not[.h-*]>img[src]:only-of-type:not[.h-*] + './*[not(contains(concat(" ", @class), " h-")) and count(../*) = 1 and count(img) = 1]/img[not(contains(concat(" ", @class), " h-")) and @src]', + // .h-x>:only-child:not[.h-*]>object[data]:only-of-type:not[.h-*] + './*[not(contains(concat(" ", @class), " h-")) and count(../*) = 1 and count(object) = 1]/object[not(contains(concat(" ", @class), " h-")) and @data]', ); foreach ($xpaths as $path) { $els = $this->xpath->query($path, $e); - if ($els->length == 1) { + if ($els !== false && $els->length === 1) { $el = $els->item(0); - $hClasses = mfNamesFromElement($el, 'h-'); - - // no nested h- - if (empty($hClasses)) { - - if ($el->tagName == 'img') { - return $el->getAttribute('src'); - } else if ($el->tagName == 'object' && $el->hasAttribute('data')) { - return $el->getAttribute('data'); - } - - } // no nested h- + if ($el->tagName == 'img') { + return $this->resolveUrl($el->getAttribute('src')); + } else if ($el->tagName == 'object') { + return $this->resolveUrl($el->getAttribute('data')); + } } } diff --git a/tests/Mf2/ParseImpliedTest.php b/tests/Mf2/ParseImpliedTest.php index 48a821c..7e8a36a 100644 --- a/tests/Mf2/ParseImpliedTest.php +++ b/tests/Mf2/ParseImpliedTest.php @@ -293,6 +293,26 @@ public function testIgnoredPhotoObjectInNestedH() { $this->assertArrayNotHasKey('photo', $result['items'][0]['properties']); } + /** + * @see https://github.com/indieweb/php-mf2/issues/190 + */ + public function testIgnoredMultiChildrenWithNestedPhotoImg() { + $input = '
Max Mustermann
'; + $result = Mf2\parse($input); + + $this->assertArrayNotHasKey('photo', $result['items'][0]['properties']); + } + + /** + * @see https://github.com/indieweb/php-mf2/issues/190 + */ + public function testIgnoredMultiChildrenWithNestedPhotoObject() { + $input = '
Max Mustermann
'; + $result = Mf2\parse($input); + + $this->assertArrayNotHasKey('photo', $result['items'][0]['properties']); + } + /** * Imply properties only on explicit h-x class name root microformat element (no backcompat roots) * @see http://microformats.org/wiki/microformats2-parsing#parsing_for_implied_properties