Skip to content

Commit

Permalink
Fixed #48, partial merge of #49
Browse files Browse the repository at this point in the history
This commit manually merges the test from #49, with slightly simpler fixes that in the PR, as well
as additional tests and fixes for case-insensitivity issues (i.e. case-sensitivity is good) and
better handling of masses of weird whitespace in class attributes, as is sometimes produced by
templating engines.
  • Loading branch information
barnabywalters committed Jun 18, 2014
1 parent 1d6500b commit c628e69
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
12 changes: 7 additions & 5 deletions Mf2/Parser.php
Expand Up @@ -122,12 +122,13 @@ function unicodeTrim($str) {
* @param string $prefix The prefix to look for
* @return string|array The prefixed name of the first microfomats class found or false
*/
function mfNamesFromClass($class, $prefix = 'h-') {
function mfNamesFromClass($class, $prefix='h-') {
$class = str_replace([' ', ' ', "\n"], ' ', $class);
$classes = explode(' ', $class);
$matches = array();

foreach ($classes as $classname) {
if (stristr(' ' . $classname, ' ' . $prefix) !== false) {
if (strpos($classname, $prefix) === 0 && $classname !== $prefix) {
$matches[] = ($prefix === 'h-') ? $classname : substr($classname, strlen($prefix));
}
}
Expand All @@ -145,12 +146,13 @@ function mfNamesFromClass($class, $prefix = 'h-') {
* @return array
*/
function nestedMfPropertyNamesFromClass($class) {
$prefixes = array(' p-', ' u-', ' dt-', ' e-');
$prefixes = array('p-', 'u-', 'dt-', 'e-');
$propertyNames = array();


$class = str_replace([' ', ' ', "\n"], ' ', $class);
foreach (explode(' ', $class) as $classname) {
foreach ($prefixes as $prefix) {
if (stristr(' ' . $classname, $prefix)) {
if (strpos($classname, $prefix) === 0 and $classname !== $prefix) {
$propertyNames = array_merge($propertyNames, mfNamesFromClass($classname, ltrim($prefix)));
}
}
Expand Down
41 changes: 37 additions & 4 deletions tests/Mf2/ParserTest.php
Expand Up @@ -30,29 +30,51 @@ public function testMicroformatNameFromClassReturnsFullRootName() {
$expected = array('h-card');
$actual = Mf2\mfNamesFromClass('someclass h-card someotherclass', 'h-');

$this->assertEquals($actual, $expected);
$this->assertEquals($expected, $actual);
}

public function testMicroformatNameFromClassHandlesMultipleHNames() {
$expected = array('h-card', 'h-person');
$actual = Mf2\mfNamesFromClass('someclass h-card someotherclass h-person yetanotherclass', 'h-');

$this->assertEquals($actual, $expected);
$this->assertEquals($expected, $actual);
}

public function testMicroformatStripsPrefixFromPropertyClassname() {
$expected = array('name');
$actual = Mf2\mfNamesFromClass('someclass p-name someotherclass', 'p-');

$this->assertEquals($actual, $expected);
$this->assertEquals($expected, $actual);
}

public function testNestedMicroformatPropertyNameWorks() {
$expected = array('location', 'author');
$test = 'someclass p-location someotherclass u-author';
$actual = Mf2\nestedMfPropertyNamesFromClass($test);

$this->assertEquals($actual, $expected);
$this->assertEquals($expected, $actual);
}

public function testMicroformatNamesFromClassIgnoresPrefixesWithoutNames() {
$expected = array();
$actual = Mf2\mfNamesFromClass('someclass h- someotherclass', 'h-');

$this->assertEquals($expected, $actual);
}

public function testMicroformatNamesFromClassHandlesExcessiveWhitespace() {
$expected = array('h-card');
$actual = Mf2\mfNamesFromClass(' someclass
h-card someotherclass ', 'h-');

$this->assertEquals($expected, $actual);
}

public function testMicroformatNamesFromClassIgnoresUppercaseClassnames() {
$expected = array();
$actual = Mf2\mfNamesFromClass('H-ENTRY', 'h-');

$this->assertEquals($expected, $actual);
}

public function testParseE() {
Expand Down Expand Up @@ -201,4 +223,15 @@ public function testFetchMicroformats() {
$this->assertNull($mf);
$this->assertContains('jpeg', $curlInfo['content_type']);
}

/**
* @see https://github.com/indieweb/php-mf2/issues/48
*/
public function testIgnoreClassesEndingInHyphen() {
$input = '<span class="h-entry"> <span class="e-">foo</span> </span>';
$parser = new Parser($input);
$output = $parser->parse();

$this->assertArrayNotHasKey('0', $output['items'][0]['properties']);
}
}

0 comments on commit c628e69

Please sign in to comment.