Skip to content

Commit

Permalink
Merge branch 'dissolve-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
barnabywalters committed Apr 29, 2015
2 parents 3cd6ef2 + d05e131 commit e208e99
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 25 deletions.
91 changes: 69 additions & 22 deletions Mf2/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ function mfNamesFromClass($class, $prefix='h-') {
$matches = array();

foreach ($classes as $classname) {
$compare_classname = strtolower(' ' . $classname);
$compare_prefix = strtolower(' ' . $prefix);
if (stristr($compare_classname, $compare_prefix) !== false && ($compare_classname != $compare_prefix)) {
$compare_classname = ' ' . $classname;
$compare_prefix = ' ' . $prefix;
if (strstr($compare_classname, $compare_prefix) !== false && ($compare_classname != $compare_prefix)) {
$matches[] = ($prefix === 'h-') ? $classname : substr($classname, strlen($prefix));
}
}
Expand Down Expand Up @@ -192,28 +192,27 @@ function convertTimeFormat($time) {
$hh = $mm = $ss = '';
preg_match('/(\d{1,2}):?(\d{2})?:?(\d{2})?(a\.?m\.?|p\.?m\.?)?/i', $time, $matches);

// if no am/pm specified
// If no am/pm is specified:
if (empty($matches[4])) {
return $time;
}
// else am/pm specified
else {
} else {
// Otherwise, am/pm is specified.
$meridiem = strtolower(str_replace('.', '', $matches[4]));

// hours
// Hours.
$hh = $matches[1];

// add 12 to the pm hours
// Add 12 to hours if pm applies.
if ($meridiem == 'pm' && ($hh < 12)) {
$hh += 12;
}

$hh = str_pad($hh, 2, '0', STR_PAD_LEFT);

// minutes
// Minutes.
$mm = (empty($matches[2]) ) ? '00' : $matches[2];

// seconds, only if supplied
// Seconds, only if supplied.
if (!empty($matches[3])) {
$ss = $matches[3];
}
Expand Down Expand Up @@ -591,16 +590,16 @@ public function parseDT(\DOMElement $dt, &$dates = array()) {
$dtValue = $dt->nodeValue;
}

if ( preg_match('/(\d{4}-\d{2}-\d{2})/', $dtValue, $matches) ) {
if (preg_match('/(\d{4}-\d{2}-\d{2})/', $dtValue, $matches)) {
$dates[] = $matches[0];
}
}

/**
* if $dtValue is only a time and there are recently parsed dates,
* form the full date-time using the most recnetly parsed dt- value
* form the full date-time using the most recently parsed dt- value
*/
if ( (preg_match('/^\d{1,2}:\d{1,2}(Z?[+|-]\d{2}:?\d{2})?/', $dtValue) or preg_match('/^\d{1,2}[a|p]m/', $dtValue)) && !empty($dates) ) {
if ((preg_match('/^\d{1,2}:\d{1,2}(Z?[+|-]\d{2}:?\d{2})?/', $dtValue) or preg_match('/^\d{1,2}[a|p]m/', $dtValue)) && !empty($dates)) {
$dtValue = convertTimeFormat($dtValue);
$dtValue = end($dates) . 'T' . unicodeTrim($dtValue, 'T');
}
Expand Down Expand Up @@ -689,6 +688,11 @@ public function parseH(\DOMElement $e) {
$this->elementPrefixParsed($subMF, 'e');
}

if($e->tagName == 'area') {
$coords = $e->getAttribute('coords');
$shape = $e->getAttribute('shape');
}

// Handle p-*
foreach ($this->xpath->query('.//*[contains(concat(" ", @class) ," p-")]', $e) as $p) {
if ($this->isElementParsed($p, 'p'))
Expand Down Expand Up @@ -762,22 +766,43 @@ public function parseH(\DOMElement $e) {
if (!array_key_exists('name', $return)) {
try {
// Look for img @alt
if ($e->tagName == 'img' and $e->getAttribute('alt') != '')
if (($e->tagName == 'img' or $e->tagName == 'area') and $e->getAttribute('alt') != '')
throw new Exception($e->getAttribute('alt'));

if ($e->tagName == 'abbr' and $e->hasAttribute('title'))
throw new Exception($e->getAttribute('title'));

// Look for nested img @alt
foreach ($this->xpath->query('./img[count(preceding-sibling::*)+count(following-sibling::*)=0]', $e) as $em) {
if ($em->getAttribute('alt') != '')
$emNames = mfNamesFromElement($em, 'h-');
if (empty($emNames) && $em->getAttribute('alt') != '') {
throw new Exception($em->getAttribute('alt'));
}
}

// Look for nested area @alt
foreach ($this->xpath->query('./area[count(preceding-sibling::*)+count(following-sibling::*)=0]', $e) as $em) {
$emNames = mfNamesFromElement($em, 'h-');
if (empty($emNames) && $em->getAttribute('alt') != '') {
throw new Exception($em->getAttribute('alt'));
}
}


// Look for double nested img @alt
foreach ($this->xpath->query('./*[count(preceding-sibling::*)+count(following-sibling::*)=0]/img[count(preceding-sibling::*)+count(following-sibling::*)=0]', $e) as $em) {
if ($em->getAttribute('alt') != '')
$emNames = mfNamesFromElement($em, 'h-');
if (empty($emNames) && $em->getAttribute('alt') != '') {
throw new Exception($em->getAttribute('alt'));
}
}

// Look for double nested img @alt
foreach ($this->xpath->query('./*[count(preceding-sibling::*)+count(following-sibling::*)=0]/area[count(preceding-sibling::*)+count(following-sibling::*)=0]', $e) as $em) {
$emNames = mfNamesFromElement($em, 'h-');
if (empty($emNames) && $em->getAttribute('alt') != '') {
throw new Exception($em->getAttribute('alt'));
}
}

throw new Exception($e->nodeValue);
Expand Down Expand Up @@ -812,13 +837,25 @@ public function parseH(\DOMElement $e) {
// Check for u-url
if (!array_key_exists('url', $return)) {
// Look for img @src
if ($e->tagName == 'a')
if ($e->tagName == 'a' or $e->tagName == 'area')
$url = $e->getAttribute('href');

// Look for nested img @src
// Look for nested a @href
foreach ($this->xpath->query('./a[count(preceding-sibling::a)+count(following-sibling::a)=0]', $e) as $em) {
$url = $em->getAttribute('href');
break;
$emNames = mfNamesFromElement($em, 'h-');
if (empty($emNames)) {
$url = $em->getAttribute('href');
break;
}
}

// Look for nested area @src
foreach ($this->xpath->query('./area[count(preceding-sibling::area)+count(following-sibling::area)=0]', $e) as $em) {
$emNames = mfNamesFromElement($em, 'h-');
if (empty($emNames)) {
$url = $em->getAttribute('href');
break;
}
}

if (!empty($url))
Expand All @@ -833,8 +870,18 @@ public function parseH(\DOMElement $e) {
'type' => $mfTypes,
'properties' => $return
);
if (!empty($children))

if (!empty($shape)) {
$parsed['shape'] = $shape;
}

if (!empty($coords)) {
$parsed['coords'] = $coords;
}

if (!empty($children)) {
$parsed['children'] = array_values(array_filter($children));
}
return $parsed;
}

Expand Down
6 changes: 3 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0"?>
<phpunit bootstrap="tests/mf2/bootstrap.php">
<phpunit bootstrap="tests/Mf2/bootstrap.php">
<testsuites>
<testsuite name="php-mf2">
<directory suffix="Test.php">tests/mf2</directory>
<directory suffix="Test.php">tests/Mf2</directory>
</testsuite>
</testsuites>
</phpunit>
</phpunit>
54 changes: 54 additions & 0 deletions tests/Mf2/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public function testHtmlEncodesNonEProperties() {
$this->assertEquals('<dt>', $output['items'][0]['properties']['published'][0]);
$this->assertEquals('<u>', $output['items'][0]['properties']['url'][0]);
}


public function testHtmlEncodesImpliedProperties() {
$input = '<a class="h-card" href="&lt;url&gt;"><img src="&lt;img&gt;" />&lt;name&gt;</a>';
Expand Down Expand Up @@ -257,4 +258,57 @@ public function testConvertsNestedImgElementToAltOrSrc() {
$result = Mf2\parse($input, 'http://waterpigs.co.uk/articles/five-legged-elephant');
$this->assertEquals('It is a strange thing to see a five legged elephant', $result['items'][0]['properties']['content'][0]['value']);
}

// parser not respecting not[h-*] in rule "else if .h-x>a[href]:only-of-type:not[.h-*] then use that [href] for url"
public function testNotImpliedUrlFromHCard() {
$input = <<<EOT
<span class="h-entry">
<a class="h-card" href="http://test.com">John Q</a>
</span>
EOT;

$parser = new Parser($input);
$output = $parser->parse();

$this->assertArrayNotHasKey('url', $output['items'][0]['properties']);
}

public function testAreaTag() {
$input = <<<EOT
<div class="h-entry">
<area class="p-category h-card" href="http://personB.example.com" alt="Person Bee" shape="rect" coords="100,100,120,120">
</div>
EOT;

$parser = new Parser($input);
$output = $parser->parse();

$this->assertEquals('', $output['items'][0]['properties']['name'][0]);
$this->assertEquals('rect', $output['items'][0]['properties']['category'][0]['shape']);
$this->assertEquals('100,100,120,120', $output['items'][0]['properties']['category'][0]['coords']);
$this->assertEquals('Person Bee', $output['items'][0]['properties']['category'][0]['value']);
}

public function testParseHcardInCategory() {
$input = <<<EOT
<span class="h-entry">
<a class="p-author h-card" href="http://a.example.com/">Alice</a> tagged
<a href="http://b.example.com/" class="u-category h-card">Bob Smith</a> in
<a class="u-tag-of u-in-reply-to" href="http://s.example.com/permalink47">
<img src="http://s.example.com/photo47.png" alt="a photo of Bob and Cole" />
</a>
</span>
EOT;

$parser = new Parser($input);
$output = $parser->parse();

$this->assertContains('h-entry', $output['items'][0]['type']);
$this->assertArrayHasKey('category', $output['items'][0]['properties']);
$this->assertContains('h-card', $output['items'][0]['properties']['category'][0]['type']);
$this->assertArrayHasKey('name', $output['items'][0]['properties']['category'][0]['properties']);
$this->assertEquals('Bob Smith', $output['items'][0]['properties']['category'][0]['properties']['name'][0]);
$this->assertArrayHasKey('url', $output['items'][0]['properties']['category'][0]['properties']);
$this->assertEquals('http://b.example.com/', $output['items'][0]['properties']['category'][0]['properties']['url'][0]);
}
}

0 comments on commit e208e99

Please sign in to comment.