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

Unified Diff: lib/uri/uri.dart

Issue 10832092: Added origin property to Uri class Added unit test coverage for origin property (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fixed comments, removed redundant toString, fixed tests 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tests/utils/uri_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/uri/uri.dart
diff --git a/lib/uri/uri.dart b/lib/uri/uri.dart
index 7c6bfdd9ee27798c5c1495a5762bbf409dea254e..c05363827c9de9255f5c249067930e9082ae0446 100644
--- a/lib/uri/uri.dart
+++ b/lib/uri/uri.dart
@@ -171,6 +171,38 @@ class Uri {
return (userInfo != "") || (domain != "") || (port != 0);
}
+ /**
+ * For http/https schemes returns URI's [origin][] - scheme://domain:port.
+ * For all other schemes throws IllegalArgumentException.
+ * [origin]: http://www.w3.org/TR/2011/WD-html5-20110405/origin-0.html#origin
+ */
+ String get origin() {
+ if (scheme == "") {
+ // TODO(aprelev@gmail.com): Use StateException instead
+ throw new IllegalArgumentException("Cannot use origin without a scheme");
+ }
+ if (scheme != "http" && scheme != "https") {
+ // TODO(aprelev@gmail.com): Use StateException instead
+ throw new IllegalArgumentException(
+ "origin is applicable to http/https schemes only. Not \'$scheme\'");
+ }
+ StringBuffer sb = new StringBuffer();
+ sb.add(scheme);
+ sb.add(":");
+ if (domain == null || domain == "") {
+ // TODO(aprelev@gmail.com): Use StateException instead
+ throw new IllegalArgumentException("Cannot use origin without a domain");
+ }
+
+ sb.add("//");
+ sb.add(domain);
+ if (port != 0) {
+ sb.add(":");
+ sb.add(port);
+ }
+ return sb.toString();
+ }
+
String toString() {
StringBuffer sb = new StringBuffer();
_addIfNonEmpty(sb, scheme, scheme, ':');
« no previous file with comments | « no previous file | tests/utils/uri_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698