Auto merge of #7452 - nox:cleanup-attributes, r=nox

Introduce VirtualMethods::attribute_mutated()



<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/7452)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2015-09-02 08:14:33 -06:00
commit eaf90c0b1c
33 changed files with 634 additions and 943 deletions

View file

@ -12,7 +12,7 @@ use dom::bindings::codegen::InheritTypes::{HTMLBodyElementDerived, HTMLElementCa
use dom::bindings::js::Root;
use dom::bindings::utils::Reflectable;
use dom::document::Document;
use dom::element::ElementTypeId;
use dom::element::{AttributeMutation, ElementTypeId};
use dom::eventtarget::{EventTarget, EventTargetTypeId};
use dom::htmlelement::{HTMLElement, HTMLElementTypeId};
use dom::node::{Node, NodeTypeId, window_from_node, document_from_node};
@ -125,56 +125,40 @@ impl VirtualMethods for HTMLBodyElement {
chan.send(event).unwrap();
}
fn after_set_attr(&self, attr: &Attr) {
if let Some(ref s) = self.super_type() {
s.after_set_attr(attr);
}
let name = attr.local_name();
if name.starts_with("on") {
static FORWARDED_EVENTS: &'static [&'static str] =
&["onfocus", "onload", "onscroll", "onafterprint", "onbeforeprint",
"onbeforeunload", "onhashchange", "onlanguagechange", "onmessage",
"onoffline", "ononline", "onpagehide", "onpageshow", "onpopstate",
"onstorage", "onresize", "onunload", "onerror"];
let window = window_from_node(self);
let (cx, url, reflector) = (window.r().get_cx(),
window.r().get_url(),
window.r().reflector().get_jsobject());
let evtarget =
if FORWARDED_EVENTS.iter().any(|&event| &**name == event) {
EventTargetCast::from_ref(window.r())
} else {
EventTargetCast::from_ref(self)
};
evtarget.set_event_handler_uncompiled(cx, url, reflector,
&name[2..],
(**attr.value()).to_owned());
}
match attr.local_name() {
&atom!("bgcolor") => {
self.background_color.set(str::parse_legacy_color(&attr.value()).ok())
}
&atom!("background") => {
let doc = document_from_node(self);
let base = doc.r().url();
*self.background.borrow_mut() = UrlParser::new().base_url(&base).parse(&attr.value()).ok();
}
_ => {}
}
}
fn before_remove_attr(&self, attr: &Attr) {
match self.super_type() {
Some(ref s) => s.before_remove_attr(attr),
_ => {}
}
match attr.local_name() {
&atom!("bgcolor") => self.background_color.set(None),
&atom!("background") => *self.background.borrow_mut() = None,
fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation) {
self.super_type().unwrap().attribute_mutated(attr, mutation);
match (attr.local_name(), mutation) {
(&atom!(bgcolor), _) => {
self.background_color.set(mutation.new_value(attr).and_then(|value| {
str::parse_legacy_color(&value).ok()
}));
},
(&atom!(background), _) => {
*self.background.borrow_mut() = mutation.new_value(attr).and_then(|value| {
let base = document_from_node(self).url();
UrlParser::new().base_url(&base).parse(&value).ok()
});
},
(name, AttributeMutation::Set(_)) if name.starts_with("on") => {
static FORWARDED_EVENTS: &'static [&'static str] =
&["onfocus", "onload", "onscroll", "onafterprint", "onbeforeprint",
"onbeforeunload", "onhashchange", "onlanguagechange", "onmessage",
"onoffline", "ononline", "onpagehide", "onpageshow", "onpopstate",
"onstorage", "onresize", "onunload", "onerror"];
let window = window_from_node(self);
let (cx, url, reflector) = (window.get_cx(),
window.get_url(),
window.reflector().get_jsobject());
let evtarget =
if FORWARDED_EVENTS.iter().any(|&event| &**name == event) {
EventTargetCast::from_ref(window.r())
} else {
EventTargetCast::from_ref(self)
};
evtarget.set_event_handler_uncompiled(cx, url, reflector,
&name[2..],
(**attr.value()).to_owned());
},
_ => {}
}
}