Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce instances when p-name is implied #142

Merged
merged 3 commits into from
Mar 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 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 All @@ -1402,7 +1411,7 @@ public function parse_recursive(DOMElement $context = null, $depth = 0) {
// 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];
$prefixSpecificResult['value'] = (empty($prefixSpecificResult['properties']['name'][0])) ? '' : $prefixSpecificResult['properties']['name'][0];
} elseif (in_array('e-', $prefixes)) {
$eParsedResult = $this->parseE($node);
$prefixSpecificResult['html'] = $eParsedResult['html'];
Expand Down
4 changes: 3 additions & 1 deletion tests/Mf2/ClassicMicroformatsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,9 @@ public function testMixedMf2andMf1Case4() {
$parser = new Parser($input);
$result = $parser->parse();

$this->assertCount(1, $result['items'][0]['properties']);
// p-name is no longer implied for this test due to nested microformat
// see https://github.com/microformats/microformats2-parsing/issues/6
$this->assertArrayNotHasKey('name', $result['items'][0]['properties']);
$this->assertArrayNotHasKey('content', $result['items'][0]['properties']);
$this->assertArrayHasKey('children', $result['items'][0]);
$this->assertEquals('h-feed', $result['items'][0]['children'][0]['type'][0]);
Expand Down
4 changes: 3 additions & 1 deletion tests/Mf2/ParsePTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ public function testBrWhitespaceIssue69() {
$this->assertEquals('Street Name 9' . "\n" . '12345 NY, USA', $result['items'][0]['properties']['adr'][0]);
$this->assertEquals('Street Name 9', $result['items'][0]['properties']['street-address'][0]);
$this->assertEquals('12345 NY, USA', $result['items'][0]['properties']['locality'][0]);
$this->assertEquals('Street Name 9' . "\n" . '12345 NY, USA', $result['items'][0]['properties']['name'][0]);
// p-name is no longer implied for this test due to other p-*
// see https://github.com/microformats/microformats2-parsing/issues/6
$this->assertArrayNotHasKey('name', $result['items'][0]['properties']);
}

/**
Expand Down
63 changes: 61 additions & 2 deletions tests/Mf2/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,9 @@ public function testAreaTag() {
$parser = new Parser($input);
$output = $parser->parse();

$this->assertEquals('Person Bee', $output['items'][0]['properties']['name'][0]);
// p-name is no longer implied for this test due to nested microformat
// see https://github.com/microformats/microformats2-parsing/issues/6
$this->assertArrayNotHasKey('name', $output['items'][0]['properties']);
$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']);
Expand Down Expand Up @@ -435,7 +437,9 @@ public function testWhitespaceBetweenElements() {
$output = $parser->parse();

$this->assertContains('h-entry', $output['items'][0]['type']);
$this->assertNotContains('attendingHomebrew', $output['items'][0]['properties']['name'][0]);
// p-name is no longer implied for this test due to other p-*
// see https://github.com/microformats/microformats2-parsing/issues/6
$this->assertArrayNotHasKey('name', $output['items'][0]['properties']);
}


Expand Down Expand Up @@ -540,4 +544,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']);
}

}