add origin to location and url api

This commit is contained in:
Chandler Abraham 2016-01-21 11:06:41 -08:00
parent 1ba1fb0b7f
commit 1ee9ccba21
11 changed files with 62 additions and 607 deletions

View file

@ -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()))
}