mirror of
https://github.com/servo/servo.git
synced 2025-08-05 05:30:08 +01:00
Implement JSManaged for DOM objects.
This commit is contained in:
parent
061269f963
commit
625325434b
137 changed files with 3644 additions and 2778 deletions
|
@ -3,11 +3,15 @@
|
|||
* 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::utils::ErrorResult;
|
||||
use dom::document::AbstractDocument;
|
||||
use dom::element::HTMLImageElementTypeId;
|
||||
use dom::document::Document;
|
||||
use dom::element::{Element, HTMLImageElementTypeId};
|
||||
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
|
||||
use dom::htmlelement::HTMLElement;
|
||||
use dom::node::{AbstractNode, Node};
|
||||
use dom::node::{Node, ElementNodeTypeId, NodeHelpers};
|
||||
use extra::url::Url;
|
||||
use servo_util::geometry::to_px;
|
||||
use layout_interface::{ContentBoxQuery, ContentBoxResponse};
|
||||
|
@ -17,22 +21,45 @@ use servo_util::url::parse_url;
|
|||
use servo_util::namespace::Null;
|
||||
use servo_util::str::DOMString;
|
||||
|
||||
use extra::serialize::{Encoder, Encodable};
|
||||
|
||||
#[deriving(Encodable)]
|
||||
pub struct HTMLImageElement {
|
||||
htmlelement: HTMLElement,
|
||||
extra: Untraceable,
|
||||
}
|
||||
|
||||
struct Untraceable {
|
||||
image: Option<Url>,
|
||||
}
|
||||
|
||||
impl<S: Encoder> Encodable<S> 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: AbstractDocument) -> HTMLImageElement {
|
||||
pub fn new_inherited(localName: DOMString, document: JS<Document>) -> HTMLImageElement {
|
||||
HTMLImageElement {
|
||||
htmlelement: HTMLElement::new_inherited(HTMLImageElementTypeId, localName, document),
|
||||
image: None,
|
||||
extra: Untraceable {
|
||||
image: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new(localName: DOMString, document: AbstractDocument) -> AbstractNode {
|
||||
let element = HTMLImageElement::new_inherited(localName, document);
|
||||
Node::reflect_node(@mut element, document, HTMLImageElementBinding::Wrap)
|
||||
pub fn new(localName: DOMString, document: &JS<Document>) -> JS<HTMLImageElement> {
|
||||
let element = HTMLImageElement::new_inherited(localName, document.clone());
|
||||
Node::reflect_node(~element, document, HTMLImageElementBinding::Wrap)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,12 +68,12 @@ impl HTMLImageElement {
|
|||
/// prefetching the image. This method must be called after `src` is changed.
|
||||
pub fn update_image(&mut self, image_cache: ImageCacheTask, url: Option<Url>) {
|
||||
let elem = &mut self.htmlelement.element;
|
||||
let src_opt = elem.get_attribute(Null, "src").map(|x| x.Value());
|
||||
let src_opt = elem.get_attribute(Null, "src").map(|x| x.get().Value());
|
||||
match src_opt {
|
||||
None => {}
|
||||
Some(src) => {
|
||||
let img_url = parse_url(src, url);
|
||||
self.image = Some(img_url.clone());
|
||||
self.extra.image = Some(img_url.clone());
|
||||
|
||||
// inform the image cache to load this, but don't store a
|
||||
// handle.
|
||||
|
@ -61,7 +88,7 @@ impl HTMLImageElement {
|
|||
pub fn AfterSetAttr(&mut self, name: DOMString, _value: DOMString) {
|
||||
if "src" == name {
|
||||
let document = self.htmlelement.element.node.owner_doc();
|
||||
let window = document.document().window;
|
||||
let window = document.get().window.get();
|
||||
let url = window.page.url.as_ref().map(|&(ref url, _)| url.clone());
|
||||
self.update_image(window.image_cache_task.clone(), url);
|
||||
}
|
||||
|
@ -73,7 +100,7 @@ impl HTMLImageElement {
|
|||
// `self.update_image()` will see the missing src attribute and return early.
|
||||
if "src" == name {
|
||||
let document = self.htmlelement.element.node.owner_doc();
|
||||
let window = document.document().window;
|
||||
let window = document.get().window.get();
|
||||
self.update_image(window.image_cache_task.clone(), None);
|
||||
}
|
||||
}
|
||||
|
@ -86,13 +113,13 @@ impl HTMLImageElement {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn Src(&self, _abstract_self: AbstractNode) -> DOMString {
|
||||
pub fn Src(&self, _abstract_self: &JS<HTMLImageElement>) -> DOMString {
|
||||
~""
|
||||
}
|
||||
|
||||
pub fn SetSrc(&mut self, abstract_self: AbstractNode, src: DOMString) -> ErrorResult {
|
||||
pub fn SetSrc(&mut self, abstract_self: &JS<HTMLImageElement>, src: DOMString) -> ErrorResult {
|
||||
let node = &mut self.htmlelement.element;
|
||||
node.set_attr(abstract_self, ~"src", src.clone());
|
||||
node.set_attr(&ElementCast::from(abstract_self), ~"src", src.clone());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -120,37 +147,43 @@ impl HTMLImageElement {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn Width(&self, abstract_self: AbstractNode) -> u32 {
|
||||
let node = &self.htmlelement.element.node;
|
||||
let page = node.owner_doc().document().window.page;
|
||||
pub fn Width(&self, abstract_self: &JS<HTMLImageElement>) -> u32 {
|
||||
let node: JS<Node> = NodeCast::from(abstract_self);
|
||||
let doc = node.get().owner_doc();
|
||||
let page = doc.get().window.get().page;
|
||||
let (port, chan) = Chan::new();
|
||||
match page.query_layout(ContentBoxQuery(abstract_self, chan), port) {
|
||||
let addr = node.to_trusted_node_address();
|
||||
match page.query_layout(ContentBoxQuery(addr, chan), port) {
|
||||
ContentBoxResponse(rect) => {
|
||||
to_px(rect.size.width) as u32
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn SetWidth(&mut self, abstract_self: AbstractNode, width: u32) -> ErrorResult {
|
||||
let node = &mut self.htmlelement.element;
|
||||
node.set_attr(abstract_self, ~"width", width.to_str());
|
||||
pub fn SetWidth(&mut self, abstract_self: &JS<HTMLImageElement>, width: u32) -> ErrorResult {
|
||||
let mut elem: JS<Element> = ElementCast::from(abstract_self);
|
||||
let mut elem_clone = elem.clone();
|
||||
elem.get_mut().set_attr(&mut elem_clone, ~"width", width.to_str());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn Height(&self, abstract_self: AbstractNode) -> u32 {
|
||||
pub fn Height(&self, abstract_self: &JS<HTMLImageElement>) -> u32 {
|
||||
let node = &self.htmlelement.element.node;
|
||||
let page = node.owner_doc().document().window.page;
|
||||
let doc = node.owner_doc();
|
||||
let page = doc.get().window.get().page;
|
||||
let (port, chan) = Chan::new();
|
||||
match page.query_layout(ContentBoxQuery(abstract_self, chan), port) {
|
||||
let this_node: JS<Node> = NodeCast::from(abstract_self);
|
||||
let addr = this_node.to_trusted_node_address();
|
||||
match page.query_layout(ContentBoxQuery(addr, chan), port) {
|
||||
ContentBoxResponse(rect) => {
|
||||
to_px(rect.size.height) as u32
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn SetHeight(&mut self, abstract_self: AbstractNode, height: u32) -> ErrorResult {
|
||||
pub fn SetHeight(&mut self, abstract_self: &JS<HTMLImageElement>, height: u32) -> ErrorResult {
|
||||
let node = &mut self.htmlelement.element;
|
||||
node.set_attr(abstract_self, ~"height", height.to_str());
|
||||
node.set_attr(&ElementCast::from(abstract_self), ~"height", height.to_str());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue