summaryrefslogtreecommitdiff
path: root/test/HTML5/Parser/TokenizerTest.php
blob: 291bd0f2b61ae158a06faeafa837f781079fe0b5 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
namespace HTML5\Parser;
require __DIR__ . '/../TestCase.php';
require 'EventStack.php';

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

    $scanner->debug = $debug;

    return array(
      new Tokenizer($scanner, $eventHandler),
      $eventHandler,
    );
  }

  public function parse($string, $debug = FALSE) {
    list($tok, $events) = $this->createTokenizer($string, $debug);
    $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 &");


  }

  public function testBrokenCharacterReference() {
    // Test with broken charref
    $str = '&foo';
    $events = $this->parse($str);
    $e1 = $events->get(0);
    $this->assertEquals('error', $e1['name']);
  }

  public function testBogusComment() {
    $bogus = array(
      '</+this is a bogus comment. +>',
      '<!+this is a bogus comment. !>',
    );
    foreach ($bogus as $str) {
      $events = $this->parse($str . '   ');
      $e0 = $events->get(0);
      $this->assertEquals('error', $e0['name']);
      $e1 = $events->get(1);
      $this->assertEquals('comment', $e1['name']);
      $this->assertEquals($str, $e1['data'][0]);
    }
  }

  public function testEndTag() {
    $succeed = array(
      '</a>' => 'a',
      '</test>' => 'test',
      '</test 
      >' => 'test',
      '</thisIsTheTagThatDoesntEndItJustGoesOnAndOnMyFriend>' =>
        'thisIsTheTagThatDoesntEndItJustGoesOnAndOnMyFriend',
      // See 8.2.4.10, which requires this and does not say error.
      '</a<b>' => 'a<b', 
    );
    foreach ($succeed as $test => $result) {
      $events = $this->parse($test);
      $this->assertEquals(2, $events->depth());
      $e1 = $events->get(0);
      $this->assertEquals('endTag', $e1['name'], "Parsing $test expects $result.");
      $this->assertEquals($result, $e1['data'][0], "Parse end tag " . $test);
    }


    // Recoverable failures
    $fail = array(
      '</a class="monkey">' => 'a',
      '</a <b>' => 'a',
      '</a <b <c>' => 'a',
      '</a is the loneliest letter>' => 'a',
      '</a' => 'a',
    );
    foreach ($fail as $test => $result) {
      $events = $this->parse($test);
      // Should have triggered an error.
      $e0 = $events->get(0);
      $this->assertEquals('error', $e0['name'], "Parsing $test expects a leading error." . print_r($events, TRUE));
      // Should have tried to parse anyway.
      $e1 = $events->get(1);
      $this->assertEquals('endTag', $e1['name'], "Parsing $test expects resolution to $result." . print_r($events, TRUE));
      $this->assertEquals($result, $e1['data'][0], "Parse end tag " . $test);
      $this->assertEquals(3, $events->depth());
    }

    // BogoComments
    $comments = array(
      '</>' => '</>',
      '</ >' => '</ >',
      '</ a>' => '</ a>',
    );
    foreach ($comments as $test => $result) {
      $events = $this->parse($test);
      // Should have triggered an error.
      $e0 = $events->get(0);
      $this->assertEquals('error', $e0['name'], "Parsing $test expects a leading error." . print_r($events, TRUE));
      // Should have tried to parse anyway.
      $e1 = $events->get(1);
      $this->assertEquals('comment', $e1['name'], "Parsing $test expects comment." . print_r($events, TRUE));
      $this->assertEquals($result, $e1['data'][0], "Parse end tag " . $test);
      $this->assertEquals(3, $events->depth());
    }
  }

  public function testComment() {
    $good = array(
      '<!--easy-->' => 'easy',
      '<!-- 1 > 0 -->' => ' 1 > 0 ',
      '<!-- --$i -->' => ' --$i ',
      '<!----$i-->' => '--$i',
      '<!-- 1 > 0 -->' => ' 1 > 0 ',
      "<!--\nHello World.\na-->" => "\nHello World.\na",
      '<!-- <!-- -->' => ' <!-- ',
    );
    foreach ($good as $test => $expected) {
      $events = $this->parse($test);
      $e1 = $events->get(0);
      $this->assertEquals('comment', $e1['name'], 'Expected a comment for ' . $test);
      $this->assertEquals($expected, $e1['data'][0]);
    }

    $fail = array(
      '<!-->' => '',
      '<!--Hello' => 'Hello',
      "<!--\0Hello" => UTF8Utils::FFFD . 'Hello',
    );
    foreach ($fail as $test => $expected) {
      $events = $this->parse($test);
      $e0 = $events->get(0);
      $this->assertEquals('error', $e0['name'], 'Expected an error for ' . $test . print_r($events, TRUE));

      $e1 = $events->get(1);
      $this->assertEquals('comment', $e1['name'], 'Expected a comment for ' . $test);
      $this->assertEquals($expected, $e1['data'][0]);
    }

  }
}