diff options
author | Anthony Ramine <n.oxyde@gmail.com> | 2015-08-28 13:32:38 +0200 |
---|---|---|
committer | Anthony Ramine <n.oxyde@gmail.com> | 2015-09-02 15:45:38 +0200 |
commit | 58e1bd0e57a3c69307e35a25ed23af3dedf05c50 (patch) | |
tree | 4a37e912de8a15479cc4b97024eb47b5b77b8c9e /components/script/dom/htmlimageelement.rs | |
parent | 5672142042973bdb8e208357a702bb4a93b94658 (diff) | |
download | servo-58e1bd0e57a3c69307e35a25ed23af3dedf05c50.tar.gz servo-58e1bd0e57a3c69307e35a25ed23af3dedf05c50.zip |
Introduce VirtualMethods::attribute_mutated()
This replaces before_remove_attr(), after_remove_attr() and after_set_attr().
The virtual method takes the mutated attribute and an AttributeMutation value
to disambiguate between "attribute is changed", "attribute is added" and
"attribute is removed".
In the case of "attribute is changed", the mutation value contains a reference
to the old value of the mutated attribute, which is used to unregister outdated
named elements when the "id" attribute is changed on an element.
This greatly simplifies the handling of attributes, which in many cases don't
have any specific behaviour whether they are removed or changed or added. It
also fixes a few bugs where things were put in before_remove_attr() instead of
after_remove_attr() (e.g. when removing an href attribute from a base element).
A few helper functions in Element were also renamed and made private.
Diffstat (limited to 'components/script/dom/htmlimageelement.rs')
-rw-r--r-- | components/script/dom/htmlimageelement.rs | 34 |
1 files changed, 10 insertions, 24 deletions
diff --git a/components/script/dom/htmlimageelement.rs b/components/script/dom/htmlimageelement.rs index dcb69a44104..fe811bf2cf8 100644 --- a/components/script/dom/htmlimageelement.rs +++ b/components/script/dom/htmlimageelement.rs @@ -15,7 +15,7 @@ use dom::bindings::global::GlobalRef; use dom::bindings::js::{LayoutJS, Root}; use dom::bindings::refcounted::Trusted; use dom::document::Document; -use dom::element::ElementTypeId; +use dom::element::{AttributeMutation, ElementTypeId}; use dom::event::{Event, EventBubbles, EventCancelable}; use dom::eventtarget::{EventTarget, EventTargetTypeId}; use dom::htmlelement::{HTMLElement, HTMLElementTypeId}; @@ -108,7 +108,7 @@ impl Runnable for ImageResponseHandlerRunnable { impl HTMLImageElement { /// Makes the local `image` member match the status of the `src` attribute and starts /// prefetching the image. This method must be called after `src` is changed. - fn update_image(&self, value: Option<(DOMString, &Url)>) { + fn update_image(&self, value: Option<(DOMString, Url)>) { let node = NodeCast::from_ref(self); let document = node.owner_doc(); let window = document.r().window(); @@ -120,7 +120,7 @@ impl HTMLImageElement { *self.image.borrow_mut() = None; } Some((src, base_url)) => { - let img_url = UrlParser::new().base_url(base_url).parse(&src); + let img_url = UrlParser::new().base_url(&base_url).parse(&src); // FIXME: handle URL parse errors more gracefully. let img_url = img_url.unwrap(); *self.url.borrow_mut() = Some(img_url.clone()); @@ -300,29 +300,15 @@ impl VirtualMethods for HTMLImageElement { Some(htmlelement as &VirtualMethods) } - fn after_set_attr(&self, attr: &Attr) { - if let Some(ref s) = self.super_type() { - s.after_set_attr(attr); - } - + fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation) { + self.super_type().unwrap().attribute_mutated(attr, mutation); match attr.local_name() { - &atom!("src") => { - let window = window_from_node(self); - let url = window.r().get_url(); - self.update_image(Some(((**attr.value()).to_owned(), &url))); + &atom!(src) => { + self.update_image(mutation.new_value(attr).map(|value| { + ((**value).to_owned(), window_from_node(self).get_url()) + })); }, - _ => () - } - } - - fn before_remove_attr(&self, attr: &Attr) { - if let Some(ref s) = self.super_type() { - s.before_remove_attr(attr); - } - - match attr.local_name() { - &atom!("src") => self.update_image(None), - _ => () + _ => {}, } } |