From e01b98eb84dcdcd8779c0df8eefd75adc99d087e Mon Sep 17 00:00:00 2001 From: Martijn van der Ven Date: Sun, 25 Mar 2018 13:00:14 +0200 Subject: [PATCH] Stop adding whitespace in html that is not in the source document --- Mf2/Parser.php | 15 +++++++++++---- tests/Mf2/ParserTest.php | 7 +++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Mf2/Parser.php b/Mf2/Parser.php index 4d72f94..cce846e 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 daa8fcb..751dcad 100644 --- a/tests/Mf2/ParserTest.php +++ b/tests/Mf2/ParserTest.php @@ -696,5 +696,12 @@ 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

'; + $output = Mf2\parse($input); + $this->assertEquals('

1

2

', $output['items'][0]['properties']['content'][0]['html']); + } }