diff options
author | Matt Farina <[email protected]> | 2013-05-27 17:38:02 -0400 |
---|---|---|
committer | Matt Farina <[email protected]> | 2013-05-27 17:38:02 -0400 |
commit | 0e7cd49d390160603563c17c76dea82eba1824b9 (patch) | |
tree | beae415abb00ddcaa025d5b93aff793c5dfd8b07 /test/HTML5/Serializer/OutputRulesTest.php | |
parent | 3f7e489e0eab9b34d6c707d13726eaa195bef2eb (diff) |
Seperated the Traverser from the Output generation.
The Traverser now simply walks through a document. The OutputRules convert
the nodes into output html. The rules is a configurable options. By default
OutputRules will generate html close to the html5 that was parsed. Alternate
rule implementation (e.g., minify rules, pretty spacing rules) can be set
as the default or on an individual case.
Diffstat (limited to 'test/HTML5/Serializer/OutputRulesTest.php')
-rw-r--r-- | test/HTML5/Serializer/OutputRulesTest.php | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/test/HTML5/Serializer/OutputRulesTest.php b/test/HTML5/Serializer/OutputRulesTest.php new file mode 100644 index 0000000..3322e5a --- /dev/null +++ b/test/HTML5/Serializer/OutputRulesTest.php @@ -0,0 +1,103 @@ +<?php +namespace HTML5\Tests; + +use \HTML5\Serializer\OutputRules; +use \HTML5\Serializer\Traverser; +use \HTML5\Parser; + +require_once __DIR__ . '/../TestCase.php'; + +class OutputRulesTest extends \HTML5\Tests\TestCase { + + protected $markup = '<!doctype html> + <html lang="en"> + <head> + <meta charset="utf-8"> + <title>Test</title> + </head> + <body> + <p>This is a test.</p> + </body> + </html>'; + + /** + * Using reflection we make a protected method accessible for testing. + * + * @param string $name + * The name of the method on the Traverser class to test. + * + * @return \ReflectionMethod + * \ReflectionMethod for the specified method + */ + function getProtectedMethod($name) { + $class = new \ReflectionClass('\HTML5\Serializer\OutputRules'); + $method = $class->getMethod($name); + $method->setAccessible(true); + return $method; + } + + function getOutputRules($options = array()) { + $options = $options + \HTML5::options(); + $stream = fopen('php://temp', 'w'); + $dom = \HTML5::loadHTML($this->markup); + $t = new Traverser($dom, $stream, $options); + + $o = new OutputRules($t, $stream, $options); + + return array($o, $stream); + } + + function testText() { + $dom = \HTML5::loadHTML('<!doctype html> + <html lang="en"> + <head> + <script>baz();</script> + </head> + </html>'); + + $stream = fopen('php://temp', 'w'); + $t = new Traverser($dom, $stream, \HTML5::options()); + $o = new OutputRules($t, $stream, \HTML5::options()); + + $list = $dom->getElementsByTagName('script'); + $o->text($list->item(0)->childNodes->item(0)); + $this->assertEquals('baz();', stream_get_contents($stream, -1, 0)); + } + + function testNl() { + list($o, $s) = $this->getOutputRules(); + + $m = $this->getProtectedMethod('nl'); + $m->invoke($o); + $this->assertEquals(PHP_EOL, stream_get_contents($s, -1, 0)); + } + + function testWr() { + list($o, $s) = $this->getOutputRules(); + + $m = $this->getProtectedMethod('wr'); + $m->invoke($o, 'foo'); + $this->assertEquals('foo', stream_get_contents($s, -1, 0)); + } + + function testEnc() { + + // Test basic escaping of text. + $tests = array( + '&\'<>"' => '&'<>"', + 'This + is. a < test' => 'This + is. a < test', + ); + + list($o, $s) = $this->getOutputRules(); + $m = $this->getProtectedMethod('enc'); + foreach ($tests as $test => $expected) { + $this->assertEquals($expected, $m->invoke($o, $test)); + } + + list($o, $s) = $this->getOutputRules(array('encode' => TRUE)); + $m = $this->getProtectedMethod('enc'); + + $this->assertEquals('.+#', $m->invoke($o, '.+#')); + } + +}
\ No newline at end of file |