From cb19d836b7339a3546544f2bf6eeed3c9cdf2b6f Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Sat, 22 Nov 2014 00:40:47 -0500 Subject: [PATCH 1/8] test out adding parsing --- Mf2/Parser.php | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/Mf2/Parser.php b/Mf2/Parser.php index 27805f2..6959edf 100644 --- a/Mf2/Parser.php +++ b/Mf2/Parser.php @@ -688,6 +688,15 @@ public function parseH(\DOMElement $e) { $this->elementPrefixParsed($subMF, 'dt'); $this->elementPrefixParsed($subMF, 'e'); } + if($e->tagName == 'area') { + if(!empty($e->getAttribute('coords'))){ + $coords = $e->getAttribute('coords'); + } + if(!empty($e->getAttribute('shape'))){ + $shape = $e->getAttribute('shape'); + } + + } // Handle p-* foreach ($this->xpath->query('.//*[contains(concat(" ", @class) ," p-")]', $e) as $p) { @@ -762,7 +771,7 @@ public function parseH(\DOMElement $e) { if (!array_key_exists('name', $return)) { try { // Look for img @alt - if ($e->tagName == 'img' and $e->getAttribute('alt') != '') + if (($e->tagName == 'img' or $e->tagName == 'area') and $e->getAttribute('alt') != '') throw new Exception($e->getAttribute('alt')); if ($e->tagName == 'abbr' and $e->hasAttribute('title')) @@ -774,12 +783,25 @@ public function parseH(\DOMElement $e) { throw new Exception($em->getAttribute('alt')); } + // Look for nested area @alt + foreach ($this->xpath->query('./area[count(preceding-sibling::*)+count(following-sibling::*)=0]', $e) as $em) { + if ($em->getAttribute('alt') != '') + throw new Exception($em->getAttribute('alt')); + } + + // Look for double nested img @alt foreach ($this->xpath->query('./*[count(preceding-sibling::*)+count(following-sibling::*)=0]/img[count(preceding-sibling::*)+count(following-sibling::*)=0]', $e) as $em) { if ($em->getAttribute('alt') != '') throw new Exception($em->getAttribute('alt')); } + // Look for double nested img @alt + foreach ($this->xpath->query('./*[count(preceding-sibling::*)+count(following-sibling::*)=0]/area[count(preceding-sibling::*)+count(following-sibling::*)=0]', $e) as $em) { + if ($em->getAttribute('alt') != '') + throw new Exception($em->getAttribute('alt')); + } + throw new Exception($e->nodeValue); } catch (Exception $exc) { $return['name'][] = unicodeTrim($exc->getMessage()); @@ -812,7 +834,7 @@ public function parseH(\DOMElement $e) { // Check for u-url if (!array_key_exists('url', $return)) { // Look for img @src - if ($e->tagName == 'a') + if ($e->tagName == 'a' or $e->tagName == 'area') $url = $e->getAttribute('href'); // Look for nested img @src @@ -820,6 +842,12 @@ public function parseH(\DOMElement $e) { $url = $em->getAttribute('href'); break; } + + // Look for nested area @src + foreach ($this->xpath->query('./area[count(preceding-sibling::area)+count(following-sibling::area)=0]', $e) as $em) { + $url = $em->getAttribute('href'); + break; + } if (!empty($url)) $return['url'][] = $this->resolveUrl($url); @@ -833,6 +861,12 @@ public function parseH(\DOMElement $e) { 'type' => $mfTypes, 'properties' => $return ); + if (!empty($shape)){ + $parsed['shape'] = $shape; + } + if (!empty($coords)){ + $parsed['coords'] = $coords; + } if (!empty($children)) $parsed['children'] = array_values(array_filter($children)); return $parsed; From 954a8a00bd5a324185f91c083d070e44f44388c8 Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Mon, 24 Nov 2014 15:47:29 -0500 Subject: [PATCH 2/8] fix bug in original parser not checking for sub h-* items --- Mf2/Parser.php | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/Mf2/Parser.php b/Mf2/Parser.php index 6959edf..f6fb6e7 100644 --- a/Mf2/Parser.php +++ b/Mf2/Parser.php @@ -779,27 +779,35 @@ public function parseH(\DOMElement $e) { // Look for nested img @alt foreach ($this->xpath->query('./img[count(preceding-sibling::*)+count(following-sibling::*)=0]', $e) as $em) { - if ($em->getAttribute('alt') != '') + $emNames = mfNamesFromElement($em, 'h-'); + if(empty($emNames) && $em->getAttribute('alt') != ''){ throw new Exception($em->getAttribute('alt')); + } } // Look for nested area @alt foreach ($this->xpath->query('./area[count(preceding-sibling::*)+count(following-sibling::*)=0]', $e) as $em) { - if ($em->getAttribute('alt') != '') + $emNames = mfNamesFromElement($em, 'h-'); + if(empty($emNames) && $em->getAttribute('alt') != ''){ throw new Exception($em->getAttribute('alt')); + } } // Look for double nested img @alt foreach ($this->xpath->query('./*[count(preceding-sibling::*)+count(following-sibling::*)=0]/img[count(preceding-sibling::*)+count(following-sibling::*)=0]', $e) as $em) { - if ($em->getAttribute('alt') != '') + $emNames = mfNamesFromElement($em, 'h-'); + if(empty($emNames) && $em->getAttribute('alt') != ''){ throw new Exception($em->getAttribute('alt')); + } } // Look for double nested img @alt foreach ($this->xpath->query('./*[count(preceding-sibling::*)+count(following-sibling::*)=0]/area[count(preceding-sibling::*)+count(following-sibling::*)=0]', $e) as $em) { - if ($em->getAttribute('alt') != '') + $emNames = mfNamesFromElement($em, 'h-'); + if(empty($emNames) && $em->getAttribute('alt') != ''){ throw new Exception($em->getAttribute('alt')); + } } throw new Exception($e->nodeValue); @@ -837,16 +845,22 @@ public function parseH(\DOMElement $e) { if ($e->tagName == 'a' or $e->tagName == 'area') $url = $e->getAttribute('href'); - // Look for nested img @src + // Look for nested a @href foreach ($this->xpath->query('./a[count(preceding-sibling::a)+count(following-sibling::a)=0]', $e) as $em) { - $url = $em->getAttribute('href'); - break; + $emNames = mfNamesFromElement($em, 'h-'); + if(empty($emNames)){ + $url = $em->getAttribute('href'); + break; + } } // Look for nested area @src foreach ($this->xpath->query('./area[count(preceding-sibling::area)+count(following-sibling::area)=0]', $e) as $em) { - $url = $em->getAttribute('href'); - break; + $emNames = mfNamesFromElement($em, 'h-'); + if(empty($emNames)){ + $url = $em->getAttribute('href'); + break; + } } if (!empty($url)) From ccb632041bc01f2a1303d37b37be86be849818dd Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Fri, 5 Dec 2014 16:18:38 -0500 Subject: [PATCH 3/8] case sensative file system --- phpunit.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index 07b176f..08a3463 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,8 +1,8 @@ - + - tests/mf2 + tests/Mf2 - \ No newline at end of file + From 9a12cce0e5f500cbebf69cada0fb9aa202d14ef8 Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Fri, 5 Dec 2014 16:58:22 -0500 Subject: [PATCH 4/8] adding tests to validate update --- tests/Mf2/ParserTest.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/Mf2/ParserTest.php b/tests/Mf2/ParserTest.php index 3d5303a..17d46b5 100644 --- a/tests/Mf2/ParserTest.php +++ b/tests/Mf2/ParserTest.php @@ -129,6 +129,7 @@ public function testHtmlEncodesNonEProperties() { $this->assertEquals('
', $output['items'][0]['properties']['published'][0]); $this->assertEquals('', $output['items'][0]['properties']['url'][0]); } + public function testHtmlEncodesImpliedProperties() { $input = '<name>'; @@ -257,4 +258,31 @@ public function testConvertsNestedImgElementToAltOrSrc() { $result = Mf2\parse($input, 'http://waterpigs.co.uk/articles/five-legged-elephant'); $this->assertEquals('It is a strange thing to see a five legged elephant', $result['items'][0]['properties']['content'][0]['value']); } + + // parser not respecting not[h-*] in rule "else if .h-x>a[href]:only-of-type:not[.h-*] then use that [href] for url" + public function testNotImpliedUrlFromHCard() { + $input = ' + John Q + '; + + $parser = new Parser($input); + $output = $parser->parse(); + + $this->assertArrayNotHasKey('url', $output['items'][0]['properties']); + } + + public function testAreaTag() { + $input = '
+ Person Bee +
'; + + $parser = new Parser($input); + $output = $parser->parse(); + + $this->assertEquals('', $output['items'][0]['properties']['name'][0]); + $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']); + + } } From 67f4d2e077e5987df77af8ebdeca87d82c67a542 Mon Sep 17 00:00:00 2001 From: BenRoberts Date: Wed, 7 Jan 2015 14:09:42 -0500 Subject: [PATCH 5/8] fix php complaint --- Mf2/Parser.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Mf2/Parser.php b/Mf2/Parser.php index f6fb6e7..8c4ffae 100644 --- a/Mf2/Parser.php +++ b/Mf2/Parser.php @@ -689,12 +689,8 @@ public function parseH(\DOMElement $e) { $this->elementPrefixParsed($subMF, 'e'); } if($e->tagName == 'area') { - if(!empty($e->getAttribute('coords'))){ - $coords = $e->getAttribute('coords'); - } - if(!empty($e->getAttribute('shape'))){ - $shape = $e->getAttribute('shape'); - } + $coords = $e->getAttribute('coords'); + $shape = $e->getAttribute('shape'); } From e097f5954d0a2dc09ba1657dea5dd19a7210fd5b Mon Sep 17 00:00:00 2001 From: BenRoberts Date: Fri, 20 Mar 2015 14:24:33 -0400 Subject: [PATCH 6/8] fix case sensitivity unit test failing --- Mf2/Parser.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Mf2/Parser.php b/Mf2/Parser.php index 8c4ffae..bbba5f2 100644 --- a/Mf2/Parser.php +++ b/Mf2/Parser.php @@ -127,9 +127,9 @@ function mfNamesFromClass($class, $prefix='h-') { $matches = array(); foreach ($classes as $classname) { - $compare_classname = strtolower(' ' . $classname); - $compare_prefix = strtolower(' ' . $prefix); - if (stristr($compare_classname, $compare_prefix) !== false && ($compare_classname != $compare_prefix)) { + $compare_classname = ' ' . $classname; + $compare_prefix = ' ' . $prefix; + if (strstr($compare_classname, $compare_prefix) !== false && ($compare_classname != $compare_prefix)) { $matches[] = ($prefix === 'h-') ? $classname : substr($classname, strlen($prefix)); } } From 17124a3f524fbff9a618692013ca3f380b474531 Mon Sep 17 00:00:00 2001 From: BenRoberts Date: Sun, 22 Mar 2015 12:46:46 -0400 Subject: [PATCH 7/8] add test of h-card in category --- tests/Mf2/ParserTest.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/Mf2/ParserTest.php b/tests/Mf2/ParserTest.php index 17d46b5..bc60348 100644 --- a/tests/Mf2/ParserTest.php +++ b/tests/Mf2/ParserTest.php @@ -285,4 +285,25 @@ public function testAreaTag() { $this->assertEquals('Person Bee', $output['items'][0]['properties']['category'][0]['value']); } + + public function testParseHcardInCategory() { + + $input = ' + Alice tagged + Bob Smith in + + a photo of Bob and Cole + '; + + $parser = new Parser($input); + $output = $parser->parse(); + + $this->assertContains('h-entry', $output['items'][0]['type']); + $this->assertArrayHasKey('category', $output['items'][0]['properties']); + $this->assertContains('h-card', $output['items'][0]['properties']['category'][0]['type']); + $this->assertArrayHasKey('name', $output['items'][0]['properties']['category'][0]['properties']); + $this->assertEquals('Bob Smith', $output['items'][0]['properties']['category'][0]['properties']['name'][0]); + $this->assertArrayHasKey('url', $output['items'][0]['properties']['category'][0]['properties']); + $this->assertEquals('http://b.example.com/', $output['items'][0]['properties']['category'][0]['properties']['url'][0]); + } } From cdffcad6dd63ba6bbdcc8c47d48645f8047a825d Mon Sep 17 00:00:00 2001 From: BenRoberts Date: Fri, 27 Mar 2015 20:18:54 -0400 Subject: [PATCH 8/8] correct test --- tests/Mf2/ParserTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Mf2/ParserTest.php b/tests/Mf2/ParserTest.php index bc60348..fadce67 100644 --- a/tests/Mf2/ParserTest.php +++ b/tests/Mf2/ParserTest.php @@ -290,7 +290,7 @@ public function testParseHcardInCategory() { $input = ' Alice tagged - Bob Smith in + Bob Smith in a photo of Bob and Cole ';