Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(540)

Side by Side Diff: dart/lib/uri/uri.dart

Issue 10869008: Added origin property to Uri class Added unit test coverage for origin property (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | dart/tests/utils/uri_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 } 164 }
165 return new Uri.fromComponents(targetScheme, targetUserInfo, targetDomain, 165 return new Uri.fromComponents(targetScheme, targetUserInfo, targetDomain,
166 targetPort, targetPath, targetQuery, 166 targetPort, targetPath, targetQuery,
167 reference.fragment); 167 reference.fragment);
168 } 168 }
169 169
170 bool hasAuthority() { 170 bool hasAuthority() {
171 return (userInfo != "") || (domain != "") || (port != 0); 171 return (userInfo != "") || (domain != "") || (port != 0);
172 } 172 }
173 173
174 /**
175 * For http/https schemes returns URI's [origin][] - scheme://domain:port.
176 * For all other schemes throws IllegalArgumentException.
177 * [origin]: http://www.w3.org/TR/2011/WD-html5-20110405/origin-0.html#origin
178 */
179 String get origin() {
180 if (scheme == "") {
181 // TODO(aprelev@gmail.com): Use StateException instead
182 throw new IllegalArgumentException("Cannot use origin without a scheme");
183 }
184 if (scheme != "http" && scheme != "https") {
185 // TODO(aprelev@gmail.com): Use StateException instead
186 throw new IllegalArgumentException(
187 "origin is applicable to http/https schemes only. Not \'$scheme\'");
188 }
189 StringBuffer sb = new StringBuffer();
190 sb.add(scheme);
191 sb.add(":");
192 if (domain == null || domain == "") {
193 // TODO(aprelev@gmail.com): Use StateException instead
194 throw new IllegalArgumentException("Cannot use origin without a domain");
195 }
196
197 sb.add("//");
198 sb.add(domain);
199 if (port != 0) {
200 sb.add(":");
201 sb.add(port);
202 }
203 return sb.toString();
204 }
205
174 String toString() { 206 String toString() {
175 StringBuffer sb = new StringBuffer(); 207 StringBuffer sb = new StringBuffer();
176 _addIfNonEmpty(sb, scheme, scheme, ':'); 208 _addIfNonEmpty(sb, scheme, scheme, ':');
177 if (hasAuthority() || (scheme == "file")) { 209 if (hasAuthority() || (scheme == "file")) {
178 sb.add("//"); 210 sb.add("//");
179 _addIfNonEmpty(sb, userInfo, userInfo, "@"); 211 _addIfNonEmpty(sb, userInfo, userInfo, "@");
180 sb.add(domain === null ? "null" : domain); 212 sb.add(domain === null ? "null" : domain);
181 if (port != 0) { 213 if (port != 0) {
182 sb.add(":"); 214 sb.add(":");
183 sb.add(port.toString()); 215 sb.add(port.toString());
184 } 216 }
185 } 217 }
186 sb.add(path === null ? "null" : path); 218 sb.add(path === null ? "null" : path);
187 _addIfNonEmpty(sb, query, "?", query); 219 _addIfNonEmpty(sb, query, "?", query);
188 _addIfNonEmpty(sb, fragment, "#", fragment); 220 _addIfNonEmpty(sb, fragment, "#", fragment);
189 return sb.toString(); 221 return sb.toString();
190 } 222 }
191 223
192 static void _addIfNonEmpty(StringBuffer sb, String test, 224 static void _addIfNonEmpty(StringBuffer sb, String test,
193 String first, String second) { 225 String first, String second) {
194 if ("" != test) { 226 if ("" != test) {
195 sb.add(first === null ? "null" : first); 227 sb.add(first === null ? "null" : first);
196 sb.add(second === null ? "null" : second); 228 sb.add(second === null ? "null" : second);
197 } 229 }
198 } 230 }
199 } 231 }
OLDNEW
« no previous file with comments | « no previous file | dart/tests/utils/uri_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698