diff --git a/Mf2/Parser.php b/Mf2/Parser.php index ed1f28f..317db25 100644 --- a/Mf2/Parser.php +++ b/Mf2/Parser.php @@ -588,13 +588,13 @@ public function parseP(\DOMElement $p) { $this->resolveChildUrls($p); - if ($p->tagName == 'img' and $p->getAttribute('alt') !== '') { + if ($p->tagName == 'img' and $p->hasAttribute('alt')) { $pValue = $p->getAttribute('alt'); - } elseif ($p->tagName == 'area' and $p->getAttribute('alt') !== '') { + } elseif ($p->tagName == 'area' and $p->hasAttribute('alt')) { $pValue = $p->getAttribute('alt'); - } elseif ($p->tagName == 'abbr' and $p->getAttribute('title') !== '') { + } elseif ($p->tagName == 'abbr' and $p->hasAttribute('title')) { $pValue = $p->getAttribute('title'); - } elseif (in_array($p->tagName, array('data', 'input')) and $p->getAttribute('value') !== '') { + } elseif (in_array($p->tagName, array('data', 'input')) and $p->hasAttribute('value')) { $pValue = $p->getAttribute('value'); } else { $pValue = unicodeTrim($this->innerText($p)); @@ -611,11 +611,11 @@ public function parseP(\DOMElement $p) { * @todo make this adhere to value-class */ public function parseU(\DOMElement $u) { - if (($u->tagName == 'a' or $u->tagName == 'area') and $u->getAttribute('href') !== null) { + if (($u->tagName == 'a' or $u->tagName == 'area') and $u->hasAttribute('href')) { $uValue = $u->getAttribute('href'); - } elseif (in_array($u->tagName, array('img', 'audio', 'video', 'source')) and $u->getAttribute('src') !== null) { + } elseif (in_array($u->tagName, array('img', 'audio', 'video', 'source')) and $u->hasAttribute('src')) { $uValue = $u->getAttribute('src'); - } elseif ($u->tagName == 'object' and $u->getAttribute('data') !== null) { + } elseif ($u->tagName == 'object' and $u->hasAttribute('data')) { $uValue = $u->getAttribute('data'); } @@ -627,9 +627,9 @@ public function parseU(\DOMElement $u) { if ($classTitle !== null) { return $classTitle; - } elseif ($u->tagName == 'abbr' and $u->getAttribute('title') !== null) { + } elseif ($u->tagName == 'abbr' and $u->hasAttribute('title')) { return $u->getAttribute('title'); - } elseif (in_array($u->tagName, array('data', 'input')) and $u->getAttribute('value') !== null) { + } elseif (in_array($u->tagName, array('data', 'input')) and $u->hasAttribute('value')) { return $u->getAttribute('value'); } else { return unicodeTrim($this->textContent($u)); @@ -1128,7 +1128,7 @@ public function parseImpliedPhoto(\DOMElement $e) { if ($el->tagName == 'img') { return $el->getAttribute('src'); - } else if ($el->tagName == 'object' && $el->getAttribute('data') != '') { + } else if ($el->tagName == 'object' && $el->hasAttribute('data')) { return $el->getAttribute('data'); } diff --git a/tests/Mf2/ParsePTest.php b/tests/Mf2/ParsePTest.php index 1e7d0af..55f4a98 100644 --- a/tests/Mf2/ParsePTest.php +++ b/tests/Mf2/ParsePTest.php @@ -67,6 +67,18 @@ public function testParsePHandlesData() { $this->assertEquals('Example User', $output['items'][0]['properties']['name'][0]); } + /** + * @group parseP + */ + public function testParsePHandlesDataWithBlankValueAttribute() { + $input = '
Example User
'; + $parser = new Parser($input); + $output = $parser->parse(); + + $this->assertArrayHasKey('name', $output['items'][0]['properties']); + $this->assertEquals('', $output['items'][0]['properties']['name'][0]); + } + /** * @group parseP */ diff --git a/tests/Mf2/ParseUTest.php b/tests/Mf2/ParseUTest.php index 1f017d4..b10957b 100644 --- a/tests/Mf2/ParseUTest.php +++ b/tests/Mf2/ParseUTest.php @@ -25,7 +25,31 @@ public function testParseUHandlesA() { $this->assertArrayHasKey('url', $output['items'][0]['properties']); $this->assertEquals('http://example.com', $output['items'][0]['properties']['url'][0]); } - + + /** + * @group parseU + */ + public function testParseUHandlesEmptyHrefAttribute() { + $input = '
Awesome example website
'; + $parser = new Parser($input, "http://example.com/"); + $output = $parser->parse(); + + $this->assertArrayHasKey('url', $output['items'][0]['properties']); + $this->assertEquals('http://example.com/', $output['items'][0]['properties']['url'][0]); + } + + /** + * @group parseU + */ + public function testParseUHandlesMissingHrefAttribute() { + $input = '
Awesome example website
'; + $parser = new Parser($input, "http://example.com/"); + $output = $parser->parse(); + + $this->assertArrayHasKey('url', $output['items'][0]['properties']); + $this->assertEquals('Awesome example website', $output['items'][0]['properties']['url'][0]); + } + /** * @group parseU */ @@ -73,6 +97,18 @@ public function testParseUHandlesAbbr() { $this->assertArrayHasKey('photo', $output['items'][0]['properties']); $this->assertEquals('http://example.com/someimage.png', $output['items'][0]['properties']['photo'][0]); } + + /** + * @group parseU + */ + public function testParseUHandlesAbbrNoTitle() { + $input = '
no title attribute
'; + $parser = new Parser($input); + $output = $parser->parse(); + + $this->assertArrayHasKey('photo', $output['items'][0]['properties']); + $this->assertEquals('no title attribute', $output['items'][0]['properties']['photo'][0]); + } /** * @group parseU @@ -161,6 +197,17 @@ public function testParseUHandlesVideo() { $this->assertEquals('http://example.com/video.mp4', $output['items'][0]['properties']['video'][0]); } + /** + * @group parseU + */ + public function testParseUHandlesVideoNoSrc() { + $input = '
'; + $parser = new Parser($input); + $output = $parser->parse(); + + $this->assertArrayHasKey('video', $output['items'][0]['properties']); + $this->assertEquals('no video support', $output['items'][0]['properties']['video'][0]); + } /** * @group parseU