| 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. |
| 80 for (int i = 0; i < text.length; i++) { | 81 for (int i = 0; i < text.length; i++) { |
| 81 if (canonical.indexOf(text[i]) >= 0) { | 82 if (canonical.indexOf(text[i]) >= 0) { |
| 82 result.add(text[i]); | 83 result.add(text[i]); |
| 83 } else { | 84 } else { |
| 84 int ch = text.charCodeAt(i); | 85 int ch = text.charCodeAt(i); |
| 85 if (ch >= 0xD800 && ch < 0xDC00) { | 86 if (ch >= 0x10000) i++; |
| 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 } | |
| 96 for (int codepoint in codepointsToUtf8([ch])) { | 87 for (int codepoint in codepointsToUtf8([ch])) { |
| 97 result.add(byteToHex(codepoint)); | 88 result.add(byteToHex(codepoint)); |
| 98 } | 89 } |
| 99 } | 90 } |
| 100 } | 91 } |
| 101 return result.toString(); | 92 return result.toString(); |
| 102 } | 93 } |
| 103 | 94 |
| 104 /** | 95 /** |
| 105 * Convert a byte (2 character hex sequence) in string [s] starting | 96 * Convert a byte (2 character hex sequence) in string [s] starting |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 if (i == text.length) | 130 if (i == text.length) |
| 140 break; | 131 break; |
| 141 ch = text[i]; | 132 ch = text[i]; |
| 142 } | 133 } |
| 143 result.add(decodeUtf8(codepoints)); | 134 result.add(decodeUtf8(codepoints)); |
| 144 } | 135 } |
| 145 } | 136 } |
| 146 return result.toString(); | 137 return result.toString(); |
| 147 } | 138 } |
| 148 | 139 |
| OLD | NEW |