Skip to content

Commit

Permalink
Add failing tests and fix for microformats/microformats2-parsing#6
Browse files Browse the repository at this point in the history
  • Loading branch information
gRegorLove committed Mar 4, 2018
1 parent 68b1715 commit c390d69
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 6 deletions.
21 changes: 15 additions & 6 deletions Mf2/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -920,9 +920,10 @@ private function removeTags(\DOMElement &$e, $tagName) {
*
* @param DOMElement $e The element to parse
* @param bool $is_backcompat Whether using backcompat parsing or not
* @param bool $has_nested_mf Whether this microformat has a nested microformat
* @return array A representation of the values contained within microformat $e
*/
public function parseH(\DOMElement $e, $is_backcompat = false) {
public function parseH(\DOMElement $e, $is_backcompat = false, $has_nested_mf = false) {
// If it’s already been parsed (e.g. is a child mf), skip
if ($this->parsed->contains($e)) {
return null;
Expand Down Expand Up @@ -958,6 +959,7 @@ public function parseH(\DOMElement $e, $is_backcompat = false) {
continue;
}

$prefixes[] = 'p-';
$pValue = $this->parseP($p);

// Add the value to the array for it’s p- properties
Expand All @@ -982,6 +984,7 @@ public function parseH(\DOMElement $e, $is_backcompat = false) {
continue;
}

$prefixes[] = 'u-';
$uValue = $this->parseU($u);

// Add the value to the array for it’s property types
Expand All @@ -1006,6 +1009,7 @@ public function parseH(\DOMElement $e, $is_backcompat = false) {
continue;
}

$prefixes[] = 'dt-';
$dtValue = $this->parseDT($dt, $dates, $impliedTimezone);

if ($dtValue) {
Expand Down Expand Up @@ -1040,6 +1044,7 @@ public function parseH(\DOMElement $e, $is_backcompat = false) {
continue;
}

$prefixes[] = 'e-';
$eValue = $this->parseE($em);

if ($eValue) {
Expand All @@ -1052,9 +1057,10 @@ public function parseH(\DOMElement $e, $is_backcompat = false) {
$this->elementPrefixParsed($em, 'e');
}

// Implied Properties
// Check for p-name
if (!array_key_exists('name', $return) && !$is_backcompat) {
// Imply 'name' only under specific conditions
$imply_name = (!$has_nested_mf && !array_key_exists('name', $return) && !$is_backcompat && !in_array('p-', $prefixes) && !in_array('e-', $prefixes));

if ($imply_name) {
try {
// Look for img @alt
if (($e->tagName == 'img' or $e->tagName == 'area') and $e->getAttribute('alt') != '') {
Expand Down Expand Up @@ -1337,7 +1343,7 @@ public function parse($convertClassic = true, DOMElement $context = null) {
* Parse microformats recursively
* Keeps track of whether inside a backcompat root or not
* @param DOMElement $context: node to start with
* @param int $depth: recusion depth
* @param int $depth: recursion depth
* @return array
*/
public function parse_recursive(DOMElement $context = null, $depth = 0) {
Expand Down Expand Up @@ -1380,8 +1386,11 @@ public function parse_recursive(DOMElement $context = null, $depth = 0) {

}

// set bool flag for nested mf
$has_nested_mf = ($children || $merge_properties);

// parse for root mf
$result = $this->parseH($node, $is_backcompat);
$result = $this->parseH($node, $is_backcompat, $has_nested_mf);

// merge nested mf properties
if ( $merge_properties && isset($result['properties']) ) {
Expand Down
55 changes: 55 additions & 0 deletions tests/Mf2/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -540,4 +540,59 @@ public function testClassNameNumbers() {
$this->assertArrayNotHasKey('column1', $output['items'][0]['properties']);
}

/**
* @see https://github.com/microformats/microformats2-parsing/issues/6#issuecomment-366473390
*/
public function testNoImpliedNameWhenE()
{
$input = '<article class="h-entry">
<div class="e-content">
<p>Wanted content.</p>
</div>
<footer>
<p>Footer to be ignored.</p>
</footer>
</article>';
$output = Mf2\parse($input);

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

/**
* @see https://github.com/microformats/microformats2-parsing/issues/6#issuecomment-366473390
*/
public function testNoImpliedNameWhenP()
{
$input = '<article class="h-entry">
<div class="p-content">
<p>Wanted content.</p>
</div>
<footer>
<p>Footer to be ignored.</p>
</footer>
</article>';
$output = Mf2\parse($input);

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

/**
* @see https://github.com/microformats/microformats2-parsing/issues/6#issuecomment-366473390
*/
public function testNoImpliedNameWhenNestedMicroformat()
{
$input = '<article class="h-entry">
<div class="u-like-of h-cite">
<p>I really like <a class="p-name u-url" href="http://microformats.org/">Microformats</a></p>
</div>
<footer>
<p>Footer to be ignored.</p>
</footer>
</article>';
$output = Mf2\parse($input);

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

}

0 comments on commit c390d69

Please sign in to comment.