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 | |
|
ahe
2012/08/21 20:36:28
Comments should be proper sentences. That is, they
| |
| 172 * Throws IllegalArgumentException for all other schemes | |
|
ahe
2012/08/21 20:36:28
Not sure if you want a period or comma on the prev
| |
| 173 * See (http://www.w3.org/TR/2011/WD-html5-20110405/origin-0.html#origin) | |
|
ahe
2012/08/21 20:36:28
There is an example on lines 13-15 for how to incl
| |
| 174 */ | |
| 175 String get origin() { | |
| 176 if (scheme == "") { | |
| 177 // TODO(aprelev@gmail.com): Use StateException instead | |
| 178 throw new IllegalArgumentException("Cannot use origin without a scheme"); | |
| 179 } | |
| 180 if (scheme != "http" && scheme != "https") { | |
| 181 // TODO(aprelev@gmail.com): Use StateException instead | |
| 182 throw new IllegalArgumentException( | |
| 183 "origin is applicable to http/https schemes only. Not \'$scheme\'"); | |
| 184 } | |
| 185 StringBuffer sb = new StringBuffer(); | |
| 186 sb.add(scheme); | |
| 187 sb.add(":"); | |
| 188 if (domain == null || domain == "") { | |
| 189 // TODO(aprelev@gmail.com): Use StateException instead | |
| 190 throw new IllegalArgumentException("Cannot use origin without a domain"); | |
| 191 } | |
| 192 | |
| 193 sb.add("//"); | |
| 194 sb.add(domain); | |
| 195 if (port != 0) { | |
| 196 sb.add(":"); | |
| 197 sb.add(port.toString()); | |
|
ahe
2012/08/21 20:36:28
I believe toString is no longer necessary.
| |
| 198 } | |
| 199 return sb.toString(); | |
| 200 } | |
| 201 | |
| 170 String toString() { | 202 String toString() { |
| 171 StringBuffer sb = new StringBuffer(); | 203 StringBuffer sb = new StringBuffer(); |
| 172 _addIfNonEmpty(sb, scheme, scheme, ':'); | 204 _addIfNonEmpty(sb, scheme, scheme, ':'); |
| 173 if (hasAuthority() || (scheme == "file")) { | 205 if (hasAuthority() || (scheme == "file")) { |
| 174 sb.add("//"); | 206 sb.add("//"); |
| 175 _addIfNonEmpty(sb, userInfo, userInfo, "@"); | 207 _addIfNonEmpty(sb, userInfo, userInfo, "@"); |
| 176 sb.add(domain === null ? "null" : domain); | 208 sb.add(domain === null ? "null" : domain); |
| 177 if (port != 0) { | 209 if (port != 0) { |
| 178 sb.add(":"); | 210 sb.add(":"); |
| 179 sb.add(port.toString()); | 211 sb.add(port.toString()); |
| 180 } | 212 } |
| 181 } | 213 } |
| 182 sb.add(path === null ? "null" : path); | 214 sb.add(path === null ? "null" : path); |
| 183 _addIfNonEmpty(sb, query, "?", query); | 215 _addIfNonEmpty(sb, query, "?", query); |
| 184 _addIfNonEmpty(sb, fragment, "#", fragment); | 216 _addIfNonEmpty(sb, fragment, "#", fragment); |
| 185 return sb.toString(); | 217 return sb.toString(); |
| 186 } | 218 } |
| 187 | 219 |
| 188 static void _addIfNonEmpty(StringBuffer sb, String test, | 220 static void _addIfNonEmpty(StringBuffer sb, String test, |
| 189 String first, String second) { | 221 String first, String second) { |
| 190 if ("" != test) { | 222 if ("" != test) { |
| 191 sb.add(first === null ? "null" : first); | 223 sb.add(first === null ? "null" : first); |
| 192 sb.add(second === null ? "null" : second); | 224 sb.add(second === null ? "null" : second); |
| 193 } | 225 } |
| 194 } | 226 } |
| 195 } | 227 } |
| OLD | NEW |