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

Do not mutate DOMDocument #175

Merged
merged 2 commits into from
Aug 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Mf2/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
Expand Down
22 changes: 22 additions & 0 deletions tests/Mf2/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
}