From 5940ed35ea9182fa3dd79fead5422b81370a4d4e Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Wed, 24 May 2017 11:04:52 -0700 Subject: [PATCH 1/2] adds failing tests and fixes for #118 there were no tests for missing attributes, so this adds a few, and fixes the parse to test for the presence of attributes rather than the incorrect check `getAttribute('href') !== null` (getAttribute never returns null). fixes #118 --- Mf2/Parser.php | 20 ++++++------- tests/Mf2/ParsePTest.php | 12 ++++++++ tests/Mf2/ParseUTest.php | 62 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 83 insertions(+), 11 deletions(-) diff --git a/Mf2/Parser.php b/Mf2/Parser.php index 11c6302..82e39a9 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)); @@ -1132,7 +1132,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..1a8e818 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 @@ -175,6 +222,19 @@ public function testParseUHandlesSource() { $this->assertEquals('http://example.com/video.ogg', $output['items'][0]['properties']['video'][1]); } + /** + * @group parseU + */ + public function testParseUHandlesVideoPoster() { + $input = '
'; + $parser = new Parser($input); + $output = $parser->parse(); + + $this->assertArrayHasKey('video', $output['items'][0]['properties']); + $this->assertEquals('http://example.com/video.mp4', $output['items'][0]['properties']['video'][0]); + $this->assertEquals('http://example.com/posterimage.jpg', $output['items'][0]['properties']['photo'][0]); + } + /** * @group parseU */ From d205d750b6ee1e539e0a8f71f8365ae0333be1ff Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Wed, 24 May 2017 11:07:05 -0700 Subject: [PATCH 2/2] committed this test too early sorry --- tests/Mf2/ParseUTest.php | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/tests/Mf2/ParseUTest.php b/tests/Mf2/ParseUTest.php index 1a8e818..b10957b 100644 --- a/tests/Mf2/ParseUTest.php +++ b/tests/Mf2/ParseUTest.php @@ -222,19 +222,6 @@ public function testParseUHandlesSource() { $this->assertEquals('http://example.com/video.ogg', $output['items'][0]['properties']['video'][1]); } - /** - * @group parseU - */ - public function testParseUHandlesVideoPoster() { - $input = '
'; - $parser = new Parser($input); - $output = $parser->parse(); - - $this->assertArrayHasKey('video', $output['items'][0]['properties']); - $this->assertEquals('http://example.com/video.mp4', $output['items'][0]['properties']['video'][0]); - $this->assertEquals('http://example.com/posterimage.jpg', $output['items'][0]['properties']['photo'][0]); - } - /** * @group parseU */