|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once dirname(__FILE__) . '/../autorun.php'; |
| 4 | + |
| 5 | +class HTML5_InputStreamTest extends UnitTestCase |
| 6 | +{ |
| 7 | + public function invalidReplaceTestHandler($input, $name) { |
| 8 | + $stream = new HTML5_InputStream($input); |
| 9 | + $this->assertIdentical("\xEF\xBF\xBD", $stream->remainingChars(), $name); |
| 10 | + } |
| 11 | + |
| 12 | + public function testInvalidReplace() { |
| 13 | + // Above U+10FFFF |
| 14 | + $this->invalidReplaceTestHandler("\xF5\x90\x80\x80", 'U+110000'); |
| 15 | + |
| 16 | + // Incomplete |
| 17 | + $this->invalidReplaceTestHandler("\xDF", 'Incomplete two byte sequence (missing final byte)'); |
| 18 | + $this->invalidReplaceTestHandler("\xEF\xBF", 'Incomplete three byte sequence (missing final byte)'); |
| 19 | + $this->invalidReplaceTestHandler("\xF4\xBF\xBF", 'Incomplete four byte sequence (missing final byte)'); |
| 20 | + |
| 21 | + // Min/max continuation bytes |
| 22 | + $this->invalidReplaceTestHandler("\x80", 'Lone 80 continuation byte'); |
| 23 | + $this->invalidReplaceTestHandler("\xBF", 'Lone BF continuation byte'); |
| 24 | + |
| 25 | + // Invalid bytes (these can never occur) |
| 26 | + $this->invalidReplaceTestHandler("\xFE", 'Invalid FE byte'); |
| 27 | + $this->invalidReplaceTestHandler("\xFF", 'Invalid FF byte'); |
| 28 | + |
| 29 | + // Min/max overlong |
| 30 | + $this->invalidReplaceTestHandler("\xC0\x80", 'Overlong representation of U+0000'); |
| 31 | + $this->invalidReplaceTestHandler("\xE0\x80\x80", 'Overlong representation of U+0000'); |
| 32 | + $this->invalidReplaceTestHandler("\xF0\x80\x80\x80", 'Overlong representation of U+0000'); |
| 33 | + $this->invalidReplaceTestHandler("\xF8\x80\x80\x80\x80", 'Overlong representation of U+0000'); |
| 34 | + $this->invalidReplaceTestHandler("\xFC\x80\x80\x80\x80\x80", 'Overlong representation of U+0000'); |
| 35 | + $this->invalidReplaceTestHandler("\xC1\xBF", 'Overlong representation of U+007F'); |
| 36 | + $this->invalidReplaceTestHandler("\xE0\x9F\xBF", 'Overlong representation of U+07FF'); |
| 37 | + $this->invalidReplaceTestHandler("\xF0\x8F\xBF\xBF", 'Overlong representation of U+FFFF'); |
| 38 | + } |
| 39 | + |
| 40 | + public function testStripLeadingBOM() { |
| 41 | + $leading = new HTML5_InputStream("\xEF\xBB\xBFa"); |
| 42 | + $this->assertIdentical('a', $leading->char(), 'BOM should be stripped'); |
| 43 | + } |
| 44 | + |
| 45 | + public function testZWNBSP() { |
| 46 | + $stream = new HTML5_InputStream("a\xEF\xBB\xBF"); |
| 47 | + $this->assertIdentical("a\xEF\xBB\xBF", $stream->remainingChars(), 'A non-leading U+FEFF (BOM/ZWNBSP) should remain'); |
| 48 | + } |
| 49 | + |
| 50 | + public function testNull() { |
| 51 | + $stream = new HTML5_InputStream("\0\0\0"); |
| 52 | + $this->assertIdentical("\xEF\xBF\xBD\xEF\xBF\xBD\xEF\xBF\xBD", $stream->remainingChars(), 'Null character should be replaced by U+FFFD'); |
| 53 | + $this->assertIdentical(3, count($stream->errors), 'Null character should be throw parse error'); |
| 54 | + } |
| 55 | + |
| 56 | + public function testCRLF() { |
| 57 | + $stream = new HTML5_InputStream("\r\n"); |
| 58 | + $this->assertIdentical("\n", $stream->remainingChars(), 'CRLF should be replaced by LF'); |
| 59 | + } |
| 60 | + |
| 61 | + public function testCR() { |
| 62 | + $stream = new HTML5_InputStream("\r"); |
| 63 | + $this->assertIdentical("\n", $stream->remainingChars(), 'CR should be replaced by LF'); |
| 64 | + } |
| 65 | + |
| 66 | + public function invalidParseErrorTestHandler($input, $numErrors, $name) { |
| 67 | + $stream = new HTML5_InputStream($input); |
| 68 | + $this->assertIdentical($input, $stream->remainingChars(), $name . ' (stream content)'); |
| 69 | + $this->assertIdentical($numErrors, count($stream->errors), $name . ' (number of errors)'); |
| 70 | + } |
| 71 | + |
| 72 | + public function testInvalidParseError() { |
| 73 | + // C0 controls (except U+0000 and U+000D due to different handling) |
| 74 | + $this->invalidParseErrorTestHandler("\x01", 1, 'U+0001 (C0 control)'); |
| 75 | + $this->invalidParseErrorTestHandler("\x02", 1, 'U+0002 (C0 control)'); |
| 76 | + $this->invalidParseErrorTestHandler("\x03", 1, 'U+0003 (C0 control)'); |
| 77 | + $this->invalidParseErrorTestHandler("\x04", 1, 'U+0004 (C0 control)'); |
| 78 | + $this->invalidParseErrorTestHandler("\x05", 1, 'U+0005 (C0 control)'); |
| 79 | + $this->invalidParseErrorTestHandler("\x06", 1, 'U+0006 (C0 control)'); |
| 80 | + $this->invalidParseErrorTestHandler("\x07", 1, 'U+0007 (C0 control)'); |
| 81 | + $this->invalidParseErrorTestHandler("\x08", 1, 'U+0008 (C0 control)'); |
| 82 | + $this->invalidParseErrorTestHandler("\x09", 0, 'U+0009 (C0 control)'); |
| 83 | + $this->invalidParseErrorTestHandler("\x0A", 0, 'U+000A (C0 control)'); |
| 84 | + $this->invalidParseErrorTestHandler("\x0B", 1, 'U+000B (C0 control)'); |
| 85 | + $this->invalidParseErrorTestHandler("\x0C", 0, 'U+000C (C0 control)'); |
| 86 | + $this->invalidParseErrorTestHandler("\x0E", 1, 'U+000E (C0 control)'); |
| 87 | + $this->invalidParseErrorTestHandler("\x0F", 1, 'U+000F (C0 control)'); |
| 88 | + $this->invalidParseErrorTestHandler("\x10", 1, 'U+0010 (C0 control)'); |
| 89 | + $this->invalidParseErrorTestHandler("\x11", 1, 'U+0011 (C0 control)'); |
| 90 | + $this->invalidParseErrorTestHandler("\x12", 1, 'U+0012 (C0 control)'); |
| 91 | + $this->invalidParseErrorTestHandler("\x13", 1, 'U+0013 (C0 control)'); |
| 92 | + $this->invalidParseErrorTestHandler("\x14", 1, 'U+0014 (C0 control)'); |
| 93 | + $this->invalidParseErrorTestHandler("\x15", 1, 'U+0015 (C0 control)'); |
| 94 | + $this->invalidParseErrorTestHandler("\x16", 1, 'U+0016 (C0 control)'); |
| 95 | + $this->invalidParseErrorTestHandler("\x17", 1, 'U+0017 (C0 control)'); |
| 96 | + $this->invalidParseErrorTestHandler("\x18", 1, 'U+0018 (C0 control)'); |
| 97 | + $this->invalidParseErrorTestHandler("\x19", 1, 'U+0019 (C0 control)'); |
| 98 | + $this->invalidParseErrorTestHandler("\x1A", 1, 'U+001A (C0 control)'); |
| 99 | + $this->invalidParseErrorTestHandler("\x1B", 1, 'U+001B (C0 control)'); |
| 100 | + $this->invalidParseErrorTestHandler("\x1C", 1, 'U+001C (C0 control)'); |
| 101 | + $this->invalidParseErrorTestHandler("\x1D", 1, 'U+001D (C0 control)'); |
| 102 | + $this->invalidParseErrorTestHandler("\x1E", 1, 'U+001E (C0 control)'); |
| 103 | + $this->invalidParseErrorTestHandler("\x1F", 1, 'U+001F (C0 control)'); |
| 104 | + |
| 105 | + // DEL (U+007F) |
| 106 | + $this->invalidParseErrorTestHandler("\x7F", 1, 'U+007F'); |
| 107 | + |
| 108 | + // C1 Controls |
| 109 | + $this->invalidParseErrorTestHandler("\xC2\x80", 1, 'U+0080 (C1 control)'); |
| 110 | + $this->invalidParseErrorTestHandler("\xC2\x9F", 1, 'U+009F (C1 control)'); |
| 111 | + $this->invalidParseErrorTestHandler("\xC2\xA0", 0, 'U+00A0 (first codepoint above highest C1 control)'); |
| 112 | + |
| 113 | + // Single UTF-16 surrogates |
| 114 | + $this->invalidParseErrorTestHandler("\xED\xA0\x80", 1, 'U+D800 (UTF-16 surrogate character)'); |
| 115 | + $this->invalidParseErrorTestHandler("\xED\xAD\xBF", 1, 'U+DB7F (UTF-16 surrogate character)'); |
| 116 | + $this->invalidParseErrorTestHandler("\xED\xAE\x80", 1, 'U+DB80 (UTF-16 surrogate character)'); |
| 117 | + $this->invalidParseErrorTestHandler("\xED\xAF\xBF", 1, 'U+DBFF (UTF-16 surrogate character)'); |
| 118 | + $this->invalidParseErrorTestHandler("\xED\xB0\x80", 1, 'U+DC00 (UTF-16 surrogate character)'); |
| 119 | + $this->invalidParseErrorTestHandler("\xED\xBE\x80", 1, 'U+DF80 (UTF-16 surrogate character)'); |
| 120 | + $this->invalidParseErrorTestHandler("\xED\xBF\xBF", 1, 'U+DFFF (UTF-16 surrogate character)'); |
| 121 | + |
| 122 | + // Paired UTF-16 surrogates |
| 123 | + $this->invalidParseErrorTestHandler("\xED\xA0\x80\xED\xB0\x80", 2, 'U+D800 U+DC00 (paired UTF-16 surrogates)'); |
| 124 | + $this->invalidParseErrorTestHandler("\xED\xA0\x80\xED\xBF\xBF", 2, 'U+D800 U+DFFF (paired UTF-16 surrogates)'); |
| 125 | + $this->invalidParseErrorTestHandler("\xED\xAD\xBF\xED\xB0\x80", 2, 'U+DB7F U+DC00 (paired UTF-16 surrogates)'); |
| 126 | + $this->invalidParseErrorTestHandler("\xED\xAD\xBF\xED\xBF\xBF", 2, 'U+DB7F U+DFFF (paired UTF-16 surrogates)'); |
| 127 | + $this->invalidParseErrorTestHandler("\xED\xAE\x80\xED\xB0\x80", 2, 'U+DB80 U+DC00 (paired UTF-16 surrogates)'); |
| 128 | + $this->invalidParseErrorTestHandler("\xED\xAE\x80\xED\xBF\xBF", 2, 'U+DB80 U+DFFF (paired UTF-16 surrogates)'); |
| 129 | + $this->invalidParseErrorTestHandler("\xED\xAF\xBF\xED\xB0\x80", 2, 'U+DBFF U+DC00 (paired UTF-16 surrogates)'); |
| 130 | + $this->invalidParseErrorTestHandler("\xED\xAF\xBF\xED\xBF\xBF", 2, 'U+DBFF U+DFFF (paired UTF-16 surrogates)'); |
| 131 | + |
| 132 | + // Charcters surrounding surrogates |
| 133 | + $this->invalidParseErrorTestHandler("\xED\x9F\xBF", 0, 'U+D7FF (one codepoint below lowest surrogate codepoint)'); |
| 134 | + $this->invalidParseErrorTestHandler("\xEF\xBF\xBD", 0, 'U+DE00 (one codepoint above highest surrogate codepoint)'); |
| 135 | + |
| 136 | + // Permanent noncharacters |
| 137 | + $this->invalidParseErrorTestHandler("\xEF\xB7\x90", 1, 'U+FDD0 (permanent noncharacter)'); |
| 138 | + $this->invalidParseErrorTestHandler("\xEF\xB7\xAF", 1, 'U+FDEF (permanent noncharacter)'); |
| 139 | + $this->invalidParseErrorTestHandler("\xEF\xBF\xBE", 1, 'U+FFFE (permanent noncharacter)'); |
| 140 | + $this->invalidParseErrorTestHandler("\xEF\xBF\xBF", 1, 'U+FFFF (permanent noncharacter)'); |
| 141 | + $this->invalidParseErrorTestHandler("\xF0\x9F\xBF\xBE", 1, 'U+1FFFE (permanent noncharacter)'); |
| 142 | + $this->invalidParseErrorTestHandler("\xF0\x9F\xBF\xBF", 1, 'U+1FFFF (permanent noncharacter)'); |
| 143 | + $this->invalidParseErrorTestHandler("\xF0\xAF\xBF\xBE", 1, 'U+2FFFE (permanent noncharacter)'); |
| 144 | + $this->invalidParseErrorTestHandler("\xF0\xAF\xBF\xBF", 1, 'U+2FFFF (permanent noncharacter)'); |
| 145 | + $this->invalidParseErrorTestHandler("\xF0\xBF\xBF\xBE", 1, 'U+3FFFE (permanent noncharacter)'); |
| 146 | + $this->invalidParseErrorTestHandler("\xF0\xBF\xBF\xBF", 1, 'U+3FFFF (permanent noncharacter)'); |
| 147 | + $this->invalidParseErrorTestHandler("\xF1\x8F\xBF\xBE", 1, 'U+4FFFE (permanent noncharacter)'); |
| 148 | + $this->invalidParseErrorTestHandler("\xF1\x8F\xBF\xBF", 1, 'U+4FFFF (permanent noncharacter)'); |
| 149 | + $this->invalidParseErrorTestHandler("\xF1\x9F\xBF\xBE", 1, 'U+5FFFE (permanent noncharacter)'); |
| 150 | + $this->invalidParseErrorTestHandler("\xF1\x9F\xBF\xBF", 1, 'U+5FFFF (permanent noncharacter)'); |
| 151 | + $this->invalidParseErrorTestHandler("\xF1\xAF\xBF\xBE", 1, 'U+6FFFE (permanent noncharacter)'); |
| 152 | + $this->invalidParseErrorTestHandler("\xF1\xAF\xBF\xBF", 1, 'U+6FFFF (permanent noncharacter)'); |
| 153 | + $this->invalidParseErrorTestHandler("\xF1\xBF\xBF\xBE", 1, 'U+7FFFE (permanent noncharacter)'); |
| 154 | + $this->invalidParseErrorTestHandler("\xF1\xBF\xBF\xBF", 1, 'U+7FFFF (permanent noncharacter)'); |
| 155 | + $this->invalidParseErrorTestHandler("\xF2\x8F\xBF\xBE", 1, 'U+8FFFE (permanent noncharacter)'); |
| 156 | + $this->invalidParseErrorTestHandler("\xF2\x8F\xBF\xBF", 1, 'U+8FFFF (permanent noncharacter)'); |
| 157 | + $this->invalidParseErrorTestHandler("\xF2\x9F\xBF\xBE", 1, 'U+9FFFE (permanent noncharacter)'); |
| 158 | + $this->invalidParseErrorTestHandler("\xF2\x9F\xBF\xBF", 1, 'U+9FFFF (permanent noncharacter)'); |
| 159 | + $this->invalidParseErrorTestHandler("\xF2\xAF\xBF\xBE", 1, 'U+AFFFE (permanent noncharacter)'); |
| 160 | + $this->invalidParseErrorTestHandler("\xF2\xAF\xBF\xBF", 1, 'U+AFFFF (permanent noncharacter)'); |
| 161 | + $this->invalidParseErrorTestHandler("\xF2\xBF\xBF\xBE", 1, 'U+BFFFE (permanent noncharacter)'); |
| 162 | + $this->invalidParseErrorTestHandler("\xF2\xBF\xBF\xBF", 1, 'U+BFFFF (permanent noncharacter)'); |
| 163 | + $this->invalidParseErrorTestHandler("\xF3\x8F\xBF\xBE", 1, 'U+CFFFE (permanent noncharacter)'); |
| 164 | + $this->invalidParseErrorTestHandler("\xF3\x8F\xBF\xBF", 1, 'U+CFFFF (permanent noncharacter)'); |
| 165 | + $this->invalidParseErrorTestHandler("\xF3\x9F\xBF\xBE", 1, 'U+DFFFE (permanent noncharacter)'); |
| 166 | + $this->invalidParseErrorTestHandler("\xF3\x9F\xBF\xBF", 1, 'U+DFFFF (permanent noncharacter)'); |
| 167 | + $this->invalidParseErrorTestHandler("\xF3\xAF\xBF\xBE", 1, 'U+EFFFE (permanent noncharacter)'); |
| 168 | + $this->invalidParseErrorTestHandler("\xF3\xAF\xBF\xBF", 1, 'U+EFFFF (permanent noncharacter)'); |
| 169 | + $this->invalidParseErrorTestHandler("\xF3\xBF\xBF\xBE", 1, 'U+FFFFE (permanent noncharacter)'); |
| 170 | + $this->invalidParseErrorTestHandler("\xF3\xBF\xBF\xBF", 1, 'U+FFFFF (permanent noncharacter)'); |
| 171 | + $this->invalidParseErrorTestHandler("\xF4\x8F\xBF\xBE", 1, 'U+10FFFE (permanent noncharacter)'); |
| 172 | + $this->invalidParseErrorTestHandler("\xF4\x8F\xBF\xBF", 1, 'U+10FFFF (permanent noncharacter)'); |
| 173 | + } |
| 174 | +} |
0 commit comments