Skip to content

Commit 64aab44

Browse files
authored
Merge pull request #169 from Zegnat/optional-html5-parser
Add support and test for alternative HTML5 parser
2 parents 62d395b + 73b29de commit 64aab44

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@ php:
77
- 7.1
88
- 7.2
99
- nightly
10+
env:
11+
- COMPOSER_REQUIRE=""
12+
- COMPOSER_REQUIRE="composer require masterminds/html5"
13+
install:
14+
- $COMPOSER_REQUIRE
1015
before_script: composer install

Mf2/Parser.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,13 @@ class Parser {
337337
public function __construct($input, $url = null, $jsonMode = false) {
338338
libxml_use_internal_errors(true);
339339
if (is_string($input)) {
340-
$doc = new DOMDocument();
341-
@$doc->loadHTML(unicodeToHtmlEntities($input));
340+
if (class_exists('Masterminds\\HTML5')) {
341+
$doc = new \Masterminds\HTML5(array('disable_html_ns' => true));
342+
$doc = $doc->loadHTML($input);
343+
} else {
344+
$doc = new DOMDocument();
345+
@$doc->loadHTML(unicodeToHtmlEntities($input));
346+
}
342347
} elseif (is_a($input, 'DOMDocument')) {
343348
$doc = $input;
344349
} else {

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
},
2424
"license": "CC0-1.0",
2525
"suggest": {
26-
"barnabywalters/mf-cleaner": "To more easily handle the canonical data php-mf2 gives you"
26+
"barnabywalters/mf-cleaner": "To more easily handle the canonical data php-mf2 gives you",
27+
"masterminds/html5": "Alternative HTML parser for PHP, for better HTML5 support."
2728
}
2829
}

tests/Mf2/ParserTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,5 +776,19 @@ public function testUniqueAndAlphabeticalMfClasses() {
776776
$this->assertEquals(array('h-cite', 'h-entry'), $output['items'][0]['type']);
777777
}
778778

779+
/**
780+
* The default DOMDocument parser will trip here because it does not know HTML5 elements.
781+
* @see https://html.spec.whatwg.org/#optional-tags:the-p-element
782+
**/
783+
public function testHtml5OptionalPEndTag() {
784+
if (!class_exists('Masterminds\\HTML5')) {
785+
$this->markTestSkipped('masterminds/html5 is required for this test.');
786+
}
787+
$input = '<div class="h-entry"><p class="p-name">Name<article>Not Name</article></div>';
788+
$output = Mf2\parse($input);
789+
790+
$this->assertEquals('Name', $output['items'][0]['properties']['name'][0]);
791+
}
792+
779793
}
780794

0 commit comments

Comments
 (0)