diff --git a/Mf2/Parser.php b/Mf2/Parser.php index 7955624..39e90c6 100644 --- a/Mf2/Parser.php +++ b/Mf2/Parser.php @@ -817,7 +817,7 @@ public function parseE(\DOMElement $e) { if($this->lang) { // Language if ( $html_lang = $this->language($e) ) { - $return['html-lang'] = $html_lang; + $return['lang'] = $html_lang; } } @@ -1078,13 +1078,6 @@ public function parseH(\DOMElement $e, $is_backcompat = false) { } } - if($this->lang) { - // Language - if ( $html_lang = $this->language($e) ) { - $return['html-lang'] = $html_lang; - } - } - // Make sure things are in alphabetical order sort($mfTypes); @@ -1094,6 +1087,13 @@ public function parseH(\DOMElement $e, $is_backcompat = false) { 'properties' => $return ); + if($this->lang) { + // Language + if ( $html_lang = $this->language($e) ) { + $parsed['lang'] = $html_lang; + } + } + if (!empty($shape)) { $parsed['shape'] = $shape; } diff --git a/tests/Mf2/ParseLanguageTest.php b/tests/Mf2/ParseLanguageTest.php index 97a2f00..ae8ba29 100644 --- a/tests/Mf2/ParseLanguageTest.php +++ b/tests/Mf2/ParseLanguageTest.php @@ -26,7 +26,8 @@ public function testHtmlLangOnly() $parser->lang = true; $result = $parser->parse(); - $this->assertEquals('en', $result['items'][0]['properties']['html-lang']); + $this->assertArrayHasKey('lang', $result['items'][0]); + $this->assertEquals('en', $result['items'][0]['lang']); } # end method testHtmlLangOnly() /** @@ -39,7 +40,8 @@ public function testHEntryLangOnly() $parser->lang = true; $result = $parser->parse(); - $this->assertEquals('en', $result['items'][0]['properties']['html-lang']); + $this->assertArrayHasKey('lang', $result['items'][0]); + $this->assertEquals('en', $result['items'][0]['lang']); } # end method testHEntryLangOnly() /** @@ -52,7 +54,8 @@ public function testHtmlAndHEntryLang() $parser->lang = true; $result = $parser->parse(); - $this->assertEquals('es', $result['items'][0]['properties']['html-lang']); + $this->assertArrayHasKey('lang', $result['items'][0]); + $this->assertEquals('es', $result['items'][0]['lang']); } # end method testHtmlAndHEntryLang() /** @@ -65,7 +68,8 @@ public function testFragmentHEntryLangOnly() $parser->lang = true; $result = $parser->parse(); - $this->assertEquals('en', $result['items'][0]['properties']['html-lang']); + $this->assertArrayHasKey('lang', $result['items'][0]); + $this->assertEquals('en', $result['items'][0]['lang']); } # end method testFragmentHEntryLangOnly() /** @@ -78,7 +82,7 @@ public function testFragmentHEntryNoLang() $parser->lang = true; $result = $parser->parse(); - $this->assertFalse(isset($result['items'][0]['properties']['html-lang'])); + $this->assertArrayNotHasKey('lang', $result['items'][0]); } # end method testFragmentHEntryNoLang() /** @@ -91,7 +95,7 @@ public function testFragmentHEntryNoLangXML() $parser = new Parser($input); $result = $parser->parse(); - $this->assertFalse(isset($result['items'][0]['properties']['html-lang'])); + $this->assertArrayNotHasKey('lang', $result['items'][0]); } # end method testFragmentHEntryNoLangXML() /** @@ -105,8 +109,10 @@ public function testMultiLanguageInheritance() $parser->lang = true; $result = $parser->parse(); - $this->assertEquals('en', $result['items'][0]['properties']['html-lang']); - $this->assertEquals('es', $result['items'][1]['properties']['html-lang']); + $this->assertArrayHasKey('lang', $result['items'][0]); + $this->assertArrayHasKey('lang', $result['items'][1]); + $this->assertEquals('en', $result['items'][0]['lang']); + $this->assertEquals('es', $result['items'][1]['lang']); } # end method testMultiLanguageInheritance() /** @@ -120,10 +126,17 @@ public function testMultiLanguageFeed() $parser->lang = true; $result = $parser->parse(); - $this->assertEquals('en', $result['items'][0]['properties']['html-lang']); - $this->assertEquals('en', $result['items'][0]['children'][0]['properties']['html-lang']); - $this->assertEquals('es', $result['items'][0]['children'][1]['properties']['html-lang']); - $this->assertEquals('fr', $result['items'][0]['children'][2]['properties']['html-lang']); + $this->assertArrayHasKey('lang', $result['items'][0]); + $this->assertEquals('en', $result['items'][0]['lang']); + + $this->assertArrayHasKey('lang', $result['items'][0]['children'][0]); + $this->assertEquals('en', $result['items'][0]['children'][0]['lang']); + + $this->assertArrayHasKey('lang', $result['items'][0]['children'][1]); + $this->assertEquals('es', $result['items'][0]['children'][1]['lang']); + + $this->assertArrayHasKey('lang', $result['items'][0]['children'][2]); + $this->assertEquals('fr', $result['items'][0]['children'][2]['lang']); } # end method testMultiLanguageFeed() /** @@ -136,7 +149,8 @@ public function testMetaContentLanguage() $parser->lang = true; $result = $parser->parse(); - $this->assertEquals('es', $result['items'][0]['properties']['html-lang']); + $this->assertArrayHasKey('lang', $result['items'][0]); + $this->assertEquals('es', $result['items'][0]['lang']); } # end method testMetaContentLanguage() /** @@ -230,7 +244,26 @@ public function testVoxpelliCom() $parser->lang = true; $result = $parser->parse(); - $this->assertEquals('sv', $result['items'][0]['properties']['html-lang']); + $this->assertArrayHasKey('lang', $result['items'][0]); + $this->assertEquals('sv', $result['items'][0]['lang']); } # end method testVoxpelliCom() + + /** + * @see https://github.com/indieweb/php-mf2/issues/96#issuecomment-304457341 + */ + public function testNoLangInParsedProperties() { + $input = '
+

En svensk titel

+
With an english summary
+
Och svensk huvudtext
+
'; + $parser = new Parser($input); + $parser->lang = true; + $result = $parser->parse(); + + $this->assertArrayNotHasKey('lang', $result['items'][0]['properties']); + $this->assertArrayHasKey('lang', $result['items'][0]); + } + }