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 getTraverserProtectedProperty($name) { $class = new \ReflectionClass('\HTML5\Serializer\Traverser'); $property = $class->getProperty($name); $property->setAccessible(true); return $property; } function getOutputRules($options = array()) { $options = $options + \HTML5::options(); $stream = fopen('php://temp', 'w'); $dom = \HTML5::loadHTML($this->markup); $t = new Traverser($dom, $stream, $options); $p = $this->getTraverserProtectedProperty('rules'); $o = $p->getValue($t); return array($o, $stream); } function testDocument() { $dom = \HTML5::loadHTML('foo'); $stream = fopen('php://temp', 'w'); $t = new Traverser($dom, $stream, \HTML5::options()); $p = $this->getTraverserProtectedProperty('rules'); $o = $p->getValue($t); $o->document($dom); $this->assertEquals("\nfoo\n", stream_get_contents($stream, -1, 0)); } function testDoctype() { $dom = \HTML5::loadHTML('foo'); $stream = fopen('php://temp', 'w'); $t = new Traverser($dom, $stream, \HTML5::options()); $p = $this->getTraverserProtectedProperty('rules'); $o = $p->getValue($t); $m = $this->getProtectedMethod('doctype'); $m->invoke($o, 'foo'); $this->assertEquals("\n", stream_get_contents($stream, -1, 0)); } function testElement() { $dom = \HTML5::loadHTML('
foo bar baz
'); $stream = fopen('php://temp', 'w'); $t = new Traverser($dom, $stream, \HTML5::options()); $p = $this->getTraverserProtectedProperty('rules'); $o = $p->getValue($t); $list = $dom->getElementsByTagName('div'); $o->element($list->item(0)); $this->assertEquals('
foo bar baz
', stream_get_contents($stream, -1, 0)); } function testOpenTag() { $dom = \HTML5::loadHTML('
foo bar baz
'); $stream = fopen('php://temp', 'w'); $t = new Traverser($dom, $stream, \HTML5::options()); $p = $this->getTraverserProtectedProperty('rules'); $o = $p->getValue($t); $list = $dom->getElementsByTagName('div'); $m = $this->getProtectedMethod('openTag'); $m->invoke($o, $list->item(0)); $this->assertEquals('
', stream_get_contents($stream, -1, 0)); } function testCData() { $dom = \HTML5::loadHTML('
'); $stream = fopen('php://temp', 'w'); $t = new Traverser($dom, $stream, \HTML5::options()); $p = $this->getTraverserProtectedProperty('rules'); $o = $p->getValue($t); $list = $dom->getElementsByTagName('div'); $o->cdata($list->item(0)->childNodes->item(0)); $this->assertEquals('', stream_get_contents($stream, -1, 0)); } function testComment() { $dom = \HTML5::loadHTML('
'); $stream = fopen('php://temp', 'w'); $t = new Traverser($dom, $stream, \HTML5::options()); $p = $this->getTraverserProtectedProperty('rules'); $o = $p->getValue($t); $list = $dom->getElementsByTagName('div'); $o->comment($list->item(0)->childNodes->item(0)); $this->assertEquals('', stream_get_contents($stream, -1, 0)); } function testText() { $dom = \HTML5::loadHTML(' '); $stream = fopen('php://temp', 'w'); $t = new Traverser($dom, $stream, \HTML5::options()); $p = $this->getTraverserProtectedProperty('rules'); $o = $p->getValue($t); $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_entities' => TRUE)); $m = $this->getProtectedMethod('enc'); $this->assertEquals('.+#', $m->invoke($o, '.+#')); } function testAttrs() { $dom = \HTML5::loadHTML('
foo bar baz
'); $stream = fopen('php://temp', 'w'); $t = new Traverser($dom, $stream, \HTML5::options()); $p = $this->getTraverserProtectedProperty('rules'); $o = $p->getValue($t); $list = $dom->getElementsByTagName('div'); $m = $this->getProtectedMethod('attrs'); $m->invoke($o, $list->item(0)); $content = stream_get_contents($stream, -1, 0); $this->assertEquals(' id="foo" class="bar baz" disabled', $content); } function testSvg() { $dom = \HTML5::loadHTML('
foo bar baz
'); $stream = fopen('php://temp', 'w'); $t = new Traverser($dom, $stream, \HTML5::options()); $p = $this->getTraverserProtectedProperty('rules'); $o = $p->getValue($t); $list = $dom->getElementsByTagName('svg'); $o->element($list->item(0)); $contents = stream_get_contents($stream, -1, 0); $this->assertRegExp('||', $contents); $this->assertRegExp('||', $contents); $this->assertRegExp('||', $contents); } function testMath() { $dom = \HTML5::loadHTML('
foo bar baz
x ± y '); $stream = fopen('php://temp', 'w'); $t = new Traverser($dom, $stream, \HTML5::options()); $p = $this->getTraverserProtectedProperty('rules'); $o = $p->getValue($t); $list = $dom->getElementsByTagName('math'); $o->element($list->item(0)); $content = stream_get_contents($stream, -1, 0); $this->assertRegExp('||', $content); $this->assertRegExp('||', $content); } }