2 namespace Masterminds\HTML5\Tests\Parser;
4 use Masterminds\HTML5\Parser\StringInputStream;
6 class StringInputStreamTest extends \Masterminds\HTML5\Tests\TestCase
10 * A canary test to make sure the basics are setup and working.
12 public function testConstruct()
14 $s = new StringInputStream("abc");
16 $this->assertInstanceOf('\Masterminds\HTML5\Parser\StringInputStream', $s);
19 public function testNext()
21 $s = new StringInputStream("abc");
24 $this->assertEquals('b', $s->current());
26 $this->assertEquals('c', $s->current());
29 public function testKey()
31 $s = new StringInputStream("abc");
33 $this->assertEquals(0, $s->key());
36 $this->assertEquals(1, $s->key());
39 public function testPeek()
41 $s = new StringInputStream("abc");
43 $this->assertEquals('b', $s->peek());
46 $this->assertEquals('c', $s->peek());
49 public function testCurrent()
51 $s = new StringInputStream("abc");
53 // Before scanning the string begins the current is empty.
54 $this->assertEquals('a', $s->current());
57 $this->assertEquals('b', $s->current());
59 // Test movement through the string.
61 $this->assertEquals('c', $s->current());
64 public function testColumnOffset()
66 $s = new StringInputStream("abc\ndef\n");
67 $this->assertEquals(0, $s->columnOffset());
69 $this->assertEquals(1, $s->columnOffset());
71 $this->assertEquals(2, $s->columnOffset());
73 $this->assertEquals(3, $s->columnOffset());
75 $this->assertEquals(0, $s->columnOffset());
77 $canary = $s->current(); // e
78 $this->assertEquals('e', $canary);
79 $this->assertEquals(1, $s->columnOffset());
81 $s = new StringInputStream("abc");
82 $this->assertEquals(0, $s->columnOffset());
84 $this->assertEquals(1, $s->columnOffset());
86 $this->assertEquals(2, $s->columnOffset());
89 public function testCurrentLine()
91 $txt = "1\n2\n\n\n\n3";
92 $stream = new StringInputStream($txt);
93 $this->assertEquals(1, $stream->currentLine());
95 // Advance over 1 and LF on to line 2 value 2.
98 $canary = $stream->current();
99 $this->assertEquals(2, $stream->currentLine());
100 $this->assertEquals('2', $canary);
102 // Advance over 4x LF
108 $this->assertEquals(6, $stream->currentLine());
109 $this->assertEquals('3', $stream->current());
111 // Make sure it doesn't do 7.
112 $this->assertEquals(6, $stream->currentLine());
115 public function testRemainingChars()
118 $s = new StringInputStream($text);
119 $this->assertEquals($text, $s->remainingChars());
122 $s = new StringInputStream($text);
123 $s->next(); // Pop one.
124 $this->assertEquals('bcd', $s->remainingChars());
127 public function testCharsUnitl()
129 $text = "abcdefffffffghi";
130 $s = new StringInputStream($text);
131 $this->assertEquals('', $s->charsUntil('a'));
132 // Pointer at 'a', moves 2 to 'c'
133 $this->assertEquals('ab', $s->charsUntil('w', 2));
135 // Pointer at 'c', moves to first 'f'
136 $this->assertEquals('cde', $s->charsUntil('fzxv'));
138 // Only get five 'f's
139 $this->assertEquals('fffff', $s->charsUntil('g', 5));
141 // Get just the last two 'f's
142 $this->assertEquals('ff', $s->charsUntil('g'));
144 // This should scan to the end.
145 $this->assertEquals('ghi', $s->charsUntil('w', 9));
148 public function testCharsWhile()
150 $text = "abcdefffffffghi";
151 $s = new StringInputStream($text);
153 $this->assertEquals('ab', $s->charsWhile('ba'));
155 $this->assertEquals('', $s->charsWhile('a'));
156 $this->assertEquals('cde', $s->charsWhile('cdeba'));
157 $this->assertEquals('ff', $s->charsWhile('f', 2));
158 $this->assertEquals('fffff', $s->charsWhile('f'));
159 $this->assertEquals('g', $s->charsWhile('fg'));
160 $this->assertEquals('hi', $s->charsWhile('fghi', 99));
163 public function testBOM()
165 // Ignore in-text BOM.
166 $stream = new StringInputStream("a\xEF\xBB\xBF");
167 $this->assertEquals("a\xEF\xBB\xBF", $stream->remainingChars(), 'A non-leading U+FEFF (BOM/ZWNBSP) should remain');
170 $leading = new StringInputStream("\xEF\xBB\xBFa");
171 $this->assertEquals('a', $leading->current(), 'BOM should be stripped');
174 public function testCarriageReturn()
176 // Replace NULL with Unicode replacement.
177 $stream = new StringInputStream("\0\0\0");
178 $this->assertEquals("\xEF\xBF\xBD\xEF\xBF\xBD\xEF\xBF\xBD", $stream->remainingChars(), 'Null character should be replaced by U+FFFD');
179 $this->assertEquals(3, count($stream->errors), 'Null character should set parse error: ' . print_r($stream->errors, true));
181 // Remove CR when next to LF.
182 $stream = new StringInputStream("\r\n");
183 $this->assertEquals("\n", $stream->remainingChars(), 'CRLF should be replaced by LF');
185 // Convert CR to LF when on its own.
186 $stream = new StringInputStream("\r");
187 $this->assertEquals("\n", $stream->remainingChars(), 'CR should be replaced by LF');
190 public function invalidParseErrorTestHandler($input, $numErrors, $name)
192 $stream = new StringInputStream($input, 'UTF-8');
193 $this->assertEquals($input, $stream->remainingChars(), $name . ' (stream content)');
194 $this->assertEquals($numErrors, count($stream->errors), $name . ' (number of errors)');
197 public function testInvalidReplace()
199 $invalidTest = array(
202 "\xC0\x80a" => 'Overlong representation of U+0000',
203 "\xE0\x80\x80a" => 'Overlong representation of U+0000',
204 "\xF0\x80\x80\x80a" => 'Overlong representation of U+0000',
205 "\xF8\x80\x80\x80\x80a" => 'Overlong representation of U+0000',
206 "\xFC\x80\x80\x80\x80\x80a" => 'Overlong representation of U+0000',
207 "\xC1\xBFa" => 'Overlong representation of U+007F',
208 "\xE0\x9F\xBFa" => 'Overlong representation of U+07FF',
209 "\xF0\x8F\xBF\xBFa" => 'Overlong representation of U+FFFF',
211 "a\xDF" => 'Incomplete two byte sequence (missing final byte)',
212 "a\xEF\xBF" => 'Incomplete three byte sequence (missing final byte)',
213 "a\xF4\xBF\xBF" => 'Incomplete four byte sequence (missing final byte)',
215 // Min/max continuation bytes
216 "a\x80" => 'Lone 80 continuation byte',
217 "a\xBF" => 'Lone BF continuation byte',
219 // Invalid bytes (these can never occur)
220 "a\xFE" => 'Invalid FE byte',
221 "a\xFF" => 'Invalid FF byte'
223 foreach ($invalidTest as $test => $note) {
224 $stream = new StringInputStream($test);
225 $this->assertEquals('a', $stream->remainingChars(), $note);
229 // It appears that iconv just leaves these alone. Not sure what to
232 * $converted = array( "a\xF5\x90\x80\x80" => 'U+110000, off unicode planes.', ); foreach ($converted as $test => $note) { $stream = new StringInputStream($test); $this->assertEquals(2, mb_strlen($stream->remainingChars()), $note); }
236 public function testInvalidParseError()
238 // C0 controls (except U+0000 and U+000D due to different handling)
239 $this->invalidParseErrorTestHandler("\x01", 1, 'U+0001 (C0 control)');
240 $this->invalidParseErrorTestHandler("\x02", 1, 'U+0002 (C0 control)');
241 $this->invalidParseErrorTestHandler("\x03", 1, 'U+0003 (C0 control)');
242 $this->invalidParseErrorTestHandler("\x04", 1, 'U+0004 (C0 control)');
243 $this->invalidParseErrorTestHandler("\x05", 1, 'U+0005 (C0 control)');
244 $this->invalidParseErrorTestHandler("\x06", 1, 'U+0006 (C0 control)');
245 $this->invalidParseErrorTestHandler("\x07", 1, 'U+0007 (C0 control)');
246 $this->invalidParseErrorTestHandler("\x08", 1, 'U+0008 (C0 control)');
247 $this->invalidParseErrorTestHandler("\x09", 0, 'U+0009 (C0 control)');
248 $this->invalidParseErrorTestHandler("\x0A", 0, 'U+000A (C0 control)');
249 $this->invalidParseErrorTestHandler("\x0B", 1, 'U+000B (C0 control)');
250 $this->invalidParseErrorTestHandler("\x0C", 0, 'U+000C (C0 control)');
251 $this->invalidParseErrorTestHandler("\x0E", 1, 'U+000E (C0 control)');
252 $this->invalidParseErrorTestHandler("\x0F", 1, 'U+000F (C0 control)');
253 $this->invalidParseErrorTestHandler("\x10", 1, 'U+0010 (C0 control)');
254 $this->invalidParseErrorTestHandler("\x11", 1, 'U+0011 (C0 control)');
255 $this->invalidParseErrorTestHandler("\x12", 1, 'U+0012 (C0 control)');
256 $this->invalidParseErrorTestHandler("\x13", 1, 'U+0013 (C0 control)');
257 $this->invalidParseErrorTestHandler("\x14", 1, 'U+0014 (C0 control)');
258 $this->invalidParseErrorTestHandler("\x15", 1, 'U+0015 (C0 control)');
259 $this->invalidParseErrorTestHandler("\x16", 1, 'U+0016 (C0 control)');
260 $this->invalidParseErrorTestHandler("\x17", 1, 'U+0017 (C0 control)');
261 $this->invalidParseErrorTestHandler("\x18", 1, 'U+0018 (C0 control)');
262 $this->invalidParseErrorTestHandler("\x19", 1, 'U+0019 (C0 control)');
263 $this->invalidParseErrorTestHandler("\x1A", 1, 'U+001A (C0 control)');
264 $this->invalidParseErrorTestHandler("\x1B", 1, 'U+001B (C0 control)');
265 $this->invalidParseErrorTestHandler("\x1C", 1, 'U+001C (C0 control)');
266 $this->invalidParseErrorTestHandler("\x1D", 1, 'U+001D (C0 control)');
267 $this->invalidParseErrorTestHandler("\x1E", 1, 'U+001E (C0 control)');
268 $this->invalidParseErrorTestHandler("\x1F", 1, 'U+001F (C0 control)');
271 $this->invalidParseErrorTestHandler("\x7F", 1, 'U+007F');
274 $this->invalidParseErrorTestHandler("\xC2\x80", 1, 'U+0080 (C1 control)');
275 $this->invalidParseErrorTestHandler("\xC2\x9F", 1, 'U+009F (C1 control)');
276 $this->invalidParseErrorTestHandler("\xC2\xA0", 0, 'U+00A0 (first codepoint above highest C1 control)');
278 // Charcters surrounding surrogates
279 $this->invalidParseErrorTestHandler("\xED\x9F\xBF", 0, 'U+D7FF (one codepoint below lowest surrogate codepoint)');
280 $this->invalidParseErrorTestHandler("\xEF\xBF\xBD", 0, 'U+DE00 (one codepoint above highest surrogate codepoint)');
282 // Permanent noncharacters
283 $this->invalidParseErrorTestHandler("\xEF\xB7\x90", 1, 'U+FDD0 (permanent noncharacter)');
284 $this->invalidParseErrorTestHandler("\xEF\xB7\xAF", 1, 'U+FDEF (permanent noncharacter)');
285 $this->invalidParseErrorTestHandler("\xEF\xBF\xBE", 1, 'U+FFFE (permanent noncharacter)');
286 $this->invalidParseErrorTestHandler("\xEF\xBF\xBF", 1, 'U+FFFF (permanent noncharacter)');
287 $this->invalidParseErrorTestHandler("\xF0\x9F\xBF\xBE", 1, 'U+1FFFE (permanent noncharacter)');
288 $this->invalidParseErrorTestHandler("\xF0\x9F\xBF\xBF", 1, 'U+1FFFF (permanent noncharacter)');
289 $this->invalidParseErrorTestHandler("\xF0\xAF\xBF\xBE", 1, 'U+2FFFE (permanent noncharacter)');
290 $this->invalidParseErrorTestHandler("\xF0\xAF\xBF\xBF", 1, 'U+2FFFF (permanent noncharacter)');
291 $this->invalidParseErrorTestHandler("\xF0\xBF\xBF\xBE", 1, 'U+3FFFE (permanent noncharacter)');
292 $this->invalidParseErrorTestHandler("\xF0\xBF\xBF\xBF", 1, 'U+3FFFF (permanent noncharacter)');
293 $this->invalidParseErrorTestHandler("\xF1\x8F\xBF\xBE", 1, 'U+4FFFE (permanent noncharacter)');
294 $this->invalidParseErrorTestHandler("\xF1\x8F\xBF\xBF", 1, 'U+4FFFF (permanent noncharacter)');
295 $this->invalidParseErrorTestHandler("\xF1\x9F\xBF\xBE", 1, 'U+5FFFE (permanent noncharacter)');
296 $this->invalidParseErrorTestHandler("\xF1\x9F\xBF\xBF", 1, 'U+5FFFF (permanent noncharacter)');
297 $this->invalidParseErrorTestHandler("\xF1\xAF\xBF\xBE", 1, 'U+6FFFE (permanent noncharacter)');
298 $this->invalidParseErrorTestHandler("\xF1\xAF\xBF\xBF", 1, 'U+6FFFF (permanent noncharacter)');
299 $this->invalidParseErrorTestHandler("\xF1\xBF\xBF\xBE", 1, 'U+7FFFE (permanent noncharacter)');
300 $this->invalidParseErrorTestHandler("\xF1\xBF\xBF\xBF", 1, 'U+7FFFF (permanent noncharacter)');
301 $this->invalidParseErrorTestHandler("\xF2\x8F\xBF\xBE", 1, 'U+8FFFE (permanent noncharacter)');
302 $this->invalidParseErrorTestHandler("\xF2\x8F\xBF\xBF", 1, 'U+8FFFF (permanent noncharacter)');
303 $this->invalidParseErrorTestHandler("\xF2\x9F\xBF\xBE", 1, 'U+9FFFE (permanent noncharacter)');
304 $this->invalidParseErrorTestHandler("\xF2\x9F\xBF\xBF", 1, 'U+9FFFF (permanent noncharacter)');
305 $this->invalidParseErrorTestHandler("\xF2\xAF\xBF\xBE", 1, 'U+AFFFE (permanent noncharacter)');
306 $this->invalidParseErrorTestHandler("\xF2\xAF\xBF\xBF", 1, 'U+AFFFF (permanent noncharacter)');
307 $this->invalidParseErrorTestHandler("\xF2\xBF\xBF\xBE", 1, 'U+BFFFE (permanent noncharacter)');
308 $this->invalidParseErrorTestHandler("\xF2\xBF\xBF\xBF", 1, 'U+BFFFF (permanent noncharacter)');
309 $this->invalidParseErrorTestHandler("\xF3\x8F\xBF\xBE", 1, 'U+CFFFE (permanent noncharacter)');
310 $this->invalidParseErrorTestHandler("\xF3\x8F\xBF\xBF", 1, 'U+CFFFF (permanent noncharacter)');
311 $this->invalidParseErrorTestHandler("\xF3\x9F\xBF\xBE", 1, 'U+DFFFE (permanent noncharacter)');
312 $this->invalidParseErrorTestHandler("\xF3\x9F\xBF\xBF", 1, 'U+DFFFF (permanent noncharacter)');
313 $this->invalidParseErrorTestHandler("\xF3\xAF\xBF\xBE", 1, 'U+EFFFE (permanent noncharacter)');
314 $this->invalidParseErrorTestHandler("\xF3\xAF\xBF\xBF", 1, 'U+EFFFF (permanent noncharacter)');
315 $this->invalidParseErrorTestHandler("\xF3\xBF\xBF\xBE", 1, 'U+FFFFE (permanent noncharacter)');
316 $this->invalidParseErrorTestHandler("\xF3\xBF\xBF\xBF", 1, 'U+FFFFF (permanent noncharacter)');
317 $this->invalidParseErrorTestHandler("\xF4\x8F\xBF\xBE", 1, 'U+10FFFE (permanent noncharacter)');
318 $this->invalidParseErrorTestHandler("\xF4\x8F\xBF\xBF", 1, 'U+10FFFF (permanent noncharacter)');
320 // MPB: These pass on some versions of iconv, and fail on others. Since we aren't in the
321 // business of writing tests against iconv, I've just commented these out. Should revisit
324 * $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)');