Skip to content

Commit

Permalink
Add support and test for alternative HTML5 parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Zegnat committed Mar 28, 2018
1 parent 347d301 commit 2572b0c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
9 changes: 7 additions & 2 deletions Mf2/Parser.php
Expand Up @@ -337,8 +337,13 @@ class Parser {
public function __construct($input, $url = null, $jsonMode = false) {
libxml_use_internal_errors(true);
if (is_string($input)) {
$doc = new DOMDocument();
@$doc->loadHTML(unicodeToHtmlEntities($input));
if (class_exists('Masterminds\\HTML5')) {
$doc = new \Masterminds\HTML5(array('disable_html_ns' => true));
$doc = $doc->loadHTML($input);
} else {
$doc = new DOMDocument();
@$doc->loadHTML(unicodeToHtmlEntities($input));
}
} elseif (is_a($input, 'DOMDocument')) {
$doc = $input;
} else {
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -23,6 +23,7 @@
},
"license": "CC0-1.0",
"suggest": {
"barnabywalters/mf-cleaner": "To more easily handle the canonical data php-mf2 gives you"
"barnabywalters/mf-cleaner": "To more easily handle the canonical data php-mf2 gives you",
"masterminds/html5": "Alternative HTML parser for PHP, for better HTML5 support."
}
}
14 changes: 14 additions & 0 deletions tests/Mf2/ParserTest.php
Expand Up @@ -776,5 +776,19 @@ public function testUniqueAndAlphabeticalMfClasses() {
$this->assertEquals(array('h-cite', 'h-entry'), $output['items'][0]['type']);
}

/**
* The default DOMDocument parser will trip here because it does not know HTML5 elements.
* @see https://html.spec.whatwg.org/#optional-tags:the-p-element
**/
public function testHtml5OptionalPEndTag() {
if (!class_exists('Masterminds\\HTML5')) {
$this->markTestSkipped('masterminds/html5 is required for this test.');
}
$input = '<div class="h-entry"><p class="p-name">Name<article>Not Name</article></div>';
$output = Mf2\parse($input);

$this->assertEquals('Name', $output['items'][0]['properties']['name'][0]);
}

}

0 comments on commit 2572b0c

Please sign in to comment.