Skip to content

Commit

Permalink
Merge pull request #165 from Zegnat/fix-e-html-output
Browse files Browse the repository at this point in the history
Stop adding whitespace in html that is not in the source document
  • Loading branch information
aaronpk authored Mar 25, 2018
2 parents 2c6677d + 5689f8b commit 5eeef8b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
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

0 comments on commit 5eeef8b

Please sign in to comment.