From d2a32e3c8048de6e6c0d68f53d39d0c75b8a602a Mon Sep 17 00:00:00 2001 From: Martijn van der Ven Date: Sun, 27 May 2018 12:10:35 +0200 Subject: [PATCH 1/2] Add failing test for mutating DOMDocument A DOMDocument instance being passed to the parser should not have changed after parsing. This could potentially trip-up further use of the same DOMDocument instance. See https://github.com/indieweb/php-mf2/issues/174. --- tests/Mf2/ParserTest.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/Mf2/ParserTest.php b/tests/Mf2/ParserTest.php index c8bb68a..9bc4aae 100644 --- a/tests/Mf2/ParserTest.php +++ b/tests/Mf2/ParserTest.php @@ -790,5 +790,27 @@ public function testHtml5OptionalPEndTag() { $this->assertEquals('Name', $output['items'][0]['properties']['name'][0]); } + /** + * Make sure the parser does not mutate any DOMDocument instances passed to the constructor. + * @see https://github.com/indieweb/php-mf2/issues/174 + * @see https://github.com/microformats/mf2py/issues/104 + */ + public function testNotMutatingPassedInDOM() { + $input = file_get_contents(__DIR__ . '/snarfed.org.html'); + + // Use same parsing as Parser::__construct(), twice to have a comparison object. + libxml_use_internal_errors(true); + $refDoc = new \DOMDocument(); + @$refDoc->loadHTML(Mf2\unicodeToHtmlEntities($input)); + $inputDoc = new \DOMDocument(); + @$inputDoc->loadHTML(Mf2\unicodeToHtmlEntities($input)); + + // For completion sake, test PHP itself. + $this->assertEquals($refDoc, $inputDoc, 'PHP could not create identical DOMDocument instances.'); + + // Parse one DOMDocument instance, and test if it is still the same as the other. + Mf2\parse($inputDoc, 'http://snarfed.org/2013-10-23_oauth-dropins'); + $this->assertEquals($refDoc, $inputDoc, 'Parsing mutated the DOMDocument.'); + } } From ffa5b67e30d9df8df69122931cab077694e72c61 Mon Sep 17 00:00:00 2001 From: Martijn van der Ven Date: Sun, 27 May 2018 12:15:04 +0200 Subject: [PATCH 2/2] Fix #174 by cloning input DOMDocument instance --- Mf2/Parser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Mf2/Parser.php b/Mf2/Parser.php index 166efd3..b36555f 100644 --- a/Mf2/Parser.php +++ b/Mf2/Parser.php @@ -345,7 +345,7 @@ public function __construct($input, $url = null, $jsonMode = false) { @$doc->loadHTML(unicodeToHtmlEntities($input)); } } elseif (is_a($input, 'DOMDocument')) { - $doc = $input; + $doc = clone $input; } else { $doc = new DOMDocument(); @$doc->loadHTML('');