Skip to content

Commit

Permalink
Implement initial part of history.state
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrewster committed Jan 30, 2018
1 parent 76b4e5c commit 198ea8f
Show file tree
Hide file tree
Showing 26 changed files with 130 additions and 105 deletions.
94 changes: 93 additions & 1 deletion components/script/dom/history.rs
Expand Up @@ -10,25 +10,38 @@ use dom::bindings::error::{Error, ErrorResult, Fallible};
use dom::bindings::inheritance::Castable;
use dom::bindings::reflector::{Reflector, reflect_dom_object};
use dom::bindings::root::{Dom, DomRoot};
use dom::bindings::str::{DOMString, USVString};
use dom::bindings::structuredclone::StructuredCloneData;
use dom::globalscope::GlobalScope;
use dom::window::Window;
use dom_struct::dom_struct;
use ipc_channel::ipc;
use js::jsapi::{HandleValue, Heap, JSContext};
use js::jsval::{JSVal, NullValue, UndefinedValue};
use msg::constellation_msg::TraversalDirection;
use script_traits::ScriptMsg;

enum PushOrReplace {
Push,
Replace,
}

// https://html.spec.whatwg.org/multipage/#the-history-interface
#[dom_struct]
pub struct History {
reflector_: Reflector,
window: Dom<Window>,
state: Heap<JSVal>,
}

impl History {
pub fn new_inherited(window: &Window) -> History {
let state = Heap::default();
state.set(NullValue());
History {
reflector_: Reflector::new(),
window: Dom::from_ref(&window),
state: state,
}
}

Expand All @@ -48,9 +61,68 @@ impl History {
let _ = self.window.upcast::<GlobalScope>().script_to_constellation_chan().send(msg);
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 {
// 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
fn GetLength(&self) -> Fallible<u32> {
if !self.window.Document().is_fully_active() {
Expand All @@ -60,7 +132,7 @@ impl HistoryMethods for History {
let msg = ScriptMsg::JointSessionHistoryLength(sender);
let _ = self.window.upcast::<GlobalScope>().script_to_constellation_chan().send(msg);
Ok(recv.recv().unwrap())
}
}

// https://html.spec.whatwg.org/multipage/#dom-history-go
fn Go(&self, delta: i32) -> ErrorResult {
Expand All @@ -84,4 +156,24 @@ impl HistoryMethods for History {
fn Forward(&self) -> ErrorResult {
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)
}
}
12 changes: 6 additions & 6 deletions components/script/dom/webidls/History.webidl
Expand Up @@ -11,16 +11,16 @@ interface History {
readonly attribute unsigned long length;
// [Throws]
// attribute ScrollRestoration scrollRestoration;
// [Throws]
// readonly attribute any state;
[Throws]
readonly attribute any state;
[Throws]
void go(optional long delta = 0);
[Throws]
void back();
[Throws]
void forward();
// [Throws]
// void pushState(any data, DOMString title, optional USVString? url = null);
// [Throws]
// void replaceState(any data, DOMString title, optional USVString? url = null);
[Throws]
void pushState(any data, DOMString title, optional USVString? url = null);
[Throws]
void replaceState(any data, DOMString title, optional USVString? url = null);
};
@@ -1,5 +1,7 @@
[scroll-restoration-fragment-scrolling-samedoc.html]
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]
expected: FAIL
expected: TIMEOUT

@@ -1,5 +1,7 @@
[scroll-restoration-navigation-samedoc.html]
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]
expected: FAIL
expected: TIMEOUT

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

@@ -1,8 +1,5 @@
[005.html]
type: testharness
[history.pushState support is needed for this testcase]
expected: FAIL

[<body onpopstate="..."> should register a listener for the popstate event]
expected: FAIL

Expand Down

This file was deleted.

@@ -1,14 +1,5 @@
[007.html]
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]
expected: FAIL

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

[pushed location should be reflected immediately]
expected: FAIL

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

[replaced location should be reflected immediately]
expected: FAIL

Expand Down

This file was deleted.

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

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

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

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

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

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

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

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

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

24 changes: 0 additions & 24 deletions tests/wpt/metadata/html/dom/interfaces.html.ini
Expand Up @@ -4455,9 +4455,6 @@
[BarProp interface: attribute visible]
expected: FAIL

[History interface: attribute state]
expected: FAIL

[History interface: operation pushState(any,DOMString,DOMString)]
expected: FAIL

Expand Down Expand Up @@ -13644,30 +13641,9 @@
[Window interface: calling createImageBitmap(ImageBitmapSource, long, long, long, long, ImageBitmapOptions) on window with too few arguments must throw TypeError]
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]
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]
expected: FAIL

Expand Down
@@ -1,3 +1,8 @@
[formAction_document_address.html]
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

0 comments on commit 198ea8f

Please sign in to comment.