OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Javascript-like URI encode/decode functions. | 6 * Javascript-like URI encode/decode functions. |
7 * The documentation here borrows heavily from the original Javascript | 7 * The documentation here borrows heavily from the original Javascript |
8 * doumentation on MDN at: | 8 * doumentation on MDN at: |
9 * https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects | 9 * https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects |
10 */ | 10 */ |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 | 70 |
71 /** | 71 /** |
72 * This is the internal implementation of JavaScript's encodeURI function. | 72 * This is the internal implementation of JavaScript's encodeURI function. |
73 * It encodes all characters in the string [text] except for those | 73 * It encodes all characters in the string [text] except for those |
74 * that appear in [canonical], and returns the escaped string. | 74 * that appear in [canonical], and returns the escaped string. |
75 */ | 75 */ |
76 String _uriEncode(String canonical, String text) { | 76 String _uriEncode(String canonical, String text) { |
77 final String hex = '0123456789ABCDEF'; | 77 final String hex = '0123456789ABCDEF'; |
78 var byteToHex = (int v) => '%${hex[v >> 4]}${hex[v&0xf]}'; | 78 var byteToHex = (int v) => '%${hex[v >> 4]}${hex[v&0xf]}'; |
79 StringBuffer result = new StringBuffer(); | 79 StringBuffer result = new StringBuffer(); |
80 // TODO(erikcorry): Use the new character iterator. | |
81 for (int i = 0; i < text.length; i++) { | 80 for (int i = 0; i < text.length; i++) { |
82 if (canonical.indexOf(text[i]) >= 0) { | 81 if (canonical.indexOf(text[i]) >= 0) { |
83 result.add(text[i]); | 82 result.add(text[i]); |
84 } else { | 83 } else { |
85 int ch = text.charCodeAt(i); | 84 int ch = text.charCodeAt(i); |
86 if (ch >= 0x10000) i++; | 85 if (ch >= 0xD800 && ch < 0xDC00) { |
| 86 // Low surrogate. We expect a next char high surrogate. |
| 87 ++i; |
| 88 int nextCh = text.length == i ? 0 : text.charCodeAt(i); |
| 89 if (nextCh >= 0xDC00 && nextCh < 0xE000) { |
| 90 // convert the pair to a U+10000 codepoint |
| 91 ch = 0x10000 + ((ch-0xD800) << 10) + (nextCh - 0xDC00); |
| 92 } else { |
| 93 throw new ArgumentError('Malformed URI'); |
| 94 } |
| 95 } |
87 for (int codepoint in codepointsToUtf8([ch])) { | 96 for (int codepoint in codepointsToUtf8([ch])) { |
88 result.add(byteToHex(codepoint)); | 97 result.add(byteToHex(codepoint)); |
89 } | 98 } |
90 } | 99 } |
91 } | 100 } |
92 return result.toString(); | 101 return result.toString(); |
93 } | 102 } |
94 | 103 |
95 /** | 104 /** |
96 * Convert a byte (2 character hex sequence) in string [s] starting | 105 * Convert a byte (2 character hex sequence) in string [s] starting |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 if (i == text.length) | 139 if (i == text.length) |
131 break; | 140 break; |
132 ch = text[i]; | 141 ch = text[i]; |
133 } | 142 } |
134 result.add(decodeUtf8(codepoints)); | 143 result.add(decodeUtf8(codepoints)); |
135 } | 144 } |
136 } | 145 } |
137 return result.toString(); | 146 return result.toString(); |
138 } | 147 } |
139 | 148 |
OLD | NEW |