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); $r = new OutputRules($stream, $options); $t = new Traverser($dom, $stream, $r, $options); return array($r, $stream); } function testDocument() { $dom = \HTML5::loadHTML('foo'); $stream = fopen('php://temp', 'w'); $r = new OutputRules($stream, \HTML5::options()); $t = new Traverser($dom, $stream, $r, \HTML5::options()); $r->document($dom); $expected = '' . PHP_EOL . 'foo' . PHP_EOL; $this->assertEquals($expected, stream_get_contents($stream, -1, 0)); } function testDoctype() { $dom = \HTML5::loadHTML('foo'); $stream = fopen('php://temp', 'w'); $r = new OutputRules($stream, \HTML5::options()); $t = new Traverser($dom, $stream, $r, \HTML5::options()); $m = $this->getProtectedMethod('doctype'); $m->invoke($r, 'foo'); $this->assertEquals("" . PHP_EOL, stream_get_contents($stream, -1, 0)); } function testElement() { $dom = \HTML5::loadHTML('
foo bar baz
'); $stream = fopen('php://temp', 'w'); $r = new OutputRules($stream, \HTML5::options()); $t = new Traverser($dom, $stream, $r, \HTML5::options()); $list = $dom->getElementsByTagName('div'); $r->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'); $r = new OutputRules($stream, \HTML5::options()); $t = new Traverser($dom, $stream, $r, \HTML5::options()); $list = $dom->getElementsByTagName('div'); $m = $this->getProtectedMethod('openTag'); $m->invoke($r, $list->item(0)); $this->assertEquals('
', stream_get_contents($stream, -1, 0)); } function testCData() { $dom = \HTML5::loadHTML('
'); $stream = fopen('php://temp', 'w'); $r = new OutputRules($stream, \HTML5::options()); $t = new Traverser($dom, $stream, $r, \HTML5::options()); $list = $dom->getElementsByTagName('div'); $r->cdata($list->item(0)->childNodes->item(0)); $this->assertEquals('', stream_get_contents($stream, -1, 0)); $dom = \HTML5::loadHTML('
'); $dom->getElementById('foo')->appendChild(new \DOMCdataSection("]]>Foo<[![CDATA test ]]>")); $stream = fopen('php://temp', 'w'); $r = new OutputRules($stream, \HTML5::options()); $t = new Traverser($dom, $stream, $r, \HTML5::options()); $list = $dom->getElementsByTagName('div'); $r->cdata($list->item(0)->childNodes->item(0)); $this->assertEquals('Foo<[![CDATA test ]]]]>]]>', stream_get_contents($stream, -1, 0)); } function testComment() { $dom = \HTML5::loadHTML('
'); $stream = fopen('php://temp', 'w'); $r = new OutputRules($stream, \HTML5::options()); $t = new Traverser($dom, $stream, $r, \HTML5::options()); $list = $dom->getElementsByTagName('div'); $r->comment($list->item(0)->childNodes->item(0)); $this->assertEquals('', stream_get_contents($stream, -1, 0)); $dom = \HTML5::loadHTML('
'); $dom->getElementById('foo')->appendChild(new \DOMComment(' --> Foo -->')); $stream = fopen('php://temp', 'w'); $r = new OutputRules($stream, \HTML5::options()); $t = new Traverser($dom, $stream, $r, \HTML5::options()); $list = $dom->getElementsByTagName('div'); $r->comment($list->item(0)->childNodes->item(0)); // Could not find more definitive guidelines on what this should be. Went with // what the HTML5 spec says and what \DOMDocument::saveXML() produces. $this->assertEquals(' --> Foo -->-->', stream_get_contents($stream, -1, 0)); } function testText() { $dom = \HTML5::loadHTML(' '); $stream = fopen('php://temp', 'w'); $r = new OutputRules($stream, \HTML5::options()); $t = new Traverser($dom, $stream, $r, \HTML5::options()); $list = $dom->getElementsByTagName('script'); $r->text($list->item(0)->childNodes->item(0)); $this->assertEquals('baz();', stream_get_contents($stream, -1, 0)); $dom = \HTML5::loadHTML(' '); $dom->getElementById('foo')->appendChild(new \DOMText('')); $stream = fopen('php://temp', 'w'); $r = new OutputRules($stream, \HTML5::options()); $t = new Traverser($dom, $stream, $r, \HTML5::options()); $item = $dom->getElementById('foo'); $r->text($item->firstChild); $this->assertEquals('<script>alert("hi");</script>', 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 getEncData(){ return array( array(FALSE, '&\'<>"', '&\'<>"', '&'<>"'), array(FALSE, 'This + is. a < test', 'This + is. a < test', 'This + is. a < test'), array(FALSE, '.+#', '.+#', '.+#'), array(TRUE, '.+#\'', '.+#\'', '.+#''), array(TRUE, '&".<', '&".<', '&".<'), array(TRUE, '&\'<>"', '&\'<>"', '&'<>"'), array(TRUE, "\xc2\xa0\"'", ' "\'', ' "''), ); } /** * Test basic encoding of text. * @dataProvider getEncData */ function testEnc($isAttribute, $test, $expected, $expectedEncoded) { list($o, $s) = $this->getOutputRules(); $m = $this->getProtectedMethod('enc'); $this->assertEquals($expected, $m->invoke($o, $test, $isAttribute)); list($o, $s) = $this->getOutputRules(array('encode_entities' => TRUE)); $m = $this->getProtectedMethod('enc'); $this->assertEquals($expectedEncoded, $m->invoke($o, $test, $isAttribute)); } /** * Test basic encoding of text. * @dataProvider getEncData */ function testEscape($isAttribute, $test, $expected, $expectedEncoded) { list($o, $s) = $this->getOutputRules(); $m = $this->getProtectedMethod('escape'); $this->assertEquals($expected, $m->invoke($o, $test, $isAttribute)); } function testAttrs() { $dom = \HTML5::loadHTML('
foo bar baz
'); $stream = fopen('php://temp', 'w'); $r = new OutputRules($stream, \HTML5::options()); $t = new Traverser($dom, $stream, $r, \HTML5::options()); $list = $dom->getElementsByTagName('div'); $m = $this->getProtectedMethod('attrs'); $m->invoke($r, $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'); $r = new OutputRules($stream, \HTML5::options()); $t = new Traverser($dom, $stream, $r, \HTML5::options()); $list = $dom->getElementsByTagName('svg'); $r->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'); $r = new OutputRules($stream, \HTML5::options()); $t = new Traverser($dom, $stream, $r, \HTML5::options()); $list = $dom->getElementsByTagName('math'); $r->element($list->item(0)); $content = stream_get_contents($stream, -1, 0); $this->assertRegExp('||', $content); $this->assertRegExp('||', $content); } function testProcessorInstruction() { $dom = \HTML5::loadHTMLFragment(''); $stream = fopen('php://temp', 'w'); $r = new OutputRules($stream, \HTML5::options()); $t = new Traverser($dom, $stream, $r, \HTML5::options()); $r->processorInstruction($dom->firstChild); $content = stream_get_contents($stream, -1, 0); $this->assertRegExp('|<\?foo bar \?>|', $content); } }