Implement initial part of history.state

This commit is contained in:
Connor Brewster 2017-10-26 14:47:31 -05:00
parent 76b4e5cefb
commit 198ea8f767
26 changed files with 130 additions and 105 deletions

View file

@ -10,25 +10,38 @@ use dom::bindings::error::{Error, ErrorResult, Fallible};
use dom::bindings::inheritance::Castable; use dom::bindings::inheritance::Castable;
use dom::bindings::reflector::{Reflector, reflect_dom_object}; use dom::bindings::reflector::{Reflector, reflect_dom_object};
use dom::bindings::root::{Dom, DomRoot}; use dom::bindings::root::{Dom, DomRoot};
use dom::bindings::str::{DOMString, USVString};
use dom::bindings::structuredclone::StructuredCloneData;
use dom::globalscope::GlobalScope; use dom::globalscope::GlobalScope;
use dom::window::Window; use dom::window::Window;
use dom_struct::dom_struct; use dom_struct::dom_struct;
use ipc_channel::ipc; use ipc_channel::ipc;
use js::jsapi::{HandleValue, Heap, JSContext};
use js::jsval::{JSVal, NullValue, UndefinedValue};
use msg::constellation_msg::TraversalDirection; use msg::constellation_msg::TraversalDirection;
use script_traits::ScriptMsg; use script_traits::ScriptMsg;
enum PushOrReplace {
Push,
Replace,
}
// https://html.spec.whatwg.org/multipage/#the-history-interface // https://html.spec.whatwg.org/multipage/#the-history-interface
#[dom_struct] #[dom_struct]
pub struct History { pub struct History {
reflector_: Reflector, reflector_: Reflector,
window: Dom<Window>, window: Dom<Window>,
state: Heap<JSVal>,
} }
impl History { impl History {
pub fn new_inherited(window: &Window) -> History { pub fn new_inherited(window: &Window) -> History {
let state = Heap::default();
state.set(NullValue());
History { History {
reflector_: Reflector::new(), reflector_: Reflector::new(),
window: Dom::from_ref(&window), window: Dom::from_ref(&window),
state: state,
} }
} }
@ -48,9 +61,68 @@ impl History {
let _ = self.window.upcast::<GlobalScope>().script_to_constellation_chan().send(msg); let _ = self.window.upcast::<GlobalScope>().script_to_constellation_chan().send(msg);
Ok(()) Ok(())
} }
// https://html.spec.whatwg.org/multipage/#dom-history-pushstate
// https://html.spec.whatwg.org/multipage/#dom-history-replacestate
fn push_or_replace_state(&self,
cx: *mut JSContext,
data: HandleValue,
_title: DOMString,
_url: Option<USVString>,
_push_or_replace: PushOrReplace) -> ErrorResult {
// Step 1
let document = self.window.Document();
// Step 2
if !document.is_fully_active() {
return Err(Error::Security);
}
// TODO: Step 3 Optionally abort these steps
// https://github.com/servo/servo/issues/19159
// TODO: Step 4
// Step 5
let serialized_data = StructuredCloneData::write(cx, data)?;
// TODO: Steps 6-7 Url Handling
// https://github.com/servo/servo/issues/19157
// TODO: Step 8 Push/Replace session history entry
// https://github.com/servo/servo/issues/19156
// TODO: Step 9 Update current entry to represent a GET request
// https://github.com/servo/servo/issues/19156
// TODO: Step 10 Set document's URL to new URL
// https://github.com/servo/servo/issues/19157
// Step 11
let global_scope = self.window.upcast::<GlobalScope>();
rooted!(in(cx) let mut state = UndefinedValue());
serialized_data.read(&global_scope, state.handle_mut());
// Step 12
self.state.set(state.get());
// TODO: Step 13 Update Document's latest entry to current entry
// https://github.com/servo/servo/issues/19158
Ok(())
}
} }
impl HistoryMethods for History { impl HistoryMethods for History {
// https://html.spec.whatwg.org/multipage/#dom-history-state
#[allow(unsafe_code)]
unsafe fn GetState(&self, _cx: *mut JSContext) -> Fallible<JSVal> {
if !self.window.Document().is_fully_active() {
return Err(Error::Security);
}
Ok(self.state.get())
}
// https://html.spec.whatwg.org/multipage/#dom-history-length // https://html.spec.whatwg.org/multipage/#dom-history-length
fn GetLength(&self) -> Fallible<u32> { fn GetLength(&self) -> Fallible<u32> {
if !self.window.Document().is_fully_active() { if !self.window.Document().is_fully_active() {
@ -60,7 +132,7 @@ impl HistoryMethods for History {
let msg = ScriptMsg::JointSessionHistoryLength(sender); let msg = ScriptMsg::JointSessionHistoryLength(sender);
let _ = self.window.upcast::<GlobalScope>().script_to_constellation_chan().send(msg); let _ = self.window.upcast::<GlobalScope>().script_to_constellation_chan().send(msg);
Ok(recv.recv().unwrap()) Ok(recv.recv().unwrap())
} }
// https://html.spec.whatwg.org/multipage/#dom-history-go // https://html.spec.whatwg.org/multipage/#dom-history-go
fn Go(&self, delta: i32) -> ErrorResult { fn Go(&self, delta: i32) -> ErrorResult {
@ -84,4 +156,24 @@ impl HistoryMethods for History {
fn Forward(&self) -> ErrorResult { fn Forward(&self) -> ErrorResult {
self.traverse_history(TraversalDirection::Forward(1)) self.traverse_history(TraversalDirection::Forward(1))
} }
// https://html.spec.whatwg.org/multipage/#dom-history-pushstate
#[allow(unsafe_code)]
unsafe fn PushState(&self,
cx: *mut JSContext,
data: HandleValue,
title: DOMString,
url: Option<USVString>) -> ErrorResult {
self.push_or_replace_state(cx, data, title, url, PushOrReplace::Push)
}
// https://html.spec.whatwg.org/multipage/#dom-history-replacestate
#[allow(unsafe_code)]
unsafe fn ReplaceState(&self,
cx: *mut JSContext,
data: HandleValue,
title: DOMString,
url: Option<USVString>) -> ErrorResult {
self.push_or_replace_state(cx, data, title, url, PushOrReplace::Replace)
}
} }

View file

@ -11,16 +11,16 @@ interface History {
readonly attribute unsigned long length; readonly attribute unsigned long length;
// [Throws] // [Throws]
// attribute ScrollRestoration scrollRestoration; // attribute ScrollRestoration scrollRestoration;
// [Throws] [Throws]
// readonly attribute any state; readonly attribute any state;
[Throws] [Throws]
void go(optional long delta = 0); void go(optional long delta = 0);
[Throws] [Throws]
void back(); void back();
[Throws] [Throws]
void forward(); void forward();
// [Throws] [Throws]
// void pushState(any data, DOMString title, optional USVString? url = null); void pushState(any data, DOMString title, optional USVString? url = null);
// [Throws] [Throws]
// void replaceState(any data, DOMString title, optional USVString? url = null); void replaceState(any data, DOMString title, optional USVString? url = null);
}; };

View file

@ -1,5 +1,7 @@
[scroll-restoration-fragment-scrolling-samedoc.html] [scroll-restoration-fragment-scrolling-samedoc.html]
type: testharness type: testharness
expected: TIMEOUT
bug: https://github.com/servo/servo/issues/14970
[Manual scroll restoration should take precedent over scrolling to fragment in cross doc navigation] [Manual scroll restoration should take precedent over scrolling to fragment in cross doc navigation]
expected: FAIL expected: TIMEOUT

View file

@ -1,5 +1,7 @@
[scroll-restoration-navigation-samedoc.html] [scroll-restoration-navigation-samedoc.html]
type: testharness type: testharness
expected: TIMEOUT
bug: https://github.com/servo/servo/issues/14970
[history.{push,replace}State retain scroll restoration mode and navigation in the same document respects it] [history.{push,replace}State retain scroll restoration mode and navigation in the same document respects it]
expected: FAIL expected: TIMEOUT

View file

@ -1,5 +1,7 @@
[popstate_event.html] [popstate_event.html]
type: testharness type: testharness
expected: TIMEOUT
bug: https://github.com/servo/servo/issues/19905
[Queue a task to fire popstate event] [Queue a task to fire popstate event]
expected: FAIL expected: TIMEOUT

View file

@ -1,8 +1,5 @@
[005.html] [005.html]
type: testharness type: testharness
[history.pushState support is needed for this testcase]
expected: FAIL
[<body onpopstate="..."> should register a listener for the popstate event] [<body onpopstate="..."> should register a listener for the popstate event]
expected: FAIL expected: FAIL

View file

@ -1,14 +0,0 @@
[006.html]
type: testharness
[history.state should initially be null]
expected: FAIL
[history.state should still be null onload]
expected: FAIL
[history.state should still be null after onload]
expected: FAIL
[writing to history.state should be silently ignored and not throw an error]
expected: FAIL

View file

@ -1,14 +1,5 @@
[007.html] [007.html]
type: testharness type: testharness
[history.state should initially be null]
expected: FAIL
[history.pushState support is needed for this testcase]
expected: FAIL
[history.state should reflect pushed state]
expected: FAIL
[popstate event should fire before onload fires] [popstate event should fire before onload fires]
expected: FAIL expected: FAIL

View file

@ -1,8 +1,5 @@
[011.html] [011.html]
type: testharness type: testharness
[pushState should be able to set the location state]
expected: FAIL
[pushed location should be reflected immediately] [pushed location should be reflected immediately]
expected: FAIL expected: FAIL

View file

@ -1,8 +1,5 @@
[012.html] [012.html]
type: testharness type: testharness
[replaceState should be able to set the location state]
expected: FAIL
[replaced location should be reflected immediately] [replaced location should be reflected immediately]
expected: FAIL expected: FAIL

View file

@ -1,5 +0,0 @@
[combination_history_001.html]
type: testharness
[Combine pushState and replaceSate methods]
expected: FAIL

View file

@ -1,5 +1,6 @@
[combination_history_004.html] [combination_history_004.html]
type: testharness type: testharness
expected: TIMEOUT
[After calling of back method, check length] [After calling of back method, check length]
expected: FAIL expected: TIMEOUT

View file

@ -1,5 +1,6 @@
[combination_history_005.html] [combination_history_005.html]
type: testharness type: testharness
expected: TIMEOUT
[After calling of forward method, check length] [After calling of forward method, check length]
expected: FAIL expected: TIMEOUT

View file

@ -1,5 +1,6 @@
[combination_history_006.html] [combination_history_006.html]
type: testharness type: testharness
expected: TIMEOUT
[After calling of go method, check length] [After calling of go method, check length]
expected: FAIL expected: TIMEOUT

View file

@ -1,5 +1,6 @@
[combination_history_007.html] [combination_history_007.html]
type: testharness type: testharness
expected: TIMEOUT
[After calling of back and pushState method, check length] [After calling of back and pushState method, check length]
expected: FAIL expected: TIMEOUT

View file

@ -1,5 +1,6 @@
[history_back.html] [history_back.html]
type: testharness type: testharness
expected: TIMEOUT
[history back] [history back]
expected: FAIL expected: TIMEOUT

View file

@ -1,5 +1,6 @@
[history_forward.html] [history_forward.html]
type: testharness type: testharness
expected: TIMEOUT
[history forward] [history forward]
expected: FAIL expected: TIMEOUT

View file

@ -1,5 +1,6 @@
[history_go_minus.html] [history_go_minus.html]
type: testharness type: testharness
expected: TIMEOUT
[history go minus] [history go minus]
expected: FAIL expected: TIMEOUT

View file

@ -1,5 +1,6 @@
[history_go_plus.html] [history_go_plus.html]
type: testharness type: testharness
expected: TIMEOUT
[history go plus] [history go plus]
expected: FAIL expected: TIMEOUT

View file

@ -1,5 +0,0 @@
[history_pushstate.html]
type: testharness
[history pushState]
expected: FAIL

View file

@ -1,5 +0,0 @@
[history_pushstate_nooptionalparam.html]
type: testharness
[history pushState NoOptionalParam]
expected: FAIL

View file

@ -1,5 +0,0 @@
[history_replacestate.html]
type: testharness
[history replaceState]
expected: FAIL

View file

@ -1,5 +0,0 @@
[history_replacestate_nooptionalparam.html]
type: testharness
[history replaceStateNoOptionalParam]
expected: FAIL

View file

@ -1,5 +0,0 @@
[history_state.html]
type: testharness
[history state]
expected: FAIL

View file

@ -4455,9 +4455,6 @@
[BarProp interface: attribute visible] [BarProp interface: attribute visible]
expected: FAIL expected: FAIL
[History interface: attribute state]
expected: FAIL
[History interface: operation pushState(any,DOMString,DOMString)] [History interface: operation pushState(any,DOMString,DOMString)]
expected: FAIL expected: FAIL
@ -13644,30 +13641,9 @@
[Window interface: calling createImageBitmap(ImageBitmapSource, long, long, long, long, ImageBitmapOptions) on window with too few arguments must throw TypeError] [Window interface: calling createImageBitmap(ImageBitmapSource, long, long, long, long, ImageBitmapOptions) on window with too few arguments must throw TypeError]
expected: FAIL expected: FAIL
[History interface: operation pushState(any, DOMString, USVString)]
expected: FAIL
[History interface: operation replaceState(any, DOMString, USVString)]
expected: FAIL
[History interface: window.history must inherit property "scrollRestoration" with the proper type] [History interface: window.history must inherit property "scrollRestoration" with the proper type]
expected: FAIL expected: FAIL
[History interface: window.history must inherit property "state" with the proper type]
expected: FAIL
[History interface: window.history must inherit property "pushState(any, DOMString, USVString)" with the proper type]
expected: FAIL
[History interface: calling pushState(any, DOMString, USVString) on window.history with too few arguments must throw TypeError]
expected: FAIL
[History interface: window.history must inherit property "replaceState(any, DOMString, USVString)" with the proper type]
expected: FAIL
[History interface: calling replaceState(any, DOMString, USVString) on window.history with too few arguments must throw TypeError]
expected: FAIL
[ApplicationCache interface: window.applicationCache must inherit property "UNCACHED" with the proper type] [ApplicationCache interface: window.applicationCache must inherit property "UNCACHED" with the proper type]
expected: FAIL expected: FAIL

View file

@ -1,3 +1,8 @@
[formAction_document_address.html] [formAction_document_address.html]
type: testharness type: testharness
expected: ERROR [Check if button.formAction is the document's new address when formaction content attribute is missing and pushState has been used]
expected: FAIL
[Check if input.formAction is the document's new address when formaction content attribute is missing and pushState has been used]
expected: FAIL