| 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 part of dart.uri; | 5 part of dart.uri; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Javascript-like URI encode/decode functions. | 8 * Javascript-like URI encode/decode functions. |
| 9 * The documentation here borrows heavily from the original Javascript | 9 * The documentation here borrows heavily from the original Javascript |
| 10 * doumentation on MDN at: | 10 * doumentation on MDN at: |
| 11 * https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects | 11 * https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects |
| 12 */ | 12 */ |
| 13 | 13 |
| 14 /** | 14 /** |
| 15 * A JavaScript-like URI encoder. Encodes Uniform Resource Identifier [uri] | 15 * A JavaScript-like URI encoder. Encodes Uniform Resource Identifier [uri] |
| 16 * by replacing each instance of certain characters by one, two, three, or four | 16 * by replacing each instance of certain characters by one, two, three, or four |
| 17 * escape sequences representing the UTF-8 encoding of the character (will | 17 * escape sequences representing the UTF-8 encoding of the character (will |
| 18 * only be four escape sequences for characters composed of two "surrogate" | 18 * only be four escape sequences for characters composed of two "surrogate" |
| 19 * characters). This assumes that [uri] is a complete URI, so does not encode | 19 * characters). This assumes that [uri] is a complete URI, so does not encode |
| 20 * reserved characters that have special meaning in the URI: [:#;,/?:@&=+\$:] | 20 * reserved characters that have special meaning in the URI: [:#;,/?:@&=+\$:] |
| 21 * It returns the escaped URI. | 21 * It returns the escaped URI. |
| 22 */ | 22 */ |
| 23 String encodeUri(String uri) { | 23 String encodeUri(String uri) { |
| 24 return _uriEncode( | 24 return _uriEncode( |
| 25 "-_.!~*'()#;,/?:@&=\$0123456789" | 25 "-_.!~*'()#;,/?:@&=+\$0123456789" |
| 26 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", uri); | 26 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", uri); |
| 27 } | 27 } |
| 28 | 28 |
| 29 /** | 29 /** |
| 30 * An implementation of JavaScript's decodeURIComponent function. | 30 * An implementation of JavaScript's decodeURIComponent function. |
| 31 * Decodes a Uniform Resource Identifier [uri] previously created by | 31 * Decodes a Uniform Resource Identifier [uri] previously created by |
| 32 * encodeURI or by a similar routine. It replaces each escape sequence | 32 * encodeURI or by a similar routine. It replaces each escape sequence |
| 33 * in [uri] with the character that it represents. It does not decode | 33 * in [uri] with the character that it represents. It does not decode |
| 34 * escape sequences that could not have been introduced by encodeURI. | 34 * escape sequences that could not have been introduced by encodeURI. |
| 35 * It returns the unescaped URI. | 35 * It returns the unescaped URI. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 * It encodes all characters in the string [text] except for those | 75 * It encodes all characters in the string [text] except for those |
| 76 * that appear in [canonical], and returns the escaped string. | 76 * that appear in [canonical], and returns the escaped string. |
| 77 */ | 77 */ |
| 78 String _uriEncode(String canonical, String text) { | 78 String _uriEncode(String canonical, String text) { |
| 79 final String hex = '0123456789ABCDEF'; | 79 final String hex = '0123456789ABCDEF'; |
| 80 var byteToHex = (int v) => '%${hex[v >> 4]}${hex[v&0xf]}'; | 80 var byteToHex = (int v) => '%${hex[v >> 4]}${hex[v&0xf]}'; |
| 81 StringBuffer result = new StringBuffer(); | 81 StringBuffer result = new StringBuffer(); |
| 82 for (int i = 0; i < text.length; i++) { | 82 for (int i = 0; i < text.length; i++) { |
| 83 if (canonical.indexOf(text[i]) >= 0) { | 83 if (canonical.indexOf(text[i]) >= 0) { |
| 84 result.add(text[i]); | 84 result.add(text[i]); |
| 85 } else if (text[i] == " ") { | |
| 86 result.add("+"); | |
| 87 } else { | 85 } else { |
| 88 int ch = text.charCodeAt(i); | 86 int ch = text.charCodeAt(i); |
| 89 if (ch >= 0xD800 && ch < 0xDC00) { | 87 if (ch >= 0xD800 && ch < 0xDC00) { |
| 90 // Low surrogate. We expect a next char high surrogate. | 88 // Low surrogate. We expect a next char high surrogate. |
| 91 ++i; | 89 ++i; |
| 92 int nextCh = text.length == i ? 0 : text.charCodeAt(i); | 90 int nextCh = text.length == i ? 0 : text.charCodeAt(i); |
| 93 if (nextCh >= 0xDC00 && nextCh < 0xE000) { | 91 if (nextCh >= 0xDC00 && nextCh < 0xE000) { |
| 94 // convert the pair to a U+10000 codepoint | 92 // convert the pair to a U+10000 codepoint |
| 95 ch = 0x10000 + ((ch-0xD800) << 10) + (nextCh - 0xDC00); | 93 ch = 0x10000 + ((ch-0xD800) << 10) + (nextCh - 0xDC00); |
| 96 } else { | 94 } else { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 /** | 130 /** |
| 133 * A JavaScript-like decodeURI function. It unescapes the string [text] and | 131 * A JavaScript-like decodeURI function. It unescapes the string [text] and |
| 134 * returns the unescaped string. | 132 * returns the unescaped string. |
| 135 */ | 133 */ |
| 136 String _uriDecode(String text) { | 134 String _uriDecode(String text) { |
| 137 StringBuffer result = new StringBuffer(); | 135 StringBuffer result = new StringBuffer(); |
| 138 List<int> codepoints = new List<int>(); | 136 List<int> codepoints = new List<int>(); |
| 139 for (int i = 0; i < text.length;) { | 137 for (int i = 0; i < text.length;) { |
| 140 String ch = text[i]; | 138 String ch = text[i]; |
| 141 if (ch != '%') { | 139 if (ch != '%') { |
| 142 if (ch == '+') { | 140 result.add(ch); |
| 143 result.add(" "); | |
| 144 } else { | |
| 145 result.add(ch); | |
| 146 } | |
| 147 i++; | 141 i++; |
| 148 } else { | 142 } else { |
| 149 codepoints.clear(); | 143 codepoints.clear(); |
| 150 while (ch == '%') { | 144 while (ch == '%') { |
| 151 if (++i > text.length - 2) { | 145 if (++i > text.length - 2) { |
| 152 throw new ArgumentError('Truncated URI'); | 146 throw new ArgumentError('Truncated URI'); |
| 153 } | 147 } |
| 154 codepoints.add(_hexCharPairToByte(text, i)); | 148 codepoints.add(_hexCharPairToByte(text, i)); |
| 155 i += 2; | 149 i += 2; |
| 156 if (i == text.length) | 150 if (i == text.length) |
| 157 break; | 151 break; |
| 158 ch = text[i]; | 152 ch = text[i]; |
| 159 } | 153 } |
| 160 result.add(decodeUtf8(codepoints)); | 154 result.add(decodeUtf8(codepoints)); |
| 161 } | 155 } |
| 162 } | 156 } |
| 163 return result.toString(); | 157 return result.toString(); |
| 164 } | 158 } |
| 165 | 159 |
| OLD | NEW |