Browse Source

Add Unicode and ASCII serializations of origins

Simon Sapin 10 years ago
parent
commit
1a22a90ae5
2 changed files with 43 additions and 5 deletions
  1. 40 2
      src/origin.rs
  2. 3 3
      src/webidl.rs

+ 40 - 2
src/origin.rs

@@ -6,8 +6,11 @@
 // option. This file may not be copied, modified, or distributed
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 // except according to those terms.
 
 
-use Url;
 use host::Host;
 use host::Host;
+use idna::domain_to_unicode;
+use parser::default_port;
+use std::sync::Arc;
+use Url;
 
 
 impl Url {
 impl Url {
     /// Return the origin of this URL (https://url.spec.whatwg.org/#origin)
     /// Return the origin of this URL (https://url.spec.whatwg.org/#origin)
@@ -46,7 +49,42 @@ pub enum Origin {
 impl Origin {
 impl Origin {
     /// Creates a new opaque origin that is only equal to itself.
     /// Creates a new opaque origin that is only equal to itself.
     pub fn new_opaque() -> Origin {
     pub fn new_opaque() -> Origin {
-        Origin::Opaque(OpaqueOrigin(Box::new(0)))
+        Origin::Opaque(OpaqueOrigin(Arc::new(0)))
+    }
+
+    /// https://html.spec.whatwg.org/multipage/#ascii-serialisation-of-an-origin
+    pub fn ascii_serialization(&self) -> String {
+        match *self {
+            Origin::Opaque(_) => "null".to_owned(),
+            Origin::Tuple(ref scheme, ref host, port) => {
+                if default_port(scheme) == Some(port) {
+                    format!("{}://{}", scheme, host)
+                } else {
+                    format!("{}://{}:{}", scheme, host, port)
+                }
+            }
+        }
+    }
+
+    /// https://html.spec.whatwg.org/multipage/#unicode-serialisation-of-an-origin
+    pub fn unicode_serialization(&self) -> String {
+        match *self {
+            Origin::Opaque(_) => "null".to_owned(),
+            Origin::Tuple(ref scheme, ref host, port) => {
+                let host = match *host {
+                    Host::Domain(ref domain) => {
+                        let (domain, _errors) = domain_to_unicode(domain);
+                        Host::Domain(domain)
+                    }
+                    _ => host.clone()
+                };
+                if default_port(scheme) == Some(port) {
+                    format!("{}://{}", scheme, host)
+                } else {
+                    format!("{}://{}:{}", scheme, host, port)
+                }
+            }
+        }
     }
     }
 }
 }
 
 

+ 3 - 3
src/webidl.rs

@@ -31,9 +31,9 @@ impl WebIdl {
         Ok(())
         Ok(())
     }
     }
 
 
-    /// **Not implemented yet** Getter for https://url.spec.whatwg.org/#dom-url-origin
-    pub fn origin(_url: &Url) -> String {
-        unimplemented!()  // FIXME
+    /// Getter for https://url.spec.whatwg.org/#dom-url-origin
+    pub fn origin(url: &Url) -> String {
+        url.origin().unicode_serialization()
     }
     }
 
 
     /// Getter for https://url.spec.whatwg.org/#dom-url-protocol
     /// Getter for https://url.spec.whatwg.org/#dom-url-protocol