From 08952f780409e33d38df0a1ba660dc38420b1f65 Mon Sep 17 00:00:00 2001 From: Gregor Morrill Date: Wed, 14 Mar 2018 18:19:40 -0700 Subject: [PATCH] Add tests and fix for #151 --- Mf2/Parser.php | 5 +- tests/Mf2/CombinedMicroformatsTest.php | 120 ++++++++++++++++--------- 2 files changed, 83 insertions(+), 42 deletions(-) diff --git a/Mf2/Parser.php b/Mf2/Parser.php index cad95a1..4fa03cd 100644 --- a/Mf2/Parser.php +++ b/Mf2/Parser.php @@ -1398,13 +1398,16 @@ 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])) ? '' : $prefixSpecificResult['properties']['name'][0]; + $prefixSpecificResult['value'] = (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']); + } elseif (in_array('dt-', $prefixes)) { + $parsed_property = $this->parseDT($node); + $prefixSpecificResult['value'] = ($parsed_property) ? $parsed_property : ''; } $mfs['properties'][$property][] = $prefixSpecificResult; diff --git a/tests/Mf2/CombinedMicroformatsTest.php b/tests/Mf2/CombinedMicroformatsTest.php index 2b3a32b..33d8764 100644 --- a/tests/Mf2/CombinedMicroformatsTest.php +++ b/tests/Mf2/CombinedMicroformatsTest.php @@ -255,46 +255,46 @@ public function testMicroformatsNestedUnderUPropertyClassnamesDeriveValueFromURL

Name

Hello World

'; $expected = '{ "items": [{ - "type": ["h-entry"], - "properties": { - "name": ["Name"], - "content": [{ - "html": "Hello World", - "value": "Hello World" - }], - "comment": [{ - "type": ["h-cite"], - "properties": { - "author": [{ - "type": ["h-card"], - "properties": { - "name": ["Jane Bloggs"], - "url": ["http:\/\/jane.example.com\/"] - }, - "value": "http:\/\/jane.example.com\/" - }], - "content": ["lol"], - "name": ["lol"], - "url": ["http:\/\/example.org\/post1234"], - "published": ["2015-07-12 12:03"] - }, - "value": "http:\/\/example.org\/post1234" - }] - } - }], - "rels":[], - "rel-urls": [] + "type": ["h-entry"], + "properties": { + "name": ["Name"], + "content": [{ + "html": "Hello World", + "value": "Hello World" + }], + "comment": [{ + "type": ["h-cite"], + "properties": { + "author": [{ + "type": ["h-card"], + "properties": { + "name": ["Jane Bloggs"], + "url": ["http:\/\/jane.example.com\/"] + }, + "value": "http:\/\/jane.example.com\/" + }], + "content": ["lol"], + "name": ["lol"], + "url": ["http:\/\/example.org\/post1234"], + "published": ["2015-07-12 12:03"] + }, + "value": "http:\/\/example.org\/post1234" + }] + } + }], + "rels":[], + "rel-urls": [] }'; - $mf = Mf2\parse($input); + $mf = Mf2\parse($input); $this->assertJsonStringEqualsJsonString(json_encode($mf), $expected); $this->assertEquals($mf['items'][0]['properties']['comment'][0]['value'], 'http://example.org/post1234'); @@ -345,19 +345,57 @@ public function testNestedMf1() { } public function testNoUrlFromRelOnMf2() { - $input = <<< END + $input = <<< END

Title of Post

This is the post

END; - $parser = new Parser($input); - $output = $parser->parse(); + $parser = new Parser($input); + $output = $parser->parse(); - $this->assertArrayHasKey('name', $output['items'][0]['properties']); - $this->assertArrayHasKey('content', $output['items'][0]['properties']); - $this->assertArrayNotHasKey('url', $output['items'][0]['properties']); + $this->assertArrayHasKey('name', $output['items'][0]['properties']); + $this->assertArrayHasKey('content', $output['items'][0]['properties']); + $this->assertArrayNotHasKey('url', $output['items'][0]['properties']); } + /** + * Simplified h-entry with `p-location h-adr` from https://aaronparecki.com/2018/03/14/3/ + * Whitespace cleaned up for easier test assertion + * @see https://github.com/indieweb/php-mf2/issues/151 + */ + public function testNestedValuePProperty() { + $input = <<< END +
+ +Portland, Oregon 44°F + + + +
+END; + $parser = new Parser($input); + $output = $parser->parse(); + + $this->assertArrayHasKey('value', $output['items'][0]['properties']['location'][0]); + $this->assertEquals("Portland, Oregon • 44°F", $output['items'][0]['properties']['location'][0]['value']); + } + + /** + * @see https://github.com/indieweb/php-mf2/issues/151 + */ + public function testNestedValueDTProperty() { + $input = <<< END +
+
1997-12-12
+
+END; + $parser = new Parser($input); + $output = $parser->parse(); + + $this->assertArrayHasKey('value', $output['items'][0]['properties']['acme'][0]); + $this->assertEquals('1997-12-12', $output['items'][0]['properties']['acme'][0]['value']); + } + }