Skip to content
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

Fix <img> handling in implied p-name #199

Merged
merged 1 commit into from
Aug 24, 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
18 changes: 10 additions & 8 deletions Mf2/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,19 +463,21 @@ private function resolveChildUrls(DOMElement $el) {
}
}

/**
* The following two methods implements plain text parsing.
* @see https://wiki.zegnat.net/media/textparsing.html
**/
public function textContent(DOMElement $element)
/**
* The following two methods implements plain text parsing.
* @param DOMElement $element
* @param bool $implied
* @see https://wiki.zegnat.net/media/textparsing.html
**/
public function textContent(DOMElement $element, $implied=false)
{
return preg_replace(
'/(^[\t\n\f\r ]+| +(?=\n)|(?<=\n) +| +(?= )|[\t\n\f\r ]+$)/',
'',
$this->elementToString($element)
$this->elementToString($element, $implied)
);
}
private function elementToString(DOMElement $input)
private function elementToString(DOMElement $input, $implied=false)
{
$output = '';
foreach ($input->childNodes as $child) {
Expand All @@ -488,7 +490,7 @@ private function elementToString(DOMElement $input)
} else if ($tagName === 'IMG') {
if ($child->hasAttribute('alt')) {
$output .= ' ' . trim($child->getAttribute('alt'), "\t\n\f\r ") . ' ';
} else if ($child->hasAttribute('src')) {
} else if (!$implied && $child->hasAttribute('src')) {
$output .= ' ' . $this->resolveUrl(trim($child->getAttribute('src'), "\t\n\f\r ")) . ' ';
}
} else if ($tagName === 'BR') {
Expand Down
11 changes: 11 additions & 0 deletions tests/Mf2/ParseImpliedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,5 +364,16 @@ public function testNoImpliedUrl() {
$this->assertArrayNotHasKey('url', $result['items'][0]['properties']);
}

/**
* Do not use img src in implied p-name
* @see https://github.com/microformats/php-mf2/issues/180
*/
public function testNoImgSrcImpliedName() {
$input = '<p class="h-card">My Name <img src="http://xyz" /></p>';
$result = Mf2\parse($input);

$this->assertArrayHasKey('name', $result['items'][0]['properties']);
$this->assertEquals('My Name', $result['items'][0]['properties']['name'][0]);
}
}