Skip to content

Commit

Permalink
Merge pull request #121 from jkphl/master
Browse files Browse the repository at this point in the history
Fixed language detection to support parsing of HTML fragments
  • Loading branch information
aaronpk committed May 27, 2017
2 parents 7c7f502 + f101447 commit 84bd6ef
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Mf2/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ public function language(DOMElement $el)
return unicodeTrim($node->getAttribute('content'));
}
}
} else {
} elseif ($el->parentNode instanceof DOMElement) {
// check the parent node
return $this->language($el->parentNode);
}
Expand Down
37 changes: 37 additions & 0 deletions tests/Mf2/ParseLanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,43 @@ public function testHtmlAndHEntryLang()
$this->assertEquals('es', $result['items'][0]['properties']['html-lang']);
} # end method testHtmlAndHEntryLang()

/**
* Test HTML fragment with only h-entry lang
*/
public function testFragmentHEntryLangOnly()
{
$input = '<div class="h-entry" lang="en">This test is in English.</div>';
$parser = new Parser($input);
$result = $parser->parse();

$this->assertEquals('en', $result['items'][0]['properties']['html-lang']);
} # end method testFragmentHEntryLangOnly()

/**
* Test HTML fragment with no lang
*/
public function testFragmentHEntryNoLang()
{
$input = '<div class="h-entry">This test is in English.</div>';
$parser = new Parser($input);
$result = $parser->parse();

$this->assertFalse(isset($result['items'][0]['properties']['html-lang']));
} # end method testFragmentHEntryNoLang()

/**
* Test HTML fragment with no lang, loaded with loadXML()
*/
public function testFragmentHEntryNoLangXML()
{
$input = new \DOMDocument();
$input->loadXML('<div class="h-entry">This test is in English.</div>');
$parser = new Parser($input);
$result = $parser->parse();

$this->assertFalse(isset($result['items'][0]['properties']['html-lang']));
} # end method testFragmentHEntryNoLangXML()

/**
* Test with different <html lang>, h-entry lang, and h-entry without lang,
* which should inherit from the <html lang>
Expand Down

0 comments on commit 84bd6ef

Please sign in to comment.