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"; |
(...skipping 19 matching lines...) Expand all Loading... | |
30 // 0-9 | 30 // 0-9 |
31 if (code >= 48 && code <= 57) return code - 48; | 31 if (code >= 48 && code <= 57) return code - 48; |
32 // A-F | 32 // A-F |
33 if (code >= 65 && code <= 70) return code - 55; | 33 if (code >= 65 && code <= 70) return code - 55; |
34 // a-f | 34 // a-f |
35 if (code >= 97 && code <= 102) return code - 87; | 35 if (code >= 97 && code <= 102) return code - 87; |
36 | 36 |
37 return -1; | 37 return -1; |
38 } | 38 } |
39 | 39 |
40 // Does the char code correspond to an alpha-numeric char. | |
41 function isAlphaNumeric(cc) { | |
42 // a - z | |
43 if (97 <= cc && cc <= 122) return true; | |
44 // A - Z | |
45 if (65 <= cc && cc <= 90) return true; | |
46 // 0 - 9 | |
47 if (48 <= cc && cc <= 57) return true; | |
48 | |
49 return false; | |
50 } | |
51 | |
52 // Lazily initialized. | |
53 var hexCharCodeArray = 0; | |
54 | |
55 function URIAddEncodedOctetToBuffer(octet, result, index) { | |
56 result[index++] = 37; // Char code of '%'. | |
57 result[index++] = hexCharCodeArray[octet >> 4]; | |
58 result[index++] = hexCharCodeArray[octet & 0x0F]; | |
59 return index; | |
60 } | |
61 | |
62 function URIEncodeOctets(octets, result, index) { | |
63 if (hexCharCodeArray === 0) { | |
64 hexCharCodeArray = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, | |
65 65, 66, 67, 68, 69, 70]; | |
66 } | |
67 index = URIAddEncodedOctetToBuffer(octets[0], result, index); | |
68 if (octets[1]) index = URIAddEncodedOctetToBuffer(octets[1], result, index); | |
69 if (octets[2]) index = URIAddEncodedOctetToBuffer(octets[2], result, index); | |
70 if (octets[3]) index = URIAddEncodedOctetToBuffer(octets[3], result, index); | |
71 return index; | |
72 } | |
73 | |
74 function URIEncodeSingle(cc, result, index) { | |
75 var x = (cc >> 12) & 0xF; | |
76 var y = (cc >> 6) & 63; | |
77 var z = cc & 63; | |
78 var octets = new InternalArray(3); | |
79 if (cc <= 0x007F) { | |
80 octets[0] = cc; | |
81 } else if (cc <= 0x07FF) { | |
82 octets[0] = y + 192; | |
83 octets[1] = z + 128; | |
84 } else { | |
85 octets[0] = x + 224; | |
86 octets[1] = y + 128; | |
87 octets[2] = z + 128; | |
88 } | |
89 return URIEncodeOctets(octets, result, index); | |
90 } | |
91 | |
92 function URIEncodePair(cc1 , cc2, result, index) { | |
93 var u = ((cc1 >> 6) & 0xF) + 1; | |
94 var w = (cc1 >> 2) & 0xF; | |
95 var x = cc1 & 3; | |
96 var y = (cc2 >> 6) & 0xF; | |
97 var z = cc2 & 63; | |
98 var octets = new InternalArray(4); | |
99 octets[0] = (u >> 2) + 240; | |
100 octets[1] = (((u & 3) << 4) | w) + 128; | |
101 octets[2] = ((x << 4) | y) + 128; | |
102 octets[3] = z + 128; | |
103 return URIEncodeOctets(octets, result, index); | |
104 } | |
105 | |
106 function URIHexCharsToCharCode(highChar, lowChar) { | 40 function URIHexCharsToCharCode(highChar, lowChar) { |
107 var highCode = HexValueOf(highChar); | 41 var highCode = HexValueOf(highChar); |
108 var lowCode = HexValueOf(lowChar); | 42 var lowCode = HexValueOf(lowChar); |
109 if (highCode == -1 || lowCode == -1) throw MakeURIError(); | 43 if (highCode == -1 || lowCode == -1) throw MakeURIError(); |
110 return (highCode << 4) | lowCode; | 44 return (highCode << 4) | lowCode; |
111 } | 45 } |
112 | 46 |
113 // Callers must ensure that |result| is a sufficiently long sequential | 47 // Callers must ensure that |result| is a sufficiently long sequential |
114 // two-byte string! | 48 // two-byte string! |
115 function URIDecodeOctets(octets, result, index) { | 49 function URIDecodeOctets(octets, result, index) { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
161 if (value < 0x10000) { | 95 if (value < 0x10000) { |
162 %_TwoByteSeqStringSetChar(index++, value, result); | 96 %_TwoByteSeqStringSetChar(index++, value, result); |
163 } else { | 97 } else { |
164 %_TwoByteSeqStringSetChar(index++, (value >> 10) + 0xd7c0, result); | 98 %_TwoByteSeqStringSetChar(index++, (value >> 10) + 0xd7c0, result); |
165 %_TwoByteSeqStringSetChar(index++, (value & 0x3ff) + 0xdc00, result); | 99 %_TwoByteSeqStringSetChar(index++, (value & 0x3ff) + 0xdc00, result); |
166 } | 100 } |
167 return index; | 101 return index; |
168 } | 102 } |
169 | 103 |
170 // ECMA-262, section 15.1.3 | 104 // ECMA-262, section 15.1.3 |
171 function Encode(uri, unescape) { | 105 function EncodeJs(uri, isUri) { |
172 uri = TO_STRING(uri); | 106 uri = TO_STRING(uri); |
173 var uriLength = uri.length; | 107 return %Encode(uri, isUri); |
174 var array = new InternalArray(uriLength); | |
175 var index = 0; | |
176 for (var k = 0; k < uriLength; k++) { | |
177 var cc1 = %_StringCharCodeAt(uri, k); | |
178 if (unescape(cc1)) { | |
179 array[index++] = cc1; | |
180 } else { | |
181 if (cc1 >= 0xDC00 && cc1 <= 0xDFFF) throw MakeURIError(); | |
182 if (cc1 < 0xD800 || cc1 > 0xDBFF) { | |
183 index = URIEncodeSingle(cc1, array, index); | |
184 } else { | |
185 k++; | |
186 if (k == uriLength) throw MakeURIError(); | |
187 var cc2 = %_StringCharCodeAt(uri, k); | |
188 if (cc2 < 0xDC00 || cc2 > 0xDFFF) throw MakeURIError(); | |
189 index = URIEncodePair(cc1, cc2, array, index); | |
190 } | |
191 } | |
192 } | |
193 | |
194 var result = %NewString(array.length, NEW_ONE_BYTE_STRING); | |
195 for (var i = 0; i < array.length; i++) { | |
196 %_OneByteSeqStringSetChar(i, array[i], result); | |
197 } | |
198 return result; | |
199 } | 108 } |
200 | 109 |
201 // ECMA-262, section 15.1.3 | 110 // ECMA-262, section 15.1.3 |
202 function Decode(uri, reserved) { | 111 function Decode(uri, reserved) { |
203 uri = TO_STRING(uri); | 112 uri = TO_STRING(uri); |
204 var uriLength = uri.length; | 113 var uriLength = uri.length; |
205 var one_byte = %NewString(uriLength, NEW_ONE_BYTE_STRING); | 114 var one_byte = %NewString(uriLength, NEW_ONE_BYTE_STRING); |
206 var index = 0; | 115 var index = 0; |
207 var k = 0; | 116 var k = 0; |
208 | 117 |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
311 } | 220 } |
312 | 221 |
313 // ECMA-262 - 15.1.3.2. | 222 // ECMA-262 - 15.1.3.2. |
314 function URIDecodeComponent(component) { | 223 function URIDecodeComponent(component) { |
315 var reservedPredicate = function(cc) { return false; }; | 224 var reservedPredicate = function(cc) { return false; }; |
316 return Decode(component, reservedPredicate); | 225 return Decode(component, reservedPredicate); |
317 } | 226 } |
318 | 227 |
319 // ECMA-262 - 15.1.3.3. | 228 // ECMA-262 - 15.1.3.3. |
320 function URIEncode(uri) { | 229 function URIEncode(uri) { |
321 var unescapePredicate = function(cc) { | 230 return EncodeJs(uri, true); |
Yang
2016/05/12 07:39:20
Let's remove this indirection and inline the conte
Franzi
2016/05/13 09:42:02
Done.
| |
322 if (isAlphaNumeric(cc)) return true; | |
323 // ! | |
324 if (cc == 33) return true; | |
325 // #$ | |
326 if (35 <= cc && cc <= 36) return true; | |
327 // &'()*+,-./ | |
328 if (38 <= cc && cc <= 47) return true; | |
329 // :; | |
330 if (58 <= cc && cc <= 59) return true; | |
331 // = | |
332 if (cc == 61) return true; | |
333 // ?@ | |
334 if (63 <= cc && cc <= 64) return true; | |
335 // _ | |
336 if (cc == 95) return true; | |
337 // ~ | |
338 if (cc == 126) return true; | |
339 | |
340 return false; | |
341 }; | |
342 return Encode(uri, unescapePredicate); | |
343 } | 231 } |
344 | 232 |
345 // ECMA-262 - 15.1.3.4 | 233 // ECMA-262 - 15.1.3.4 |
346 function URIEncodeComponent(component) { | 234 function URIEncodeComponent(component) { |
347 var unescapePredicate = function(cc) { | 235 return EncodeJs(component, false); |
Yang
2016/05/12 07:39:20
Same here.
Franzi
2016/05/13 09:42:02
Done.
| |
348 if (isAlphaNumeric(cc)) return true; | |
349 // ! | |
350 if (cc == 33) return true; | |
351 // '()* | |
352 if (39 <= cc && cc <= 42) return true; | |
353 // -. | |
354 if (45 <= cc && cc <= 46) return true; | |
355 // _ | |
356 if (cc == 95) return true; | |
357 // ~ | |
358 if (cc == 126) return true; | |
359 | |
360 return false; | |
361 }; | |
362 return Encode(component, unescapePredicate); | |
363 } | 236 } |
364 | 237 |
365 // ------------------------------------------------------------------- | 238 // ------------------------------------------------------------------- |
366 // Install exported functions. | 239 // Install exported functions. |
367 | 240 |
368 // Set up non-enumerable URI functions on the global object and set | 241 // Set up non-enumerable URI functions on the global object and set |
369 // their names. | 242 // their names. |
370 utils.InstallFunctions(global, DONT_ENUM, [ | 243 utils.InstallFunctions(global, DONT_ENUM, [ |
371 "escape", URIEscapeJS, | 244 "escape", URIEscapeJS, |
372 "unescape", URIUnescapeJS, | 245 "unescape", URIUnescapeJS, |
373 "decodeURI", URIDecode, | 246 "decodeURI", URIDecode, |
374 "decodeURIComponent", URIDecodeComponent, | 247 "decodeURIComponent", URIDecodeComponent, |
375 "encodeURI", URIEncode, | 248 "encodeURI", URIEncode, |
376 "encodeURIComponent", URIEncodeComponent | 249 "encodeURIComponent", URIEncodeComponent |
377 ]); | 250 ]); |
378 | 251 |
379 }) | 252 }) |
OLD | NEW |