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

check that properties is an array before accessing #197

Merged
merged 3 commits into from
Aug 23, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Mf2/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 : '';
Expand Down
17 changes: 17 additions & 0 deletions tests/Mf2/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
<div class="h-entry">
<li class="h-cite u-comment">
<div class="vcard"></div>
</li>
</div>
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']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor update: can use assertEmpty() for these.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does assertEmpty do if the value is null or not an array? I want to ensure that properties exists and is an array with no items.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think null value would pass the assertion; not sure about an empty object. assertInternalType can be used to verify it's an array.

}
}