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

Stop adding whitespace in html that is not in the source document #165

Merged
merged 2 commits into from
Mar 25, 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
15 changes: 11 additions & 4 deletions Mf2/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -897,10 +897,17 @@ public function parseE(\DOMElement $e) {
// TODO: as it is this is not relative to only children, make this .// and rerun tests
$this->resolveChildUrls($e);

$html = '';
foreach ($e->childNodes as $node) {
$html .= $node->ownerDocument->saveHTML($node);
}
// Temporarily move all descendants into a separate DocumentFragment.
// This way we can DOMDocument::saveHTML on the entire collection at once.
// Running DOMDocument::saveHTML per node may add whitespace that isn't in source.
// See https://stackoverflow.com/q/38317903
$innerNodes = $e->ownerDocument->createDocumentFragment();
while ($e->hasChildNodes()) {
$innerNodes->appendChild($e->firstChild);
}
$html = $e->ownerDocument->saveHtml($innerNodes);
// Put the nodes back in place.
$e->appendChild($innerNodes);

$return = array(
'html' => unicodeTrim($html),
Expand Down
7 changes: 7 additions & 0 deletions tests/Mf2/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,13 @@ public function testMultiLevelRecursion() {
$this->assertEquals('Comment D', $three['children'][1]['properties']['name'][0]);
$this->assertEquals('Four', $output['items'][0]['children'][3]['properties']['name'][0]);
}

public function testNoErrantWhitespaceOnEHtml()
{
$input = '<div class="h-entry"><div class="e-content"><p>1</p><p>2</p></div></div>';
$output = Mf2\parse($input);
$this->assertEquals('<p>1</p><p>2</p>', $output['items'][0]['properties']['content'][0]['html']);
}

/**
* @see https://github.com/indieweb/php-mf2/issues/158
Expand Down