Refactor some window_from_node calls based on feedback

This commit is contained in:
Arseniy Ivanov 2017-02-03 14:45:55 -05:00
parent 1bd1bddacf
commit 72ec00e0b5
3 changed files with 37 additions and 25 deletions

View file

@ -151,8 +151,9 @@ impl HTMLElementMethods for HTMLElement {
// https://html.spec.whatwg.org/multipage/#handler-onload // https://html.spec.whatwg.org/multipage/#handler-onload
fn GetOnload(&self) -> Option<Rc<EventHandlerNonNull>> { fn GetOnload(&self) -> Option<Rc<EventHandlerNonNull>> {
if self.is_body_or_frameset() { if self.is_body_or_frameset() {
if document_from_node(self).has_browsing_context() { let document = document_from_node(self);
window_from_node(self).GetOnload() if document.has_browsing_context() {
document.window().GetOnload()
} else { } else {
None None
} }
@ -164,8 +165,9 @@ impl HTMLElementMethods for HTMLElement {
// https://html.spec.whatwg.org/multipage/#handler-onload // https://html.spec.whatwg.org/multipage/#handler-onload
fn SetOnload(&self, listener: Option<Rc<EventHandlerNonNull>>) { fn SetOnload(&self, listener: Option<Rc<EventHandlerNonNull>>) {
if self.is_body_or_frameset() { if self.is_body_or_frameset() {
if document_from_node(self).has_browsing_context() { let document = document_from_node(self);
window_from_node(self).SetOnload(listener) if document.has_browsing_context() {
document.window().SetOnload(listener)
} }
} else { } else {
self.upcast::<EventTarget>().set_event_handler_common("load", listener) self.upcast::<EventTarget>().set_event_handler_common("load", listener)
@ -175,8 +177,9 @@ impl HTMLElementMethods for HTMLElement {
// https://html.spec.whatwg.org/multipage/#handler-onresize // https://html.spec.whatwg.org/multipage/#handler-onresize
fn GetOnresize(&self) -> Option<Rc<EventHandlerNonNull>> { fn GetOnresize(&self) -> Option<Rc<EventHandlerNonNull>> {
if self.is_body_or_frameset() { if self.is_body_or_frameset() {
if document_from_node(self).has_browsing_context() { let document = document_from_node(self);
window_from_node(self).GetOnload() if document.has_browsing_context() {
document.window().GetOnload()
} else { } else {
None None
} }
@ -188,8 +191,9 @@ impl HTMLElementMethods for HTMLElement {
// https://html.spec.whatwg.org/multipage/#handler-onresize // https://html.spec.whatwg.org/multipage/#handler-onresize
fn SetOnresize(&self, listener: Option<Rc<EventHandlerNonNull>>) { fn SetOnresize(&self, listener: Option<Rc<EventHandlerNonNull>>) {
if self.is_body_or_frameset() { if self.is_body_or_frameset() {
if document_from_node(self).has_browsing_context() { let document = document_from_node(self);
window_from_node(self).SetOnresize(listener); if document.has_browsing_context() {
document.window().SetOnresize(listener);
} }
} else { } else {
self.upcast::<EventTarget>().set_event_handler_common("resize", listener) self.upcast::<EventTarget>().set_event_handler_common("resize", listener)
@ -199,8 +203,9 @@ impl HTMLElementMethods for HTMLElement {
// https://html.spec.whatwg.org/multipage/#handler-onblur // https://html.spec.whatwg.org/multipage/#handler-onblur
fn GetOnblur(&self) -> Option<Rc<EventHandlerNonNull>> { fn GetOnblur(&self) -> Option<Rc<EventHandlerNonNull>> {
if self.is_body_or_frameset() { if self.is_body_or_frameset() {
if document_from_node(self).has_browsing_context() { let document = document_from_node(self);
window_from_node(self).GetOnblur() if document.has_browsing_context() {
document.window().GetOnblur()
} else { } else {
None None
} }
@ -212,8 +217,9 @@ impl HTMLElementMethods for HTMLElement {
// https://html.spec.whatwg.org/multipage/#handler-onblur // https://html.spec.whatwg.org/multipage/#handler-onblur
fn SetOnblur(&self, listener: Option<Rc<EventHandlerNonNull>>) { fn SetOnblur(&self, listener: Option<Rc<EventHandlerNonNull>>) {
if self.is_body_or_frameset() { if self.is_body_or_frameset() {
if document_from_node(self).has_browsing_context() { let document = document_from_node(self);
window_from_node(self).SetOnblur(listener) if document.has_browsing_context() {
document.window().SetOnblur(listener)
} }
} else { } else {
self.upcast::<EventTarget>().set_event_handler_common("blur", listener) self.upcast::<EventTarget>().set_event_handler_common("blur", listener)
@ -223,8 +229,9 @@ impl HTMLElementMethods for HTMLElement {
// https://html.spec.whatwg.org/multipage/#handler-onfocus // https://html.spec.whatwg.org/multipage/#handler-onfocus
fn GetOnfocus(&self) -> Option<Rc<EventHandlerNonNull>> { fn GetOnfocus(&self) -> Option<Rc<EventHandlerNonNull>> {
if self.is_body_or_frameset() { if self.is_body_or_frameset() {
if document_from_node(self).has_browsing_context() { let document = document_from_node(self);
window_from_node(self).GetOnfocus() if document.has_browsing_context() {
document.window().GetOnfocus()
} else { } else {
None None
} }
@ -236,8 +243,9 @@ impl HTMLElementMethods for HTMLElement {
// https://html.spec.whatwg.org/multipage/#handler-onfocus // https://html.spec.whatwg.org/multipage/#handler-onfocus
fn SetOnfocus(&self, listener: Option<Rc<EventHandlerNonNull>>) { fn SetOnfocus(&self, listener: Option<Rc<EventHandlerNonNull>>) {
if self.is_body_or_frameset() { if self.is_body_or_frameset() {
if document_from_node(self).has_browsing_context() { let document = document_from_node(self);
window_from_node(self).SetOnfocus(listener) if document.has_browsing_context() {
document.window().SetOnfocus(listener)
} }
} else { } else {
self.upcast::<EventTarget>().set_event_handler_common("focus", listener) self.upcast::<EventTarget>().set_event_handler_common("focus", listener)
@ -247,8 +255,9 @@ impl HTMLElementMethods for HTMLElement {
// https://html.spec.whatwg.org/multipage/#handler-onscroll // https://html.spec.whatwg.org/multipage/#handler-onscroll
fn GetOnscroll(&self) -> Option<Rc<EventHandlerNonNull>> { fn GetOnscroll(&self) -> Option<Rc<EventHandlerNonNull>> {
if self.is_body_or_frameset() { if self.is_body_or_frameset() {
if document_from_node(self).has_browsing_context() { let document = document_from_node(self);
window_from_node(self).GetOnscroll() if document.has_browsing_context() {
document.window().GetOnscroll()
} else { } else {
None None
} }
@ -260,8 +269,9 @@ impl HTMLElementMethods for HTMLElement {
// https://html.spec.whatwg.org/multipage/#handler-onscroll // https://html.spec.whatwg.org/multipage/#handler-onscroll
fn SetOnscroll(&self, listener: Option<Rc<EventHandlerNonNull>>) { fn SetOnscroll(&self, listener: Option<Rc<EventHandlerNonNull>>) {
if self.is_body_or_frameset() { if self.is_body_or_frameset() {
if document_from_node(self).has_browsing_context() { let document = document_from_node(self);
window_from_node(self).SetOnscroll(listener) if document.has_browsing_context() {
document.window().SetOnscroll(listener)
} }
} else { } else {
self.upcast::<EventTarget>().set_event_handler_common("scroll", listener) self.upcast::<EventTarget>().set_event_handler_common("scroll", listener)

View file

@ -10,7 +10,7 @@ use dom::bindings::js::Root;
use dom::bindings::str::DOMString; use dom::bindings::str::DOMString;
use dom::document::Document; use dom::document::Document;
use dom::htmlelement::HTMLElement; use dom::htmlelement::HTMLElement;
use dom::node::{Node, document_from_node, window_from_node}; use dom::node::{Node, document_from_node};
use html5ever_atoms::LocalName; use html5ever_atoms::LocalName;
#[dom_struct] #[dom_struct]

View file

@ -354,16 +354,18 @@ macro_rules! define_event_handler(
macro_rules! define_window_owned_event_handler( macro_rules! define_window_owned_event_handler(
($handler: ident, $event_type: ident, $getter: ident, $setter: ident) => ( ($handler: ident, $event_type: ident, $getter: ident, $setter: ident) => (
fn $getter(&self) -> Option<::std::rc::Rc<$handler>> { fn $getter(&self) -> Option<::std::rc::Rc<$handler>> {
if document_from_node(self).has_browsing_context() { let document = document_from_node(self);
window_from_node(self).$getter() if document.has_browsing_context() {
document.window().$getter()
} else { } else {
None None
} }
} }
fn $setter(&self, listener: Option<::std::rc::Rc<$handler>>) { fn $setter(&self, listener: Option<::std::rc::Rc<$handler>>) {
if document_from_node(self).has_browsing_context() { let document = document_from_node(self);
window_from_node(self).$setter(listener) if document.has_browsing_context() {
document.window().$setter(listener)
} }
} }
) )