Implement window forwarding for body event handlers.

This commit is contained in:
Josh Matthews 2014-04-21 22:43:20 -04:00 committed by Ms2ger
parent 2d6153772c
commit 33955f0ab2
7 changed files with 74 additions and 7 deletions

View file

@ -134,4 +134,3 @@ DOMInterfaces = {
'TestBinding': {},
}

View file

@ -10,7 +10,9 @@ use dom::document::Document;
use dom::element::HTMLBodyElementTypeId;
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::htmlelement::HTMLElement;
use dom::node::{Node, ElementNodeTypeId};
use dom::node::{Node, ElementNodeTypeId, window_from_node};
use dom::window::WindowMethods;
use js::jsapi::{JSContext, JSObject};
use servo_util::str::DOMString;
#[deriving(Encodable)]
@ -50,6 +52,8 @@ pub trait HTMLBodyElementMethods {
fn SetBgColor(&self, _bg_color: DOMString) -> ErrorResult;
fn Background(&self) -> DOMString;
fn SetBackground(&self, _background: DOMString) -> ErrorResult;
fn GetOnunload(&self, cx: *mut JSContext) -> *mut JSObject;
fn SetOnunload(&mut self, cx: *mut JSContext, listener: *mut JSObject);
}
impl<'a> HTMLBodyElementMethods for JSRef<'a, HTMLBodyElement> {
@ -100,4 +104,14 @@ impl<'a> HTMLBodyElementMethods for JSRef<'a, HTMLBodyElement> {
fn SetBackground(&self, _background: DOMString) -> ErrorResult {
Ok(())
}
fn GetOnunload(&self, cx: *mut JSContext) -> *mut JSObject {
let win = window_from_node(self).root();
win.deref().GetOnunload(cx)
}
fn SetOnunload(&mut self, cx: *mut JSContext, listener: *mut JSObject) {
let mut win = window_from_node(self).root();
win.SetOnunload(cx, listener)
}
}

View file

@ -3,19 +3,22 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use dom::bindings::codegen::BindingDeclarations::HTMLElementBinding;
use dom::bindings::codegen::InheritTypes::ElementCast;
use dom::bindings::codegen::InheritTypes::HTMLElementDerived;
use dom::bindings::codegen::InheritTypes::{ElementCast, HTMLFrameSetElementDerived};
use dom::bindings::codegen::InheritTypes::{HTMLElementDerived, HTMLBodyElementDerived};
use dom::bindings::codegen::InheritTypes::EventTargetCast;
use dom::bindings::js::{JSRef, Temporary};
use dom::bindings::error::{ErrorResult, Fallible};
use dom::document::Document;
use dom::element::{Element, ElementTypeId, HTMLElementTypeId};
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::node::{Node, ElementNodeTypeId};
use dom::node::{Node, ElementNodeTypeId, window_from_node};
use dom::virtualmethods::VirtualMethods;
use js::jsapi::JSContext;
use dom::window::WindowMethods;
use js::jsapi::{JSContext, JSObject};
use js::jsval::{JSVal, NullValue};
use servo_util::namespace;
use servo_util::str::DOMString;
use std::ptr;
#[deriving(Encodable)]
pub struct HTMLElement {
@ -45,6 +48,17 @@ impl HTMLElement {
}
}
trait PrivateHTMLElementHelpers {
fn is_body_or_frameset(&self) -> bool;
}
impl<'a> PrivateHTMLElementHelpers for JSRef<'a, HTMLElement> {
fn is_body_or_frameset(&self) -> bool {
let eventtarget: &JSRef<EventTarget> = EventTargetCast::from_ref(self);
eventtarget.is_htmlbodyelement() || eventtarget.is_htmlframesetelement()
}
}
pub trait HTMLElementMethods {
fn Title(&self) -> DOMString;
fn SetTitle(&mut self, _title: DOMString);
@ -76,6 +90,8 @@ pub trait HTMLElementMethods {
fn OffsetLeft(&self) -> i32;
fn OffsetWidth(&self) -> i32;
fn OffsetHeight(&self) -> i32;
fn GetOnload(&self, cx: *mut JSContext) -> *mut JSObject;
fn SetOnload(&mut self, cx: *mut JSContext, listener: *mut JSObject);
}
impl<'a> HTMLElementMethods for JSRef<'a, HTMLElement> {
@ -195,6 +211,22 @@ impl<'a> HTMLElementMethods for JSRef<'a, HTMLElement> {
fn OffsetHeight(&self) -> i32 {
0
}
fn GetOnload(&self, cx: *mut JSContext) -> *mut JSObject {
if self.is_body_or_frameset() {
let win = window_from_node(self).root();
win.deref().GetOnload(cx)
} else {
ptr::mut_null()
}
}
fn SetOnload(&mut self, cx: *mut JSContext, listener: *mut JSObject) {
if self.is_body_or_frameset() {
let mut win = window_from_node(self).root();
win.SetOnload(cx, listener)
}
}
}
impl<'a> VirtualMethods for JSRef<'a, HTMLElement> {

View file

@ -23,4 +23,4 @@ partial interface HTMLBodyElement {
[SetterThrows] attribute DOMString background;
};
//HTMLBodyElement implements WindowEventHandlers;
HTMLBodyElement implements WindowEventHandlers;

View file

@ -54,3 +54,5 @@ partial interface HTMLElement {
readonly attribute long offsetWidth;
readonly attribute long offsetHeight;
};
HTMLElement implements GlobalEventHandlers;
//HTMLElement implements OnErrorEventHandlerForNodes;

View file

@ -0,0 +1,17 @@
<html>
<head>
<script src="harness.js"></script>
</head>
<body>
<script>
document.getElementsByTagName('body')[0].onload = function(ev) {
is_a(ev, Event);
ev.preventDefault();
is(ev.defaultPrevented, false);
is(ev.target, document);
is(ev.currentTarget, window);
finish();
}
</script>
</body>
</html>

View file

@ -16,6 +16,9 @@
}
}
window.onload = function(ev) {
_fail("this inline handler should be overwritten");
}
window.onload = function(ev) {
onloads++;
check(ev);