Skip to content

Commit 84bd6ef

Browse files
authored
Merge pull request #121 from jkphl/master
Fixed language detection to support parsing of HTML fragments
2 parents 7c7f502 + f101447 commit 84bd6ef

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

Mf2/Parser.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ public function language(DOMElement $el)
508508
return unicodeTrim($node->getAttribute('content'));
509509
}
510510
}
511-
} else {
511+
} elseif ($el->parentNode instanceof DOMElement) {
512512
// check the parent node
513513
return $this->language($el->parentNode);
514514
}

tests/Mf2/ParseLanguageTest.php

+37
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,43 @@ public function testHtmlAndHEntryLang()
5252
$this->assertEquals('es', $result['items'][0]['properties']['html-lang']);
5353
} # end method testHtmlAndHEntryLang()
5454

55+
/**
56+
* Test HTML fragment with only h-entry lang
57+
*/
58+
public function testFragmentHEntryLangOnly()
59+
{
60+
$input = '<div class="h-entry" lang="en">This test is in English.</div>';
61+
$parser = new Parser($input);
62+
$result = $parser->parse();
63+
64+
$this->assertEquals('en', $result['items'][0]['properties']['html-lang']);
65+
} # end method testFragmentHEntryLangOnly()
66+
67+
/**
68+
* Test HTML fragment with no lang
69+
*/
70+
public function testFragmentHEntryNoLang()
71+
{
72+
$input = '<div class="h-entry">This test is in English.</div>';
73+
$parser = new Parser($input);
74+
$result = $parser->parse();
75+
76+
$this->assertFalse(isset($result['items'][0]['properties']['html-lang']));
77+
} # end method testFragmentHEntryNoLang()
78+
79+
/**
80+
* Test HTML fragment with no lang, loaded with loadXML()
81+
*/
82+
public function testFragmentHEntryNoLangXML()
83+
{
84+
$input = new \DOMDocument();
85+
$input->loadXML('<div class="h-entry">This test is in English.</div>');
86+
$parser = new Parser($input);
87+
$result = $parser->parse();
88+
89+
$this->assertFalse(isset($result['items'][0]['properties']['html-lang']));
90+
} # end method testFragmentHEntryNoLangXML()
91+
5592
/**
5693
* Test with different <html lang>, h-entry lang, and h-entry without lang,
5794
* which should inherit from the <html lang>

0 commit comments

Comments
 (0)