Skip to content
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