Skip to content
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

Fix parsing of lang attribute #124

Merged
merged 1 commit 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
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]);
}

}