/* 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/. */ use dom::attr::AttrValue; use dom::bindings::codegen::Bindings::HTMLImageElementBinding; use dom::bindings::codegen::Bindings::HTMLImageElementBinding::HTMLImageElementMethods; use dom::bindings::codegen::InheritTypes::{NodeCast, ElementCast, HTMLElementCast, HTMLImageElementDerived}; use dom::bindings::js::{JS, JSRef, Temporary}; use dom::bindings::trace::Untraceable; use dom::bindings::utils::{Reflectable, Reflector}; use dom::document::Document; use dom::element::{Element, HTMLImageElementTypeId}; use dom::element::AttributeHandlers; 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; use servo_util::str::DOMString; use url::{Url, UrlParser}; use std::cell::RefCell; #[deriving(Encodable)] #[must_root] pub struct HTMLImageElement { pub htmlelement: HTMLElement, image: Untraceable>>, } impl HTMLImageElementDerived for EventTarget { fn is_htmlimageelement(&self) -> bool { self.type_id == NodeTargetTypeId(ElementNodeTypeId(HTMLImageElementTypeId)) } } trait PrivateHTMLImageElementHelpers { fn update_image(&self, value: Option<(DOMString, &Url)>); } impl<'a> PrivateHTMLImageElementHelpers for JSRef<'a, 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)>) { let node: JSRef = NodeCast::from_ref(*self); let document = node.owner_doc().root(); let window = document.deref().window.root(); let image_cache = &window.image_cache_task; match value { None => { *self.image.deref().borrow_mut() = None; } Some((src, base_url)) => { let img_url = UrlParser::new().base_url(base_url).parse(src.as_slice()); // FIXME: handle URL parse errors more gracefully. let img_url = img_url.unwrap(); *self.image.deref().borrow_mut() = Some(img_url.clone()); // inform the image cache to load this, but don't store a // handle. // // TODO (Issue #84): don't prefetch if we are within a //