diff options
Diffstat (limited to 'test/HTML5/Parser')
-rw-r--r-- | test/HTML5/Parser/DOMTreeBuilderTest.php | 91 | ||||
-rw-r--r-- | test/HTML5/Parser/EventStack.php | 6 |
2 files changed, 93 insertions, 4 deletions
diff --git a/test/HTML5/Parser/DOMTreeBuilderTest.php b/test/HTML5/Parser/DOMTreeBuilderTest.php index 6ffae75..a901238 100644 --- a/test/HTML5/Parser/DOMTreeBuilderTest.php +++ b/test/HTML5/Parser/DOMTreeBuilderTest.php @@ -1,11 +1,98 @@ <?php /** * @file - * Test the Scanner. This requires the InputStream tests are all good. + * Test the Tree Builder. */ namespace HTML5\Parser; +use HTML5\Elements; + require_once __DIR__ . '/../TestCase.php'; -class DOMTreeParserTest extends \HTML5\Tests\TestCase { +/** + * These tests are functional, not necessarily unit tests. + */ +class DOMTreeBuilderTest extends \HTML5\Tests\TestCase { + + /** + * Convenience function for parsing. + */ + protected function parse($string) { + $treeBuilder = new DOMTreeBuilder(); + $input = new StringInputStream($string); + $scanner = new Scanner($input); + $parser = new Tokenizer($scanner, $treeBuilder); + + $parser->parse(); + + return $treeBuilder->document(); + } + + public function testDocument() { + $html = "<!DOCTYPE html><html></html>"; + $doc = $this->parse($html); + + $this->assertInstanceOf('\DOMDocument', $doc); + $this->assertEquals('html', $doc->documentElement->tagName); + } + + public function testElements() { + $html = "<!DOCTYPE html><html><head><title></title></head><body></body></html>"; + $doc = $this->parse($html); + $root = $doc->documentElement; + + $this->assertEquals('html', $root->tagName); + $this->assertEquals('html', $root->localName); + $this->assertEquals('html', $root->nodeName); + + $this->assertEquals(2, $root->childNodes->length); + $kids = $root->childNodes; + + $this->assertEquals('head', $kids->item(0)->tagName); + $this->assertEquals('body', $kids->item(1)->tagName); + + $head = $kids->item(0); + $this->assertEquals(1, $head->childNodes->length); + $this->assertEquals('title', $head->childNodes->item(0)->tagName); + } + + public function testAttributes() { + $html = "<!DOCTYPE html> + <html> + <head><title></title></head> + <body id='a' class='b c'></body> + </html>"; + $doc = $this->parse($html); + $root = $doc->documentElement; + + $body = $root->GetElementsByTagName('body')->item(0); + $this->assertEquals('body', $body->tagName); + $this->assertTrue($body->hasAttributes()); + $this->assertEquals('a', $body->getAttribute('id')); + $this->assertEquals('b c', $body->getAttribute('class')); + + $body2 = $doc->getElementById('a'); + $this->assertEquals('body', $body2->tagName); + $this->assertEquals('a', $body2->getAttribute('id')); + } + + public function testComment() { + $this->markTestIncomplete("Incomplete."); + } + + public function testCDATA() { + $this->markTestIncomplete("Incomplete."); + } + + public function testText() { + $this->markTestIncomplete("Incomplete."); + } + + public function testParseErrors() { + $this->markTestIncomplete("Incomplete."); + } + + public function testProcessingInstruction() { + $this->markTestIncomplete("Incomplete."); + } } diff --git a/test/HTML5/Parser/EventStack.php b/test/HTML5/Parser/EventStack.php index c9ac20e..1f56ea9 100644 --- a/test/HTML5/Parser/EventStack.php +++ b/test/HTML5/Parser/EventStack.php @@ -1,13 +1,15 @@ <?php namespace HTML5\Parser; +use HTML5\Elements; + /** * This testing class gathers events from a parser and builds a stack of events. * It is useful for checking the output of a tokenizer. * * IMPORTANT: * - * The startTag event also kicks the parser into TEXTMODE_RAW when it encounters + * The startTag event also kicks the parser into TEXT_RAW when it encounters * script or pre tags. This is to match the behavior required by the HTML5 spec, * which says that the tree builder must tell the tokenizer when to switch states. */ @@ -49,7 +51,7 @@ class EventStack implements EventHandler { $args = func_get_args(); $this->store('startTag', $args); if ($name == 'pre' || $name == 'script') { - return Tokenizer::TEXTMODE_RAW; + return Elements::TEXT_RAW; } } |