Skip to content

Fixed language detection to support parsing of HTML fragments #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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