saveHTML();
return $out;
}
/**
* Wrap a html5 fragment in a html5 document to run through the parser.
*
* @param string $markup
*
* @return string
* html5 fragment wrapped in a document.
*/
protected function prepareHtml($markup) {
return '
' . $markup . '';
}
public function testSaveHTML() {
$html = 'test';
$dom = \HTML5::loadHTML($html);
$this->assertTrue($dom instanceof \DOMDocument, "Canary");
$ser = new Serializer($dom, \HTML5::options());
$out = $ser->saveHTML();
$this->assertTrue(count($out) >= count($html), 'Byte counts');
$this->assertRegExp('//', $out, 'Has DOCTYPE.');
$this->assertRegExp('/test<\/body>/', $out, 'Has body text.');
}
public function testSave() {
$html = 'test';
$dom = \HTML5::loadHTML($html);
$this->assertTrue($dom instanceof \DOMDocument, "Canary");
// Test saving to a stream.
$ser = new Serializer($dom, \HTML5::options());
$out = fopen("php://temp", "w");
$ser->save($out);
rewind($out);
$res = stream_get_contents($out);
$this->assertTrue(count($res) >= count($html));
// Test saving to a file on the file system.
$tmpfname = tempnam(sys_get_temp_dir(), "html5-php");
$ser = new Serializer($dom, \HTML5::options());
$ser->save($tmpfname);
$content = file_get_contents($tmpfname);
$this->assertRegExp('|test|', $content);
unlink($tmpfname);
}
public function testElements() {
// Should have content.
$res = $this->cycle($this->prepareHtml('FOO
'));
$this->assertRegExp('|FOO
|', $res);
// Should be empty
$res = $this->cycle($this->prepareHtml(''));
$this->assertRegExp('||', $res);
// Should have no closing tag.
$res = $this->cycle($this->prepareHtml('
'));
$this->assertRegExp('|
|', $res);
}
public function testAttributes() {
$res = $this->cycle($this->prepareHtml('FOO
'));
$this->assertRegExp('|FOO
|', $res);
// XXX: Note that spec does NOT require attrs in the same order.
$res = $this->cycle($this->prepareHtml('FOO
'));
$this->assertRegExp('|FOO
|', $res);
$res = $this->cycle($this->prepareHtml('FOO
'));
$this->assertRegExp('|FOO
|', $res);
}
public function testPCData() {
$res = $this->cycle($this->prepareHtml('This is a test.'));
$this->assertRegExp('|This is a test.|', $res);
$res = $this->cycle($this->prepareHtml('This
is
a
test.'));
// Check that newlines are there, but don't count spaces.
$this->assertRegExp('|This\n\s*is\n\s*a\n\s*test.|', $res);
$res = $this->cycle($this->prepareHtml('This is a test.'));
$this->assertRegExp('|This is a test.|', $res);
}
public function testUnescaped() {
$res = $this->cycle($this->prepareHtml(''));
$this->assertRegExp('|2 < 1|', $res);
$res = $this->cycle($this->prepareHtml(''));
$this->assertRegExp('|div>div>div|', $res);
}
public function testEntities() {
$res = $this->cycle($this->prepareHtml('Apples & bananas.'));
$this->assertRegExp('|Apples & bananas.|', $res);
}
public function testComment() {
$res = $this->cycle($this->prepareHtml('ab'));
$this->assertRegExp('||', $res);
}
public function testCDATA() {
$res = $this->cycle($this->prepareHtml('a a test. ]]>b'));
$this->assertRegExp('| a test\. \]\]>|', $res);
}
}