From 32fb90738402960116d05af5792755f9ec37d1ab Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Mon, 18 Aug 2014 20:23:19 +0530 Subject: [PATCH 1/3] Create macros for simplifying element attribute getters (fixes #1931) --- src/components/script/dom/macros.rs | 47 +++++++++++++++++++++++++++++ src/components/script/script.rs | 1 + 2 files changed, 48 insertions(+) create mode 100644 src/components/script/dom/macros.rs diff --git a/src/components/script/dom/macros.rs b/src/components/script/dom/macros.rs new file mode 100644 index 00000000000..bfc5d99ec97 --- /dev/null +++ b/src/components/script/dom/macros.rs @@ -0,0 +1,47 @@ +/* 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/. */ + +#![macro_escape] + +#[macro_export] +macro_rules! make_getters( + ( $($attr:ident),+ ) => ( + $( + fn $attr(&self) -> DOMString { + use dom::element::{Element, AttributeHandlers}; + use dom::bindings::codegen::InheritTypes::ElementCast; + let element: &JSRef = ElementCast::from_ref(self); + element.get_string_attribute(stringify!($attr)) + } + )+ + ); +) + +#[macro_export] +macro_rules! make_bool_getters( + ( $($attr:ident),+ ) => ( + $( + fn $attr(&self) -> bool { + use dom::element::{Element, AttributeHandlers}; + use dom::bindings::codegen::InheritTypes::ElementCast; + let element: &JSRef = ElementCast::from_ref(self); + element.has_attribute(stringify!($attr)) + } + )+ + ); +) + +#[macro_export] +macro_rules! make_uint_getters( + ( $($attr:ident),+ ) => ( + $( + fn $attr(&self) -> u32 { + use dom::element::{Element, AttributeHandlers}; + use dom::bindings::codegen::InheritTypes::ElementCast; + let element: &JSRef = ElementCast::from_ref(self); + element.get_uint_attribute(stringify!($attr)) + } + )+ + ); +) \ No newline at end of file diff --git a/src/components/script/script.rs b/src/components/script/script.rs index 513286fae8e..f912dec518f 100644 --- a/src/components/script/script.rs +++ b/src/components/script/script.rs @@ -73,6 +73,7 @@ pub mod dom { #[path="bindings/codegen/InterfaceTypes.rs"] pub mod types; + pub mod macros; pub mod attr; pub mod attrlist; From 738ee342b30b2808b30db31c7f599ea17b95c123 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Tue, 19 Aug 2014 23:14:12 +0530 Subject: [PATCH 2/3] Use getter macros in HTMLImageElement --- src/components/script/dom/element.rs | 1 - src/components/script/dom/htmlimageelement.rs | 72 +++---------------- 2 files changed, 8 insertions(+), 65 deletions(-) diff --git a/src/components/script/dom/element.rs b/src/components/script/dom/element.rs index 425529fe6d4..0a84e4e2c39 100644 --- a/src/components/script/dom/element.rs +++ b/src/components/script/dom/element.rs @@ -435,7 +435,6 @@ impl<'a> AttributeHandlers for JSRef<'a, Element> { } fn get_uint_attribute(&self, name: &str) -> u32 { - assert!(name == name.to_ascii_lower().as_slice()); let attribute = self.get_attribute(Null, name).root(); match attribute { Some(attribute) => { diff --git a/src/components/script/dom/htmlimageelement.rs b/src/components/script/dom/htmlimageelement.rs index 73fc3d07646..3da178cef7c 100644 --- a/src/components/script/dom/htmlimageelement.rs +++ b/src/components/script/dom/htmlimageelement.rs @@ -96,123 +96,67 @@ impl LayoutHTMLImageElementHelpers for JS { } impl<'a> HTMLImageElementMethods for JSRef<'a, HTMLImageElement> { - fn Alt(&self) -> DOMString { - let element: &JSRef = ElementCast::from_ref(self); - element.get_string_attribute("alt") - } + + make_getters!(Alt, Src, UseMap, Name, Align, LongDesc, Border) + make_bool_getters!(IsMap) + make_uint_getters!(Width, Height, Hspace, Vspace) fn SetAlt(&self, alt: DOMString) { let element: &JSRef = ElementCast::from_ref(self); element.set_string_attribute("alt", alt) } - fn Src(&self) -> DOMString { - let element: &JSRef = ElementCast::from_ref(self); - element.get_string_attribute("src") - } - fn SetSrc(&self, src: DOMString) { let element: &JSRef = ElementCast::from_ref(self); element.set_url_attribute("src", src) } - fn UseMap(&self) -> DOMString { - let element: &JSRef = ElementCast::from_ref(self); - element.get_string_attribute("useMap") - } - fn SetUseMap(&self, use_map: DOMString) { let element: &JSRef = ElementCast::from_ref(self); element.set_string_attribute("useMap", use_map) } - fn IsMap(&self) -> bool { - let element: &JSRef = ElementCast::from_ref(self); - from_str::(element.get_string_attribute("hspace").as_slice()).unwrap() - } - fn SetIsMap(&self, is_map: bool) { let element: &JSRef = ElementCast::from_ref(self); element.set_string_attribute("isMap", is_map.to_string()) } - fn Width(&self) -> u32 { - let node: &JSRef = NodeCast::from_ref(self); - let rect = node.get_bounding_content_box(); - to_px(rect.size.width) as u32 - } fn SetWidth(&self, width: u32) { let elem: &JSRef = ElementCast::from_ref(self); elem.set_uint_attribute("width", width) } - fn Height(&self) -> u32 { - let node: &JSRef = NodeCast::from_ref(self); - let rect = node.get_bounding_content_box(); - to_px(rect.size.height) as u32 - } - fn SetHeight(&self, height: u32) { let elem: &JSRef = ElementCast::from_ref(self); elem.set_uint_attribute("height", height) } - fn Name(&self) -> DOMString { - let element: &JSRef = ElementCast::from_ref(self); - element.get_string_attribute("name") - } - fn SetName(&self, name: DOMString) { let element: &JSRef = ElementCast::from_ref(self); element.set_string_attribute("name", name) } - fn Align(&self) -> DOMString { - let element: &JSRef = ElementCast::from_ref(self); - element.get_string_attribute("align") - } - fn SetAlign(&self, align: DOMString) { let element: &JSRef = ElementCast::from_ref(self); element.set_string_attribute("align", align) } - fn Hspace(&self) -> u32 { - let element: &JSRef = ElementCast::from_ref(self); - element.get_uint_attribute("hspace") - } - fn SetHspace(&self, hspace: u32) { let element: &JSRef = ElementCast::from_ref(self); element.set_uint_attribute("hspace", hspace) } - fn Vspace(&self) -> u32 { - let element: &JSRef = ElementCast::from_ref(self); - element.get_uint_attribute("vspace") - } - fn SetVspace(&self, vspace: u32) { let element: &JSRef = ElementCast::from_ref(self); element.set_uint_attribute("vspace", vspace) } - fn LongDesc(&self) -> DOMString { - let element: &JSRef = ElementCast::from_ref(self); - element.get_string_attribute("longdesc") - } - fn SetLongDesc(&self, longdesc: DOMString) { let element: &JSRef = ElementCast::from_ref(self); element.set_string_attribute("longdesc", longdesc) } - fn Border(&self) -> DOMString { - let element: &JSRef = ElementCast::from_ref(self); - element.get_string_attribute("border") - } - fn SetBorder(&self, border: DOMString) { let element: &JSRef = ElementCast::from_ref(self); element.set_string_attribute("border", border) @@ -225,9 +169,9 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLImageElement> { Some(htmlelement as &VirtualMethods) } - fn after_set_attr(&self, name: &Atom, value: DOMString) { + fn after_set_attr(&self, name: DOMString, value: DOMString) { match self.super_type() { - Some(ref s) => s.after_set_attr(name, value.clone()), + Some(ref s) => s.after_set_attr(name.clone(), value.clone()), _ => (), } @@ -238,9 +182,9 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLImageElement> { } } - fn before_remove_attr(&self, name: &Atom, value: DOMString) { + fn before_remove_attr(&self, name: DOMString, value: DOMString) { match self.super_type() { - Some(ref s) => s.before_remove_attr(name, value.clone()), + Some(ref s) => s.before_remove_attr(name.clone(), value.clone()), _ => (), } From 0c743483ea323b5e54fbaedf90dcd8cc321aaf45 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Tue, 19 Aug 2014 23:14:51 +0530 Subject: [PATCH 3/3] Split up macro invocation --- src/components/script/dom/element.rs | 1 + src/components/script/dom/htmlimageelement.rs | 43 +++++++++++--- src/components/script/dom/macros.rs | 59 +++++++++---------- 3 files changed, 63 insertions(+), 40 deletions(-) diff --git a/src/components/script/dom/element.rs b/src/components/script/dom/element.rs index 0a84e4e2c39..425529fe6d4 100644 --- a/src/components/script/dom/element.rs +++ b/src/components/script/dom/element.rs @@ -435,6 +435,7 @@ impl<'a> AttributeHandlers for JSRef<'a, Element> { } fn get_uint_attribute(&self, name: &str) -> u32 { + assert!(name == name.to_ascii_lower().as_slice()); let attribute = self.get_attribute(Null, name).root(); match attribute { Some(attribute) => { diff --git a/src/components/script/dom/htmlimageelement.rs b/src/components/script/dom/htmlimageelement.rs index 3da178cef7c..6717711292e 100644 --- a/src/components/script/dom/htmlimageelement.rs +++ b/src/components/script/dom/htmlimageelement.rs @@ -16,7 +16,6 @@ use dom::eventtarget::{EventTarget, NodeTargetTypeId}; use dom::htmlelement::HTMLElement; use dom::node::{Node, ElementNodeTypeId, NodeHelpers, window_from_node}; use dom::virtualmethods::VirtualMethods; - use servo_net::image_cache_task; use servo_util::atom::Atom; use servo_util::geometry::to_px; @@ -96,67 +95,93 @@ impl LayoutHTMLImageElementHelpers for JS { } impl<'a> HTMLImageElementMethods for JSRef<'a, HTMLImageElement> { - - make_getters!(Alt, Src, UseMap, Name, Align, LongDesc, Border) - make_bool_getters!(IsMap) - make_uint_getters!(Width, Height, Hspace, Vspace) + make_getter!(Alt) fn SetAlt(&self, alt: DOMString) { let element: &JSRef = ElementCast::from_ref(self); element.set_string_attribute("alt", alt) } + make_getter!(Src) + fn SetSrc(&self, src: DOMString) { let element: &JSRef = ElementCast::from_ref(self); element.set_url_attribute("src", src) } + make_getter!(UseMap) + fn SetUseMap(&self, use_map: DOMString) { let element: &JSRef = ElementCast::from_ref(self); element.set_string_attribute("useMap", use_map) } + make_bool_getter!(IsMap) + fn SetIsMap(&self, is_map: bool) { let element: &JSRef = ElementCast::from_ref(self); element.set_string_attribute("isMap", is_map.to_string()) } + fn Width(&self) -> u32 { + let node: &JSRef = NodeCast::from_ref(self); + let rect = node.get_bounding_content_box(); + to_px(rect.size.width) as u32 + } fn SetWidth(&self, width: u32) { let elem: &JSRef = ElementCast::from_ref(self); elem.set_uint_attribute("width", width) } + fn Height(&self) -> u32 { + let node: &JSRef = NodeCast::from_ref(self); + let rect = node.get_bounding_content_box(); + to_px(rect.size.height) as u32 + } + fn SetHeight(&self, height: u32) { let elem: &JSRef = ElementCast::from_ref(self); elem.set_uint_attribute("height", height) } + make_getter!(Name) + fn SetName(&self, name: DOMString) { let element: &JSRef = ElementCast::from_ref(self); element.set_string_attribute("name", name) } + make_getter!(Align) + fn SetAlign(&self, align: DOMString) { let element: &JSRef = ElementCast::from_ref(self); element.set_string_attribute("align", align) } + make_uint_getter!(Hspace) + fn SetHspace(&self, hspace: u32) { let element: &JSRef = ElementCast::from_ref(self); element.set_uint_attribute("hspace", hspace) } + make_uint_getter!(Vspace) + fn SetVspace(&self, vspace: u32) { let element: &JSRef = ElementCast::from_ref(self); element.set_uint_attribute("vspace", vspace) } + make_getter!(LongDesc) + fn SetLongDesc(&self, longdesc: DOMString) { let element: &JSRef = ElementCast::from_ref(self); element.set_string_attribute("longdesc", longdesc) } + make_getter!(Border) + fn SetBorder(&self, border: DOMString) { let element: &JSRef = ElementCast::from_ref(self); element.set_string_attribute("border", border) @@ -169,9 +194,9 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLImageElement> { Some(htmlelement as &VirtualMethods) } - fn after_set_attr(&self, name: DOMString, value: DOMString) { + fn after_set_attr(&self, name: &Atom, value: DOMString) { match self.super_type() { - Some(ref s) => s.after_set_attr(name.clone(), value.clone()), + Some(ref s) => s.after_set_attr(name, value.clone()), _ => (), } @@ -182,9 +207,9 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLImageElement> { } } - fn before_remove_attr(&self, name: DOMString, value: DOMString) { + fn before_remove_attr(&self, name: &Atom, value: DOMString) { match self.super_type() { - Some(ref s) => s.before_remove_attr(name.clone(), value.clone()), + Some(ref s) => s.before_remove_attr(name, value.clone()), _ => (), } diff --git a/src/components/script/dom/macros.rs b/src/components/script/dom/macros.rs index bfc5d99ec97..6cfca77593d 100644 --- a/src/components/script/dom/macros.rs +++ b/src/components/script/dom/macros.rs @@ -5,43 +5,40 @@ #![macro_escape] #[macro_export] -macro_rules! make_getters( - ( $($attr:ident),+ ) => ( - $( - fn $attr(&self) -> DOMString { - use dom::element::{Element, AttributeHandlers}; - use dom::bindings::codegen::InheritTypes::ElementCast; - let element: &JSRef = ElementCast::from_ref(self); - element.get_string_attribute(stringify!($attr)) - } - )+ +macro_rules! make_getter( + ( $attr:ident ) => ( + fn $attr(&self) -> DOMString { + use dom::element::{Element, AttributeHandlers}; + use dom::bindings::codegen::InheritTypes::ElementCast; + use std::ascii::StrAsciiExt; + let element: &JSRef = ElementCast::from_ref(self); + element.get_string_attribute(stringify!($attr).to_ascii_lower().as_slice()) + } ); ) #[macro_export] -macro_rules! make_bool_getters( - ( $($attr:ident),+ ) => ( - $( - fn $attr(&self) -> bool { - use dom::element::{Element, AttributeHandlers}; - use dom::bindings::codegen::InheritTypes::ElementCast; - let element: &JSRef = ElementCast::from_ref(self); - element.has_attribute(stringify!($attr)) - } - )+ +macro_rules! make_bool_getter( + ( $attr:ident ) => ( + fn $attr(&self) -> bool { + use dom::element::{Element, AttributeHandlers}; + use dom::bindings::codegen::InheritTypes::ElementCast; + use std::ascii::StrAsciiExt; + let element: &JSRef = ElementCast::from_ref(self); + element.has_attribute(stringify!($attr).to_ascii_lower().as_slice()) + } ); ) #[macro_export] -macro_rules! make_uint_getters( - ( $($attr:ident),+ ) => ( - $( - fn $attr(&self) -> u32 { - use dom::element::{Element, AttributeHandlers}; - use dom::bindings::codegen::InheritTypes::ElementCast; - let element: &JSRef = ElementCast::from_ref(self); - element.get_uint_attribute(stringify!($attr)) - } - )+ +macro_rules! make_uint_getter( + ( $attr:ident ) => ( + fn $attr(&self) -> u32 { + use dom::element::{Element, AttributeHandlers}; + use dom::bindings::codegen::InheritTypes::ElementCast; + use std::ascii::StrAsciiExt; + let element: &JSRef = ElementCast::from_ref(self); + element.get_uint_attribute(stringify!($attr).to_ascii_lower().as_slice()) + } ); -) \ No newline at end of file +)