Implement URL and trivially missing URLUtils members

Fixes #6322.
This commit is contained in:
Anthony Ramine 2015-06-13 21:49:49 +02:00
parent e7808c526c
commit a8e4558e82
26 changed files with 310 additions and 956 deletions

View file

@ -42,16 +42,46 @@ impl<'a> LocationMethods for &'a Location {
self.window.root().r().load_url(url);
}
// https://url.spec.whatwg.org/#dom-urlutils-hash
fn Hash(self) -> USVString {
UrlHelper::Hash(&self.get_url())
}
// https://url.spec.whatwg.org/#dom-urlutils-href
fn Href(self) -> USVString {
UrlHelper::Href(&self.get_url())
}
// https://url.spec.whatwg.org/#dom-urlutils-host
fn Host(self) -> USVString {
UrlHelper::Host(&self.get_url())
}
// https://url.spec.whatwg.org/#dom-urlutils-hostname
fn Hostname(self) -> USVString {
UrlHelper::Hostname(&self.get_url())
}
// https://url.spec.whatwg.org/#dom-urlutils-password
fn Password(self) -> USVString {
UrlHelper::Password(&self.get_url())
}
// https://url.spec.whatwg.org/#dom-urlutils-pathname
fn Pathname(self) -> USVString {
UrlHelper::Pathname(&self.get_url())
}
// https://url.spec.whatwg.org/#dom-urlutils-port
fn Port(self) -> USVString {
UrlHelper::Port(&self.get_url())
}
// https://url.spec.whatwg.org/#dom-urlutils-protocol
fn Protocol(self) -> USVString {
UrlHelper::Protocol(&self.get_url())
}
// https://url.spec.whatwg.org/#URLUtils-stringification-behavior
fn Stringifier(self) -> DOMString {
self.Href().0
@ -62,9 +92,9 @@ impl<'a> LocationMethods for &'a Location {
UrlHelper::Search(&self.get_url())
}
// https://url.spec.whatwg.org/#dom-urlutils-hash
fn Hash(self) -> USVString {
UrlHelper::Hash(&self.get_url())
// https://url.spec.whatwg.org/#dom-urlutils-username
fn Username(self) -> USVString {
UrlHelper::Username(&self.get_url())
}
}

View file

@ -308,6 +308,7 @@ pub mod textdecoder;
pub mod textencoder;
pub mod treewalker;
pub mod uievent;
pub mod url;
pub mod urlhelper;
pub mod urlsearchparams;
pub mod userscripts;

View file

@ -0,0 +1,150 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use dom::bindings::codegen::Bindings::URLBinding::{self, URLMethods};
use dom::bindings::error::{Error, Fallible};
use dom::bindings::global::GlobalRef;
use dom::bindings::js::Root;
use dom::bindings::utils::{Reflector, reflect_dom_object};
use dom::bindings::str::USVString;
use dom::urlhelper::UrlHelper;
use url::{Host, Url, UrlParser};
use util::str::DOMString;
use std::borrow::ToOwned;
// https://url.spec.whatwg.org/#url
#[dom_struct]
pub struct URL {
reflector_: Reflector,
// https://url.spec.whatwg.org/#concept-urlutils-url
url: Url,
}
impl URL {
fn new_inherited(url: Url) -> URL {
URL {
reflector_: Reflector::new(),
url: url,
}
}
pub fn new(global: GlobalRef, url: Url) -> Root<URL> {
reflect_dom_object(box URL::new_inherited(url),
global, URLBinding::Wrap)
}
}
impl URL {
// https://url.spec.whatwg.org/#constructors
pub fn Constructor(global: GlobalRef, url: USVString,
base: Option<USVString>)
-> Fallible<Root<URL>> {
let parsed_base = match base {
None => {
// Step 1.
None
},
Some(base) =>
// Step 2.1.
match Url::parse(&base.0) {
Ok(base) => Some(base),
Err(error) => {
// Step 2.2.
return Err(Error::Type(format!("could not parse base: {}", error)));
}
}
};
// Step 3.
let parsed_url = match parser_with_base(parsed_base.as_ref()).parse(&url.0) {
Ok(url) => url,
Err(error) => {
// Step 4.
return Err(Error::Type(format!("could not parse URL: {}", error)));
}
};
// Steps 5-8.
Ok(URL::new(global, parsed_url))
}
// https://url.spec.whatwg.org/#dom-url-domaintoasciidomain
pub fn DomainToASCII(_: GlobalRef, origin: USVString) -> USVString {
// Step 1.
let ascii_domain = Host::parse(&origin.0);
if let Ok(Host::Domain(string)) = ascii_domain {
// Step 3.
USVString(string.to_owned())
} else {
// Step 2.
USVString("".to_owned())
}
}
}
impl<'a> URLMethods for &'a URL {
// https://url.spec.whatwg.org/#dom-urlutils-hash
fn Hash(self) -> USVString {
UrlHelper::Hash(&self.url)
}
// https://url.spec.whatwg.org/#dom-urlutils-host
fn Host(self) -> USVString {
UrlHelper::Host(&self.url)
}
// https://url.spec.whatwg.org/#dom-urlutils-hostname
fn Hostname(self) -> USVString {
UrlHelper::Hostname(&self.url)
}
// https://url.spec.whatwg.org/#dom-urlutils-href
fn Href(self) -> USVString {
UrlHelper::Href(&self.url)
}
// https://url.spec.whatwg.org/#dom-urlutils-password
fn Password(self) -> USVString {
UrlHelper::Password(&self.url)
}
// https://url.spec.whatwg.org/#dom-urlutils-pathname
fn Pathname(self) -> USVString {
UrlHelper::Pathname(&self.url)
}
// https://url.spec.whatwg.org/#dom-urlutils-port
fn Port(self) -> USVString {
UrlHelper::Port(&self.url)
}
// https://url.spec.whatwg.org/#dom-urlutils-protocol
fn Protocol(self) -> USVString {
UrlHelper::Protocol(&self.url)
}
// https://url.spec.whatwg.org/#dom-urlutils-search
fn Search(self) -> USVString {
UrlHelper::Search(&self.url)
}
// https://url.spec.whatwg.org/#URLUtils-stringification-behavior
fn Stringifier(self) -> DOMString {
self.Href().0
}
// https://url.spec.whatwg.org/#dom-urlutils-username
fn Username(self) -> USVString {
UrlHelper::Username(&self.url)
}
}
fn parser_with_base<'a>(base: Option<&'a Url>) -> UrlParser<'a> {
let mut parser = UrlParser::new();
if let Some(base) = base {
parser.base_url(base);
}
parser
}

View file

@ -7,24 +7,11 @@ use dom::bindings::str::USVString;
use url::{Url, SchemeData};
use std::borrow::ToOwned;
use std::fmt::Write;
pub struct UrlHelper;
impl UrlHelper {
// https://url.spec.whatwg.org/#dom-urlutils-href
pub fn Href(url: &Url) -> USVString {
USVString(url.serialize())
}
// https://url.spec.whatwg.org/#dom-urlutils-search
pub fn Search(url: &Url) -> USVString {
USVString(match url.query {
None => "".to_owned(),
Some(ref query) if query.is_empty() => "".to_owned(),
Some(ref query) => format!("?{}", query)
})
}
// https://url.spec.whatwg.org/#dom-urlutils-hash
pub fn Hash(url: &Url) -> USVString {
USVString(match url.fragment {
@ -34,6 +21,35 @@ impl UrlHelper {
})
}
// https://url.spec.whatwg.org/#dom-urlutils-host
pub fn Host(url: &Url) -> USVString {
USVString(match url.scheme_data {
SchemeData::NonRelative(..) => "".to_owned(),
SchemeData::Relative(ref scheme_data) => {
let mut host = scheme_data.host.serialize();
if let Some(port) = scheme_data.port {
write!(host, ":{}", port).unwrap();
}
host
},
})
}
// https://url.spec.whatwg.org/#dom-urlutils-hostname
pub fn Hostname(url: &Url) -> USVString {
USVString(url.serialize_host().unwrap_or_else(|| "".to_owned()))
}
// https://url.spec.whatwg.org/#dom-urlutils-href
pub fn Href(url: &Url) -> USVString {
USVString(url.serialize())
}
// https://url.spec.whatwg.org/#dom-urlutils-password
pub fn Password(url: &Url) -> USVString {
USVString(url.password().unwrap_or("").to_owned())
}
// https://url.spec.whatwg.org/#dom-urlutils-pathname
pub fn Pathname(url: &Url) -> USVString {
// FIXME: Url null check is skipped for now
@ -43,6 +59,19 @@ impl UrlHelper {
})
}
// https://url.spec.whatwg.org/#dom-urlutils-port
pub fn Port(url: &Url) -> USVString {
USVString(match url.port() {
None => "".to_owned(),
Some(port) => port.to_string(),
})
}
// https://url.spec.whatwg.org/#dom-urlutils-protocol
pub fn Protocol(url: &Url) -> USVString {
USVString(format!("{}:", url.scheme.clone()))
}
// https://html.spec.whatwg.org/multipage/#same-origin
pub fn SameOrigin(urlA: &Url, urlB: &Url) -> bool {
if urlA.host() != urlB.host() {
@ -56,4 +85,18 @@ impl UrlHelper {
}
return true
}
// https://url.spec.whatwg.org/#dom-urlutils-search
pub fn Search(url: &Url) -> USVString {
USVString(match url.query {
None => "".to_owned(),
Some(ref query) if query.is_empty() => "".to_owned(),
Some(ref query) => format!("?{}", query)
})
}
// https://url.spec.whatwg.org/#dom-urlutils-username
pub fn Username(url: &Url) -> USVString {
USVString(url.username().unwrap_or("").to_owned())
}
}

View file

@ -0,0 +1,12 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
// https://url.spec.whatwg.org/#url
[Constructor(USVString url, optional USVString base)/*,
Exposed=(Window,Worker)*/]
interface URL {
static USVString domainToASCII(USVString domain);
// static USVString domainToUnicode(USVString domain);
};
URL implements URLUtils;

View file

@ -10,11 +10,17 @@ interface URLUtils {
readonly attribute USVString href;
//readonly attribute USVString origin;
// attribute USVString protocol;
readonly attribute USVString protocol;
// attribute USVString username;
readonly attribute USVString username;
// attribute USVString password;
readonly attribute USVString password;
// attribute USVString host;
readonly attribute USVString host;
// attribute USVString hostname;
readonly attribute USVString hostname;
// attribute USVString port;
readonly attribute USVString port;
// attribute USVString pathname;
readonly attribute USVString pathname;
// attribute USVString search;

View file

@ -11,11 +11,15 @@ interface URLUtilsReadOnly {
readonly attribute USVString href;
//readonly attribute USVString origin;
//readonly attribute USVString protocol;
//readonly attribute USVString host;
//readonly attribute USVString hostname;
//readonly attribute USVString port;
//readonly attribute USVString pathname;
readonly attribute USVString protocol;
readonly attribute USVString host;
readonly attribute USVString hostname;
readonly attribute USVString port;
readonly attribute USVString pathname;
readonly attribute USVString search;
readonly attribute USVString hash;
// This is only doing as well as gecko right now, bug 824857 is on file for
// adding attribute stringifier support.
stringifier;
};

View file

@ -12,6 +12,7 @@ use dom::urlhelper::UrlHelper;
use dom::workerglobalscope::WorkerGlobalScope;
use url::Url;
use util::str::DOMString;
// https://html.spec.whatwg.org/multipage/#worker-locations
#[dom_struct]
@ -36,16 +37,49 @@ impl WorkerLocation {
}
impl<'a> WorkerLocationMethods for &'a WorkerLocation {
// https://url.spec.whatwg.org/#dom-urlutils-hash
fn Hash(self) -> USVString {
UrlHelper::Hash(&self.url)
}
// https://url.spec.whatwg.org/#dom-urlutils-host
fn Host(self) -> USVString {
UrlHelper::Host(&self.url)
}
// https://url.spec.whatwg.org/#dom-urlutils-hostname
fn Hostname(self) -> USVString {
UrlHelper::Hostname(&self.url)
}
// https://url.spec.whatwg.org/#dom-urlutils-href
fn Href(self) -> USVString {
UrlHelper::Href(&self.url)
}
// https://url.spec.whatwg.org/#dom-urlutils-pathname
fn Pathname(self) -> USVString {
UrlHelper::Pathname(&self.url)
}
// https://url.spec.whatwg.org/#dom-urlutils-port
fn Port(self) -> USVString {
UrlHelper::Port(&self.url)
}
// https://url.spec.whatwg.org/#dom-urlutils-protocol
fn Protocol(self) -> USVString {
UrlHelper::Protocol(&self.url)
}
// https://url.spec.whatwg.org/#dom-urlutils-search
fn Search(self) -> USVString {
UrlHelper::Search(&self.url)
}
fn Hash(self) -> USVString {
UrlHelper::Hash(&self.url)
// https://url.spec.whatwg.org/#URLUtils-stringification-behavior
fn Stringifier(self) -> DOMString {
self.Href().0
}
}