Auto merge of #12552 - ConnorGBrewster:history_interface, r=asajeffrey

History interface Go, Back, and Forward

<!-- Please describe your changes on the following line: -->
r? @asajeffrey

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #5670 (github issue number if applicable).

<!-- Either: -->
- [X] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

implement go, forward, back

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/12552)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-07-22 15:34:14 -05:00 committed by GitHub
commit 0e887ca8d3
18 changed files with 324 additions and 66 deletions

View file

@ -0,0 +1,70 @@
/* 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::HistoryBinding;
use dom::bindings::codegen::Bindings::HistoryBinding::HistoryMethods;
use dom::bindings::codegen::Bindings::LocationBinding::LocationMethods;
use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, Root};
use dom::bindings::reflector::{Reflector, reflect_dom_object};
use dom::window::Window;
use msg::constellation_msg::TraversalDirection;
use script_traits::ScriptMsg as ConstellationMsg;
// https://html.spec.whatwg.org/multipage/#the-history-interface
#[dom_struct]
pub struct History {
reflector_: Reflector,
window: JS<Window>,
}
impl History {
pub fn new_inherited(window: &Window) -> History {
History {
reflector_: Reflector::new(),
window: JS::from_ref(&window),
}
}
pub fn new(window: &Window) -> Root<History> {
reflect_dom_object(box History::new_inherited(window),
GlobalRef::Window(window),
HistoryBinding::Wrap)
}
}
impl History {
fn traverse_history(&self, direction: TraversalDirection) {
let pipeline = self.window.pipeline();
let msg = ConstellationMsg::TraverseHistory(Some(pipeline), direction);
let _ = self.window.constellation_chan().send(msg);
}
}
impl HistoryMethods for History {
// https://html.spec.whatwg.org/multipage/#dom-history-go
fn Go(&self, delta: i32) {
let direction = if delta > 0 {
TraversalDirection::Forward(delta as usize)
} else if delta < 0 {
TraversalDirection::Back(-delta as usize)
} else {
self.window.Location().Reload();
return;
};
self.traverse_history(direction);
}
// https://html.spec.whatwg.org/multipage/#dom-history-back
fn Back(&self) {
self.traverse_history(TraversalDirection::Back(1));
}
// https://html.spec.whatwg.org/multipage/#dom-history-forward
fn Forward(&self) {
self.traverse_history(TraversalDirection::Forward(1));
}
}

View file

@ -270,6 +270,7 @@ pub mod forcetouchevent;
pub mod formdata;
pub mod hashchangeevent;
pub mod headers;
pub mod history;
pub mod htmlanchorelement;
pub mod htmlappletelement;
pub mod htmlareaelement;

View file

@ -0,0 +1,18 @@
/* 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/. */
// enum ScrollRestoration { "auto", "manual" };
// https://html.spec.whatwg.org/multipage/#the-history-interface
[Exposed=(Window,Worker)]
interface History {
// readonly attribute unsigned long length;
// attribute ScrollRestoration scrollRestoration;
// readonly attribute any state;
void go(optional long delta = 0);
void back();
void forward();
// void pushState(any data, DOMString title, optional USVString? url = null);
// void replaceState(any data, DOMString title, optional USVString? url = null);
};

View file

@ -11,7 +11,7 @@
[Unforgeable] readonly attribute Document document;
// attribute DOMString name;
[/*PutForwards=href, */Unforgeable] readonly attribute Location location;
//readonly attribute History history;
readonly attribute History history;
//[Replaceable] readonly attribute BarProp locationbar;
//[Replaceable] readonly attribute BarProp menubar;
//[Replaceable] readonly attribute BarProp personalbar;

View file

@ -30,6 +30,7 @@ use dom::document::Document;
use dom::element::Element;
use dom::event::Event;
use dom::eventtarget::EventTarget;
use dom::history::History;
use dom::htmliframeelement::build_mozbrowser_custom_event;
use dom::location::Location;
use dom::navigator::Navigator;
@ -158,6 +159,7 @@ pub struct Window {
#[ignore_heap_size_of = "channels are hard"]
image_cache_chan: ImageCacheChan,
browsing_context: MutNullableHeap<JS<BrowsingContext>>,
history: MutNullableHeap<JS<History>>,
performance: MutNullableHeap<JS<Performance>>,
navigation_start: u64,
navigation_start_precise: f64,
@ -475,6 +477,11 @@ impl WindowMethods for Window {
self.browsing_context().active_document()
}
// https://html.spec.whatwg.org/multipage/#dom-history
fn History(&self) -> Root<History> {
self.history.or_init(|| History::new(self))
}
// https://html.spec.whatwg.org/multipage/#dom-location
fn Location(&self) -> Root<Location> {
self.Document().GetLocation().unwrap()
@ -1648,6 +1655,7 @@ impl Window {
mem_profiler_chan: mem_profiler_chan,
time_profiler_chan: time_profiler_chan,
devtools_chan: devtools_chan,
history: Default::default(),
browsing_context: Default::default(),
performance: Default::default(),
navigation_start: (current_time.sec * 1000 + current_time.nsec as i64 / 1000000) as u64,