Skip to content

Commit

Permalink
Merge pull request #61 from goetas/issue_50
Browse files Browse the repository at this point in the history
Handling empty documents
  • Loading branch information
technosophos committed Oct 1, 2014
2 parents 496b803 + 75fe1de commit f50e879
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/HTML5/Serializer/OutputRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,10 @@ public function setTraverser(\Masterminds\HTML5\Serializer\Traverser $traverser)
public function document($dom)
{
$this->doctype();
$this->traverser->node($dom->documentElement);
$this->nl();
if ($dom->documentElement) {
$this->traverser->node($dom->documentElement);
$this->nl();
}
}

protected function doctype()
Expand Down
13 changes: 13 additions & 0 deletions test/HTML5/Serializer/OutputRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ public function testDocument()
$this->assertEquals($expected, stream_get_contents($stream, - 1, 0));
}

public function testEmptyDocument()
{
$dom = $this->html5->loadHTML('');

$stream = fopen('php://temp', 'w');
$r = new OutputRules($stream, $this->html5->getOptions());
$t = new Traverser($dom, $stream, $r, $this->html5->getOptions());

$r->document($dom);
$expected = '<!DOCTYPE html>' . PHP_EOL;
$this->assertEquals($expected, stream_get_contents($stream, - 1, 0));
}

public function testDoctype()
{
$dom = $this->html5->loadHTML('<!doctype html><html lang="en"><body>foo</body></html>');
Expand Down

0 comments on commit f50e879

Please sign in to comment.