diff --git a/Mf2/Parser.php b/Mf2/Parser.php index 64bc442..7048794 100644 --- a/Mf2/Parser.php +++ b/Mf2/Parser.php @@ -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), diff --git a/tests/Mf2/ParserTest.php b/tests/Mf2/ParserTest.php index dd75eb2..e5767ab 100644 --- a/tests/Mf2/ParserTest.php +++ b/tests/Mf2/ParserTest.php @@ -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 = '
1
2
1
2
', $output['items'][0]['properties']['content'][0]['html']); + } /** * @see https://github.com/indieweb/php-mf2/issues/158