Skip to content

Commit 5eeef8b

Browse files
authored
Merge pull request #165 from Zegnat/fix-e-html-output
Stop adding whitespace in html that is not in the source document
2 parents 2c6677d + 5689f8b commit 5eeef8b

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

Mf2/Parser.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -897,10 +897,17 @@ public function parseE(\DOMElement $e) {
897897
// TODO: as it is this is not relative to only children, make this .// and rerun tests
898898
$this->resolveChildUrls($e);
899899

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

905912
$return = array(
906913
'html' => unicodeTrim($html),

tests/Mf2/ParserTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,13 @@ public function testMultiLevelRecursion() {
696696
$this->assertEquals('Comment D', $three['children'][1]['properties']['name'][0]);
697697
$this->assertEquals('Four', $output['items'][0]['children'][3]['properties']['name'][0]);
698698
}
699+
700+
public function testNoErrantWhitespaceOnEHtml()
701+
{
702+
$input = '<div class="h-entry"><div class="e-content"><p>1</p><p>2</p></div></div>';
703+
$output = Mf2\parse($input);
704+
$this->assertEquals('<p>1</p><p>2</p>', $output['items'][0]['properties']['content'][0]['html']);
705+
}
699706

700707
/**
701708
* @see https://github.com/indieweb/php-mf2/issues/158

0 commit comments

Comments
 (0)