Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(87)

Side by Side Diff: src/uri.js

Issue 292173011: Harden a few builtins (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: rebased Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/promise.js ('k') | tools/generate-runtime-tests.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"; 5 "use strict";
6 6
7 // This file relies on the fact that the following declaration has been made 7 // This file relies on the fact that the following declaration has been made
8 // in runtime.js: 8 // in runtime.js:
9 // var $Array = global.Array; 9 // var $Array = global.Array;
10 10
11 // ------------------------------------------------------------------- 11 // -------------------------------------------------------------------
12 12
13 // This file contains support for URI manipulations written in 13 // This file contains support for URI manipulations written in
14 // JavaScript. 14 // JavaScript.
15 15
16 // Lazily initialized. 16
17 var hexCharArray = 0; 17 (function() {
18 var hexCharCodeArray = 0; 18
19 19 // -------------------------------------------------------------------
20 20 // Define internal helper functions.
21 function URIAddEncodedOctetToBuffer(octet, result, index) { 21
22 result[index++] = 37; // Char code of '%'. 22 function HexValueOf(code) {
23 result[index++] = hexCharCodeArray[octet >> 4]; 23 // 0-9
24 result[index++] = hexCharCodeArray[octet & 0x0F]; 24 if (code >= 48 && code <= 57) return code - 48;
25 return index; 25 // A-F
26 } 26 if (code >= 65 && code <= 70) return code - 55;
27 27 // a-f
28 28 if (code >= 97 && code <= 102) return code - 87;
29 function URIEncodeOctets(octets, result, index) { 29
30 if (hexCharCodeArray === 0) { 30 return -1;
31 hexCharCodeArray = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 31 }
32 65, 66, 67, 68, 69, 70]; 32
33 } 33 // Does the char code correspond to an alpha-numeric char.
34 index = URIAddEncodedOctetToBuffer(octets[0], result, index); 34 function isAlphaNumeric(cc) {
35 if (octets[1]) index = URIAddEncodedOctetToBuffer(octets[1], result, index); 35 // a - z
36 if (octets[2]) index = URIAddEncodedOctetToBuffer(octets[2], result, index); 36 if (97 <= cc && cc <= 122) return true;
37 if (octets[3]) index = URIAddEncodedOctetToBuffer(octets[3], result, index); 37 // A - Z
38 return index; 38 if (65 <= cc && cc <= 90) return true;
39 } 39 // 0 - 9
40 40 if (48 <= cc && cc <= 57) return true;
41 41
42 function URIEncodeSingle(cc, result, index) { 42 return false;
43 var x = (cc >> 12) & 0xF; 43 }
44 var y = (cc >> 6) & 63; 44
45 var z = cc & 63; 45 //Lazily initialized.
46 var octets = new $Array(3); 46 var hexCharCodeArray = 0;
47 if (cc <= 0x007F) { 47
48 octets[0] = cc; 48 function URIAddEncodedOctetToBuffer(octet, result, index) {
49 } else if (cc <= 0x07FF) { 49 result[index++] = 37; // Char code of '%'.
50 octets[0] = y + 192; 50 result[index++] = hexCharCodeArray[octet >> 4];
51 octets[1] = z + 128; 51 result[index++] = hexCharCodeArray[octet & 0x0F];
52 } else { 52 return index;
53 octets[0] = x + 224; 53 }
54 octets[1] = y + 128; 54
55 octets[2] = z + 128; 55 function URIEncodeOctets(octets, result, index) {
56 } 56 if (hexCharCodeArray === 0) {
57 return URIEncodeOctets(octets, result, index); 57 hexCharCodeArray = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
58 } 58 65, 66, 67, 68, 69, 70];
59 59 }
60 60 index = URIAddEncodedOctetToBuffer(octets[0], result, index);
61 function URIEncodePair(cc1 , cc2, result, index) { 61 if (octets[1]) index = URIAddEncodedOctetToBuffer(octets[1], result, index);
62 var u = ((cc1 >> 6) & 0xF) + 1; 62 if (octets[2]) index = URIAddEncodedOctetToBuffer(octets[2], result, index);
63 var w = (cc1 >> 2) & 0xF; 63 if (octets[3]) index = URIAddEncodedOctetToBuffer(octets[3], result, index);
64 var x = cc1 & 3; 64 return index;
65 var y = (cc2 >> 6) & 0xF; 65 }
66 var z = cc2 & 63; 66
67 var octets = new $Array(4); 67 function URIEncodeSingle(cc, result, index) {
68 octets[0] = (u >> 2) + 240; 68 var x = (cc >> 12) & 0xF;
69 octets[1] = (((u & 3) << 4) | w) + 128; 69 var y = (cc >> 6) & 63;
70 octets[2] = ((x << 4) | y) + 128; 70 var z = cc & 63;
71 octets[3] = z + 128; 71 var octets = new $Array(3);
72 return URIEncodeOctets(octets, result, index); 72 if (cc <= 0x007F) {
73 } 73 octets[0] = cc;
74 74 } else if (cc <= 0x07FF) {
75 75 octets[0] = y + 192;
76 function URIHexCharsToCharCode(highChar, lowChar) { 76 octets[1] = z + 128;
77 var highCode = HexValueOf(highChar);
78 var lowCode = HexValueOf(lowChar);
79 if (highCode == -1 || lowCode == -1) {
80 throw new $URIError("URI malformed");
81 }
82 return (highCode << 4) | lowCode;
83 }
84
85
86 function URIDecodeOctets(octets, result, index) {
87 if (!IS_STRING(result)) throw new $URIError("Internal error");
88 var value;
89 var o0 = octets[0];
90 if (o0 < 0x80) {
91 value = o0;
92 } else if (o0 < 0xc2) {
93 throw new $URIError("URI malformed");
94 } else {
95 var o1 = octets[1];
96 if (o0 < 0xe0) {
97 var a = o0 & 0x1f;
98 if ((o1 < 0x80) || (o1 > 0xbf)) {
99 throw new $URIError("URI malformed");
100 }
101 var b = o1 & 0x3f;
102 value = (a << 6) + b;
103 if (value < 0x80 || value > 0x7ff) {
104 throw new $URIError("URI malformed");
105 }
106 } else { 77 } else {
107 var o2 = octets[2]; 78 octets[0] = x + 224;
108 if (o0 < 0xf0) { 79 octets[1] = y + 128;
109 var a = o0 & 0x0f; 80 octets[2] = z + 128;
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;
110 if ((o1 < 0x80) || (o1 > 0xbf)) { 121 if ((o1 < 0x80) || (o1 > 0xbf)) {
111 throw new $URIError("URI malformed"); 122 throw new $URIError("URI malformed");
112 } 123 }
113 var b = o1 & 0x3f; 124 var b = o1 & 0x3f;
114 if ((o2 < 0x80) || (o2 > 0xbf)) { 125 value = (a << 6) + b;
126 if (value < 0x80 || value > 0x7ff) {
115 throw new $URIError("URI malformed"); 127 throw new $URIError("URI malformed");
116 } 128 }
117 var c = o2 & 0x3f;
118 value = (a << 12) + (b << 6) + c;
119 if ((value < 0x800) || (value > 0xffff)) {
120 throw new $URIError("URI malformed");
121 }
122 } else { 129 } else {
123 var o3 = octets[3]; 130 var o2 = octets[2];
124 if (o0 < 0xf8) { 131 if (o0 < 0xf0) {
125 var a = (o0 & 0x07); 132 var a = o0 & 0x0f;
126 if ((o1 < 0x80) || (o1 > 0xbf)) { 133 if ((o1 < 0x80) || (o1 > 0xbf)) {
127 throw new $URIError("URI malformed"); 134 throw new $URIError("URI malformed");
128 } 135 }
129 var b = (o1 & 0x3f); 136 var b = o1 & 0x3f;
130 if ((o2 < 0x80) || (o2 > 0xbf)) { 137 if ((o2 < 0x80) || (o2 > 0xbf)) {
131 throw new $URIError("URI malformed"); 138 throw new $URIError("URI malformed");
132 } 139 }
133 var c = (o2 & 0x3f); 140 var c = o2 & 0x3f;
134 if ((o3 < 0x80) || (o3 > 0xbf)) { 141 value = (a << 12) + (b << 6) + c;
142 if ((value < 0x800) || (value > 0xffff)) {
135 throw new $URIError("URI malformed"); 143 throw new $URIError("URI malformed");
136 } 144 }
137 var d = (o3 & 0x3f); 145 } else {
138 value = (a << 18) + (b << 12) + (c << 6) + d; 146 var o3 = octets[3];
139 if ((value < 0x10000) || (value > 0x10ffff)) { 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 {
140 throw new $URIError("URI malformed"); 166 throw new $URIError("URI malformed");
141 } 167 }
168 }
169 }
170 }
171 if (0xD800 <= value && value <= 0xDFFF) {
172 throw new $URIError("URI malformed");
173 }
174 if (value < 0x10000) {
175 %_TwoByteSeqStringSetChar(result, index++, value);
176 } else {
177 %_TwoByteSeqStringSetChar(result, index++, (value >> 10) + 0xd7c0);
178 %_TwoByteSeqStringSetChar(result, index++, (value & 0x3ff) + 0xdc00);
179 }
180 return index;
181 }
182
183 // ECMA-262, section 15.1.3
184 function Encode(uri, unescape) {
185 var uriLength = uri.length;
186 var array = new InternalArray(uriLength);
187 var index = 0;
188 for (var k = 0; k < uriLength; k++) {
189 var cc1 = uri.charCodeAt(k);
190 if (unescape(cc1)) {
191 array[index++] = cc1;
192 } else {
193 if (cc1 >= 0xDC00 && cc1 <= 0xDFFF) throw new $URIError("URI malformed") ;
194 if (cc1 < 0xD800 || cc1 > 0xDBFF) {
195 index = URIEncodeSingle(cc1, array, index);
142 } else { 196 } else {
143 throw new $URIError("URI malformed"); 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);
144 } 202 }
145 } 203 }
146 } 204 }
147 } 205
148 if (0xD800 <= value && value <= 0xDFFF) { 206 var result = %NewString(array.length, NEW_ONE_BYTE_STRING);
149 throw new $URIError("URI malformed"); 207 for (var i = 0; i < array.length; i++) {
150 } 208 %_OneByteSeqStringSetChar(result, i, array[i]);
151 if (value < 0x10000) { 209 }
152 if (index < 0 || index >= result.length) { 210 return result;
153 throw new $URIError("Internal error"); 211 }
154 } 212
155 %_TwoByteSeqStringSetChar(result, index++, value); 213 // ECMA-262, section 15.1.3
156 return index; 214 function Decode(uri, reserved) {
157 } else { 215 var uriLength = uri.length;
158 if (index < 0 || index >= result.length - 1) { 216 var one_byte = %NewString(uriLength, NEW_ONE_BYTE_STRING);
159 throw new $URIError("Internal error"); 217 var index = 0;
160 } 218 var k = 0;
161 %_TwoByteSeqStringSetChar(result, index++, (value >> 10) + 0xd7c0); 219
162 %_TwoByteSeqStringSetChar(result, index++, (value & 0x3ff) + 0xdc00); 220 // Optimistically assume ascii string.
163 return index; 221 for ( ; k < uriLength; k++) {
164 } 222 var code = uri.charCodeAt(k);
165 } 223 if (code == 37) { // '%'
166 224 if (k + 2 >= uriLength) throw new $URIError("URI malformed");
167 225 var cc = URIHexCharsToCharCode(uri.charCodeAt(k+1), uri.charCodeAt(k+2)) ;
168 // ECMA-262, section 15.1.3 226 if (cc >> 7) break; // Assumption wrong, two byte string.
169 function Encode(uri, unescape) { 227 if (reserved(cc)) {
170 var uriLength = uri.length; 228 %_OneByteSeqStringSetChar(one_byte, index++, 37); // '%'.
171 var array = new InternalArray(uriLength); 229 %_OneByteSeqStringSetChar(one_byte, index++, uri.charCodeAt(k+1));
172 var index = 0; 230 %_OneByteSeqStringSetChar(one_byte, index++, uri.charCodeAt(k+2));
173 for (var k = 0; k < uriLength; k++) { 231 } else {
174 var cc1 = uri.charCodeAt(k); 232 %_OneByteSeqStringSetChar(one_byte, index++, cc);
175 if (unescape(cc1)) { 233 }
176 array[index++] = cc1; 234 k += 2;
177 } else {
178 if (cc1 >= 0xDC00 && cc1 <= 0xDFFF) throw new $URIError("URI malformed");
179 if (cc1 < 0xD800 || cc1 > 0xDBFF) {
180 index = URIEncodeSingle(cc1, array, index);
181 } else { 235 } else {
182 k++; 236 if (code > 0x7f) break; // Assumption wrong, two byte string.
183 if (k == uriLength) throw new $URIError("URI malformed"); 237 %_OneByteSeqStringSetChar(one_byte, index++, code);
184 var cc2 = uri.charCodeAt(k);
185 if (cc2 < 0xDC00 || cc2 > 0xDFFF) throw new $URIError("URI malformed");
186 index = URIEncodePair(cc1, cc2, array, index);
187 } 238 }
188 } 239 }
189 } 240
190 241 one_byte = %TruncateString(one_byte, index);
191 var result = %NewString(array.length, NEW_ONE_BYTE_STRING); 242 if (k == uriLength) return one_byte;
192 for (var i = 0; i < array.length; i++) { 243
193 %_OneByteSeqStringSetChar(result, i, array[i]); 244 // Write into two byte string.
194 } 245 var two_byte = %NewString(uriLength - k, NEW_TWO_BYTE_STRING);
195 return result; 246 index = 0;
196 } 247
197 248 for ( ; k < uriLength; k++) {
198 249 var code = uri.charCodeAt(k);
199 // ECMA-262, section 15.1.3 250 if (code == 37) { // '%'
200 function Decode(uri, reserved) { 251 if (k + 2 >= uriLength) throw new $URIError("URI malformed");
201 var uriLength = uri.length; 252 var cc = URIHexCharsToCharCode(uri.charCodeAt(++k), uri.charCodeAt(++k)) ;
202 var one_byte = %NewString(uriLength, NEW_ONE_BYTE_STRING); 253 if (cc >> 7) {
203 var index = 0; 254 var n = 0;
204 var k = 0; 255 while (((cc << ++n) & 0x80) != 0) { }
205 256 if (n == 1 || n > 4) throw new $URIError("URI malformed");
206 // Optimistically assume ascii string. 257 var octets = new $Array(n);
207 for ( ; k < uriLength; k++) { 258 octets[0] = cc;
208 var code = uri.charCodeAt(k); 259 if (k + 3 * (n - 1) >= uriLength) throw new $URIError("URI malformed") ;
209 if (code == 37) { // '%' 260 for (var i = 1; i < n; i++) {
210 if (k + 2 >= uriLength) throw new $URIError("URI malformed"); 261 if (uri.charAt(++k) != '%') throw new $URIError("URI malformed");
211 var cc = URIHexCharsToCharCode(uri.charCodeAt(k+1), uri.charCodeAt(k+2)); 262 octets[i] = URIHexCharsToCharCode(uri.charCodeAt(++k),
212 if (cc >> 7) break; // Assumption wrong, two byte string. 263 uri.charCodeAt(++k));
213 if (reserved(cc)) { 264 }
214 %_OneByteSeqStringSetChar(one_byte, index++, 37); // '%'. 265 index = URIDecodeOctets(octets, two_byte, index);
215 %_OneByteSeqStringSetChar(one_byte, index++, uri.charCodeAt(k+1)); 266 } else if (reserved(cc)) {
216 %_OneByteSeqStringSetChar(one_byte, index++, uri.charCodeAt(k+2)); 267 %_TwoByteSeqStringSetChar(two_byte, index++, 37); // '%'.
268 %_TwoByteSeqStringSetChar(two_byte, index++, uri.charCodeAt(k - 1));
269 %_TwoByteSeqStringSetChar(two_byte, index++, uri.charCodeAt(k));
270 } else {
271 %_TwoByteSeqStringSetChar(two_byte, index++, cc);
272 }
217 } else { 273 } else {
218 %_OneByteSeqStringSetChar(one_byte, index++, cc); 274 %_TwoByteSeqStringSetChar(two_byte, index++, code);
219 } 275 }
220 k += 2; 276 }
221 } else { 277
222 if (code > 0x7f) break; // Assumption wrong, two byte string. 278 two_byte = %TruncateString(two_byte, index);
223 %_OneByteSeqStringSetChar(one_byte, index++, code); 279 return one_byte + two_byte;
224 } 280 }
225 } 281
226 282 // -------------------------------------------------------------------
227 one_byte = %TruncateString(one_byte, index); 283 // Define exported functions.
228 if (k == uriLength) return one_byte; 284
229 285 // ECMA-262 - B.2.1.
230 // Write into two byte string. 286 function URIEscapeJS(str) {
231 var two_byte = %NewString(uriLength - k, NEW_TWO_BYTE_STRING); 287 var s = ToString(str);
232 index = 0; 288 return %URIEscape(s);
233 289 }
234 for ( ; k < uriLength; k++) { 290
235 var code = uri.charCodeAt(k); 291 // ECMA-262 - B.2.2.
236 if (code == 37) { // '%' 292 function URIUnescapeJS(str) {
237 if (k + 2 >= uriLength) throw new $URIError("URI malformed"); 293 var s = ToString(str);
238 var cc = URIHexCharsToCharCode(uri.charCodeAt(++k), uri.charCodeAt(++k)); 294 return %URIUnescape(s);
239 if (cc >> 7) { 295 }
240 var n = 0; 296
241 while (((cc << ++n) & 0x80) != 0) { } 297 // ECMA-262 - 15.1.3.1.
242 if (n == 1 || n > 4) throw new $URIError("URI malformed"); 298 function URIDecode(uri) {
243 var octets = new $Array(n); 299 var reservedPredicate = function(cc) {
244 octets[0] = cc; 300 // #$
245 if (k + 3 * (n - 1) >= uriLength) throw new $URIError("URI malformed"); 301 if (35 <= cc && cc <= 36) return true;
246 for (var i = 1; i < n; i++) { 302 // &
247 if (uri.charAt(++k) != '%') throw new $URIError("URI malformed"); 303 if (cc == 38) return true;
248 octets[i] = URIHexCharsToCharCode(uri.charCodeAt(++k), 304 // +,
249 uri.charCodeAt(++k)); 305 if (43 <= cc && cc <= 44) return true;
250 } 306 // /
251 index = URIDecodeOctets(octets, two_byte, index); 307 if (cc == 47) return true;
252 } else if (reserved(cc)) { 308 // :;
253 %_TwoByteSeqStringSetChar(two_byte, index++, 37); // '%'. 309 if (58 <= cc && cc <= 59) return true;
254 %_TwoByteSeqStringSetChar(two_byte, index++, uri.charCodeAt(k - 1)); 310 // =
255 %_TwoByteSeqStringSetChar(two_byte, index++, uri.charCodeAt(k)); 311 if (cc == 61) return true;
256 } else { 312 // ?@
257 %_TwoByteSeqStringSetChar(two_byte, index++, cc); 313 if (63 <= cc && cc <= 64) return true;
258 } 314
259 } else {
260 %_TwoByteSeqStringSetChar(two_byte, index++, code);
261 }
262 }
263
264 two_byte = %TruncateString(two_byte, index);
265 return one_byte + two_byte;
266 }
267
268
269 // ECMA-262 - 15.1.3.1.
270 function URIDecode(uri) {
271 var reservedPredicate = function(cc) {
272 // #$
273 if (35 <= cc && cc <= 36) return true;
274 // &
275 if (cc == 38) return true;
276 // +,
277 if (43 <= cc && cc <= 44) return true;
278 // /
279 if (cc == 47) return true;
280 // :;
281 if (58 <= cc && cc <= 59) return true;
282 // =
283 if (cc == 61) return true;
284 // ?@
285 if (63 <= cc && cc <= 64) return true;
286
287 return false;
288 };
289 var string = ToString(uri);
290 return Decode(string, reservedPredicate);
291 }
292
293
294 // ECMA-262 - 15.1.3.2.
295 function URIDecodeComponent(component) {
296 var reservedPredicate = function(cc) { return false; };
297 var string = ToString(component);
298 return Decode(string, reservedPredicate);
299 }
300
301
302 // Does the char code correspond to an alpha-numeric char.
303 function isAlphaNumeric(cc) {
304 // a - z
305 if (97 <= cc && cc <= 122) return true;
306 // A - Z
307 if (65 <= cc && cc <= 90) return true;
308 // 0 - 9
309 if (48 <= cc && cc <= 57) return true;
310
311 return false;
312 }
313
314
315 // ECMA-262 - 15.1.3.3.
316 function URIEncode(uri) {
317 var unescapePredicate = function(cc) {
318 if (isAlphaNumeric(cc)) return true;
319 // !
320 if (cc == 33) return true;
321 // #$
322 if (35 <= cc && cc <= 36) return true;
323 // &'()*+,-./
324 if (38 <= cc && cc <= 47) return true;
325 // :;
326 if (58 <= cc && cc <= 59) return true;
327 // =
328 if (cc == 61) return true;
329 // ?@
330 if (63 <= cc && cc <= 64) return true;
331 // _
332 if (cc == 95) return true;
333 // ~
334 if (cc == 126) return true;
335
336 return false;
337 };
338
339 var string = ToString(uri);
340 return Encode(string, unescapePredicate);
341 }
342
343
344 // ECMA-262 - 15.1.3.4
345 function URIEncodeComponent(component) {
346 var unescapePredicate = function(cc) {
347 if (isAlphaNumeric(cc)) return true;
348 // !
349 if (cc == 33) return true;
350 // '()*
351 if (39 <= cc && cc <= 42) return true;
352 // -.
353 if (45 <= cc && cc <= 46) return true;
354 // _
355 if (cc == 95) return true;
356 // ~
357 if (cc == 126) return true;
358
359 return false;
360 };
361
362 var string = ToString(component);
363 return Encode(string, unescapePredicate);
364 }
365
366
367 function HexValueOf(code) {
368 // 0-9
369 if (code >= 48 && code <= 57) return code - 48;
370 // A-F
371 if (code >= 65 && code <= 70) return code - 55;
372 // a-f
373 if (code >= 97 && code <= 102) return code - 87;
374
375 return -1;
376 }
377
378
379 // Convert a character code to 4-digit hex string representation
380 // 64 -> 0040, 62234 -> F31A.
381 function CharCodeToHex4Str(cc) {
382 var r = "";
383 if (hexCharArray === 0) {
384 hexCharArray = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
385 "A", "B", "C", "D", "E", "F"];
386 }
387 for (var i = 0; i < 4; ++i) {
388 var c = hexCharArray[cc & 0x0F];
389 r = c + r;
390 cc = cc >>> 4;
391 }
392 return r;
393 }
394
395
396 // Returns true if all digits in string s are valid hex numbers
397 function IsValidHex(s) {
398 for (var i = 0; i < s.length; ++i) {
399 var cc = s.charCodeAt(i);
400 if ((48 <= cc && cc <= 57) ||
401 (65 <= cc && cc <= 70) ||
402 (97 <= cc && cc <= 102)) {
403 // '0'..'9', 'A'..'F' and 'a' .. 'f'.
404 } else {
405 return false; 315 return false;
406 } 316 };
407 } 317 var string = ToString(uri);
408 return true; 318 return Decode(string, reservedPredicate);
409 } 319 }
410 320
411 321 // ECMA-262 - 15.1.3.2.
412 // ECMA-262 - B.2.1. 322 function URIDecodeComponent(component) {
413 function URIEscapeJS(str) { 323 var reservedPredicate = function(cc) { return false; };
414 var s = ToString(str); 324 var string = ToString(component);
415 return %URIEscape(s); 325 return Decode(string, reservedPredicate);
416 } 326 }
417 327
418 328 // ECMA-262 - 15.1.3.3.
419 // ECMA-262 - B.2.2. 329 function URIEncode(uri) {
420 function URIUnescapeJS(str) { 330 var unescapePredicate = function(cc) {
421 var s = ToString(str); 331 if (isAlphaNumeric(cc)) return true;
422 return %URIUnescape(s); 332 // !
423 } 333 if (cc == 33) return true;
424 334 // #$
425 335 if (35 <= cc && cc <= 36) return true;
426 // ------------------------------------------------------------------- 336 // &'()*+,-./
427 337 if (38 <= cc && cc <= 47) return true;
428 function SetUpUri() { 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
429 %CheckIsBootstrapping(); 379 %CheckIsBootstrapping();
430 380
431 // Set up non-enumerable URI functions on the global object and set 381 // Set up non-enumerable URI functions on the global object and set
432 // their names. 382 // their names.
433 InstallFunctions(global, DONT_ENUM, $Array( 383 InstallFunctions(global, DONT_ENUM, $Array(
434 "escape", URIEscapeJS, 384 "escape", URIEscapeJS,
435 "unescape", URIUnescapeJS, 385 "unescape", URIUnescapeJS,
436 "decodeURI", URIDecode, 386 "decodeURI", URIDecode,
437 "decodeURIComponent", URIDecodeComponent, 387 "decodeURIComponent", URIDecodeComponent,
438 "encodeURI", URIEncode, 388 "encodeURI", URIEncode,
439 "encodeURIComponent", URIEncodeComponent 389 "encodeURIComponent", URIEncodeComponent
440 )); 390 ));
441 } 391
442 392 })();
443 SetUpUri();
OLDNEW
« no previous file with comments | « src/promise.js ('k') | tools/generate-runtime-tests.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698