Skip to content

Commit 4887ea4

Browse files
committed
moves language parsing behind feature flag
1 parent d17880c commit 4887ea4

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

Mf2/Parser.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ class Parser {
277277

278278
public $jsonMode;
279279

280+
/** @var boolean Whether to include experimental language parsing in the result */
281+
public $lang = false;
282+
280283
/**
281284
* Elements upgraded to mf2 during backcompat
282285
* @var SplObjectStorage
@@ -811,9 +814,11 @@ public function parseE(\DOMElement $e) {
811814
'value' => unicodeTrim($this->innerText($e)),
812815
);
813816

814-
// Language
815-
if ( $html_lang = $this->language($e) ) {
816-
$return['html-lang'] = $html_lang;
817+
if($this->lang) {
818+
// Language
819+
if ( $html_lang = $this->language($e) ) {
820+
$return['html-lang'] = $html_lang;
821+
}
817822
}
818823

819824
return $return;
@@ -1073,9 +1078,11 @@ public function parseH(\DOMElement $e, $is_backcompat = false) {
10731078
}
10741079
}
10751080

1076-
// Language
1077-
if ( $html_lang = $this->language($e) ) {
1078-
$return['html-lang'] = $html_lang;
1081+
if($this->lang) {
1082+
// Language
1083+
if ( $html_lang = $this->language($e) ) {
1084+
$return['html-lang'] = $html_lang;
1085+
}
10791086
}
10801087

10811088
// Make sure things are in alphabetical order

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,24 @@ $parser->parse(true, $elementIWant); // returns a document with only the Microfo
229229
230230
```
231231
232+
### Experimental Language Parsing
233+
234+
There is still [ongoing brainstorming](http://microformats.org/wiki/microformats2-parsing-brainstorming#Parse_language_information) around how HTML language attributes should be added to the parsed result. In order to use this feature, you will need to set a flag to opt in.
235+
236+
```php
237+
$doc = '<div class="h-entry" lang="sv" id="postfrag123">
238+
<h1 class="p-name">En svensk titel</h1>
239+
<div class="e-content" lang="en">With an <em>english</em> summary</div>
240+
<div class="e-content">Och <em>svensk</em> huvudtext</div>
241+
</div>';
242+
$parser = new Mf2\Parser($doc);
243+
$parser->lang = true;
244+
$result = $parser->parse();
245+
```
246+
247+
Note that this option is still considered experimental and in development, and the parsed output may change between minor releases.
248+
249+
232250
### Generating output for JSON serialization with JSON-mode
233251
234252
Due to a quirk with the way PHP arrays work, there is an edge case ([reported](https://github.com/indieweb/php-mf2/issues/29) by Tom Morris) in which a document with no rel values, when serialised as JSON, results in an empty object as the rels value rather than an empty array. Replacing this in code with a stdClass breaks PHP iteration over the values.

tests/Mf2/ParseLanguageTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function testHtmlLangOnly()
2323
{
2424
$input = '<html lang="en"> <div class="h-entry">This test is in English.</div> </html>';
2525
$parser = new Parser($input);
26+
$parser->lang = true;
2627
$result = $parser->parse();
2728

2829
$this->assertEquals('en', $result['items'][0]['properties']['html-lang']);
@@ -35,6 +36,7 @@ public function testHEntryLangOnly()
3536
{
3637
$input = '<html> <div class="h-entry" lang="en">This test is in English.</div> </html>';
3738
$parser = new Parser($input);
39+
$parser->lang = true;
3840
$result = $parser->parse();
3941

4042
$this->assertEquals('en', $result['items'][0]['properties']['html-lang']);
@@ -47,6 +49,7 @@ public function testHtmlAndHEntryLang()
4749
{
4850
$input = '<html lang="en"> <div class="h-entry" lang="es">Esta prueba está en español.</div> </html>';
4951
$parser = new Parser($input);
52+
$parser->lang = true;
5053
$result = $parser->parse();
5154

5255
$this->assertEquals('es', $result['items'][0]['properties']['html-lang']);
@@ -59,6 +62,7 @@ public function testFragmentHEntryLangOnly()
5962
{
6063
$input = '<div class="h-entry" lang="en">This test is in English.</div>';
6164
$parser = new Parser($input);
65+
$parser->lang = true;
6266
$result = $parser->parse();
6367

6468
$this->assertEquals('en', $result['items'][0]['properties']['html-lang']);
@@ -71,6 +75,7 @@ public function testFragmentHEntryNoLang()
7175
{
7276
$input = '<div class="h-entry">This test is in English.</div>';
7377
$parser = new Parser($input);
78+
$parser->lang = true;
7479
$result = $parser->parse();
7580

7681
$this->assertFalse(isset($result['items'][0]['properties']['html-lang']));
@@ -97,6 +102,7 @@ public function testMultiLanguageInheritance()
97102
{
98103
$input = '<html lang="en"> <div class="h-entry">This test is in English.</div> <div class="h-entry" lang="es">Esta prueba está en español.</div> </html>';
99104
$parser = new Parser($input);
105+
$parser->lang = true;
100106
$result = $parser->parse();
101107

102108
$this->assertEquals('en', $result['items'][0]['properties']['html-lang']);
@@ -111,6 +117,7 @@ public function testMultiLanguageFeed()
111117
{
112118
$input = '<html> <div class="h-feed" lang="en"> <h1 class="p-name">Test Feed</h1> <div class="h-entry">This test is in English.</div> <div class="h-entry" lang="es">Esta prueba está en español.</div> <div class="h-entry" lang="fr">Ce test est en français.</div> </html>';
113119
$parser = new Parser($input);
120+
$parser->lang = true;
114121
$result = $parser->parse();
115122

116123
$this->assertEquals('en', $result['items'][0]['properties']['html-lang']);
@@ -126,6 +133,7 @@ public function testMetaContentLanguage()
126133
{
127134
$input = '<html> <meta http-equiv="Content-Language" content="es"/> <div class="h-entry">Esta prueba está en español.</div> </html>';
128135
$parser = new Parser($input);
136+
$parser->lang = true;
129137
$result = $parser->parse();
130138

131139
$this->assertEquals('es', $result['items'][0]['properties']['html-lang']);
@@ -219,6 +227,7 @@ public function testVoxpelliCom()
219227
</html>
220228
END;
221229
$parser = new Parser($input);
230+
$parser->lang = true;
222231
$result = $parser->parse();
223232

224233
$this->assertEquals('sv', $result['items'][0]['properties']['html-lang']);

0 commit comments

Comments
 (0)