From 44201e788c92e306937f3c59d9f8d81cb6348826 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Thu, 23 Aug 2018 12:36:20 -0700 Subject: [PATCH 1/2] check that properties is an array before accessing fixes #196 --- Mf2/Parser.php | 4 ++-- tests/Mf2/ParserTest.php | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Mf2/Parser.php b/Mf2/Parser.php index 834e0a5..48cf24e 100644 --- a/Mf2/Parser.php +++ b/Mf2/Parser.php @@ -1390,13 +1390,13 @@ 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'] = (empty($prefixSpecificResult['properties']['name'][0])) ? $this->parseP($node) : $prefixSpecificResult['properties']['name'][0]; + $prefixSpecificResult['value'] = (!is_array($prefixSpecificResult['properties']) || empty($prefixSpecificResult['properties']['name'][0])) ? $this->parseP($node) : $prefixSpecificResult['properties']['name'][0]; } elseif (in_array('e-', $prefixes)) { $eParsedResult = $this->parseE($node); $prefixSpecificResult['html'] = $eParsedResult['html']; $prefixSpecificResult['value'] = $eParsedResult['value']; } elseif (in_array('u-', $prefixes)) { - $prefixSpecificResult['value'] = (empty($result['properties']['url'])) ? $this->parseU($node) : reset($result['properties']['url']); + $prefixSpecificResult['value'] = (!is_array($result['properties']) || empty($result['properties']['url'])) ? $this->parseU($node) : reset($result['properties']['url']); } elseif (in_array('dt-', $prefixes)) { $parsed_property = $this->parseDT($node); $prefixSpecificResult['value'] = ($parsed_property) ? $parsed_property : ''; diff --git a/tests/Mf2/ParserTest.php b/tests/Mf2/ParserTest.php index 90f3f89..bf194ad 100644 --- a/tests/Mf2/ParserTest.php +++ b/tests/Mf2/ParserTest.php @@ -824,5 +824,22 @@ public function testNotMutatingPassedInDOM() { Mf2\parse($inputDoc, 'http://snarfed.org/2013-10-23_oauth-dropins'); $this->assertEquals($refDoc, $inputDoc, 'Parsing mutated the DOMDocument.'); } + + public function testNoImpliedURLForEmptyProperties() { + // In the 0.4.5 release, this caused an error + // https://github.com/microformats/php-mf2/issues/196 + + $input = << +
  • +
    +
  • + +EOD; + + $output = Mf2\parse($input); + $this->assertEquals([], $output['items'][0]['properties']['comment'][0]['properties']); + $this->assertEquals([], $output['items'][0]['properties']['comment'][0]['children'][0]['properties']); + } } From 500445d7b3badfc9245505b7dbd3198407785de9 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Thu, 23 Aug 2018 14:31:26 -0700 Subject: [PATCH 2/2] update assertions --- tests/Mf2/ParserTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/Mf2/ParserTest.php b/tests/Mf2/ParserTest.php index 67f9d9c..f30e2bf 100644 --- a/tests/Mf2/ParserTest.php +++ b/tests/Mf2/ParserTest.php @@ -838,8 +838,10 @@ public function testNoImpliedURLForEmptyProperties() { EOD; $output = Mf2\parse($input); - $this->assertEquals([], $output['items'][0]['properties']['comment'][0]['properties']); - $this->assertEquals([], $output['items'][0]['properties']['comment'][0]['children'][0]['properties']); + $this->assertInternalType('array', $output['items'][0]['properties']['comment'][0]['properties']); + $this->assertInternalType('array', $output['items'][0]['properties']['comment'][0]['children'][0]['properties']); + $this->assertEmpty($output['items'][0]['properties']['comment'][0]['properties']); + $this->assertEmpty($output['items'][0]['properties']['comment'][0]['children'][0]['properties']); } /**