diff options
author | Chandler Abraham <cabraham@twitter.com> | 2016-01-21 11:06:41 -0800 |
---|---|---|
committer | Chandler Abraham <cabraham@twitter.com> | 2016-01-21 11:06:41 -0800 |
commit | 1ee9ccba215535dfc1af93a3579c5bca4a21706f (patch) | |
tree | 3381c3ec9b57ad8781987aad473eb48b3fdc4b04 /components/script/dom/urlhelper.rs | |
parent | 1ba1fb0b7f46472e1f37f4e3f2e7dde025fa9f1f (diff) | |
download | servo-1ee9ccba215535dfc1af93a3579c5bca4a21706f.tar.gz servo-1ee9ccba215535dfc1af93a3579c5bca4a21706f.zip |
add origin to location and url api
Diffstat (limited to 'components/script/dom/urlhelper.rs')
-rw-r--r-- | components/script/dom/urlhelper.rs | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/components/script/dom/urlhelper.rs b/components/script/dom/urlhelper.rs index d9e26e84263..580c541502d 100644 --- a/components/script/dom/urlhelper.rs +++ b/components/script/dom/urlhelper.rs @@ -6,7 +6,7 @@ use dom::bindings::str::USVString; use std::borrow::ToOwned; use std::fmt::Write; use url::urlutils::{UrlUtils, UrlUtilsWrapper}; -use url::{SchemeData, Url, UrlParser}; +use url::{Origin, SchemeData, Url, UrlParser}; #[derive(HeapSizeOf)] pub struct UrlHelper; @@ -43,6 +43,34 @@ impl UrlHelper { let _ = wrapper.set_host(&value.0); } + pub fn Origin(url: &Url) -> USVString { + USVString(match url.origin() { + Origin::UID(_) => { + // https://html.spec.whatwg.org/multipage/#unicode-serialisation-of-an-origin + // If the origin in question is not a scheme/host/port tuple, + // then return the literal string "null" and abort these steps. + "null".to_owned() + }, + Origin::Tuple(protocol, host, _) => { + let mut origin = + format!( + "{protocol}://{host}", + protocol = protocol, + host = host + ); + if let Some(port) = + // https://html.spec.whatwg.org/multipage/#unicode-serialisation-of-an-origin + // only append the port # to the serialized origin if the port is different from + // the default port for the protocol. If url.scheme_data.port is None, that + // indicates that the port is a default port + url.relative_scheme_data().and_then(|scheme| scheme.port) { + write!(origin, ":{}", port).unwrap(); + }; + origin + } + }) + } + pub fn Hostname(url: &Url) -> USVString { USVString(url.serialize_host().unwrap_or_else(|| "".to_owned())) } |