Skip to content

Commit c35ed9a

Browse files
authored
Merge pull request #199 from gRegorLove/issue180
Fix <img> handling in implied p-name
2 parents 8247a27 + 4a2f70b commit c35ed9a

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

Mf2/Parser.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -463,19 +463,21 @@ private function resolveChildUrls(DOMElement $el) {
463463
}
464464
}
465465

466-
/**
467-
* The following two methods implements plain text parsing.
468-
* @see https://wiki.zegnat.net/media/textparsing.html
469-
**/
470-
public function textContent(DOMElement $element)
466+
/**
467+
* The following two methods implements plain text parsing.
468+
* @param DOMElement $element
469+
* @param bool $implied
470+
* @see https://wiki.zegnat.net/media/textparsing.html
471+
**/
472+
public function textContent(DOMElement $element, $implied=false)
471473
{
472474
return preg_replace(
473475
'/(^[\t\n\f\r ]+| +(?=\n)|(?<=\n) +| +(?= )|[\t\n\f\r ]+$)/',
474476
'',
475-
$this->elementToString($element)
477+
$this->elementToString($element, $implied)
476478
);
477479
}
478-
private function elementToString(DOMElement $input)
480+
private function elementToString(DOMElement $input, $implied=false)
479481
{
480482
$output = '';
481483
foreach ($input->childNodes as $child) {
@@ -488,7 +490,7 @@ private function elementToString(DOMElement $input)
488490
} else if ($tagName === 'IMG') {
489491
if ($child->hasAttribute('alt')) {
490492
$output .= ' ' . trim($child->getAttribute('alt'), "\t\n\f\r ") . ' ';
491-
} else if ($child->hasAttribute('src')) {
493+
} else if (!$implied && $child->hasAttribute('src')) {
492494
$output .= ' ' . $this->resolveUrl(trim($child->getAttribute('src'), "\t\n\f\r ")) . ' ';
493495
}
494496
} else if ($tagName === 'BR') {

tests/Mf2/ParseImpliedTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,5 +364,16 @@ public function testNoImpliedUrl() {
364364
$this->assertArrayNotHasKey('url', $result['items'][0]['properties']);
365365
}
366366

367+
/**
368+
* Do not use img src in implied p-name
369+
* @see https://github.com/microformats/php-mf2/issues/180
370+
*/
371+
public function testNoImgSrcImpliedName() {
372+
$input = '<p class="h-card">My Name <img src="http://xyz" /></p>';
373+
$result = Mf2\parse($input);
374+
375+
$this->assertArrayHasKey('name', $result['items'][0]['properties']);
376+
$this->assertEquals('My Name', $result['items'][0]['properties']['name'][0]);
377+
}
367378
}
368379

0 commit comments

Comments
 (0)