/* 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::bindings::codegen::HTMLImageElementBinding; use dom::bindings::codegen::InheritTypes::{NodeCast, HTMLImageElementDerived}; use dom::bindings::codegen::InheritTypes::{ElementCast}; use dom::bindings::js::JS; use dom::bindings::error::ErrorResult; use dom::document::Document; use dom::element::{Element, HTMLImageElementTypeId}; use dom::element::{AttributeHandlers, AfterSetAttrListener, BeforeRemoveAttrListener}; use dom::eventtarget::{EventTarget, NodeTargetTypeId}; use dom::htmlelement::HTMLElement; use dom::node::{Node, ElementNodeTypeId, NodeHelpers, window_from_node}; use servo_util::geometry::to_px; use layout_interface::{ContentBoxQuery, ContentBoxResponse}; use servo_net::image_cache_task; use servo_util::url::parse_url; use servo_util::str::DOMString; use url::Url; use serialize::{Encoder, Encodable}; #[deriving(Encodable)] pub struct HTMLImageElement { htmlelement: HTMLElement, priv extra: Untraceable, } struct Untraceable { image: Option, } impl Encodable for Untraceable { fn encode(&self, _s: &mut S) { } } impl HTMLImageElementDerived for EventTarget { fn is_htmlimageelement(&self) -> bool { match self.type_id { NodeTargetTypeId(ElementNodeTypeId(HTMLImageElementTypeId)) => true, _ => false } } } impl HTMLImageElement { pub fn new_inherited(localName: DOMString, document: JS) -> HTMLImageElement { HTMLImageElement { htmlelement: HTMLElement::new_inherited(HTMLImageElementTypeId, localName, document), extra: Untraceable { image: None, } } } pub fn new(localName: DOMString, document: &JS) -> JS { let element = HTMLImageElement::new_inherited(localName, document.clone()); Node::reflect_node(~element, document, HTMLImageElementBinding::Wrap) } } impl HTMLImageElement { pub fn image<'a>(&'a self) -> &'a Option { &self.extra.image } /// 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(&mut self, value: Option, url: Option) { let elem = &mut self.htmlelement.element; let document = elem.node.owner_doc(); let window = document.get().window.get(); let image_cache = &window.image_cache_task; match value { None => { self.extra.image = None; } Some(src) => { let img_url = parse_url(src, url); self.extra.image = 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 //