Skip to content

Commit fb6841d

Browse files
authored
Merge pull request #142 from gRegorLove/parsing-issue6
Reduce instances when p-name is implied
2 parents 68b1715 + 64ed844 commit fb6841d

File tree

4 files changed

+83
-11
lines changed

4 files changed

+83
-11
lines changed

Mf2/Parser.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -920,9 +920,10 @@ private function removeTags(\DOMElement &$e, $tagName) {
920920
*
921921
* @param DOMElement $e The element to parse
922922
* @param bool $is_backcompat Whether using backcompat parsing or not
923+
* @param bool $has_nested_mf Whether this microformat has a nested microformat
923924
* @return array A representation of the values contained within microformat $e
924925
*/
925-
public function parseH(\DOMElement $e, $is_backcompat = false) {
926+
public function parseH(\DOMElement $e, $is_backcompat = false, $has_nested_mf = false) {
926927
// If it’s already been parsed (e.g. is a child mf), skip
927928
if ($this->parsed->contains($e)) {
928929
return null;
@@ -958,6 +959,7 @@ public function parseH(\DOMElement $e, $is_backcompat = false) {
958959
continue;
959960
}
960961

962+
$prefixes[] = 'p-';
961963
$pValue = $this->parseP($p);
962964

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

987+
$prefixes[] = 'u-';
985988
$uValue = $this->parseU($u);
986989

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

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

10111015
if ($dtValue) {
@@ -1040,6 +1044,7 @@ public function parseH(\DOMElement $e, $is_backcompat = false) {
10401044
continue;
10411045
}
10421046

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

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

1055-
// Implied Properties
1056-
// Check for p-name
1057-
if (!array_key_exists('name', $return) && !$is_backcompat) {
1060+
// Imply 'name' only under specific conditions
1061+
$imply_name = (!$has_nested_mf && !array_key_exists('name', $return) && !$is_backcompat && !in_array('p-', $prefixes) && !in_array('e-', $prefixes));
1062+
1063+
if ($imply_name) {
10581064
try {
10591065
// Look for img @alt
10601066
if (($e->tagName == 'img' or $e->tagName == 'area') and $e->getAttribute('alt') != '') {
@@ -1337,7 +1343,7 @@ public function parse($convertClassic = true, DOMElement $context = null) {
13371343
* Parse microformats recursively
13381344
* Keeps track of whether inside a backcompat root or not
13391345
* @param DOMElement $context: node to start with
1340-
* @param int $depth: recusion depth
1346+
* @param int $depth: recursion depth
13411347
* @return array
13421348
*/
13431349
public function parse_recursive(DOMElement $context = null, $depth = 0) {
@@ -1380,8 +1386,11 @@ public function parse_recursive(DOMElement $context = null, $depth = 0) {
13801386

13811387
}
13821388

1389+
// set bool flag for nested mf
1390+
$has_nested_mf = ($children || $merge_properties);
1391+
13831392
// parse for root mf
1384-
$result = $this->parseH($node, $is_backcompat);
1393+
$result = $this->parseH($node, $is_backcompat, $has_nested_mf);
13851394

13861395
// merge nested mf properties
13871396
if ( $merge_properties && isset($result['properties']) ) {
@@ -1402,7 +1411,7 @@ public function parse_recursive(DOMElement $context = null, $depth = 0) {
14021411
// Note: handling microformat nesting under multiple conflicting prefixes is not currently specified by the mf2 parsing spec.
14031412
$prefixSpecificResult = $result;
14041413
if (in_array('p-', $prefixes)) {
1405-
$prefixSpecificResult['value'] = $prefixSpecificResult['properties']['name'][0];
1414+
$prefixSpecificResult['value'] = (empty($prefixSpecificResult['properties']['name'][0])) ? '' : $prefixSpecificResult['properties']['name'][0];
14061415
} elseif (in_array('e-', $prefixes)) {
14071416
$eParsedResult = $this->parseE($node);
14081417
$prefixSpecificResult['html'] = $eParsedResult['html'];

tests/Mf2/ClassicMicroformatsTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,9 @@ public function testMixedMf2andMf1Case4() {
727727
$parser = new Parser($input);
728728
$result = $parser->parse();
729729

730-
$this->assertCount(1, $result['items'][0]['properties']);
730+
// p-name is no longer implied for this test due to nested microformat
731+
// see https://github.com/microformats/microformats2-parsing/issues/6
732+
$this->assertArrayNotHasKey('name', $result['items'][0]['properties']);
731733
$this->assertArrayNotHasKey('content', $result['items'][0]['properties']);
732734
$this->assertArrayHasKey('children', $result['items'][0]);
733735
$this->assertEquals('h-feed', $result['items'][0]['children'][0]['type'][0]);

tests/Mf2/ParsePTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ public function testBrWhitespaceIssue69() {
124124
$this->assertEquals('Street Name 9' . "\n" . '12345 NY, USA', $result['items'][0]['properties']['adr'][0]);
125125
$this->assertEquals('Street Name 9', $result['items'][0]['properties']['street-address'][0]);
126126
$this->assertEquals('12345 NY, USA', $result['items'][0]['properties']['locality'][0]);
127-
$this->assertEquals('Street Name 9' . "\n" . '12345 NY, USA', $result['items'][0]['properties']['name'][0]);
127+
// p-name is no longer implied for this test due to other p-*
128+
// see https://github.com/microformats/microformats2-parsing/issues/6
129+
$this->assertArrayNotHasKey('name', $result['items'][0]['properties']);
128130
}
129131

130132
/**

tests/Mf2/ParserTest.php

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,9 @@ public function testAreaTag() {
311311
$parser = new Parser($input);
312312
$output = $parser->parse();
313313

314-
$this->assertEquals('Person Bee', $output['items'][0]['properties']['name'][0]);
314+
// p-name is no longer implied for this test due to nested microformat
315+
// see https://github.com/microformats/microformats2-parsing/issues/6
316+
$this->assertArrayNotHasKey('name', $output['items'][0]['properties']);
315317
$this->assertEquals('rect', $output['items'][0]['properties']['category'][0]['shape']);
316318
$this->assertEquals('100,100,120,120', $output['items'][0]['properties']['category'][0]['coords']);
317319
$this->assertEquals('Person Bee', $output['items'][0]['properties']['category'][0]['value']);
@@ -435,7 +437,9 @@ public function testWhitespaceBetweenElements() {
435437
$output = $parser->parse();
436438

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

441445

@@ -540,4 +544,59 @@ public function testClassNameNumbers() {
540544
$this->assertArrayNotHasKey('column1', $output['items'][0]['properties']);
541545
}
542546

547+
/**
548+
* @see https://github.com/microformats/microformats2-parsing/issues/6#issuecomment-366473390
549+
*/
550+
public function testNoImpliedNameWhenE()
551+
{
552+
$input = '<article class="h-entry">
553+
<div class="e-content">
554+
<p>Wanted content.</p>
555+
</div>
556+
<footer>
557+
<p>Footer to be ignored.</p>
558+
</footer>
559+
</article>';
560+
$output = Mf2\parse($input);
561+
562+
$this->assertArrayNotHasKey('name', $output['items'][0]['properties']);
563+
}
564+
565+
/**
566+
* @see https://github.com/microformats/microformats2-parsing/issues/6#issuecomment-366473390
567+
*/
568+
public function testNoImpliedNameWhenP()
569+
{
570+
$input = '<article class="h-entry">
571+
<div class="p-content">
572+
<p>Wanted content.</p>
573+
</div>
574+
<footer>
575+
<p>Footer to be ignored.</p>
576+
</footer>
577+
</article>';
578+
$output = Mf2\parse($input);
579+
580+
$this->assertArrayNotHasKey('name', $output['items'][0]['properties']);
581+
}
582+
583+
/**
584+
* @see https://github.com/microformats/microformats2-parsing/issues/6#issuecomment-366473390
585+
*/
586+
public function testNoImpliedNameWhenNestedMicroformat()
587+
{
588+
$input = '<article class="h-entry">
589+
<div class="u-like-of h-cite">
590+
<p>I really like <a class="p-name u-url" href="http://microformats.org/">Microformats</a></p>
591+
</div>
592+
<footer>
593+
<p>Footer to be ignored.</p>
594+
</footer>
595+
</article>';
596+
$output = Mf2\parse($input);
597+
598+
$this->assertArrayNotHasKey('name', $output['items'][0]['properties']);
599+
}
600+
543601
}
602+

0 commit comments

Comments
 (0)