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

Implement HTML id parsing proposal #207

Merged
merged 5 commits into from Dec 25, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Mf2/Parser.php
Expand Up @@ -1141,6 +1141,10 @@ public function parseH(\DOMElement $e, $is_backcompat = false, $has_nested_mf =
'type' => $mfTypes,
'properties' => $return
);

if($e->getAttribute('id') !== '') {
$parsed['id'] = $e->getAttribute("id");
}

if($this->lang) {
// Language
Expand Down
39 changes: 39 additions & 0 deletions tests/Mf2/ParseHtmlIdTest.php
@@ -0,0 +1,39 @@
<?php
/**
* Tests of the parsing methods within mf2\Parser
*/

namespace Mf2\Parser\Test;

use Mf2;
use Mf2\Parser;
use PHPUnit_Framework_TestCase;

/**
*
*/
class ParseHtmlIdTest extends PHPUnit_Framework_TestCase {
public function setUp() {
date_default_timezone_set('Europe/London');
}

/** as per https://github.com/microformats/microformats2-parsing/issues/44 */
public function testParserIdAttribute() {
$test = '<div class="h-feed" id="recentArticles"><h2 class="p-name">Recent Articles</h2><div class="hentry" id="article">Lorem Ipsum</div>
<div class="p-author h-card" id="theAuthor">Max Mustermann</div>
<div class="h-entry" id="">empty id should not be parsed</div>
<div class="h-entry" id="0">id=0 should work and not be treated false-y</div>
</div>';
$result = Mf2\parse($test);
$this->assertArrayHasKey('id', $result['items'][0]);
$this->assertEquals('recentArticles', $result['items'][0]['id']);
$this->assertArrayHasKey('id', $result['items'][0]['children'][0]);
$this->assertEquals('article', $result['items'][0]['children'][0]['id']);
$this->assertArrayHasKey('id', $result['items'][0]['properties']['author'][0]);
$this->assertEquals('theAuthor', $result['items'][0]['properties']['author'][0]['id']);
$this->assertArrayNotHasKey('id', $result['items'][0]['children'][1]);
$this->assertArrayHasKey('id', $result['items'][0]['children'][2]);
$this->assertEquals('0', $result['items'][0]['children'][2]['id']);
}
}