Skip to content

Commit

Permalink
Merge pull request microformats#124 from gRegorLove/issue96
Browse files Browse the repository at this point in the history
Fix parsing of lang attribute
  • Loading branch information
aaronpk authored May 27, 2017
2 parents dc0d90d + fb21982 commit e7c0cac
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 22 deletions.
16 changes: 8 additions & 8 deletions Mf2/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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);

Expand All @@ -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;
}
Expand Down
61 changes: 47 additions & 14 deletions tests/Mf2/ParseLanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()

/**
Expand All @@ -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()

/**
Expand All @@ -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()

/**
Expand All @@ -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()

/**
Expand All @@ -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()

/**
Expand All @@ -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()

/**
Expand All @@ -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()

/**
Expand All @@ -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()

/**
Expand All @@ -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()

/**
Expand Down Expand Up @@ -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 = '<div class="h-entry" lang="sv" id="postfrag123">
<h1 class="p-name">En svensk titel</h1>
<div class="e-content" lang="en">With an <em>english</em> summary</div>
<div class="e-content">Och <em>svensk</em> huvudtext</div>
</div>';
$parser = new Parser($input);
$parser->lang = true;
$result = $parser->parse();

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

}

0 comments on commit e7c0cac

Please sign in to comment.