Skip to content

Fix implied photo parsing #191

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

Merged
merged 4 commits into from
Aug 23, 2018
Merged
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
39 changes: 19 additions & 20 deletions Mf2/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without resolving here, the implied values from img.h-x[src] and object.h-x[data] will never get resolved. Why not keep resolveUrl() here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add resolveUrl to those two. My thinking is parseImpliedPhoto() should return an absolute URL or false.

}

}
Expand Down Expand Up @@ -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'));
}
}
}

Expand Down
20 changes: 20 additions & 0 deletions tests/Mf2/ParseImpliedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<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>';
$result = Mf2\parse($input);

$this->assertArrayNotHasKey('photo', $result['items'][0]['properties']);
}

/**
* @see https://github.com/indieweb/php-mf2/issues/190
*/
public function testIgnoredMultiChildrenWithNestedPhotoObject() {
$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>';
$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
Expand Down