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 // This file contains support for URI manipulations written in | 5 // This file contains support for URI manipulations written in |
6 // JavaScript. | 6 // JavaScript. |
7 | 7 |
8 (function(global, utils) { | 8 (function(global, utils) { |
9 | 9 |
10 "use strict"; | 10 "use strict"; |
11 | 11 |
12 %CheckIsBootstrapping(); | 12 %CheckIsBootstrapping(); |
13 | 13 |
14 //- ------------------------------------------------------------------ | 14 //- ------------------------------------------------------------------ |
15 // Imports | 15 // Imports |
16 | 16 |
17 var GlobalObject = global.Object; | 17 var GlobalObject = global.Object; |
18 var GlobalArray = global.Array; | 18 var GlobalArray = global.Array; |
19 var InternalArray = utils.InternalArray; | 19 var InternalArray = utils.InternalArray; |
20 var ToString; | |
21 | |
22 utils.Import(function(from) { | |
23 ToString = from.ToString; | |
24 }); | |
25 | 20 |
26 // ------------------------------------------------------------------- | 21 // ------------------------------------------------------------------- |
27 // Define internal helper functions. | 22 // Define internal helper functions. |
28 | 23 |
29 function HexValueOf(code) { | 24 function HexValueOf(code) { |
30 // 0-9 | 25 // 0-9 |
31 if (code >= 48 && code <= 57) return code - 48; | 26 if (code >= 48 && code <= 57) return code - 48; |
32 // A-F | 27 // A-F |
33 if (code >= 65 && code <= 70) return code - 55; | 28 if (code >= 65 && code <= 70) return code - 55; |
34 // a-f | 29 // a-f |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 %_TwoByteSeqStringSetChar(index++, value, result); | 157 %_TwoByteSeqStringSetChar(index++, value, result); |
163 } else { | 158 } else { |
164 %_TwoByteSeqStringSetChar(index++, (value >> 10) + 0xd7c0, result); | 159 %_TwoByteSeqStringSetChar(index++, (value >> 10) + 0xd7c0, result); |
165 %_TwoByteSeqStringSetChar(index++, (value & 0x3ff) + 0xdc00, result); | 160 %_TwoByteSeqStringSetChar(index++, (value & 0x3ff) + 0xdc00, result); |
166 } | 161 } |
167 return index; | 162 return index; |
168 } | 163 } |
169 | 164 |
170 // ECMA-262, section 15.1.3 | 165 // ECMA-262, section 15.1.3 |
171 function Encode(uri, unescape) { | 166 function Encode(uri, unescape) { |
172 uri = TO_STRING_INLINE(uri); | 167 uri = TO_STRING(uri); |
173 var uriLength = uri.length; | 168 var uriLength = uri.length; |
174 var array = new InternalArray(uriLength); | 169 var array = new InternalArray(uriLength); |
175 var index = 0; | 170 var index = 0; |
176 for (var k = 0; k < uriLength; k++) { | 171 for (var k = 0; k < uriLength; k++) { |
177 var cc1 = %_StringCharCodeAt(uri, k); | 172 var cc1 = %_StringCharCodeAt(uri, k); |
178 if (unescape(cc1)) { | 173 if (unescape(cc1)) { |
179 array[index++] = cc1; | 174 array[index++] = cc1; |
180 } else { | 175 } else { |
181 if (cc1 >= 0xDC00 && cc1 <= 0xDFFF) throw MakeURIError(); | 176 if (cc1 >= 0xDC00 && cc1 <= 0xDFFF) throw MakeURIError(); |
182 if (cc1 < 0xD800 || cc1 > 0xDBFF) { | 177 if (cc1 < 0xD800 || cc1 > 0xDBFF) { |
(...skipping 10 matching lines...) Expand all Loading... |
193 | 188 |
194 var result = %NewString(array.length, NEW_ONE_BYTE_STRING); | 189 var result = %NewString(array.length, NEW_ONE_BYTE_STRING); |
195 for (var i = 0; i < array.length; i++) { | 190 for (var i = 0; i < array.length; i++) { |
196 %_OneByteSeqStringSetChar(i, array[i], result); | 191 %_OneByteSeqStringSetChar(i, array[i], result); |
197 } | 192 } |
198 return result; | 193 return result; |
199 } | 194 } |
200 | 195 |
201 // ECMA-262, section 15.1.3 | 196 // ECMA-262, section 15.1.3 |
202 function Decode(uri, reserved) { | 197 function Decode(uri, reserved) { |
203 uri = TO_STRING_INLINE(uri); | 198 uri = TO_STRING(uri); |
204 var uriLength = uri.length; | 199 var uriLength = uri.length; |
205 var one_byte = %NewString(uriLength, NEW_ONE_BYTE_STRING); | 200 var one_byte = %NewString(uriLength, NEW_ONE_BYTE_STRING); |
206 var index = 0; | 201 var index = 0; |
207 var k = 0; | 202 var k = 0; |
208 | 203 |
209 // Optimistically assume one-byte string. | 204 // Optimistically assume one-byte string. |
210 for ( ; k < uriLength; k++) { | 205 for ( ; k < uriLength; k++) { |
211 var code = %_StringCharCodeAt(uri, k); | 206 var code = %_StringCharCodeAt(uri, k); |
212 if (code == 37) { // '%' | 207 if (code == 37) { // '%' |
213 if (k + 2 >= uriLength) throw MakeURIError(); | 208 if (k + 2 >= uriLength) throw MakeURIError(); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 } | 266 } |
272 | 267 |
273 two_byte = %TruncateString(two_byte, index); | 268 two_byte = %TruncateString(two_byte, index); |
274 return one_byte + two_byte; | 269 return one_byte + two_byte; |
275 } | 270 } |
276 | 271 |
277 // ------------------------------------------------------------------- | 272 // ------------------------------------------------------------------- |
278 // Define exported functions. | 273 // Define exported functions. |
279 | 274 |
280 // ECMA-262 - B.2.1. | 275 // ECMA-262 - B.2.1. |
281 function URIEscapeJS(str) { | 276 function URIEscapeJS(s) { |
282 var s = ToString(str); | |
283 return %URIEscape(s); | 277 return %URIEscape(s); |
284 } | 278 } |
285 | 279 |
286 // ECMA-262 - B.2.2. | 280 // ECMA-262 - B.2.2. |
287 function URIUnescapeJS(str) { | 281 function URIUnescapeJS(s) { |
288 var s = ToString(str); | |
289 return %URIUnescape(s); | 282 return %URIUnescape(s); |
290 } | 283 } |
291 | 284 |
292 // ECMA-262 - 15.1.3.1. | 285 // ECMA-262 - 15.1.3.1. |
293 function URIDecode(uri) { | 286 function URIDecode(uri) { |
294 var reservedPredicate = function(cc) { | 287 var reservedPredicate = function(cc) { |
295 // #$ | 288 // #$ |
296 if (35 <= cc && cc <= 36) return true; | 289 if (35 <= cc && cc <= 36) return true; |
297 // & | 290 // & |
298 if (cc == 38) return true; | 291 if (cc == 38) return true; |
299 // +, | 292 // +, |
300 if (43 <= cc && cc <= 44) return true; | 293 if (43 <= cc && cc <= 44) return true; |
301 // / | 294 // / |
302 if (cc == 47) return true; | 295 if (cc == 47) return true; |
303 // :; | 296 // :; |
304 if (58 <= cc && cc <= 59) return true; | 297 if (58 <= cc && cc <= 59) return true; |
305 // = | 298 // = |
306 if (cc == 61) return true; | 299 if (cc == 61) return true; |
307 // ?@ | 300 // ?@ |
308 if (63 <= cc && cc <= 64) return true; | 301 if (63 <= cc && cc <= 64) return true; |
309 | 302 |
310 return false; | 303 return false; |
311 }; | 304 }; |
312 var string = ToString(uri); | 305 return Decode(uri, reservedPredicate); |
313 return Decode(string, reservedPredicate); | |
314 } | 306 } |
315 | 307 |
316 // ECMA-262 - 15.1.3.2. | 308 // ECMA-262 - 15.1.3.2. |
317 function URIDecodeComponent(component) { | 309 function URIDecodeComponent(component) { |
318 var reservedPredicate = function(cc) { return false; }; | 310 var reservedPredicate = function(cc) { return false; }; |
319 var string = ToString(component); | 311 return Decode(component, reservedPredicate); |
320 return Decode(string, reservedPredicate); | |
321 } | 312 } |
322 | 313 |
323 // ECMA-262 - 15.1.3.3. | 314 // ECMA-262 - 15.1.3.3. |
324 function URIEncode(uri) { | 315 function URIEncode(uri) { |
325 var unescapePredicate = function(cc) { | 316 var unescapePredicate = function(cc) { |
326 if (isAlphaNumeric(cc)) return true; | 317 if (isAlphaNumeric(cc)) return true; |
327 // ! | 318 // ! |
328 if (cc == 33) return true; | 319 if (cc == 33) return true; |
329 // #$ | 320 // #$ |
330 if (35 <= cc && cc <= 36) return true; | 321 if (35 <= cc && cc <= 36) return true; |
331 // &'()*+,-./ | 322 // &'()*+,-./ |
332 if (38 <= cc && cc <= 47) return true; | 323 if (38 <= cc && cc <= 47) return true; |
333 // :; | 324 // :; |
334 if (58 <= cc && cc <= 59) return true; | 325 if (58 <= cc && cc <= 59) return true; |
335 // = | 326 // = |
336 if (cc == 61) return true; | 327 if (cc == 61) return true; |
337 // ?@ | 328 // ?@ |
338 if (63 <= cc && cc <= 64) return true; | 329 if (63 <= cc && cc <= 64) return true; |
339 // _ | 330 // _ |
340 if (cc == 95) return true; | 331 if (cc == 95) return true; |
341 // ~ | 332 // ~ |
342 if (cc == 126) return true; | 333 if (cc == 126) return true; |
343 | 334 |
344 return false; | 335 return false; |
345 }; | 336 }; |
346 var string = ToString(uri); | 337 return Encode(uri, unescapePredicate); |
347 return Encode(string, unescapePredicate); | |
348 } | 338 } |
349 | 339 |
350 // ECMA-262 - 15.1.3.4 | 340 // ECMA-262 - 15.1.3.4 |
351 function URIEncodeComponent(component) { | 341 function URIEncodeComponent(component) { |
352 var unescapePredicate = function(cc) { | 342 var unescapePredicate = function(cc) { |
353 if (isAlphaNumeric(cc)) return true; | 343 if (isAlphaNumeric(cc)) return true; |
354 // ! | 344 // ! |
355 if (cc == 33) return true; | 345 if (cc == 33) return true; |
356 // '()* | 346 // '()* |
357 if (39 <= cc && cc <= 42) return true; | 347 if (39 <= cc && cc <= 42) return true; |
358 // -. | 348 // -. |
359 if (45 <= cc && cc <= 46) return true; | 349 if (45 <= cc && cc <= 46) return true; |
360 // _ | 350 // _ |
361 if (cc == 95) return true; | 351 if (cc == 95) return true; |
362 // ~ | 352 // ~ |
363 if (cc == 126) return true; | 353 if (cc == 126) return true; |
364 | 354 |
365 return false; | 355 return false; |
366 }; | 356 }; |
367 var string = ToString(component); | 357 return Encode(component, unescapePredicate); |
368 return Encode(string, unescapePredicate); | |
369 } | 358 } |
370 | 359 |
371 // ------------------------------------------------------------------- | 360 // ------------------------------------------------------------------- |
372 // Install exported functions. | 361 // Install exported functions. |
373 | 362 |
374 // Set up non-enumerable URI functions on the global object and set | 363 // Set up non-enumerable URI functions on the global object and set |
375 // their names. | 364 // their names. |
376 utils.InstallFunctions(global, DONT_ENUM, [ | 365 utils.InstallFunctions(global, DONT_ENUM, [ |
377 "escape", URIEscapeJS, | 366 "escape", URIEscapeJS, |
378 "unescape", URIUnescapeJS, | 367 "unescape", URIUnescapeJS, |
379 "decodeURI", URIDecode, | 368 "decodeURI", URIDecode, |
380 "decodeURIComponent", URIDecodeComponent, | 369 "decodeURIComponent", URIDecodeComponent, |
381 "encodeURI", URIEncode, | 370 "encodeURI", URIEncode, |
382 "encodeURIComponent", URIEncodeComponent | 371 "encodeURIComponent", URIEncodeComponent |
383 ]); | 372 ]); |
384 | 373 |
385 }) | 374 }) |
OLD | NEW |