Chromium Code Reviews| 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:utf'); | 7 #import('dart:utf'); |
| 8 | 8 |
| 9 #source('encode_decode.dart'); | 9 #source('encode_decode.dart'); |
| 10 #source('helpers.dart'); | 10 #source('helpers.dart'); |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 160 targetScheme = this.scheme; | 160 targetScheme = this.scheme; |
| 161 } | 161 } |
| 162 return new Uri(targetScheme, targetUserInfo, targetDomain, targetPort, | 162 return new Uri(targetScheme, targetUserInfo, targetDomain, targetPort, |
| 163 targetPath, targetQuery, reference.fragment); | 163 targetPath, targetQuery, reference.fragment); |
| 164 } | 164 } |
| 165 | 165 |
| 166 bool hasAuthority() { | 166 bool hasAuthority() { |
| 167 return (userInfo != "") || (domain != "") || (port != 0); | 167 return (userInfo != "") || (domain != "") || (port != 0); |
| 168 } | 168 } |
| 169 | 169 |
| 170 /** | |
| 171 * Returns URI's origin: scheme://domain:port for http/https schemes | |
| 172 * Throws IllegalArgumentException for all other schemes | |
| 173 * See (http://www.w3.org/TR/2011/WD-html5-20110405/origin-0.html#origin) | |
| 174 */ | |
| 175 String get origin() { | |
|
ahe
2012/08/07 06:32:36
Please add something like:
if (scheme == "") {
| |
| 176 if (scheme != "http" && scheme != "https") { | |
| 177 throw new IllegalArgumentException( | |
|
ahe
2012/08/07 06:32:36
This should be a StateException which is coming so
| |
| 178 "origin is applicable to http/https schemes only. Not \'$scheme\'"); | |
| 179 } | |
| 180 StringBuffer sb = new StringBuffer(); | |
| 181 _addIfNonEmpty(sb, scheme, scheme, ':'); | |
|
ahe
2012/08/07 06:32:36
scheme is never empty now.
| |
| 182 if (domain != null || port != 0) { | |
|
ahe
2012/08/07 06:32:36
Right now, the invariant is that domain is never n
| |
| 183 sb.add("//"); | |
| 184 sb.add(_emptyIfNull(domain)); | |
| 185 if (port != 0) { | |
| 186 sb.add(":"); | |
| 187 sb.add(port.toString()); | |
| 188 } | |
| 189 } | |
| 190 return sb.toString(); | |
| 191 } | |
| 192 | |
| 170 String toString() { | 193 String toString() { |
| 171 StringBuffer sb = new StringBuffer(); | 194 StringBuffer sb = new StringBuffer(); |
| 172 _addIfNonEmpty(sb, scheme, scheme, ':'); | 195 _addIfNonEmpty(sb, scheme, scheme, ':'); |
| 173 if (hasAuthority() || (scheme == "file")) { | 196 if (hasAuthority() || (scheme == "file")) { |
| 174 sb.add("//"); | 197 sb.add("//"); |
| 175 _addIfNonEmpty(sb, userInfo, userInfo, "@"); | 198 _addIfNonEmpty(sb, userInfo, userInfo, "@"); |
| 176 sb.add(domain === null ? "null" : domain); | 199 sb.add(domain === null ? "null" : domain); |
| 177 if (port != 0) { | 200 if (port != 0) { |
| 178 sb.add(":"); | 201 sb.add(":"); |
| 179 sb.add(port.toString()); | 202 sb.add(port.toString()); |
| 180 } | 203 } |
| 181 } | 204 } |
| 182 sb.add(path === null ? "null" : path); | 205 sb.add(path === null ? "null" : path); |
| 183 _addIfNonEmpty(sb, query, "?", query); | 206 _addIfNonEmpty(sb, query, "?", query); |
| 184 _addIfNonEmpty(sb, fragment, "#", fragment); | 207 _addIfNonEmpty(sb, fragment, "#", fragment); |
| 185 return sb.toString(); | 208 return sb.toString(); |
| 186 } | 209 } |
| 187 | 210 |
| 188 static void _addIfNonEmpty(StringBuffer sb, String test, | 211 static void _addIfNonEmpty(StringBuffer sb, String test, |
| 189 String first, String second) { | 212 String first, String second) { |
| 190 if ("" != test) { | 213 if ("" != test) { |
| 191 sb.add(first === null ? "null" : first); | 214 sb.add(first === null ? "null" : first); |
| 192 sb.add(second === null ? "null" : second); | 215 sb.add(second === null ? "null" : second); |
| 193 } | 216 } |
| 194 } | 217 } |
| 195 } | 218 } |
| 219 | |
|
ahe
2012/08/07 06:32:36
Extra line at end of file.
| |
| OLD | NEW |