Skip to content

Commit

Permalink
Corrected handling of value and html keys of property-nested microfor…
Browse files Browse the repository at this point in the history
…mat structures

Fixes #64. As per #64, microformats nested under e-* properties dropped their
html keys. When looking into fixing this, I found several other nested cases where nested mf structure value keys
were being incorrectly assigned. This commit fixes the handling of microformats nested under p-, e- and u- properties,
with associated tests.
  • Loading branch information
barnabywalters committed Jul 12, 2015
1 parent 7510c90 commit 6701504
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
30 changes: 24 additions & 6 deletions Mf2/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,17 @@ function nestedMfPropertyNamesFromClass($class) {
$class = str_replace(array(' ', ' ', "\n"), ' ', $class);
foreach (explode(' ', $class) as $classname) {
foreach ($prefixes as $prefix) {
$compare_classname = strtolower(' ' . $classname);
if (stristr($compare_classname, $prefix) && ($compare_classname != $prefix)) {
$propertyNames = array_merge($propertyNames, mfNamesFromClass($classname, ltrim($prefix)));
// Check if $classname is a valid property classname for $prefix.
if (mb_substr($classname, 0, mb_strlen($prefix)) == $prefix && $classname != $prefix) {
$propertyName = mb_substr($classname, mb_strlen($prefix));
$propertyNames[$propertyName][] = $prefix;
}
}
}

foreach ($propertyNames as $property => $prefixes) {
$propertyNames[$property] = array_unique($prefixes);
}

return $propertyNames;
}
Expand Down Expand Up @@ -663,16 +668,29 @@ public function parseH(\DOMElement $e) {
// If result was already parsed, skip it
if (null === $result)
continue;


// In most cases, the value attribute of the nested microformat should be the p- parsed value of the elemnt.
// The only times this is different is when the microformat is nested under certain prefixes, which are handled below.
$result['value'] = $this->parseP($subMF);

// Does this µf have any property names other than h-*?
$properties = nestedMfPropertyNamesFromElement($subMF);

if (!empty($properties)) {
// Yes! It’s a nested property µf
foreach ($properties as $property) {
$return[$property][] = $result;
foreach ($properties as $property => $prefixes) {
// Note: handling microformat nesting under multiple conflicting prefixes is not currently specified by the mf2 parsing spec.
$prefixSpecificResult = $result;
if (in_array('p-', $prefixes)) {
$prefixSpecificResult['value'] = $prefixSpecificResult['properties']['name'][0];
} elseif (in_array('e-', $prefixes)) {
$eParsedResult = $this->parseE($subMF);
$prefixSpecificResult['html'] = $eParsedResult['html'];
$prefixSpecificResult['value'] = $eParsedResult['value'];
} elseif (in_array('u-', $prefixes)) {
$prefixSpecificResult['value'] = $this->parseU($subMF);
}
$return[$property][] = $prefixSpecificResult;
}
} else {
// No, it’s a child µf
Expand Down
31 changes: 30 additions & 1 deletion tests/Mf2/CombinedMicroformatsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testHEventLocationHCard() {
"start": ["2012-06-30"],
"end": ["2012-07-01"],
"location": [{
"value": "Geoloqi, 920 SW 3rd Ave. Suite 400, Portland, OR",
"value": "Geoloqi",
"type": ["h-card"],
"properties": {
"name": ["Geoloqi"],
Expand Down Expand Up @@ -220,4 +220,33 @@ public function testNestedMicroformatUnderMultipleProperties() {
$this->assertCount(1, $mf['items'][0]['properties']['like-of']);
$this->assertCount(1, $mf['items'][0]['properties']['in-reply-to']);
}

/**
* Test microformats nested under e-* property classnames retain html: key in structure
*
* @see https://github.com/indieweb/php-mf2/issues/64
*/
public function testMicroformatsNestedUnderEPropertyClassnamesRetainHtmlKey() {
$input = '<div class="h-entry"><div class="h-card e-content"><p>Hello</p></div></div>';
$mf = Mf2\parse($input);

$this->assertEquals($mf['items'][0]['properties']['content'][0]['html'], '<p>Hello</p>');
}

/**
* Test microformats nested under u-* property classnames derive value: key from parsing as u-*
*/
public function testMicroformatsNestedUnderUPropertyClassnamesDeriveValueCorrectly() {
$input = '<div class="h-entry"><img class="u-url h-card" alt="This should not be the value" src="This should be the value" /></div>';
$mf = Mf2\parse($input);

$this->assertEquals($mf['items'][0]['properties']['url'][0]['value'], 'This should be the value');
}

public function testMicroformatsNestedUnderPPropertyClassnamesDeriveValueFromFirstPName() {
$input = '<div class="h-entry"><div class="p-author h-card">This post was written by <span class="p-name">Zoe</span>.</div></div>';
$mf = Mf2\parse($input);

$this->assertEquals($mf['items'][0]['properties']['author'][0]['value'], 'Zoe');
}
}
4 changes: 2 additions & 2 deletions tests/Mf2/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public function testMicroformatStripsPrefixFromPropertyClassname() {
}

public function testNestedMicroformatPropertyNameWorks() {
$expected = array('location', 'author');
$test = 'someclass p-location someotherclass u-author';
$expected = array('location' => array('p-'), 'author' => array('u-', 'p-'));
$test = 'someclass p-location someotherclass u-author p-author';
$actual = Mf2\nestedMfPropertyNamesFromClass($test);

$this->assertEquals($expected, $actual);
Expand Down

0 comments on commit 6701504

Please sign in to comment.