Skip to content

Commit 4787005

Browse files
committed
Add failing tests and fixes for #158, #160
1 parent e044330 commit 4787005

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

Mf2/Parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ function unicodeTrim($str) {
130130
function mfNamesFromClass($class, $prefix='h-') {
131131
$class = str_replace(array(' ', ' ', "\n"), ' ', $class);
132132
$classes = explode(' ', $class);
133-
$classes = preg_grep('#^[a-z\-]+$#', $classes);
133+
$classes = preg_grep('#^(h|p|u|dt|e)-([a-z0-9]+\-)?[a-z]+(\-[a-z]+)*$#', $classes);
134134
$matches = array();
135135

136136
foreach ($classes as $classname) {

tests/Mf2/ParserTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,5 +696,68 @@ public function testMultiLevelRecursion() {
696696
$this->assertEquals('Comment D', $three['children'][1]['properties']['name'][0]);
697697
$this->assertEquals('Four', $output['items'][0]['children'][3]['properties']['name'][0]);
698698
}
699+
700+
/**
701+
* @see https://github.com/indieweb/php-mf2/issues/158
702+
*/
703+
public function testPrefixWithNumbers() {
704+
$input = '<li class="h-entry">
705+
<data class="p-name" value="Coffee"></data>
706+
<div class="p-p3k-drank h-p3k-food">
707+
<span class="value">Coffee</span>
708+
</div>
709+
</li>';
710+
$output = Mf2\parse($input);
711+
712+
$this->assertArrayHasKey('p3k-drank', $output['items'][0]['properties']);
713+
$this->assertCount(1, $output['items'][0]['properties']['p3k-drank']);
714+
$this->assertEquals('h-p3k-food', $output['items'][0]['properties']['p3k-drank'][0]['type'][0]);
715+
}
716+
717+
/**
718+
* @see https://github.com/indieweb/php-mf2/issues/160
719+
*/
720+
public function testConsecutiveDashes() {
721+
$input = '<div class="h-entry h-----">
722+
<p> <a href="http://example.com/post" class="u-in-reply--to">http://example.com/post posted:</a> </p>
723+
<span class="p-name">Too many dashes</span>
724+
<span class="p--acme-leading">leading dash</span>
725+
<span class="p-acme--middle">middle dash</span>
726+
<span class="p-acme-trailing-">trailing dash</span>
727+
</div>';
728+
$output = Mf2\parse($input);
729+
730+
$this->assertCount(1, $output['items'][0]['type']);
731+
$this->assertEquals('h-entry', $output['items'][0]['type'][0]);
732+
$this->assertCount(1, $output['items'][0]['properties']);
733+
$this->assertArrayHasKey('name', $output['items'][0]['properties']);
734+
}
735+
736+
/**
737+
* Additional test from mf2py. Covers consecutive dashes, numbers in vendor prefix, and capital letters.
738+
* Added markup for numbers-only prefix and capital letter in prefix
739+
* @see https://github.com/kartikprabhu/mf2py/blob/experimental/test/examples/class_names_format.html
740+
* @see https://github.com/indieweb/php-mf2/issues/160
741+
* @see https://github.com/indieweb/php-mf2/issues/158
742+
*/
743+
public function testMfClassRegex() {
744+
$input = '<article class="h-x-test h-p3k-entry h-feed h-Entry h-p3k-fEed h--d h-test-">
745+
<a class="u-url u-Url u-p3k-url u--url u-url- u-123-url u-123A-url" href="example.com" >URL </a>
746+
<span class="p-name p-nAme p-p3k-name p--name p-name-" >name</span>
747+
</article>';
748+
$output = Mf2\parse($input);
749+
750+
$this->assertCount(3, $output['items'][0]['type']);
751+
$this->assertContains('h-feed', $output['items'][0]['type']);
752+
$this->assertContains('h-p3k-entry', $output['items'][0]['type']);
753+
$this->assertContains('h-x-test', $output['items'][0]['type']);
754+
$this->assertCount(5, $output['items'][0]['properties']);
755+
$this->assertArrayHasKey('url', $output['items'][0]['properties']);
756+
$this->assertArrayHasKey('p3k-url', $output['items'][0]['properties']);
757+
$this->assertArrayHasKey('name', $output['items'][0]['properties']);
758+
$this->assertArrayHasKey('p3k-name', $output['items'][0]['properties']);
759+
$this->assertArrayHasKey('123-url', $output['items'][0]['properties']);
760+
}
761+
699762
}
700763

0 commit comments

Comments
 (0)