Skip to content

Commit 3610bd6

Browse files
authored
Merge pull request #175 from Zegnat/do-not-mutate-domdocument
Do not mutate DOMDocument
2 parents c52fd0d + ffa5b67 commit 3610bd6

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

Mf2/Parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ public function __construct($input, $url = null, $jsonMode = false) {
345345
@$doc->loadHTML(unicodeToHtmlEntities($input));
346346
}
347347
} elseif (is_a($input, 'DOMDocument')) {
348-
$doc = $input;
348+
$doc = clone $input;
349349
} else {
350350
$doc = new DOMDocument();
351351
@$doc->loadHTML('');

tests/Mf2/ParserTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,5 +790,27 @@ public function testHtml5OptionalPEndTag() {
790790
$this->assertEquals('Name', $output['items'][0]['properties']['name'][0]);
791791
}
792792

793+
/**
794+
* Make sure the parser does not mutate any DOMDocument instances passed to the constructor.
795+
* @see https://github.com/indieweb/php-mf2/issues/174
796+
* @see https://github.com/microformats/mf2py/issues/104
797+
*/
798+
public function testNotMutatingPassedInDOM() {
799+
$input = file_get_contents(__DIR__ . '/snarfed.org.html');
800+
801+
// Use same parsing as Parser::__construct(), twice to have a comparison object.
802+
libxml_use_internal_errors(true);
803+
$refDoc = new \DOMDocument();
804+
@$refDoc->loadHTML(Mf2\unicodeToHtmlEntities($input));
805+
$inputDoc = new \DOMDocument();
806+
@$inputDoc->loadHTML(Mf2\unicodeToHtmlEntities($input));
807+
808+
// For completion sake, test PHP itself.
809+
$this->assertEquals($refDoc, $inputDoc, 'PHP could not create identical DOMDocument instances.');
810+
811+
// Parse one DOMDocument instance, and test if it is still the same as the other.
812+
Mf2\parse($inputDoc, 'http://snarfed.org/2013-10-23_oauth-dropins');
813+
$this->assertEquals($refDoc, $inputDoc, 'Parsing mutated the DOMDocument.');
814+
}
793815
}
794816

0 commit comments

Comments
 (0)