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

@ -81,6 +81,11 @@ impl LocationMethods for Location {
self.set_url_component(value, UrlHelper::SetHost);
}
// https://html.spec.whatwg.org/multipage/#dom-location-origin
fn Origin(&self) -> USVString {
UrlHelper::Origin(&self.get_url())
}
// https://html.spec.whatwg.org/multipage/#dom-location-hostname
fn Hostname(&self) -> USVString {
UrlHelper::Hostname(&self.get_url())

View file

@ -176,6 +176,11 @@ impl URLMethods for URL {
UrlHelper::SetProtocol(&mut self.url.borrow_mut(), value);
}
// https://url.spec.whatwg.org/#dom-url-origin
fn Origin(&self) -> USVString {
UrlHelper::Origin(&self.url.borrow())
}
// https://url.spec.whatwg.org/#dom-url-search
fn Search(&self) -> USVString {
UrlHelper::Search(&self.url.borrow())

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

View file

@ -6,7 +6,7 @@
// https://html.spec.whatwg.org/multipage/#location
[Unforgeable] interface Location {
/*stringifier*/ attribute USVString href;
// attribute USVString origin;
readonly attribute USVString origin;
attribute USVString protocol;
attribute USVString host;
attribute USVString hostname;

View file

@ -11,7 +11,7 @@ interface URL {
[SetterThrows]
/*stringifier*/ attribute USVString href;
// readonly attribute USVString origin;
readonly attribute USVString origin;
attribute USVString protocol;
attribute USVString username;
attribute USVString password;

View file

@ -5,8 +5,10 @@
use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::BlobBinding::BlobMethods;
use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull;
use dom::bindings::codegen::Bindings::LocationBinding::LocationMethods;
use dom::bindings::codegen::Bindings::WebSocketBinding;
use dom::bindings::codegen::Bindings::WebSocketBinding::{BinaryType, WebSocketMethods};
use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
use dom::bindings::codegen::UnionTypes::StringOrStringSequence::{self, eString, eStringSequence};
use dom::bindings::conversions::{ToJSValConvertible};
use dom::bindings::error::{Error, Fallible};
@ -234,13 +236,12 @@ impl WebSocket {
}
// Step 6: Origin.
let origin = global.as_window().Location().Origin().0;
// Step 7.
let ws = WebSocket::new(global, resource_url.clone());
let address = Trusted::new(ws.r(), global.networking_thread_source());
let origin = global.get_url().serialize();
let connect_data = WebSocketConnectData {
resource_url: resource_url.clone(),
origin: origin,

View file

@ -15691,6 +15691,10 @@
"path": "html/browsers/history/the-location-interface/location_href.html",
"url": "/html/browsers/history/the-location-interface/location_href.html"
},
{
"path": "html/browsers/history/the-location-interface/location_origin.html",
"url": "/html/browsers/history/the-location-interface/location_origin.html"
},
{
"path": "html/browsers/history/the-location-interface/location_pathname.html",
"url": "/html/browsers/history/the-location-interface/location_pathname.html"

View file

@ -3,15 +3,9 @@
[URL interface: operation domainToUnicode(ScalarValueString)]
expected: FAIL
[URL interface: attribute origin]
expected: FAIL
[URL interface: attribute searchParams]
expected: FAIL
[URL interface: new URL("http://foo") must inherit property "origin" with the proper type (3)]
expected: FAIL
[URL interface: new URL("http://foo") must inherit property "searchParams" with the proper type (12)]
expected: FAIL

View file

@ -162,237 +162,12 @@
[Parsing: <sc://ñ.test/> against <about:blank>]
expected: FAIL
[Parsing: <http://example\t.\norg> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <http://user:pass@foo:21/bar;par?b#c> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <http:foo.com> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <\t :foo.com \n> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: < foo.com > against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <a:\t foo.com> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <http://f:21/ b ? d # e > against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <http://f:/c> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <http://f:0/c> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <http://f:00000000000000/c> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <http://f:00000000000000000000080/c> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <http://f:\n/c> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: < \t> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <:foo.com/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <:foo.com\\> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <:> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <:a> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <:/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <:\\> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <:#> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <#> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <#/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <#\\> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <#;?> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <?> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: </> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <:23> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: </:23> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <::> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <::23> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <http://a:b@c:29/d> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <http::@c:29> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <http://&a:foo(b\]c@d:2/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <http://::@c@d:2> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <http://foo.com:b@d/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <http://foo.com/\\@> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <http:\\\\foo.com\\> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <http:\\\\a\\b:c\\d@foo.com\\> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <foo:/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <foo:/bar.com/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <c:/foo> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <//foo/bar> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <http://foo/path;a??e#f#g> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <http://foo/abcd?efgh?ijkl> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <http://foo/abcd#foo?bar> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <[61:24:74\]:98> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <http:[61:27\]/:foo> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <http://[2001::1\]> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <http://[2001::1\]:80> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <http:/example.com/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <ftp:/example.com/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <https:/example.com/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <madeupscheme:/example.com/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <ftps:/example.com/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <gopher:/example.com/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <ws:/example.com/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <wss:/example.com/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <data:/example.com/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <javascript:/example.com/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <mailto:/example.com/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <http:example.com/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <ftp:example.com/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <https:example.com/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <madeupscheme:example.com/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <ftps:example.com/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <gopher:example.com/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <ws:example.com/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <wss:example.com/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <data:example.com/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <javascript:example.com/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <mailto:example.com/> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: </a/b/c> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: </a/ /c> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: </a%2fc> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: </a/%2f/c> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <data:text/html,test#test> against <http://example.org/foo/bar>]
expected: FAIL
[Parsing: <//server/file> against <file:///tmp/mock/path>]
expected: FAIL
@ -414,330 +189,6 @@
[Parsing: <file://localhost/test> against <file:///tmp/mock/path>]
expected: FAIL
[Parsing: <http://example.com/././foo> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/./.foo> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/foo/.> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/foo/./> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/foo/bar/..> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/foo/bar/../> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/foo/..bar> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/foo/bar/../ton> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/foo/bar/../ton/../../a> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/foo/../../..> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/foo/../../../ton> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/foo/%2e> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/foo/%2e%2> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/foo/%2e./%2e%2e/.%2e/%2e.bar> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com////../..> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/foo/bar//../..> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/foo/bar//..> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/foo> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/%20foo> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/foo%> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/foo%2> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/foo%2zbar> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/foo%2©zbar> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/foo%41%7a> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/foo\t‘%91> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/foo%00%51> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/(%28:%3A%29)> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/%3A%3a%3C%3c> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/foo\tbar> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com\\\\foo\\\\bar> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/%7Ffp3%3Eju%3Dduvgw%3Dd> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/@asdf%40> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/你好你好> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com/‥/foo> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com//foo> against <about:blank>]
expected: FAIL
[Parsing: <http://example.com//foo//bar> against <about:blank>]
expected: FAIL
[Parsing: <http://www.google.com/foo?bar=baz#> against <about:blank>]
expected: FAIL
[Parsing: <http://www.google.com> against <about:blank>]
expected: FAIL
[Parsing: <http://192.0x00A80001> against <about:blank>]
expected: FAIL
[Parsing: <http://www/foo%2Ehtml> against <about:blank>]
expected: FAIL
[Parsing: <http://www/foo/%2E/html> against <about:blank>]
expected: FAIL
[Parsing: <http://%25DOMAIN:foobar@foodomain.com/> against <about:blank>]
expected: FAIL
[Parsing: <http:\\\\www.google.com\\foo> against <about:blank>]
expected: FAIL
[Parsing: <http://foo:80/> against <about:blank>]
expected: FAIL
[Parsing: <http://foo:81/> against <about:blank>]
expected: FAIL
[Parsing: <https://foo:443/> against <about:blank>]
expected: FAIL
[Parsing: <https://foo:80/> against <about:blank>]
expected: FAIL
[Parsing: <ftp://foo:21/> against <about:blank>]
expected: FAIL
[Parsing: <ftp://foo:80/> against <about:blank>]
expected: FAIL
[Parsing: <gopher://foo:70/> against <about:blank>]
expected: FAIL
[Parsing: <gopher://foo:443/> against <about:blank>]
expected: FAIL
[Parsing: <ws://foo:80/> against <about:blank>]
expected: FAIL
[Parsing: <ws://foo:81/> against <about:blank>]
expected: FAIL
[Parsing: <ws://foo:443/> against <about:blank>]
expected: FAIL
[Parsing: <ws://foo:815/> against <about:blank>]
expected: FAIL
[Parsing: <wss://foo:80/> against <about:blank>]
expected: FAIL
[Parsing: <wss://foo:81/> against <about:blank>]
expected: FAIL
[Parsing: <wss://foo:443/> against <about:blank>]
expected: FAIL
[Parsing: <wss://foo:815/> against <about:blank>]
expected: FAIL
[Parsing: <http:/example.com/> against <about:blank>]
expected: FAIL
[Parsing: <ftp:/example.com/> against <about:blank>]
expected: FAIL
[Parsing: <https:/example.com/> against <about:blank>]
expected: FAIL
[Parsing: <madeupscheme:/example.com/> against <about:blank>]
expected: FAIL
[Parsing: <ftps:/example.com/> against <about:blank>]
expected: FAIL
[Parsing: <gopher:/example.com/> against <about:blank>]
expected: FAIL
[Parsing: <ws:/example.com/> against <about:blank>]
expected: FAIL
[Parsing: <wss:/example.com/> against <about:blank>]
expected: FAIL
[Parsing: <data:/example.com/> against <about:blank>]
expected: FAIL
[Parsing: <javascript:/example.com/> against <about:blank>]
expected: FAIL
[Parsing: <mailto:/example.com/> against <about:blank>]
expected: FAIL
[Parsing: <http:example.com/> against <about:blank>]
expected: FAIL
[Parsing: <ftp:example.com/> against <about:blank>]
expected: FAIL
[Parsing: <https:example.com/> against <about:blank>]
expected: FAIL
[Parsing: <madeupscheme:example.com/> against <about:blank>]
expected: FAIL
[Parsing: <ftps:example.com/> against <about:blank>]
expected: FAIL
[Parsing: <gopher:example.com/> against <about:blank>]
expected: FAIL
[Parsing: <ws:example.com/> against <about:blank>]
expected: FAIL
[Parsing: <wss:example.com/> against <about:blank>]
expected: FAIL
[Parsing: <data:example.com/> against <about:blank>]
expected: FAIL
[Parsing: <javascript:example.com/> against <about:blank>]
expected: FAIL
[Parsing: <mailto:example.com/> against <about:blank>]
expected: FAIL
[Parsing: <http:@www.example.com> against <about:blank>]
expected: FAIL
[Parsing: <http:/@www.example.com> against <about:blank>]
expected: FAIL
[Parsing: <http://@www.example.com> against <about:blank>]
expected: FAIL
[Parsing: <http:a:b@www.example.com> against <about:blank>]
expected: FAIL
[Parsing: <http:/a:b@www.example.com> against <about:blank>]
expected: FAIL
[Parsing: <http://a:b@www.example.com> against <about:blank>]
expected: FAIL
[Parsing: <http://@pple.com> against <about:blank>]
expected: FAIL
[Parsing: <http::b@www.example.com> against <about:blank>]
expected: FAIL
[Parsing: <http:/:b@www.example.com> against <about:blank>]
expected: FAIL
[Parsing: <http://:b@www.example.com> against <about:blank>]
expected: FAIL
[Parsing: <http:a:@www.example.com> against <about:blank>]
expected: FAIL
[Parsing: <http:/a:@www.example.com> against <about:blank>]
expected: FAIL
[Parsing: <http://a:@www.example.com> against <about:blank>]
expected: FAIL
[Parsing: <http://www.@pple.com> against <about:blank>]
expected: FAIL
[Parsing: <http://:@www.example.com> against <about:blank>]
expected: FAIL
[Parsing: </> against <http://www.example.com/test>]
expected: FAIL
[Parsing: </test.txt> against <http://www.example.com/test>]
expected: FAIL
[Parsing: <.> against <http://www.example.com/test>]
expected: FAIL
[Parsing: <..> against <http://www.example.com/test>]
expected: FAIL
[Parsing: <test.txt> against <http://www.example.com/test>]
expected: FAIL
[Parsing: <./test.txt> against <http://www.example.com/test>]
expected: FAIL
[Parsing: <../test.txt> against <http://www.example.com/test>]
expected: FAIL
[Parsing: <../aaa/test.txt> against <http://www.example.com/test>]
expected: FAIL
[Parsing: <../../test.txt> against <http://www.example.com/test>]
expected: FAIL
[Parsing: <中/test.txt> against <http://www.example.com/test>]
expected: FAIL
[Parsing: <http://www.example2.com> against <http://www.example.com/test>]
expected: FAIL
[Parsing: <//www.example2.com> against <http://www.example.com/test>]
expected: FAIL
[Parsing: <file:...> against <http://www.example.com/test>]
expected: FAIL
@ -747,45 +198,3 @@
[Parsing: <file:a> against <http://www.example.com/test>]
expected: FAIL
[Parsing: <http://ExAmPlE.CoM> against <http://other.com/>]
expected: FAIL
[Parsing: <http://%30%78%63%30%2e%30%32%35%30.01> against <http://other.com/>]
expected: FAIL
[Parsing: <http://%30%78%63%30%2e%30%32%35%30.01%2e> against <http://other.com/>]
expected: FAIL
[Parsing: <http://foo:💩@example.com/bar> against <http://other.com/>]
expected: FAIL
[Parsing: <https://@test@test@example:800/> against <http://doesnotmatter/>]
expected: FAIL
[Parsing: <https://@@@example> against <http://doesnotmatter/>]
expected: FAIL
[Parsing: <http://`{}:`{}@h/`{}?`{}> against <http://doesnotmatter/>]
expected: FAIL
[Parsing: </some/path> against <http://user@example.org/smth>]
expected: FAIL
[Parsing: <> against <http://user:pass@example.org:21/smth>]
expected: FAIL
[Parsing: </some/path> against <http://user:pass@example.org:21/smth>]
expected: FAIL
[Parsing: <sc:\\../> against <about:blank>]
expected: FAIL
[Parsing: <http://127.0.0.1:10100/relative_import.html> against <about:blank>]
expected: FAIL
[Parsing: <http://facebook.com/?foo=%7B%22abc%22> against <about:blank>]
expected: FAIL
[Parsing: <https://localhost:3000/jqueryui@1.2.3> against <about:blank>]
expected: FAIL

View file

@ -1,5 +0,0 @@
[003.html]
type: testharness
[WebSockets: origin]
expected: FAIL

View file

@ -0,0 +1,14 @@
<!doctype html>
<meta charset="utf-8">
<title></title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
test(function () {
assert_equals(
location.origin,
location.protocol + '//' + location.host,
"origin"
);
}, "location origin");
</script>