From c390d69c0b0b3ac9f660236957f4016da359c015 Mon Sep 17 00:00:00 2001 From: Gregor Morrill Date: Sun, 4 Mar 2018 13:42:16 -0800 Subject: [PATCH] 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

+
+ +
'; + $output = Mf2\parse($input); + + $this->assertArrayNotHasKey('name', $output['items'][0]['properties']); + } + } +