Skip to content

Commit 2753c22

Browse files
authored
Merge pull request #250 from gRegorLove/dev/issue249
Fix invalid property class name parsing
2 parents cf30431 + fb0214f commit 2753c22

File tree

2 files changed

+109
-9
lines changed

2 files changed

+109
-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: 105 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() {
@@ -914,5 +914,109 @@ public function testNewlineBeforePrefix() {
914914
$this->assertEquals('Page Title', $result['items'][0]['properties']['name'][0]);
915915
$this->assertEquals('A summary so the p-name won\'t be implied. This test demonstrates p-name is not being parsed.', $result['items'][0]['properties']['summary'][0]);
916916
}
917+
918+
/**
919+
* @see https://github.com/microformats/php-mf2/issues/249
920+
*/
921+
public function testInvalidRootsIgnored() {
922+
$input = <<<EOF
923+
<div class=" h-5 pt-2">
924+
<div class="p-4 h-entry">
925+
<a class="u-url" href="https://example.com/bar">bar </a>
926+
</div>
927+
<div class="p-4 h-entry">
928+
<a class="u-url" href="https://example.com/foo">foo</a>
929+
</div>
930+
</div>
931+
EOF;
932+
$output = Mf2\parse($input);
933+
934+
# `items` should have length=2
935+
$this->assertEquals(2, count($output['items']));
936+
937+
# each should be an h-entry
938+
$this->assertEquals('h-entry', $output['items'][0]['type'][0]);
939+
$this->assertEquals('h-entry', $output['items'][1]['type'][0]);
940+
}
941+
942+
/**
943+
* @see https://github.com/microformats/php-mf2/issues/249
944+
*/
945+
public function testInvalidNestedRootsIgnored() {
946+
$input = <<<EOF
947+
<div class=" h-5 pt-2">
948+
<div class="p-4 h-entry">
949+
<a class="u-url" href="https://example.com/bar">bar </a>
950+
</div>
951+
<div class="p-4 h-entry">
952+
<a class="u-url" href="https://example.com/foo">foo</a>
953+
</div>
954+
<div class="p-4 h-5">
955+
<a class="u-url" href="https://example.com/baz">baz</a>
956+
</div>
957+
</div>
958+
EOF;
959+
$output = Mf2\parse($input);
960+
961+
# `items` should have length=2
962+
$this->assertEquals(2, count($output['items']));
963+
964+
# each should be an h-entry
965+
$this->assertEquals('h-entry', $output['items'][0]['type'][0]);
966+
$this->assertEquals('h-entry', $output['items'][1]['type'][0]);
967+
}
968+
969+
/**
970+
* @see https://github.com/microformats/php-mf2/issues/249
971+
*/
972+
public function testInvalidNestedPropertiesIgnored1() {
973+
$input = <<<EOF
974+
<div class=" h-feed pt-2">
975+
<div class="p-4 h-entry">
976+
<a class="u-url" href="https://example.com/bar">bar </a>
977+
</div>
978+
<div class="p-4 h-entry">
979+
<a class="u-url" href="https://example.com/foo">foo</a>
980+
</div>
981+
</div>
982+
EOF;
983+
$output = Mf2\parse($input);
984+
985+
# `properties` should be empty
986+
$this->assertEquals(0, count($output['items'][0]['properties']));
987+
988+
# `children` should have length=2
989+
$this->assertArrayHasKey('children', $output['items'][0]);
990+
$this->assertEquals(2, count($output['items'][0]['children']));
991+
992+
# each child should be an h-entry
993+
$this->assertEquals('h-entry', $output['items'][0]['children'][0]['type'][0]);
994+
$this->assertEquals('h-entry', $output['items'][0]['children'][1]['type'][0]);
995+
}
996+
997+
/**
998+
* @see https://github.com/microformats/php-mf2/issues/246
999+
*/
1000+
public function testInvalidNestedPropertiesIgnored2() {
1001+
$input = <<<EOF
1002+
<article class="h-card">
1003+
<h1> TITLE </h1>
1004+
<img class="h-36 photo u-logo"
1005+
alt="An example alt title"
1006+
src="https://example.com/img.png"
1007+
/>
1008+
<p class="h-21 u-nickname"> John Doe </p>
1009+
</article>
1010+
EOF;
1011+
$output = Mf2\parse($input);
1012+
1013+
# `properties` should have length=3
1014+
$this->assertEquals(3, count($output['items'][0]['properties']));
1015+
1016+
# should have these properties
1017+
$this->assertArrayHasKey('logo', $output['items'][0]['properties']);
1018+
$this->assertArrayHasKey('nickname', $output['items'][0]['properties']);
1019+
$this->assertArrayHasKey('name', $output['items'][0]['properties']);
1020+
}
9171021
}
9181022

0 commit comments

Comments
 (0)