Skip to content

Commit

Permalink
Add failing test and fix for microformats#180
Browse files Browse the repository at this point in the history
  • Loading branch information
gRegorLove committed Aug 23, 2018
1 parent 8247a27 commit 4a2f70b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
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]);
}
}

0 comments on commit 4a2f70b

Please sign in to comment.