From 4a2f70b778272b5060419e4798b59a63d12b57a3 Mon Sep 17 00:00:00 2001 From: Gregor Morrill Date: Thu, 23 Aug 2018 15:25:24 -0700 Subject: [PATCH] Add failing test and fix for #180 --- Mf2/Parser.php | 18 ++++++++++-------- tests/Mf2/ParseImpliedTest.php | 11 +++++++++++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/Mf2/Parser.php b/Mf2/Parser.php index 2192b8c..fe5590b 100644 --- a/Mf2/Parser.php +++ b/Mf2/Parser.php @@ -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) { @@ -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') { diff --git a/tests/Mf2/ParseImpliedTest.php b/tests/Mf2/ParseImpliedTest.php index c8ffd8e..e12ed00 100644 --- a/tests/Mf2/ParseImpliedTest.php +++ b/tests/Mf2/ParseImpliedTest.php @@ -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 = '

My Name

'; + $result = Mf2\parse($input); + + $this->assertArrayHasKey('name', $result['items'][0]['properties']); + $this->assertEquals('My Name', $result['items'][0]['properties']['name'][0]); + } }