From 1ee9ccba215535dfc1af93a3579c5bca4a21706f Mon Sep 17 00:00:00 2001 From: Chandler Abraham Date: Thu, 21 Jan 2016 11:06:41 -0800 Subject: [PATCH] add origin to location and url api --- components/script/dom/location.rs | 5 + components/script/dom/url.rs | 5 + components/script/dom/urlhelper.rs | 30 +- components/script/dom/webidls/Location.webidl | 2 +- components/script/dom/webidls/URL.webidl | 2 +- components/script/dom/websocket.rs | 5 +- tests/wpt/metadata/MANIFEST.json | 4 + tests/wpt/metadata/url/interfaces.html.ini | 6 - .../wpt/metadata/url/url-constructor.html.ini | 591 ------------------ .../websockets/opening-handshake/003.html.ini | 5 - .../location_origin.html | 14 + 11 files changed, 62 insertions(+), 607 deletions(-) delete mode 100644 tests/wpt/metadata/websockets/opening-handshake/003.html.ini create mode 100644 tests/wpt/web-platform-tests/html/browsers/history/the-location-interface/location_origin.html diff --git a/components/script/dom/location.rs b/components/script/dom/location.rs index 0457da586189..08e23b159206 100644 --- a/components/script/dom/location.rs +++ b/components/script/dom/location.rs @@ -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()) diff --git a/components/script/dom/url.rs b/components/script/dom/url.rs index a0370d7f7677..adc758a8f18d 100644 --- a/components/script/dom/url.rs +++ b/components/script/dom/url.rs @@ -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()) diff --git a/components/script/dom/urlhelper.rs b/components/script/dom/urlhelper.rs index d9e26e84263b..580c541502df 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())) } diff --git a/components/script/dom/webidls/Location.webidl b/components/script/dom/webidls/Location.webidl index 8af05fbd1780..0593581d98ca 100644 --- a/components/script/dom/webidls/Location.webidl +++ b/components/script/dom/webidls/Location.webidl @@ -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; diff --git a/components/script/dom/webidls/URL.webidl b/components/script/dom/webidls/URL.webidl index 57ae34a3eba3..8c5d6e1580e8 100644 --- a/components/script/dom/webidls/URL.webidl +++ b/components/script/dom/webidls/URL.webidl @@ -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; diff --git a/components/script/dom/websocket.rs b/components/script/dom/websocket.rs index 36456cd02ff5..d70b633f0d5e 100644 --- a/components/script/dom/websocket.rs +++ b/components/script/dom/websocket.rs @@ -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, diff --git a/tests/wpt/metadata/MANIFEST.json b/tests/wpt/metadata/MANIFEST.json index 6557de4a9712..e31d211057fe 100644 --- a/tests/wpt/metadata/MANIFEST.json +++ b/tests/wpt/metadata/MANIFEST.json @@ -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" diff --git a/tests/wpt/metadata/url/interfaces.html.ini b/tests/wpt/metadata/url/interfaces.html.ini index 86abb5a8ca7b..62a48421971f 100644 --- a/tests/wpt/metadata/url/interfaces.html.ini +++ b/tests/wpt/metadata/url/interfaces.html.ini @@ -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 diff --git a/tests/wpt/metadata/url/url-constructor.html.ini b/tests/wpt/metadata/url/url-constructor.html.ini index d02962f33a17..628625cc1851 100644 --- a/tests/wpt/metadata/url/url-constructor.html.ini +++ b/tests/wpt/metadata/url/url-constructor.html.ini @@ -162,237 +162,12 @@ [Parsing: against ] expected: FAIL - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: <\t :foo.com \n> against ] - expected: FAIL - - [Parsing: < foo.com > against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: <> against ] - expected: FAIL - - [Parsing: < \t> against ] - expected: FAIL - - [Parsing: <:foo.com/> against ] - expected: FAIL - - [Parsing: <:foo.com\\> against ] - expected: FAIL - - [Parsing: <:> against ] - expected: FAIL - - [Parsing: <:a> against ] - expected: FAIL - - [Parsing: <:/> against ] - expected: FAIL - - [Parsing: <:\\> against ] - expected: FAIL - - [Parsing: <:#> against ] - expected: FAIL - - [Parsing: <#> against ] - expected: FAIL - - [Parsing: <#/> against ] - expected: FAIL - - [Parsing: <#\\> against ] - expected: FAIL - - [Parsing: <#;?> against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: <:23> against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: <::> against ] - expected: FAIL - - [Parsing: <::23> against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - [Parsing: against ] expected: FAIL [Parsing: against ] expected: FAIL - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: <[61:24:74\]:98> against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - [Parsing: against ] expected: FAIL @@ -414,330 +189,6 @@ [Parsing: against ] expected: FAIL - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: <.> against ] - expected: FAIL - - [Parsing: <..> against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: <./test.txt> against ] - expected: FAIL - - [Parsing: <../test.txt> against ] - expected: FAIL - - [Parsing: <../aaa/test.txt> against ] - expected: FAIL - - [Parsing: <../../test.txt> against ] - expected: FAIL - - [Parsing: <中/test.txt> against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - [Parsing: against ] expected: FAIL @@ -747,45 +198,3 @@ [Parsing: against ] expected: FAIL - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: <> against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - - [Parsing: against ] - expected: FAIL - diff --git a/tests/wpt/metadata/websockets/opening-handshake/003.html.ini b/tests/wpt/metadata/websockets/opening-handshake/003.html.ini deleted file mode 100644 index cfe8f2e820ae..000000000000 --- a/tests/wpt/metadata/websockets/opening-handshake/003.html.ini +++ /dev/null @@ -1,5 +0,0 @@ -[003.html] - type: testharness - [WebSockets: origin] - expected: FAIL - diff --git a/tests/wpt/web-platform-tests/html/browsers/history/the-location-interface/location_origin.html b/tests/wpt/web-platform-tests/html/browsers/history/the-location-interface/location_origin.html new file mode 100644 index 000000000000..2325f4018aa2 --- /dev/null +++ b/tests/wpt/web-platform-tests/html/browsers/history/the-location-interface/location_origin.html @@ -0,0 +1,14 @@ + + + + + +