diff options
author | Technosophos <[email protected]> | 2013-04-19 17:43:28 -0500 |
---|---|---|
committer | Technosophos <[email protected]> | 2013-04-19 17:43:28 -0500 |
commit | 9351d1c046ed3a6a82db3daa7eaab2336d5b204d (patch) | |
tree | 494bde4b282a86132e8817751dd21ea96dc8e4fa /test/HTML5 | |
parent | 4e5458898e6d9a73d3eae7b3213187407a940ce8 (diff) |
Full support for rawtext. Unit tests finished.
Diffstat (limited to 'test/HTML5')
-rw-r--r-- | test/HTML5/Parser/EventStack.php | 9 | ||||
-rw-r--r-- | test/HTML5/Parser/TokenizerTest.php | 57 |
2 files changed, 48 insertions, 18 deletions
diff --git a/test/HTML5/Parser/EventStack.php b/test/HTML5/Parser/EventStack.php index f197855..c9ac20e 100644 --- a/test/HTML5/Parser/EventStack.php +++ b/test/HTML5/Parser/EventStack.php @@ -4,6 +4,12 @@ namespace HTML5\Parser; /** * 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 + * 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. */ class EventStack implements EventHandler { protected $stack; @@ -42,6 +48,9 @@ class EventStack implements EventHandler { public function startTag($name, $attributes = array(), $selfClosing = FALSE) { $args = func_get_args(); $this->store('startTag', $args); + if ($name == 'pre' || $name == 'script') { + return Tokenizer::TEXTMODE_RAW; + } } public function endTag($name) { diff --git a/test/HTML5/Parser/TokenizerTest.php b/test/HTML5/Parser/TokenizerTest.php index 69f90b9..7ec4c76 100644 --- a/test/HTML5/Parser/TokenizerTest.php +++ b/test/HTML5/Parser/TokenizerTest.php @@ -395,38 +395,59 @@ class TokenizerTest extends \HTML5\Tests\TestCase { '<pre>hello</pre</pre>' => 'hello</pre', "<pre>\nhello</pre\n</pre>" => "\nhello</pre\n", '<pre>&</pre>' => '&', + '<pre><!--not a comment--></pre>' => '<!--not a comment-->', + '<pre><![CDATA[not a comment]]></pre>' => '<![CDATA[not a comment]]>', ); foreach ($good as $test => $expects) { - list($tok, $events) = $this->createTokenizer($test); - - $tok->setTextMode(Tokenizer::TEXTMODE_RAW, 'pre'); - $tok->parse(); - - //fprintf(STDOUT, "Test: %s\n", $test); - fprintf(STDOUT, "Test: %s %s\n", $test, print_r($events, TRUE)); - + $events = $this->parse($test); $this->assertEventEquals('startTag', 'pre', $events->get(0)); $this->assertEventEquals('text', $expects, $events->get(1)); $this->assertEventEquals('endTag', 'pre', $events->get(2)); } $bad = array( - '<pre>&</pre' => '&', + '<pre>&</pre' => '&</pre', + '<pre>Hello world' => 'Hello world', ); + foreach ($bad as $test => $expects) { + $events = $this->parse($test); + $this->assertEquals(4, $events->depth(), "Counting events for '$test': " . print_r($events, TRUE)); + $this->assertEventEquals('startTag', 'pre', $events->get(0)); + $this->assertEventError($events->get(1)); + $this->assertEventEquals('text', $expects, $events->get(2)); + } } public function testText() { - $good = array( - 'a<br>b', - '<a>test</a>', - 'a<![[ test ]]>b', - 'a&b', - 'a&b', - 'a& b& c', - ); - $this->markTestIncomplete("Need tag parsing first."); + $events = $this->parse('a<br>b'); + $this->assertEquals(4, $events->depth(), "Events: " . print_r($events, TRUE)); + $this->assertEventEquals('text', 'a', $events->get(0)); + $this->assertEventEquals('startTag', 'br', $events->get(1)); + $this->assertEventEquals('text', 'b', $events->get(2)); + + $events = $this->parse('<a>Test</a>'); + $this->assertEquals(4, $events->depth(), "Events: " . print_r($events, TRUE)); + $this->assertEventEquals('startTag', 'a', $events->get(0)); + $this->assertEventEquals('text', 'Test', $events->get(1)); + $this->assertEventEquals('endTag', 'a', $events->get(2)); + + $events = $this->parse('a<![CDATA[test]]>b'); + $this->assertEquals(4, $events->depth(), "Events: " . print_r($events, TRUE)); + $this->assertEventEquals('text', 'a', $events->get(0)); + $this->assertEventEquals('cdata', 'test', $events->get(1)); + $this->assertEventEquals('text', 'b', $events->get(2)); + + $events = $this->parse('a<!--test-->b'); + $this->assertEquals(4, $events->depth(), "Events: " . print_r($events, TRUE)); + $this->assertEventEquals('text', 'a', $events->get(0)); + $this->assertEventEquals('comment', 'test', $events->get(1)); + $this->assertEventEquals('text', 'b', $events->get(2)); + + $events = $this->parse('a&b'); + $this->assertEquals(2, $events->depth(), "Events: " . print_r($events, TRUE)); + $this->assertEventEquals('text', 'a&b', $events->get(0)); } // ================================================================ |