Skip to content

Commit 3cd0703

Browse files
committed
Update nestedMfPropertyNamesFromClass
Fixes #249 Fixes #246
1 parent 2d65354 commit 3cd0703

File tree

2 files changed

+58
-9
lines changed

2 files changed

+58
-9
lines changed

Mf2/Parser.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,10 @@ function nestedMfPropertyNamesFromClass($class) {
166166
$prefixes = array('p-', 'u-', 'dt-', 'e-');
167167
$propertyNames = array();
168168

169-
$class = str_replace(array(' ', ' ', "\n"), ' ', $class);
170-
foreach (explode(' ', $class) as $classname) {
171-
foreach ($prefixes as $prefix) {
172-
// Check if $classname is a valid property classname for $prefix.
173-
if (mb_substr($classname, 0, mb_strlen($prefix)) == $prefix && $classname != $prefix) {
174-
$propertyName = mb_substr($classname, mb_strlen($prefix));
175-
$propertyNames[$propertyName][] = $prefix;
176-
}
169+
foreach ($prefixes as $prefix) {
170+
$classes = mfNamesFromClass($class, $prefix);
171+
foreach ($classes as $property) {
172+
$propertyNames[$property][] = $prefix;
177173
}
178174
}
179175

tests/Mf2/ParserTest.php

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function testNestedMicroformatPropertyNameWorks() {
5656
$test = 'someclass p-location someotherclass u-author p-author';
5757
$actual = Mf2\nestedMfPropertyNamesFromClass($test);
5858

59-
$this->assertEquals($expected, $actual);
59+
$this->assertEqualsCanonicalizing($expected, $actual);
6060
}
6161

6262
public function testMicroformatNamesFromClassIgnoresPrefixesWithoutNames() {
@@ -898,5 +898,58 @@ public function testGetRootMfOnlyFindsValidElements() {
898898
$this->assertEquals(1, count($rootEls));
899899
$this->assertEquals('h-vendor123-name', $rootEls->item(0)->getAttribute('class'));
900900
}
901+
902+
/**
903+
* @see https://github.com/microformats/php-mf2/issues/249
904+
*/
905+
public function testInvalidNestedPropertiesIgnored1() {
906+
$input = <<<EOF
907+
<div class=" h-feed pt-2">
908+
<div class="p-4 h-entry">
909+
<a class="u-url" href="https://example.com/bar">bar </a>
910+
</div>
911+
<div class="p-4 h-entry">
912+
<a class="u-url" href="https://example.com/foo">foo</a>
913+
</div>
914+
</div>
915+
EOF;
916+
$output = Mf2\parse($input);
917+
918+
# `properties` should be empty
919+
$this->assertEquals(0, count($output['items'][0]['properties']));
920+
921+
# `children` should have length=2
922+
$this->assertArrayHasKey('children', $output['items'][0]);
923+
$this->assertEquals(2, count($output['items'][0]['children']));
924+
925+
# each child should be an h-entry
926+
$this->assertEquals('h-entry', $output['items'][0]['children'][0]['type'][0]);
927+
$this->assertEquals('h-entry', $output['items'][0]['children'][1]['type'][0]);
928+
}
929+
930+
/**
931+
* @see https://github.com/microformats/php-mf2/issues/246
932+
*/
933+
public function testInvalidNestedPropertiesIgnored2() {
934+
$input = <<<EOF
935+
<article class="h-card">
936+
<h1> TITLE </h1>
937+
<img class="h-36 photo u-logo"
938+
alt="An example alt title"
939+
src="https://example.com/img.png"
940+
/>
941+
<p class="h-21 u-nickname"> John Doe </p>
942+
</article>
943+
EOF;
944+
$output = Mf2\parse($input);
945+
946+
# `properties` should have length=3
947+
$this->assertEquals(3, count($output['items'][0]['properties']));
948+
949+
# should have these properties
950+
$this->assertArrayHasKey('logo', $output['items'][0]['properties']);
951+
$this->assertArrayHasKey('nickname', $output['items'][0]['properties']);
952+
$this->assertArrayHasKey('name', $output['items'][0]['properties']);
953+
}
901954
}
902955

0 commit comments

Comments
 (0)