From c390d69c0b0b3ac9f660236957f4016da359c015 Mon Sep 17 00:00:00 2001 From: Gregor Morrill Date: Sun, 4 Mar 2018 13:42:16 -0800 Subject: [PATCH 1/3] Add failing tests and fix for https://github.com/microformats/microformats2-parsing/issues/6 --- Mf2/Parser.php | 21 ++++++++++----- tests/Mf2/ParserTest.php | 55 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 6 deletions(-) diff --git a/Mf2/Parser.php b/Mf2/Parser.php index 0c30e88..b794661 100644 --- a/Mf2/Parser.php +++ b/Mf2/Parser.php @@ -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; @@ -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 @@ -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 @@ -1006,6 +1009,7 @@ public function parseH(\DOMElement $e, $is_backcompat = false) { continue; } + $prefixes[] = 'dt-'; $dtValue = $this->parseDT($dt, $dates, $impliedTimezone); if ($dtValue) { @@ -1040,6 +1044,7 @@ public function parseH(\DOMElement $e, $is_backcompat = false) { continue; } + $prefixes[] = 'e-'; $eValue = $this->parseE($em); if ($eValue) { @@ -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') != '') { @@ -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) { @@ -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']) ) { diff --git a/tests/Mf2/ParserTest.php b/tests/Mf2/ParserTest.php index 904866a..ac97e15 100644 --- a/tests/Mf2/ParserTest.php +++ b/tests/Mf2/ParserTest.php @@ -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 = '
+
+

Wanted content.

+
+
+

Footer to be ignored.

+
+
'; + $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 = '
+
+

Wanted content.

+
+
+

Footer to be ignored.

+
+
'; + $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 = '
+
+

I really like Microformats

+
+
+

Footer to be ignored.

+
+
'; + $output = Mf2\parse($input); + + $this->assertArrayNotHasKey('name', $output['items'][0]['properties']); + } + } + From eb66e20d099b612dd34d932c9bf6595210c416c4 Mon Sep 17 00:00:00 2001 From: Gregor Morrill Date: Sun, 4 Mar 2018 13:46:08 -0800 Subject: [PATCH 2/3] Update existing tests where name is no longer implied --- tests/Mf2/ClassicMicroformatsTest.php | 4 +++- tests/Mf2/ParsePTest.php | 4 +++- tests/Mf2/ParserTest.php | 8 ++++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/Mf2/ClassicMicroformatsTest.php b/tests/Mf2/ClassicMicroformatsTest.php index 8ca965e..b785731 100644 --- a/tests/Mf2/ClassicMicroformatsTest.php +++ b/tests/Mf2/ClassicMicroformatsTest.php @@ -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]); diff --git a/tests/Mf2/ParsePTest.php b/tests/Mf2/ParsePTest.php index 55f4a98..3ee9174 100644 --- a/tests/Mf2/ParsePTest.php +++ b/tests/Mf2/ParsePTest.php @@ -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']); } /** diff --git a/tests/Mf2/ParserTest.php b/tests/Mf2/ParserTest.php index ac97e15..9ca98d4 100644 --- a/tests/Mf2/ParserTest.php +++ b/tests/Mf2/ParserTest.php @@ -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']); @@ -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']); } From 64ed8445dee0f0cde8fe9ed868af5fb0d4f59223 Mon Sep 17 00:00:00 2001 From: Gregor Morrill Date: Sun, 4 Mar 2018 14:09:59 -0800 Subject: [PATCH 3/3] Use empty string for 'value' when microformat has no 'name' --- Mf2/Parser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Mf2/Parser.php b/Mf2/Parser.php index b794661..09fa92f 100644 --- a/Mf2/Parser.php +++ b/Mf2/Parser.php @@ -1411,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'];