summaryrefslogtreecommitdiff
path: root/test/HTML5/InputStreamTest.php
blob: 451bfdb60d88b8802aecc6032d0ef57624594c59 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
namespace HTML5\Tests;

use \HTML5\InputStream;

require_once 'TestCase.php';

class InputStreamTest extends TestCase {

  public function testChar() {
    $s = new InputStream("abc");
    $this->assertEquals('a', $s->char());
    $this->assertEquals('b', $s->char());
    $this->assertEquals('c', $s->char());
  }

  public function testColumnOffset() {
    $s = new InputStream("abc\ndef\n");
    $this->assertEquals(0, $s->columnOffset());
    $s->char();
    $this->assertEquals(1, $s->columnOffset());
    $s->char();
    $this->assertEquals(2, $s->columnOffset());
    $s->char();
    $this->assertEquals(3, $s->columnOffset());
    $s->char(); // LF
    $this->assertEquals(0, $s->columnOffset());
    $canary = $s->char(); // d
    $this->assertEquals('d', $canary);
    $this->assertEquals(1, $s->columnOffset());

    $s = new InputStream("abc");
    $this->assertEquals(0, $s->columnOffset());
    $s->char();
    $this->assertEquals(1, $s->columnOffset());
    $s->char();
    $this->assertEquals(2, $s->columnOffset());
  }

  public function testCountChars() {
    $res = InputStream::countChars('abc');
    $this->assertEquals(3, $res);

    $res = InputStream::countChars("abc\ndef");
    $this->assertEquals(7, $res);

    $res = InputStream::countChars("abc\ndef☃");
    $this->assertEquals(8, $res);
  }

  public function testCurrentLine() {
    $txt = "1\n2\n\n\n\n3";
    $stream = new InputStream($txt);
    $this->assertEquals(1, $stream->currentLine());

    // Advance over 1 and LF
    $stream->char(); $stream->char();
    // Now should be line2, value 2:
    $canary = $stream->char();
    $this->assertEquals(2, $stream->currentLine());
    $this->assertEquals('2', $canary);


    // Advance over 4x LF
    $stream->char(); $stream->char();
    $stream->char(); $stream->char();
    $this->assertEquals(6, $stream->currentLine());
    $this->assertEquals('3', $stream->char());

    // Make sure it doesn't do 7.
    $this->assertEquals(6, $stream->currentLine());
  }

  public function testRemainingChars() {
    $text = "abcd";
    $s = new InputStream($text);
    $this->assertEquals($text, $s->remainingChars());

    $text = "abcd";
    $s = new InputStream($text);
    $s->char(); // Pop one.
    $this->assertEquals('bcd', $s->remainingChars());

  }

  public function testCharsUnitl() {
    $text = "abcdefffffffghi";
    $s = new InputStream($text);
    $this->assertEquals('', $s->charsUntil('a'));
    // Pointer at 'a', moves 2 to 'c'
    $this->assertEquals('ab', $s->charsUntil('w', 2));

    // Pointer at 'c', moves to first 'f'
    $this->assertEquals('cde', $s->charsUntil('fzxv'));

    // Only get five 'f's
    $this->assertEquals('fffff', $s->charsUntil('g', 5));

    // Get just the last two 'f's
    $this->assertEquals('ff', $s->charsUntil('g'));

    // This should scan to the end.
    $this->assertEquals('ghi', $s->charsUntil('w', 9));

  }

  public function testCharsWhile() {
    $text = "abcdefffffffghi";
    $s = new InputStream($text);

    $this->assertEquals('ab', $s->charsWhile('ba'));

    $this->assertEquals('', $s->charsWhile('a'));
    $this->assertEquals('cde', $s->charsWhile('cdeba'));
    $this->assertEquals('ff', $s->charsWhile('f', 2));
    $this->assertEquals('fffff', $s->charsWhile('f'));
    $this->assertEquals('g', $s->charsWhile('fg'));
    $this->assertEquals('hi', $s->charsWhile('fghi', 99));

  }

  public function testUnget() {
    $text = "abc";
    $s = new InputStream($text);
    $s->unget(); // Should do nothing.
    $s->char(); // Advance.
    $this->assertEquals('bc', $s->remainingChars());

    $text = "abc";
    $s = new InputStream($text);
    $s->char(); $s->char();
    $s->unget(); $s->unget();
    $this->assertEquals('abc', $s->remainingChars());

    // Right now we are starting from EOF.
    $this->assertFalse($s->remainingChars());
    $s->unget();
    $this->assertEquals('c', $s->remainingChars());

    // Apparently, the lib was designed to do this. So we test it to 
    // make sure it doesn't break. Not sure why it's doing this, though.
    $s->char(); // pop 'c'
    $s->char(); // One past EOF
    $s->unget(); // this does not reverse at all.
    $s->unget(); // this does not reverse at all.
    $s->unget(); // this does not reverse at all.
    $this->assertFalse($s->remainingChars());
  }

  public function testBOM() {

    // Ignore in-text BOM.
    $stream = new InputStream("a\xEF\xBB\xBF");
    $this->assertEquals("a\xEF\xBB\xBF", $stream->remainingChars(), 'A non-leading U+FEFF (BOM/ZWNBSP) should remain');

    // Strip leading BOM
    $leading = new InputStream("\xEF\xBB\xBFa");
    $this->assertEquals('a', $leading->char(), 'BOM should be stripped');
  }

  public function testCarriageReturn() {

    // Replace NULL with Unicode replacement.
    $stream = new InputStream("\0\0\0");
    $this->assertEquals("\xEF\xBF\xBD\xEF\xBF\xBD\xEF\xBF\xBD", $stream->remainingChars(), 'Null character should be replaced by U+FFFD');
    $this->assertEquals(3, count($stream->errors), 'Null character should set parse error: ' . print_r($stream->errors, TRUE));

    // Remove CR when next to LF.
    $stream = new InputStream("\r\n");
    $this->assertEquals("\n", $stream->remainingChars(), 'CRLF should be replaced by LF');

    // Convert CR to LF when on its own.
    $stream = new InputStream("\r");
    $this->assertEquals("\n", $stream->remainingChars(), 'CR should be replaced by LF');
  }


  public function invalidParseErrorTestHandler($input, $numErrors, $name) {
    $stream = new InputStream($input);
    $this->assertEquals($input, $stream->remainingChars(), $name . ' (stream content)');
    $this->assertEquals($numErrors, count($stream->errors), $name . ' (number of errors)');
  }

  public function testInvalidReplace() {
    $invalidTest = array(

      // Min/max overlong
      "\xC0\x80a" => 'Overlong representation of U+0000',
      "\xE0\x80\x80a" => 'Overlong representation of U+0000',
      "\xF0\x80\x80\x80a" => 'Overlong representation of U+0000',
      "\xF8\x80\x80\x80\x80a" => 'Overlong representation of U+0000',
      "\xFC\x80\x80\x80\x80\x80a" => 'Overlong representation of U+0000',
      "\xC1\xBFa" => 'Overlong representation of U+007F',
      "\xE0\x9F\xBFa" => 'Overlong representation of U+07FF',
      "\xF0\x8F\xBF\xBFa" => 'Overlong representation of U+FFFF',


      "a\xDF" => 'Incomplete two byte sequence (missing final byte)',
      "a\xEF\xBF" => 'Incomplete three byte sequence (missing final byte)',
      "a\xF4\xBF\xBF" => 'Incomplete four byte sequence (missing final byte)',

      // Min/max continuation bytes
      "a\x80" => 'Lone 80 continuation byte',
      "a\xBF" => 'Lone BF continuation byte',

      // Invalid bytes (these can never occur)
      "a\xFE" => 'Invalid FE byte',
      "a\xFF" => 'Invalid FF byte',
    );
    foreach ($invalidTest as $test => $note) {
      $stream = new InputStream($test);
      $this->assertEquals('a', $stream->remainingChars(), $note);
    }

    // MPB:
    // It appears that iconv just leaves these alone. Not sure what to 
    // do.
    /*
    $converted = array(
      "a\xF5\x90\x80\x80" => 'U+110000, off unicode planes.',
    );
    foreach ($converted as $test => $note) {
      $stream = new InputStream($test);
      $this->assertEquals(2, mb_strlen($stream->remainingChars()), $note);
    }
     */
  }

  public function testInvalidParseError() {
    // C0 controls (except U+0000 and U+000D due to different handling)
    $this->invalidParseErrorTestHandler("\x01", 1, 'U+0001 (C0 control)');
    $this->invalidParseErrorTestHandler("\x02", 1, 'U+0002 (C0 control)');
    $this->invalidParseErrorTestHandler("\x03", 1, 'U+0003 (C0 control)');
    $this->invalidParseErrorTestHandler("\x04", 1, 'U+0004 (C0 control)');
    $this->invalidParseErrorTestHandler("\x05", 1, 'U+0005 (C0 control)');
    $this->invalidParseErrorTestHandler("\x06", 1, 'U+0006 (C0 control)');
    $this->invalidParseErrorTestHandler("\x07", 1, 'U+0007 (C0 control)');
    $this->invalidParseErrorTestHandler("\x08", 1, 'U+0008 (C0 control)');
    $this->invalidParseErrorTestHandler("\x09", 0, 'U+0009 (C0 control)');
    $this->invalidParseErrorTestHandler("\x0A", 0, 'U+000A (C0 control)');
    $this->invalidParseErrorTestHandler("\x0B", 1, 'U+000B (C0 control)');
    $this->invalidParseErrorTestHandler("\x0C", 0, 'U+000C (C0 control)');
    $this->invalidParseErrorTestHandler("\x0E", 1, 'U+000E (C0 control)');
    $this->invalidParseErrorTestHandler("\x0F", 1, 'U+000F (C0 control)');
    $this->invalidParseErrorTestHandler("\x10", 1, 'U+0010 (C0 control)');
    $this->invalidParseErrorTestHandler("\x11", 1, 'U+0011 (C0 control)');
    $this->invalidParseErrorTestHandler("\x12", 1, 'U+0012 (C0 control)');
    $this->invalidParseErrorTestHandler("\x13", 1, 'U+0013 (C0 control)');
    $this->invalidParseErrorTestHandler("\x14", 1, 'U+0014 (C0 control)');
    $this->invalidParseErrorTestHandler("\x15", 1, 'U+0015 (C0 control)');
    $this->invalidParseErrorTestHandler("\x16", 1, 'U+0016 (C0 control)');
    $this->invalidParseErrorTestHandler("\x17", 1, 'U+0017 (C0 control)');
    $this->invalidParseErrorTestHandler("\x18", 1, 'U+0018 (C0 control)');
    $this->invalidParseErrorTestHandler("\x19", 1, 'U+0019 (C0 control)');
    $this->invalidParseErrorTestHandler("\x1A", 1, 'U+001A (C0 control)');
    $this->invalidParseErrorTestHandler("\x1B", 1, 'U+001B (C0 control)');
    $this->invalidParseErrorTestHandler("\x1C", 1, 'U+001C (C0 control)');
    $this->invalidParseErrorTestHandler("\x1D", 1, 'U+001D (C0 control)');
    $this->invalidParseErrorTestHandler("\x1E", 1, 'U+001E (C0 control)');
    $this->invalidParseErrorTestHandler("\x1F", 1, 'U+001F (C0 control)');
    
    // DEL (U+007F)
    $this->invalidParseErrorTestHandler("\x7F", 1, 'U+007F');
    
    // C1 Controls
    $this->invalidParseErrorTestHandler("\xC2\x80", 1, 'U+0080 (C1 control)');
    $this->invalidParseErrorTestHandler("\xC2\x9F", 1, 'U+009F (C1 control)');
    $this->invalidParseErrorTestHandler("\xC2\xA0", 0, 'U+00A0 (first codepoint above highest C1 control)');
    
    // Single UTF-16 surrogates
    $this->invalidParseErrorTestHandler("\xED\xA0\x80", 1, 'U+D800 (UTF-16 surrogate character)');
    $this->invalidParseErrorTestHandler("\xED\xAD\xBF", 1, 'U+DB7F (UTF-16 surrogate character)');
    $this->invalidParseErrorTestHandler("\xED\xAE\x80", 1, 'U+DB80 (UTF-16 surrogate character)');
    $this->invalidParseErrorTestHandler("\xED\xAF\xBF", 1, 'U+DBFF (UTF-16 surrogate character)');
    $this->invalidParseErrorTestHandler("\xED\xB0\x80", 1, 'U+DC00 (UTF-16 surrogate character)');
    $this->invalidParseErrorTestHandler("\xED\xBE\x80", 1, 'U+DF80 (UTF-16 surrogate character)');
    $this->invalidParseErrorTestHandler("\xED\xBF\xBF", 1, 'U+DFFF (UTF-16 surrogate character)');
    
    // Paired UTF-16 surrogates
    $this->invalidParseErrorTestHandler("\xED\xA0\x80\xED\xB0\x80", 2, 'U+D800 U+DC00 (paired UTF-16 surrogates)');
    $this->invalidParseErrorTestHandler("\xED\xA0\x80\xED\xBF\xBF", 2, 'U+D800 U+DFFF (paired UTF-16 surrogates)');
    $this->invalidParseErrorTestHandler("\xED\xAD\xBF\xED\xB0\x80", 2, 'U+DB7F U+DC00 (paired UTF-16 surrogates)');
    $this->invalidParseErrorTestHandler("\xED\xAD\xBF\xED\xBF\xBF", 2, 'U+DB7F U+DFFF (paired UTF-16 surrogates)');
    $this->invalidParseErrorTestHandler("\xED\xAE\x80\xED\xB0\x80", 2, 'U+DB80 U+DC00 (paired UTF-16 surrogates)');
    $this->invalidParseErrorTestHandler("\xED\xAE\x80\xED\xBF\xBF", 2, 'U+DB80 U+DFFF (paired UTF-16 surrogates)');
    $this->invalidParseErrorTestHandler("\xED\xAF\xBF\xED\xB0\x80", 2, 'U+DBFF U+DC00 (paired UTF-16 surrogates)');
    $this->invalidParseErrorTestHandler("\xED\xAF\xBF\xED\xBF\xBF", 2, 'U+DBFF U+DFFF (paired UTF-16 surrogates)');
    
    // Charcters surrounding surrogates
    $this->invalidParseErrorTestHandler("\xED\x9F\xBF", 0, 'U+D7FF (one codepoint below lowest surrogate codepoint)');
    $this->invalidParseErrorTestHandler("\xEF\xBF\xBD", 0, 'U+DE00 (one codepoint above highest surrogate codepoint)');
    
    // Permanent noncharacters
    $this->invalidParseErrorTestHandler("\xEF\xB7\x90", 1, 'U+FDD0 (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xEF\xB7\xAF", 1, 'U+FDEF (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xEF\xBF\xBE", 1, 'U+FFFE (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xEF\xBF\xBF", 1, 'U+FFFF (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF0\x9F\xBF\xBE", 1, 'U+1FFFE (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF0\x9F\xBF\xBF", 1, 'U+1FFFF (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF0\xAF\xBF\xBE", 1, 'U+2FFFE (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF0\xAF\xBF\xBF", 1, 'U+2FFFF (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF0\xBF\xBF\xBE", 1, 'U+3FFFE (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF0\xBF\xBF\xBF", 1, 'U+3FFFF (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF1\x8F\xBF\xBE", 1, 'U+4FFFE (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF1\x8F\xBF\xBF", 1, 'U+4FFFF (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF1\x9F\xBF\xBE", 1, 'U+5FFFE (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF1\x9F\xBF\xBF", 1, 'U+5FFFF (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF1\xAF\xBF\xBE", 1, 'U+6FFFE (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF1\xAF\xBF\xBF", 1, 'U+6FFFF (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF1\xBF\xBF\xBE", 1, 'U+7FFFE (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF1\xBF\xBF\xBF", 1, 'U+7FFFF (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF2\x8F\xBF\xBE", 1, 'U+8FFFE (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF2\x8F\xBF\xBF", 1, 'U+8FFFF (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF2\x9F\xBF\xBE", 1, 'U+9FFFE (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF2\x9F\xBF\xBF", 1, 'U+9FFFF (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF2\xAF\xBF\xBE", 1, 'U+AFFFE (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF2\xAF\xBF\xBF", 1, 'U+AFFFF (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF2\xBF\xBF\xBE", 1, 'U+BFFFE (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF2\xBF\xBF\xBF", 1, 'U+BFFFF (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF3\x8F\xBF\xBE", 1, 'U+CFFFE (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF3\x8F\xBF\xBF", 1, 'U+CFFFF (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF3\x9F\xBF\xBE", 1, 'U+DFFFE (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF3\x9F\xBF\xBF", 1, 'U+DFFFF (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF3\xAF\xBF\xBE", 1, 'U+EFFFE (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF3\xAF\xBF\xBF", 1, 'U+EFFFF (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF3\xBF\xBF\xBE", 1, 'U+FFFFE (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF3\xBF\xBF\xBF", 1, 'U+FFFFF (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF4\x8F\xBF\xBE", 1, 'U+10FFFE (permanent noncharacter)');
    $this->invalidParseErrorTestHandler("\xF4\x8F\xBF\xBF", 1, 'U+10FFFF (permanent noncharacter)');
  }
}