OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 "use strict"; | |
6 | |
7 // This file relies on the fact that the following declaration has been made | |
8 // in runtime.js: | |
9 // var $Array = global.Array; | |
10 | |
11 // ------------------------------------------------------------------- | |
12 | |
13 // This file contains support for URI manipulations written in | 5 // This file contains support for URI manipulations written in |
14 // JavaScript. | 6 // JavaScript. |
15 | 7 |
16 | |
17 (function() { | 8 (function() { |
18 | 9 |
19 // ------------------------------------------------------------------- | 10 "use strict"; |
20 // Define internal helper functions. | 11 |
21 | 12 %CheckIsBootstrapping(); |
22 function HexValueOf(code) { | 13 |
23 // 0-9 | 14 var GlobalObject = global.Object; |
24 if (code >= 48 && code <= 57) return code - 48; | 15 var GlobalArray = global.Array; |
25 // A-F | 16 |
26 if (code >= 65 && code <= 70) return code - 55; | 17 // ------------------------------------------------------------------- |
27 // a-f | 18 // Define internal helper functions. |
28 if (code >= 97 && code <= 102) return code - 87; | 19 |
29 | 20 function HexValueOf(code) { |
30 return -1; | 21 // 0-9 |
31 } | 22 if (code >= 48 && code <= 57) return code - 48; |
32 | 23 // A-F |
33 // Does the char code correspond to an alpha-numeric char. | 24 if (code >= 65 && code <= 70) return code - 55; |
34 function isAlphaNumeric(cc) { | 25 // a-f |
35 // a - z | 26 if (code >= 97 && code <= 102) return code - 87; |
36 if (97 <= cc && cc <= 122) return true; | 27 |
37 // A - Z | 28 return -1; |
38 if (65 <= cc && cc <= 90) return true; | 29 } |
39 // 0 - 9 | 30 |
40 if (48 <= cc && cc <= 57) return true; | 31 // Does the char code correspond to an alpha-numeric char. |
41 | 32 function isAlphaNumeric(cc) { |
42 return false; | 33 // a - z |
43 } | 34 if (97 <= cc && cc <= 122) return true; |
44 | 35 // A - Z |
45 //Lazily initialized. | 36 if (65 <= cc && cc <= 90) return true; |
46 var hexCharCodeArray = 0; | 37 // 0 - 9 |
47 | 38 if (48 <= cc && cc <= 57) return true; |
48 function URIAddEncodedOctetToBuffer(octet, result, index) { | 39 |
49 result[index++] = 37; // Char code of '%'. | 40 return false; |
50 result[index++] = hexCharCodeArray[octet >> 4]; | 41 } |
51 result[index++] = hexCharCodeArray[octet & 0x0F]; | 42 |
52 return index; | 43 //Lazily initialized. |
53 } | 44 var hexCharCodeArray = 0; |
54 | 45 |
55 function URIEncodeOctets(octets, result, index) { | 46 function URIAddEncodedOctetToBuffer(octet, result, index) { |
56 if (hexCharCodeArray === 0) { | 47 result[index++] = 37; // Char code of '%'. |
57 hexCharCodeArray = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, | 48 result[index++] = hexCharCodeArray[octet >> 4]; |
58 65, 66, 67, 68, 69, 70]; | 49 result[index++] = hexCharCodeArray[octet & 0x0F]; |
59 } | 50 return index; |
60 index = URIAddEncodedOctetToBuffer(octets[0], result, index); | 51 } |
61 if (octets[1]) index = URIAddEncodedOctetToBuffer(octets[1], result, index); | 52 |
62 if (octets[2]) index = URIAddEncodedOctetToBuffer(octets[2], result, index); | 53 function URIEncodeOctets(octets, result, index) { |
63 if (octets[3]) index = URIAddEncodedOctetToBuffer(octets[3], result, index); | 54 if (hexCharCodeArray === 0) { |
64 return index; | 55 hexCharCodeArray = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, |
65 } | 56 65, 66, 67, 68, 69, 70]; |
66 | 57 } |
67 function URIEncodeSingle(cc, result, index) { | 58 index = URIAddEncodedOctetToBuffer(octets[0], result, index); |
68 var x = (cc >> 12) & 0xF; | 59 if (octets[1]) index = URIAddEncodedOctetToBuffer(octets[1], result, index); |
69 var y = (cc >> 6) & 63; | 60 if (octets[2]) index = URIAddEncodedOctetToBuffer(octets[2], result, index); |
70 var z = cc & 63; | 61 if (octets[3]) index = URIAddEncodedOctetToBuffer(octets[3], result, index); |
71 var octets = new $Array(3); | 62 return index; |
72 if (cc <= 0x007F) { | 63 } |
73 octets[0] = cc; | 64 |
74 } else if (cc <= 0x07FF) { | 65 function URIEncodeSingle(cc, result, index) { |
75 octets[0] = y + 192; | 66 var x = (cc >> 12) & 0xF; |
76 octets[1] = z + 128; | 67 var y = (cc >> 6) & 63; |
| 68 var z = cc & 63; |
| 69 var octets = new GlobalArray(3); |
| 70 if (cc <= 0x007F) { |
| 71 octets[0] = cc; |
| 72 } else if (cc <= 0x07FF) { |
| 73 octets[0] = y + 192; |
| 74 octets[1] = z + 128; |
| 75 } else { |
| 76 octets[0] = x + 224; |
| 77 octets[1] = y + 128; |
| 78 octets[2] = z + 128; |
| 79 } |
| 80 return URIEncodeOctets(octets, result, index); |
| 81 } |
| 82 |
| 83 function URIEncodePair(cc1 , cc2, result, index) { |
| 84 var u = ((cc1 >> 6) & 0xF) + 1; |
| 85 var w = (cc1 >> 2) & 0xF; |
| 86 var x = cc1 & 3; |
| 87 var y = (cc2 >> 6) & 0xF; |
| 88 var z = cc2 & 63; |
| 89 var octets = new GlobalArray(4); |
| 90 octets[0] = (u >> 2) + 240; |
| 91 octets[1] = (((u & 3) << 4) | w) + 128; |
| 92 octets[2] = ((x << 4) | y) + 128; |
| 93 octets[3] = z + 128; |
| 94 return URIEncodeOctets(octets, result, index); |
| 95 } |
| 96 |
| 97 function URIHexCharsToCharCode(highChar, lowChar) { |
| 98 var highCode = HexValueOf(highChar); |
| 99 var lowCode = HexValueOf(lowChar); |
| 100 if (highCode == -1 || lowCode == -1) { |
| 101 throw new $URIError("URI malformed"); |
| 102 } |
| 103 return (highCode << 4) | lowCode; |
| 104 } |
| 105 |
| 106 // Callers must ensure that |result| is a sufficiently long sequential |
| 107 // two-byte string! |
| 108 function URIDecodeOctets(octets, result, index) { |
| 109 var value; |
| 110 var o0 = octets[0]; |
| 111 if (o0 < 0x80) { |
| 112 value = o0; |
| 113 } else if (o0 < 0xc2) { |
| 114 throw new $URIError("URI malformed"); |
| 115 } else { |
| 116 var o1 = octets[1]; |
| 117 if (o0 < 0xe0) { |
| 118 var a = o0 & 0x1f; |
| 119 if ((o1 < 0x80) || (o1 > 0xbf)) { |
| 120 throw new $URIError("URI malformed"); |
| 121 } |
| 122 var b = o1 & 0x3f; |
| 123 value = (a << 6) + b; |
| 124 if (value < 0x80 || value > 0x7ff) { |
| 125 throw new $URIError("URI malformed"); |
| 126 } |
77 } else { | 127 } else { |
78 octets[0] = x + 224; | 128 var o2 = octets[2]; |
79 octets[1] = y + 128; | 129 if (o0 < 0xf0) { |
80 octets[2] = z + 128; | 130 var a = o0 & 0x0f; |
81 } | |
82 return URIEncodeOctets(octets, result, index); | |
83 } | |
84 | |
85 function URIEncodePair(cc1 , cc2, result, index) { | |
86 var u = ((cc1 >> 6) & 0xF) + 1; | |
87 var w = (cc1 >> 2) & 0xF; | |
88 var x = cc1 & 3; | |
89 var y = (cc2 >> 6) & 0xF; | |
90 var z = cc2 & 63; | |
91 var octets = new $Array(4); | |
92 octets[0] = (u >> 2) + 240; | |
93 octets[1] = (((u & 3) << 4) | w) + 128; | |
94 octets[2] = ((x << 4) | y) + 128; | |
95 octets[3] = z + 128; | |
96 return URIEncodeOctets(octets, result, index); | |
97 } | |
98 | |
99 function URIHexCharsToCharCode(highChar, lowChar) { | |
100 var highCode = HexValueOf(highChar); | |
101 var lowCode = HexValueOf(lowChar); | |
102 if (highCode == -1 || lowCode == -1) { | |
103 throw new $URIError("URI malformed"); | |
104 } | |
105 return (highCode << 4) | lowCode; | |
106 } | |
107 | |
108 // Callers must ensure that |result| is a sufficiently long sequential | |
109 // two-byte string! | |
110 function URIDecodeOctets(octets, result, index) { | |
111 var value; | |
112 var o0 = octets[0]; | |
113 if (o0 < 0x80) { | |
114 value = o0; | |
115 } else if (o0 < 0xc2) { | |
116 throw new $URIError("URI malformed"); | |
117 } else { | |
118 var o1 = octets[1]; | |
119 if (o0 < 0xe0) { | |
120 var a = o0 & 0x1f; | |
121 if ((o1 < 0x80) || (o1 > 0xbf)) { | 131 if ((o1 < 0x80) || (o1 > 0xbf)) { |
122 throw new $URIError("URI malformed"); | 132 throw new $URIError("URI malformed"); |
123 } | 133 } |
124 var b = o1 & 0x3f; | 134 var b = o1 & 0x3f; |
125 value = (a << 6) + b; | 135 if ((o2 < 0x80) || (o2 > 0xbf)) { |
126 if (value < 0x80 || value > 0x7ff) { | |
127 throw new $URIError("URI malformed"); | 136 throw new $URIError("URI malformed"); |
128 } | 137 } |
| 138 var c = o2 & 0x3f; |
| 139 value = (a << 12) + (b << 6) + c; |
| 140 if ((value < 0x800) || (value > 0xffff)) { |
| 141 throw new $URIError("URI malformed"); |
| 142 } |
129 } else { | 143 } else { |
130 var o2 = octets[2]; | 144 var o3 = octets[3]; |
131 if (o0 < 0xf0) { | 145 if (o0 < 0xf8) { |
132 var a = o0 & 0x0f; | 146 var a = (o0 & 0x07); |
133 if ((o1 < 0x80) || (o1 > 0xbf)) { | 147 if ((o1 < 0x80) || (o1 > 0xbf)) { |
134 throw new $URIError("URI malformed"); | 148 throw new $URIError("URI malformed"); |
135 } | 149 } |
136 var b = o1 & 0x3f; | 150 var b = (o1 & 0x3f); |
137 if ((o2 < 0x80) || (o2 > 0xbf)) { | 151 if ((o2 < 0x80) || (o2 > 0xbf)) { |
138 throw new $URIError("URI malformed"); | 152 throw new $URIError("URI malformed"); |
139 } | 153 } |
140 var c = o2 & 0x3f; | 154 var c = (o2 & 0x3f); |
141 value = (a << 12) + (b << 6) + c; | 155 if ((o3 < 0x80) || (o3 > 0xbf)) { |
142 if ((value < 0x800) || (value > 0xffff)) { | 156 throw new $URIError("URI malformed"); |
| 157 } |
| 158 var d = (o3 & 0x3f); |
| 159 value = (a << 18) + (b << 12) + (c << 6) + d; |
| 160 if ((value < 0x10000) || (value > 0x10ffff)) { |
143 throw new $URIError("URI malformed"); | 161 throw new $URIError("URI malformed"); |
144 } | 162 } |
145 } else { | 163 } else { |
146 var o3 = octets[3]; | 164 throw new $URIError("URI malformed"); |
147 if (o0 < 0xf8) { | |
148 var a = (o0 & 0x07); | |
149 if ((o1 < 0x80) || (o1 > 0xbf)) { | |
150 throw new $URIError("URI malformed"); | |
151 } | |
152 var b = (o1 & 0x3f); | |
153 if ((o2 < 0x80) || (o2 > 0xbf)) { | |
154 throw new $URIError("URI malformed"); | |
155 } | |
156 var c = (o2 & 0x3f); | |
157 if ((o3 < 0x80) || (o3 > 0xbf)) { | |
158 throw new $URIError("URI malformed"); | |
159 } | |
160 var d = (o3 & 0x3f); | |
161 value = (a << 18) + (b << 12) + (c << 6) + d; | |
162 if ((value < 0x10000) || (value > 0x10ffff)) { | |
163 throw new $URIError("URI malformed"); | |
164 } | |
165 } else { | |
166 throw new $URIError("URI malformed"); | |
167 } | |
168 } | 165 } |
169 } | 166 } |
170 } | 167 } |
171 if (0xD800 <= value && value <= 0xDFFF) { | 168 } |
172 throw new $URIError("URI malformed"); | 169 if (0xD800 <= value && value <= 0xDFFF) { |
| 170 throw new $URIError("URI malformed"); |
| 171 } |
| 172 if (value < 0x10000) { |
| 173 %_TwoByteSeqStringSetChar(index++, value, result); |
| 174 } else { |
| 175 %_TwoByteSeqStringSetChar(index++, (value >> 10) + 0xd7c0, result); |
| 176 %_TwoByteSeqStringSetChar(index++, (value & 0x3ff) + 0xdc00, result); |
| 177 } |
| 178 return index; |
| 179 } |
| 180 |
| 181 // ECMA-262, section 15.1.3 |
| 182 function Encode(uri, unescape) { |
| 183 var uriLength = uri.length; |
| 184 var array = new InternalArray(uriLength); |
| 185 var index = 0; |
| 186 for (var k = 0; k < uriLength; k++) { |
| 187 var cc1 = uri.charCodeAt(k); |
| 188 if (unescape(cc1)) { |
| 189 array[index++] = cc1; |
| 190 } else { |
| 191 if (cc1 >= 0xDC00 && cc1 <= 0xDFFF) throw new $URIError("URI malformed"); |
| 192 if (cc1 < 0xD800 || cc1 > 0xDBFF) { |
| 193 index = URIEncodeSingle(cc1, array, index); |
| 194 } else { |
| 195 k++; |
| 196 if (k == uriLength) throw new $URIError("URI malformed"); |
| 197 var cc2 = uri.charCodeAt(k); |
| 198 if (cc2 < 0xDC00 || cc2 > 0xDFFF) throw new $URIError("URI malformed"); |
| 199 index = URIEncodePair(cc1, cc2, array, index); |
| 200 } |
173 } | 201 } |
174 if (value < 0x10000) { | 202 } |
175 %_TwoByteSeqStringSetChar(index++, value, result); | 203 |
| 204 var result = %NewString(array.length, NEW_ONE_BYTE_STRING); |
| 205 for (var i = 0; i < array.length; i++) { |
| 206 %_OneByteSeqStringSetChar(i, array[i], result); |
| 207 } |
| 208 return result; |
| 209 } |
| 210 |
| 211 // ECMA-262, section 15.1.3 |
| 212 function Decode(uri, reserved) { |
| 213 var uriLength = uri.length; |
| 214 var one_byte = %NewString(uriLength, NEW_ONE_BYTE_STRING); |
| 215 var index = 0; |
| 216 var k = 0; |
| 217 |
| 218 // Optimistically assume one-byte string. |
| 219 for ( ; k < uriLength; k++) { |
| 220 var code = uri.charCodeAt(k); |
| 221 if (code == 37) { // '%' |
| 222 if (k + 2 >= uriLength) throw new $URIError("URI malformed"); |
| 223 var cc = URIHexCharsToCharCode(uri.charCodeAt(k+1), uri.charCodeAt(k+2)); |
| 224 if (cc >> 7) break; // Assumption wrong, two-byte string. |
| 225 if (reserved(cc)) { |
| 226 %_OneByteSeqStringSetChar(index++, 37, one_byte); // '%'. |
| 227 %_OneByteSeqStringSetChar(index++, uri.charCodeAt(k+1), one_byte); |
| 228 %_OneByteSeqStringSetChar(index++, uri.charCodeAt(k+2), one_byte); |
| 229 } else { |
| 230 %_OneByteSeqStringSetChar(index++, cc, one_byte); |
| 231 } |
| 232 k += 2; |
176 } else { | 233 } else { |
177 %_TwoByteSeqStringSetChar(index++, (value >> 10) + 0xd7c0, result); | 234 if (code > 0x7f) break; // Assumption wrong, two-byte string. |
178 %_TwoByteSeqStringSetChar(index++, (value & 0x3ff) + 0xdc00, result); | 235 %_OneByteSeqStringSetChar(index++, code, one_byte); |
179 } | 236 } |
180 return index; | 237 } |
181 } | 238 |
182 | 239 one_byte = %TruncateString(one_byte, index); |
183 // ECMA-262, section 15.1.3 | 240 if (k == uriLength) return one_byte; |
184 function Encode(uri, unescape) { | 241 |
185 var uriLength = uri.length; | 242 // Write into two byte string. |
186 var array = new InternalArray(uriLength); | 243 var two_byte = %NewString(uriLength - k, NEW_TWO_BYTE_STRING); |
187 var index = 0; | 244 index = 0; |
188 for (var k = 0; k < uriLength; k++) { | 245 |
189 var cc1 = uri.charCodeAt(k); | 246 for ( ; k < uriLength; k++) { |
190 if (unescape(cc1)) { | 247 var code = uri.charCodeAt(k); |
191 array[index++] = cc1; | 248 if (code == 37) { // '%' |
| 249 if (k + 2 >= uriLength) throw new $URIError("URI malformed"); |
| 250 var cc = URIHexCharsToCharCode(uri.charCodeAt(++k), uri.charCodeAt(++k)); |
| 251 if (cc >> 7) { |
| 252 var n = 0; |
| 253 while (((cc << ++n) & 0x80) != 0) { } |
| 254 if (n == 1 || n > 4) throw new $URIError("URI malformed"); |
| 255 var octets = new GlobalArray(n); |
| 256 octets[0] = cc; |
| 257 if (k + 3 * (n - 1) >= uriLength) throw new $URIError("URI malformed"); |
| 258 for (var i = 1; i < n; i++) { |
| 259 if (uri.charAt(++k) != '%') throw new $URIError("URI malformed"); |
| 260 octets[i] = URIHexCharsToCharCode(uri.charCodeAt(++k), |
| 261 uri.charCodeAt(++k)); |
| 262 } |
| 263 index = URIDecodeOctets(octets, two_byte, index); |
| 264 } else if (reserved(cc)) { |
| 265 %_TwoByteSeqStringSetChar(index++, 37, two_byte); // '%'. |
| 266 %_TwoByteSeqStringSetChar(index++, uri.charCodeAt(k - 1), two_byte); |
| 267 %_TwoByteSeqStringSetChar(index++, uri.charCodeAt(k), two_byte); |
192 } else { | 268 } else { |
193 if (cc1 >= 0xDC00 && cc1 <= 0xDFFF) throw new $URIError("URI malformed")
; | 269 %_TwoByteSeqStringSetChar(index++, cc, two_byte); |
194 if (cc1 < 0xD800 || cc1 > 0xDBFF) { | 270 } |
195 index = URIEncodeSingle(cc1, array, index); | 271 } else { |
196 } else { | 272 %_TwoByteSeqStringSetChar(index++, code, two_byte); |
197 k++; | |
198 if (k == uriLength) throw new $URIError("URI malformed"); | |
199 var cc2 = uri.charCodeAt(k); | |
200 if (cc2 < 0xDC00 || cc2 > 0xDFFF) throw new $URIError("URI malformed")
; | |
201 index = URIEncodePair(cc1, cc2, array, index); | |
202 } | |
203 } | |
204 } | 273 } |
205 | 274 } |
206 var result = %NewString(array.length, NEW_ONE_BYTE_STRING); | 275 |
207 for (var i = 0; i < array.length; i++) { | 276 two_byte = %TruncateString(two_byte, index); |
208 %_OneByteSeqStringSetChar(i, array[i], result); | 277 return one_byte + two_byte; |
209 } | 278 } |
210 return result; | 279 |
211 } | 280 // ------------------------------------------------------------------- |
212 | 281 // Define exported functions. |
213 // ECMA-262, section 15.1.3 | 282 |
214 function Decode(uri, reserved) { | 283 // ECMA-262 - B.2.1. |
215 var uriLength = uri.length; | 284 function URIEscapeJS(str) { |
216 var one_byte = %NewString(uriLength, NEW_ONE_BYTE_STRING); | 285 var s = ToString(str); |
217 var index = 0; | 286 return %URIEscape(s); |
218 var k = 0; | 287 } |
219 | 288 |
220 // Optimistically assume one-byte string. | 289 // ECMA-262 - B.2.2. |
221 for ( ; k < uriLength; k++) { | 290 function URIUnescapeJS(str) { |
222 var code = uri.charCodeAt(k); | 291 var s = ToString(str); |
223 if (code == 37) { // '%' | 292 return %URIUnescape(s); |
224 if (k + 2 >= uriLength) throw new $URIError("URI malformed"); | 293 } |
225 var cc = URIHexCharsToCharCode(uri.charCodeAt(k+1), uri.charCodeAt(k+2))
; | 294 |
226 if (cc >> 7) break; // Assumption wrong, two-byte string. | 295 // ECMA-262 - 15.1.3.1. |
227 if (reserved(cc)) { | 296 function URIDecode(uri) { |
228 %_OneByteSeqStringSetChar(index++, 37, one_byte); // '%'. | 297 var reservedPredicate = function(cc) { |
229 %_OneByteSeqStringSetChar(index++, uri.charCodeAt(k+1), one_byte); | 298 // #$ |
230 %_OneByteSeqStringSetChar(index++, uri.charCodeAt(k+2), one_byte); | 299 if (35 <= cc && cc <= 36) return true; |
231 } else { | 300 // & |
232 %_OneByteSeqStringSetChar(index++, cc, one_byte); | 301 if (cc == 38) return true; |
233 } | 302 // +, |
234 k += 2; | 303 if (43 <= cc && cc <= 44) return true; |
235 } else { | 304 // / |
236 if (code > 0x7f) break; // Assumption wrong, two-byte string. | 305 if (cc == 47) return true; |
237 %_OneByteSeqStringSetChar(index++, code, one_byte); | 306 // :; |
238 } | 307 if (58 <= cc && cc <= 59) return true; |
239 } | 308 // = |
240 | 309 if (cc == 61) return true; |
241 one_byte = %TruncateString(one_byte, index); | 310 // ?@ |
242 if (k == uriLength) return one_byte; | 311 if (63 <= cc && cc <= 64) return true; |
243 | 312 |
244 // Write into two byte string. | 313 return false; |
245 var two_byte = %NewString(uriLength - k, NEW_TWO_BYTE_STRING); | 314 }; |
246 index = 0; | 315 var string = ToString(uri); |
247 | 316 return Decode(string, reservedPredicate); |
248 for ( ; k < uriLength; k++) { | 317 } |
249 var code = uri.charCodeAt(k); | 318 |
250 if (code == 37) { // '%' | 319 // ECMA-262 - 15.1.3.2. |
251 if (k + 2 >= uriLength) throw new $URIError("URI malformed"); | 320 function URIDecodeComponent(component) { |
252 var cc = URIHexCharsToCharCode(uri.charCodeAt(++k), uri.charCodeAt(++k))
; | 321 var reservedPredicate = function(cc) { return false; }; |
253 if (cc >> 7) { | 322 var string = ToString(component); |
254 var n = 0; | 323 return Decode(string, reservedPredicate); |
255 while (((cc << ++n) & 0x80) != 0) { } | 324 } |
256 if (n == 1 || n > 4) throw new $URIError("URI malformed"); | 325 |
257 var octets = new $Array(n); | 326 // ECMA-262 - 15.1.3.3. |
258 octets[0] = cc; | 327 function URIEncode(uri) { |
259 if (k + 3 * (n - 1) >= uriLength) throw new $URIError("URI malformed")
; | 328 var unescapePredicate = function(cc) { |
260 for (var i = 1; i < n; i++) { | 329 if (isAlphaNumeric(cc)) return true; |
261 if (uri.charAt(++k) != '%') throw new $URIError("URI malformed"); | 330 // ! |
262 octets[i] = URIHexCharsToCharCode(uri.charCodeAt(++k), | 331 if (cc == 33) return true; |
263 uri.charCodeAt(++k)); | 332 // #$ |
264 } | 333 if (35 <= cc && cc <= 36) return true; |
265 index = URIDecodeOctets(octets, two_byte, index); | 334 // &'()*+,-./ |
266 } else if (reserved(cc)) { | 335 if (38 <= cc && cc <= 47) return true; |
267 %_TwoByteSeqStringSetChar(index++, 37, two_byte); // '%'. | 336 // :; |
268 %_TwoByteSeqStringSetChar(index++, uri.charCodeAt(k - 1), two_byte); | 337 if (58 <= cc && cc <= 59) return true; |
269 %_TwoByteSeqStringSetChar(index++, uri.charCodeAt(k), two_byte); | 338 // = |
270 } else { | 339 if (cc == 61) return true; |
271 %_TwoByteSeqStringSetChar(index++, cc, two_byte); | 340 // ?@ |
272 } | 341 if (63 <= cc && cc <= 64) return true; |
273 } else { | 342 // _ |
274 %_TwoByteSeqStringSetChar(index++, code, two_byte); | 343 if (cc == 95) return true; |
275 } | 344 // ~ |
276 } | 345 if (cc == 126) return true; |
277 | 346 |
278 two_byte = %TruncateString(two_byte, index); | 347 return false; |
279 return one_byte + two_byte; | 348 }; |
280 } | 349 var string = ToString(uri); |
281 | 350 return Encode(string, unescapePredicate); |
282 // ------------------------------------------------------------------- | 351 } |
283 // Define exported functions. | 352 |
284 | 353 // ECMA-262 - 15.1.3.4 |
285 // ECMA-262 - B.2.1. | 354 function URIEncodeComponent(component) { |
286 function URIEscapeJS(str) { | 355 var unescapePredicate = function(cc) { |
287 var s = ToString(str); | 356 if (isAlphaNumeric(cc)) return true; |
288 return %URIEscape(s); | 357 // ! |
289 } | 358 if (cc == 33) return true; |
290 | 359 // '()* |
291 // ECMA-262 - B.2.2. | 360 if (39 <= cc && cc <= 42) return true; |
292 function URIUnescapeJS(str) { | 361 // -. |
293 var s = ToString(str); | 362 if (45 <= cc && cc <= 46) return true; |
294 return %URIUnescape(s); | 363 // _ |
295 } | 364 if (cc == 95) return true; |
296 | 365 // ~ |
297 // ECMA-262 - 15.1.3.1. | 366 if (cc == 126) return true; |
298 function URIDecode(uri) { | 367 |
299 var reservedPredicate = function(cc) { | 368 return false; |
300 // #$ | 369 }; |
301 if (35 <= cc && cc <= 36) return true; | 370 var string = ToString(component); |
302 // & | 371 return Encode(string, unescapePredicate); |
303 if (cc == 38) return true; | 372 } |
304 // +, | 373 |
305 if (43 <= cc && cc <= 44) return true; | 374 // ------------------------------------------------------------------- |
306 // / | 375 // Install exported functions. |
307 if (cc == 47) return true; | 376 |
308 // :; | 377 // Set up non-enumerable URI functions on the global object and set |
309 if (58 <= cc && cc <= 59) return true; | 378 // their names. |
310 // = | 379 InstallFunctions(global, DONT_ENUM, GlobalArray( |
311 if (cc == 61) return true; | 380 "escape", URIEscapeJS, |
312 // ?@ | 381 "unescape", URIUnescapeJS, |
313 if (63 <= cc && cc <= 64) return true; | 382 "decodeURI", URIDecode, |
314 | 383 "decodeURIComponent", URIDecodeComponent, |
315 return false; | 384 "encodeURI", URIEncode, |
316 }; | 385 "encodeURIComponent", URIEncodeComponent |
317 var string = ToString(uri); | 386 )); |
318 return Decode(string, reservedPredicate); | |
319 } | |
320 | |
321 // ECMA-262 - 15.1.3.2. | |
322 function URIDecodeComponent(component) { | |
323 var reservedPredicate = function(cc) { return false; }; | |
324 var string = ToString(component); | |
325 return Decode(string, reservedPredicate); | |
326 } | |
327 | |
328 // ECMA-262 - 15.1.3.3. | |
329 function URIEncode(uri) { | |
330 var unescapePredicate = function(cc) { | |
331 if (isAlphaNumeric(cc)) return true; | |
332 // ! | |
333 if (cc == 33) return true; | |
334 // #$ | |
335 if (35 <= cc && cc <= 36) return true; | |
336 // &'()*+,-./ | |
337 if (38 <= cc && cc <= 47) return true; | |
338 // :; | |
339 if (58 <= cc && cc <= 59) return true; | |
340 // = | |
341 if (cc == 61) return true; | |
342 // ?@ | |
343 if (63 <= cc && cc <= 64) return true; | |
344 // _ | |
345 if (cc == 95) return true; | |
346 // ~ | |
347 if (cc == 126) return true; | |
348 | |
349 return false; | |
350 }; | |
351 var string = ToString(uri); | |
352 return Encode(string, unescapePredicate); | |
353 } | |
354 | |
355 // ECMA-262 - 15.1.3.4 | |
356 function URIEncodeComponent(component) { | |
357 var unescapePredicate = function(cc) { | |
358 if (isAlphaNumeric(cc)) return true; | |
359 // ! | |
360 if (cc == 33) return true; | |
361 // '()* | |
362 if (39 <= cc && cc <= 42) return true; | |
363 // -. | |
364 if (45 <= cc && cc <= 46) return true; | |
365 // _ | |
366 if (cc == 95) return true; | |
367 // ~ | |
368 if (cc == 126) return true; | |
369 | |
370 return false; | |
371 }; | |
372 var string = ToString(component); | |
373 return Encode(string, unescapePredicate); | |
374 } | |
375 | |
376 // ------------------------------------------------------------------- | |
377 // Install exported functions. | |
378 | |
379 %CheckIsBootstrapping(); | |
380 | |
381 // Set up non-enumerable URI functions on the global object and set | |
382 // their names. | |
383 InstallFunctions(global, DONT_ENUM, $Array( | |
384 "escape", URIEscapeJS, | |
385 "unescape", URIUnescapeJS, | |
386 "decodeURI", URIDecode, | |
387 "decodeURIComponent", URIDecodeComponent, | |
388 "encodeURI", URIEncode, | |
389 "encodeURIComponent", URIEncodeComponent | |
390 )); | |
391 | 387 |
392 })(); | 388 })(); |
OLD | NEW |