| 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 #library('dart:uri'); | 5 #library('dart:uri'); |
| 6 | 6 |
| 7 #import('dart:math'); | 7 #import('dart:math'); |
| 8 #import('dart:utf'); | 8 #import('dart:utf'); |
| 9 | 9 |
| 10 #source('encode_decode.dart'); | 10 #source('encode_decode.dart'); |
| 11 #source('helpers.dart'); | 11 #source('helpers.dart'); |
| 12 | 12 |
| 13 /** | 13 /** |
| 14 * A parsed URI, inspired by Closure's [URI][] class. Implements [RFC-3986][]. | 14 * A parsed URI, inspired by Closure's [URI][] class. Implements [RFC-3986][]. |
| 15 * [uri]: http://closure-library.googlecode.com/svn/docs/class_goog_Uri.html | 15 * [uri]: http://closure-library.googlecode.com/svn/docs/class_goog_Uri.html |
| 16 * [RFC-3986]: http://tools.ietf.org/html/rfc3986#section-4.3) | 16 * [RFC-3986]: http://tools.ietf.org/html/rfc3986#section-4.3) |
| 17 */ | 17 */ |
| 18 class Uri { | 18 class Uri { |
| 19 final String scheme; | 19 final String scheme; |
| 20 final String userInfo; | 20 final String userInfo; |
| 21 final String domain; | 21 final String domain; |
| 22 final int port; | 22 final int port; |
| 23 final String path; | 23 final String path; |
| 24 final String query; | 24 final String query; |
| 25 final String fragment; | 25 final String fragment; |
| 26 | 26 |
| 27 Uri.fromString(String uri) : this._fromMatch(_splitRe.firstMatch(uri)); | 27 Uri.fromString(String uri) : this._fromMatch(_splitRe.firstMatch(uri)); |
| 28 | 28 |
| 29 Uri._fromMatch(Match m) : | 29 Uri._fromMatch(Match m) : |
| 30 this.fromComponents(_emptyIfNull(m[_COMPONENT_SCHEME]), | 30 this.fromComponents(scheme: _emptyIfNull(m[_COMPONENT_SCHEME]), |
| 31 _emptyIfNull(m[_COMPONENT_USER_INFO]), | 31 userInfo: _emptyIfNull(m[_COMPONENT_USER_INFO]), |
| 32 _emptyIfNull(m[_COMPONENT_DOMAIN]), | 32 domain: _emptyIfNull(m[_COMPONENT_DOMAIN]), |
| 33 _parseIntOrZero(m[_COMPONENT_PORT]), | 33 port: _parseIntOrZero(m[_COMPONENT_PORT]), |
| 34 _emptyIfNull(m[_COMPONENT_PATH]), | 34 path: _emptyIfNull(m[_COMPONENT_PATH]), |
| 35 _emptyIfNull(m[_COMPONENT_QUERY_DATA]), | 35 query: _emptyIfNull(m[_COMPONENT_QUERY_DATA]), |
| 36 _emptyIfNull(m[_COMPONENT_FRAGMENT])); | 36 fragment: _emptyIfNull(m[_COMPONENT_FRAGMENT])); |
| 37 | 37 |
| 38 const Uri.fromComponents([String this.scheme = "", String this.userInfo ="", | 38 const Uri.fromComponents({this.scheme: "", |
| 39 String this.domain = "", int this.port = 0, | 39 this.userInfo: "", |
| 40 String this.path = "", String this.query = "", | 40 this.domain: "", |
| 41 String this.fragment = ""]); | 41 this.port: 0, |
| 42 this.path: "", |
| 43 this.query: "", |
| 44 this.fragment: ""}); |
| 42 | 45 |
| 43 Uri(String uri) : this.fromString(uri); | 46 Uri(String uri) : this.fromString(uri); |
| 44 | 47 |
| 45 static String _emptyIfNull(String val) => val != null ? val : ''; | 48 static String _emptyIfNull(String val) => val != null ? val : ''; |
| 46 | 49 |
| 47 static int _parseIntOrZero(String val) { | 50 static int _parseIntOrZero(String val) { |
| 48 if (val !== null && val != '') { | 51 if (val !== null && val != '') { |
| 49 return parseInt(val); | 52 return parseInt(val); |
| 50 } else { | 53 } else { |
| 51 return 0; | 54 return 0; |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 targetPath = removeDotSegments(merge(this.path, reference.path)); | 159 targetPath = removeDotSegments(merge(this.path, reference.path)); |
| 157 } | 160 } |
| 158 targetQuery = reference.query; | 161 targetQuery = reference.query; |
| 159 } | 162 } |
| 160 targetUserInfo = this.userInfo; | 163 targetUserInfo = this.userInfo; |
| 161 targetDomain = this.domain; | 164 targetDomain = this.domain; |
| 162 targetPort = this.port; | 165 targetPort = this.port; |
| 163 } | 166 } |
| 164 targetScheme = this.scheme; | 167 targetScheme = this.scheme; |
| 165 } | 168 } |
| 166 return new Uri.fromComponents(targetScheme, targetUserInfo, targetDomain, | 169 return new Uri.fromComponents(scheme: targetScheme, |
| 167 targetPort, targetPath, targetQuery, | 170 userInfo: targetUserInfo, |
| 168 reference.fragment); | 171 domain: targetDomain, |
| 172 port: targetPort, |
| 173 path: targetPath, |
| 174 query: targetQuery, |
| 175 fragment: reference.fragment); |
| 169 } | 176 } |
| 170 | 177 |
| 171 bool hasAuthority() { | 178 bool hasAuthority() { |
| 172 return (userInfo != "") || (domain != "") || (port != 0); | 179 return (userInfo != "") || (domain != "") || (port != 0); |
| 173 } | 180 } |
| 174 | 181 |
| 175 /** | 182 /** |
| 176 * For http/https schemes returns URI's [origin][] - scheme://domain:port. | 183 * For http/https schemes returns URI's [origin][] - scheme://domain:port. |
| 177 * For all other schemes throws IllegalArgumentException. | 184 * For all other schemes throws IllegalArgumentException. |
| 178 * [origin]: http://www.w3.org/TR/2011/WD-html5-20110405/origin-0.html#origin | 185 * [origin]: http://www.w3.org/TR/2011/WD-html5-20110405/origin-0.html#origin |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 } | 230 } |
| 224 | 231 |
| 225 static void _addIfNonEmpty(StringBuffer sb, String test, | 232 static void _addIfNonEmpty(StringBuffer sb, String test, |
| 226 String first, String second) { | 233 String first, String second) { |
| 227 if ("" != test) { | 234 if ("" != test) { |
| 228 sb.add(first === null ? "null" : first); | 235 sb.add(first === null ? "null" : first); |
| 229 sb.add(second === null ? "null" : second); | 236 sb.add(second === null ? "null" : second); |
| 230 } | 237 } |
| 231 } | 238 } |
| 232 } | 239 } |
| OLD | NEW |