From 3cd0703406e868b89251ec94193a19fad92281e5 Mon Sep 17 00:00:00 2001 From: Gregor Morrill Date: Wed, 11 Oct 2023 15:55:48 -0700 Subject: [PATCH 1/2] Update nestedMfPropertyNamesFromClass Fixes #249 Fixes #246 --- Mf2/Parser.php | 12 +++------ tests/Mf2/ParserTest.php | 55 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/Mf2/Parser.php b/Mf2/Parser.php index 8b6485b..17896e6 100644 --- a/Mf2/Parser.php +++ b/Mf2/Parser.php @@ -166,14 +166,10 @@ function nestedMfPropertyNamesFromClass($class) { $prefixes = array('p-', 'u-', 'dt-', 'e-'); $propertyNames = array(); - $class = str_replace(array(' ', ' ', "\n"), ' ', $class); - foreach (explode(' ', $class) as $classname) { - foreach ($prefixes as $prefix) { - // Check if $classname is a valid property classname for $prefix. - if (mb_substr($classname, 0, mb_strlen($prefix)) == $prefix && $classname != $prefix) { - $propertyName = mb_substr($classname, mb_strlen($prefix)); - $propertyNames[$propertyName][] = $prefix; - } + foreach ($prefixes as $prefix) { + $classes = mfNamesFromClass($class, $prefix); + foreach ($classes as $property) { + $propertyNames[$property][] = $prefix; } } diff --git a/tests/Mf2/ParserTest.php b/tests/Mf2/ParserTest.php index a364557..9b07dc9 100644 --- a/tests/Mf2/ParserTest.php +++ b/tests/Mf2/ParserTest.php @@ -56,7 +56,7 @@ public function testNestedMicroformatPropertyNameWorks() { $test = 'someclass p-location someotherclass u-author p-author'; $actual = Mf2\nestedMfPropertyNamesFromClass($test); - $this->assertEquals($expected, $actual); + $this->assertEqualsCanonicalizing($expected, $actual); } public function testMicroformatNamesFromClassIgnoresPrefixesWithoutNames() { @@ -898,5 +898,58 @@ public function testGetRootMfOnlyFindsValidElements() { $this->assertEquals(1, count($rootEls)); $this->assertEquals('h-vendor123-name', $rootEls->item(0)->getAttribute('class')); } + + /** + * @see https://github.com/microformats/php-mf2/issues/249 + */ + public function testInvalidNestedPropertiesIgnored1() { + $input = << +
+ bar +
+
+ foo +
+ +EOF; + $output = Mf2\parse($input); + + # `properties` should be empty + $this->assertEquals(0, count($output['items'][0]['properties'])); + + # `children` should have length=2 + $this->assertArrayHasKey('children', $output['items'][0]); + $this->assertEquals(2, count($output['items'][0]['children'])); + + # each child should be an h-entry + $this->assertEquals('h-entry', $output['items'][0]['children'][0]['type'][0]); + $this->assertEquals('h-entry', $output['items'][0]['children'][1]['type'][0]); + } + + /** + * @see https://github.com/microformats/php-mf2/issues/246 + */ + public function testInvalidNestedPropertiesIgnored2() { + $input = << +

TITLE

+ +

John Doe

+ +EOF; + $output = Mf2\parse($input); + + # `properties` should have length=3 + $this->assertEquals(3, count($output['items'][0]['properties'])); + + # should have these properties + $this->assertArrayHasKey('logo', $output['items'][0]['properties']); + $this->assertArrayHasKey('nickname', $output['items'][0]['properties']); + $this->assertArrayHasKey('name', $output['items'][0]['properties']); + } } From f92cc52235bed26b6fbfc866518a04a1f6a84bc2 Mon Sep 17 00:00:00 2001 From: Gregor Morrill Date: Wed, 11 Oct 2023 16:39:53 -0700 Subject: [PATCH 2/2] Add additional tests --- tests/Mf2/ParserTest.php | 51 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tests/Mf2/ParserTest.php b/tests/Mf2/ParserTest.php index 9b07dc9..3010ee0 100644 --- a/tests/Mf2/ParserTest.php +++ b/tests/Mf2/ParserTest.php @@ -899,6 +899,57 @@ public function testGetRootMfOnlyFindsValidElements() { $this->assertEquals('h-vendor123-name', $rootEls->item(0)->getAttribute('class')); } + /** + * @see https://github.com/microformats/php-mf2/issues/249 + */ + public function testInvalidRootsIgnored() { + $input = << +
+ bar +
+
+ foo +
+ +EOF; + $output = Mf2\parse($input); + + # `items` should have length=2 + $this->assertEquals(2, count($output['items'])); + + # each should be an h-entry + $this->assertEquals('h-entry', $output['items'][0]['type'][0]); + $this->assertEquals('h-entry', $output['items'][1]['type'][0]); + } + + /** + * @see https://github.com/microformats/php-mf2/issues/249 + */ + public function testInvalidNestedRootsIgnored() { + $input = << +
+ bar +
+
+ foo +
+
+ baz +
+ +EOF; + $output = Mf2\parse($input); + + # `items` should have length=2 + $this->assertEquals(2, count($output['items'])); + + # each should be an h-entry + $this->assertEquals('h-entry', $output['items'][0]['type'][0]); + $this->assertEquals('h-entry', $output['items'][1]['type'][0]); + } + /** * @see https://github.com/microformats/php-mf2/issues/249 */