Skip to content

Commit

Permalink
Merge pull request #119 from aaronpk/issue-118
Browse files Browse the repository at this point in the history
Fixes for elements with missing attributes (#118)
  • Loading branch information
aaronpk authored May 24, 2017
2 parents eb22f33 + d205d75 commit 26e4ed4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 11 deletions.
20 changes: 10 additions & 10 deletions Mf2/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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');
}

Expand All @@ -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));
Expand Down Expand Up @@ -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');
}

Expand Down
12 changes: 12 additions & 0 deletions tests/Mf2/ParsePTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ public function testParsePHandlesData() {
$this->assertEquals('Example User', $output['items'][0]['properties']['name'][0]);
}

/**
* @group parseP
*/
public function testParsePHandlesDataWithBlankValueAttribute() {
$input = '<div class="h-card"><data class="p-name" value="">Example User</data></div>';
$parser = new Parser($input);
$output = $parser->parse();

$this->assertArrayHasKey('name', $output['items'][0]['properties']);
$this->assertEquals('', $output['items'][0]['properties']['name'][0]);
}

/**
* @group parseP
*/
Expand Down
49 changes: 48 additions & 1 deletion tests/Mf2/ParseUTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<div class="h-card"><a class="u-url" href="">Awesome example website</a></div>';
$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 = '<div class="h-card"><a class="u-url">Awesome example website</a></div>';
$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
*/
Expand Down Expand Up @@ -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 = '<div class="h-card"><abbr class="u-photo">no title attribute</abbr></div>';
$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
Expand Down Expand Up @@ -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 = '<div class="h-entry"><video class="u-video">no video support</video></div>';
$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
Expand Down

0 comments on commit 26e4ed4

Please sign in to comment.