summaryrefslogtreecommitdiff
path: root/test/HTML5/Parser/TokenizerTest.php
blob: b85a090e5e7a4555bc56c1bba6340b61f05b65e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace HTML5\Parser;
require __DIR__ . '/../TestCase.php';
require 'EventStack.php';

class TokenizerTest extends \HTML5\Tests\TestCase {
  protected function createTokenizer($string) {
    $eventHandler = new EventStack();
    $stream = new StringInputStream($string);
    $scanner = new Scanner($stream);
    return array(
      new Tokenizer($scanner, $eventHandler),
      $eventHandler,
    );
  }

  public function parse($string) {
    list($tok, $events) = $this->createTokenizer($string);
    $tok->parse();

    return $events;
  }

  public function testParse() {
    list($tok, $events) = $this->createTokenizer('');

    $tok->parse();
    $e1 = $events->get(0);

    $this->assertEquals(1, $events->Depth());
    $this->assertEquals('eof', $e1['name']);
  }

  public function testWhitespace() {
    $spaces = '    ';
    list($tok, $events) = $this->createTokenizer($spaces);

    $tok->parse();

    $this->assertEquals(2, $events->depth());

    $e1 = $events->get(0);

    $this->assertEquals('text', $e1['name']);
    $this->assertEquals($spaces, $e1['data'][0]);
  }

  public function testCharacterReference() {
    $str = '&amp;';
    $events = $this->parse($str);

    $this->assertEquals(2, $events->depth());
    $e1 = $events->get(0);

    $this->assertEquals('&', $e1['data'][0]);

    // Test with hex charref
    $str = '&#x003c;';
    $events = $this->parse($str);
    $e1 = $events->get(0);
    $this->assertEquals('<', $e1['data'][0]);

    // Test with decimal charref
    $str = '&#38;';
    $events = $this->parse($str);
    $e1 = $events->get(0);
    $this->assertEquals('&', $e1['data'][0]);

    // Test with stand-alone ampersand
    $str = '& ';
    $events = $this->parse($str);
    $e1 = $events->get(0);
    $this->assertEquals('&', $e1['data'][0][0], "Stand-alone &");


  }

  /**
   * @expectedException \HTML5\Parser\EventStackParseError
   */
  public function testBrokenCharacterReference() {
    // Test with broken charref
    $str = '&foo';
    $events = $this->parse($str);
  }
}