Skip to content

Commit

Permalink
auto merge of #2374 : Manishearth/servo/customevent, r=jdm
Browse files Browse the repository at this point in the history
This was previously PR'd on #2218
  • Loading branch information
bors-servo committed May 23, 2014
2 parents e59557e + 4c997e8 commit c7e8e5a
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/components/script/dom/bindings/codegen/Bindings.conf
Expand Up @@ -25,6 +25,7 @@ DOMInterfaces = {
'ClientRectList': {},
'Comment': {},
'Console': {},
'CustomEvent': {},
'Document': {},
'DocumentFragment': {},
'DocumentType': {},
Expand Down
86 changes: 86 additions & 0 deletions src/components/script/dom/customevent.rs
@@ -0,0 +1,86 @@
/* 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::BindingDeclarations::CustomEventBinding;
use dom::bindings::codegen::InheritTypes::{EventCast, CustomEventDerived};
use dom::bindings::js::{JSRef, Temporary};
use dom::bindings::error::Fallible;
use dom::bindings::trace::Traceable;
use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object};
use dom::event::{Event, EventMethods, EventTypeId, CustomEventTypeId};
use dom::window::Window;
use js::jsapi::JSContext;
use js::jsval::{JSVal, NullValue};
use servo_util::str::DOMString;

#[deriving(Encodable)]
pub struct CustomEvent {
event: Event,
detail: Traceable<JSVal>
}

impl CustomEventDerived for Event {
fn is_customevent(&self) -> bool {
self.type_id == CustomEventTypeId
}
}

pub trait CustomEventMethods {
fn Detail(&self, _cx: *JSContext) -> JSVal;
fn InitCustomEvent(&mut self, _cx: *JSContext,
type_: DOMString, can_bubble: bool,
cancelable: bool, detail: JSVal);
}

impl CustomEvent {
pub fn new_inherited(type_id: EventTypeId) -> CustomEvent {
CustomEvent {
event: Event::new_inherited(type_id),
detail: Traceable::new(NullValue())
}
}

pub fn new_uninitialized(window: &JSRef<Window>) -> Temporary<CustomEvent> {
reflect_dom_object(box CustomEvent::new_inherited(CustomEventTypeId),
window,
CustomEventBinding::Wrap)
}
pub fn new(window: &JSRef<Window>, type_: DOMString, bubbles: bool, cancelable: bool, detail: JSVal) -> Temporary<CustomEvent> {
let mut ev = CustomEvent::new_uninitialized(window).root();
ev.InitCustomEvent(window.deref().get_cx(), type_, bubbles, cancelable, detail);
Temporary::from_rooted(&*ev)
}
pub fn Constructor(owner: &JSRef<Window>,
type_: DOMString,
init: &CustomEventBinding::CustomEventInit) -> Fallible<Temporary<CustomEvent>>{
Ok(CustomEvent::new(owner, type_, init.parent.bubbles, init.parent.cancelable, init.detail))
}
}

impl<'a> CustomEventMethods for JSRef<'a, CustomEvent> {
fn Detail(&self, _cx: *JSContext) -> JSVal {
self.detail.deref().clone()
}

fn InitCustomEvent(&mut self,
_cx: *JSContext,
type_: DOMString,
can_bubble: bool,
cancelable: bool,
detail: JSVal) {
self.detail = Traceable::new(detail);
let event: &mut JSRef<Event> = EventCast::from_mut_ref(self);
event.InitEvent(type_, can_bubble, cancelable);
}
}

impl Reflectable for CustomEvent {
fn reflector<'a>(&'a self) -> &'a Reflector {
self.event.reflector()
}

fn mut_reflector<'a>(&'a mut self) -> &'a mut Reflector {
self.event.mut_reflector()
}
}
2 changes: 2 additions & 0 deletions src/components/script/dom/document.rs
Expand Up @@ -13,6 +13,7 @@ use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object};
use dom::bindings::error::{ErrorResult, Fallible, NotSupported, InvalidCharacter, HierarchyRequest, NamespaceError};
use dom::bindings::utils::{xml_name_type, InvalidXMLName, Name, QName};
use dom::comment::Comment;
use dom::customevent::CustomEvent;
use dom::documentfragment::DocumentFragment;
use dom::documenttype::DocumentType;
use dom::domimplementation::DOMImplementation;
Expand Down Expand Up @@ -540,6 +541,7 @@ impl<'a> DocumentMethods for JSRef<'a, Document> {
// FIXME: Implement CustomEvent (http://dom.spec.whatwg.org/#customevent)
"uievents" | "uievent" => Ok(EventCast::from_temporary(UIEvent::new_uninitialized(&*window))),
"mouseevents" | "mouseevent" => Ok(EventCast::from_temporary(MouseEvent::new_uninitialized(&*window))),
"customevent" => Ok(EventCast::from_temporary(CustomEvent::new_uninitialized(&*window))),
"htmlevents" | "events" | "event" => Ok(Event::new(&*window)),
_ => Err(NotSupported)
}
Expand Down
7 changes: 4 additions & 3 deletions src/components/script/dom/event.rs
Expand Up @@ -34,11 +34,12 @@ pub enum EventPhase {

#[deriving(Eq, Encodable)]
pub enum EventTypeId {
CustomEventTypeId,
HTMLEventTypeId,
UIEventTypeId,
MouseEventTypeId,
KeyEventTypeId,
ProgressEventTypeId
MouseEventTypeId,
ProgressEventTypeId,
UIEventTypeId
}

#[deriving(Encodable)]
Expand Down
27 changes: 27 additions & 0 deletions src/components/script/dom/webidls/CustomEvent.webidl
@@ -0,0 +1,27 @@
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/.
*
* For more information on this interface please see
* http://dom.spec.whatwg.org/#interface-customevent
*
* To the extent possible under law, the editors have waived
* all copyright and related or neighboring rights to this work.
* In addition, as of 1 May 2014, the editors have made this specification
* available under the Open Web Foundation Agreement Version 1.0,
* which is available at
* http://www.openwebfoundation.org/legal/the-owf-1-0-agreements/owfa-1-0.
*/

[Constructor(DOMString type, optional CustomEventInit eventInitDict),
Exposed=Window,Worker]
interface CustomEvent : Event {
readonly attribute any detail;

void initCustomEvent(DOMString type, boolean bubbles, boolean cancelable, any detail);
};

dictionary CustomEventInit : EventInit {
any detail = null;
};
1 change: 1 addition & 0 deletions src/components/script/script.rs
Expand Up @@ -69,6 +69,7 @@ pub mod dom {
pub mod clientrectlist;
pub mod comment;
pub mod console;
pub mod customevent;
pub mod document;
pub mod documentfragment;
pub mod documenttype;
Expand Down

0 comments on commit c7e8e5a

Please sign in to comment.