From 0e7cd49d390160603563c17c76dea82eba1824b9 Mon Sep 17 00:00:00 2001 From: Matt Farina Date: Mon, 27 May 2013 17:38:02 -0400 Subject: 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. --- test/HTML5/Serializer/OutputRulesTest.php | 103 ++++++++++++++++++++++++++++++ test/HTML5/Serializer/SerializerTest.php | 11 ++-- test/HTML5/Serializer/TraverserTest.php | 60 +---------------- 3 files changed, 111 insertions(+), 63 deletions(-) create mode 100644 test/HTML5/Serializer/OutputRulesTest.php (limited to 'test/HTML5/Serializer') 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 @@ + + + + + Test + + +

This is a test.

+ + '; + + /** + * 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(' + + + + + '); + + $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 diff --git a/test/HTML5/Serializer/SerializerTest.php b/test/HTML5/Serializer/SerializerTest.php index 977f7dd..4a28c54 100644 --- a/test/HTML5/Serializer/SerializerTest.php +++ b/test/HTML5/Serializer/SerializerTest.php @@ -20,7 +20,8 @@ class SerializerTest extends \HTML5\Tests\TestCase { */ protected function cycle($html) { $dom = \HTML5::loadHTML($html); - $ser = new Serializer($dom); + $options = \HTML5::options(); + $ser = new Serializer($dom, $options); $out = $ser->saveHTML(); return $out; @@ -44,7 +45,7 @@ class SerializerTest extends \HTML5\Tests\TestCase { $dom = \HTML5::loadHTML($html); $this->assertTrue($dom instanceof \DOMDocument, "Canary"); - $ser = new Serializer($dom, FALSE); + $ser = new Serializer($dom, \HTML5::options()); $out = $ser->saveHTML(); $this->assertTrue(count($out) >= count($html), 'Byte counts'); @@ -59,7 +60,7 @@ class SerializerTest extends \HTML5\Tests\TestCase { $dom = \HTML5::loadHTML($html); $this->assertTrue($dom instanceof \DOMDocument, "Canary"); - $ser = new Serializer($dom, FALSE); + $ser = new Serializer($dom, \HTML5::options()); $out = fopen("php://temp", "w"); $ser->save($out); @@ -99,10 +100,10 @@ class SerializerTest extends \HTML5\Tests\TestCase { $res = $this->cycle($this->prepareHtml('This is a test.')); $this->assertRegExp('|This is a test.|', $res); - $res = $this->cycle('This + $res = $this->cycle($this->prepareHtml('This is a - test.'); + test.')); // Check that newlines are there, but don't count spaces. $this->assertRegExp('|This\n\s*is\n\s*a\n\s*test.|', $res); diff --git a/test/HTML5/Serializer/TraverserTest.php b/test/HTML5/Serializer/TraverserTest.php index fb8ed34..e160ce6 100644 --- a/test/HTML5/Serializer/TraverserTest.php +++ b/test/HTML5/Serializer/TraverserTest.php @@ -8,8 +8,6 @@ require_once __DIR__ . '/../TestCase.php'; class TraverserTest extends \HTML5\Tests\TestCase { - // Dummy markup to parse then try to traverse. Note, not using any html5 - // so we can use the old parser until ours is complete. protected $markup = ' @@ -40,7 +38,7 @@ class TraverserTest extends \HTML5\Tests\TestCase { function getTraverser() { $stream = fopen('php://temp', 'w'); $dom = \HTML5::loadHTML($this->markup); - $t = new Traverser($dom, $stream); + $t = new Traverser($dom, $stream, \HTML5::options()); // We return both the traverser and stream so we can pull from it. return array($t, $stream); @@ -54,62 +52,8 @@ class TraverserTest extends \HTML5\Tests\TestCase { $dom = \HTML5::loadHTML($this->markup); - $t = new Traverser($dom, $stream); + $t = new Traverser($dom, $stream, \HTML5::options()); $this->assertInstanceOf('\HTML5\Serializer\Traverser', $t); } - - function testNl() { - list($t, $s) = $this->getTraverser(); - - $m = $this->getProtectedMethod('nl'); - $m->invoke($t); - $this->assertEquals(PHP_EOL, stream_get_contents($s, -1, 0)); - } - - function testWr() { - list($t, $s) = $this->getTraverser(); - - $m = $this->getProtectedMethod('wr'); - $m->invoke($t, 'foo'); - $this->assertEquals('foo', stream_get_contents($s, -1, 0)); - } - - function testText() { - $dom = \HTML5::loadHTML(' - - - - - '); - - $stream = fopen('php://temp', 'w'); - $t = new Traverser($dom, $stream); - $m = $this->getProtectedMethod('text'); - - $list = $dom->getElementsByTagName('script'); - $m->invoke($t, $list->item(0)->childNodes->item(0)); - $this->assertEquals('baz();', stream_get_contents($stream, -1, 0)); - } - - function testEnc() { - - // Test basic escaping of text. - $tests = array( - '&\'<>"' => '&'<>"', - 'This + is. a < test' => 'This + is. a < test', - ); - - list($t, $s) = $this->getTraverser(); - $m = $this->getProtectedMethod('enc'); - foreach ($tests as $test => $expected) { - $this->assertEquals($expected, $m->invoke($t, $test)); - } - - list($t, $s) = $this->getTraverser(); - $t->encodeOutput(TRUE); - $m = $this->getProtectedMethod('enc'); - - $this->assertEquals('.+#', $m->invoke($t, '.+#')); - } } \ No newline at end of file -- cgit v1.2.3