script/dom/
svgimageelement.rs1use dom_struct::dom_struct;
6use html5ever::{LocalName, Prefix, local_name, ns};
7use js::rust::HandleObject;
8use style::attr::AttrValue;
9
10use crate::dom::attr::Attr;
11use crate::dom::bindings::inheritance::Castable;
12use crate::dom::bindings::root::DomRoot;
13use crate::dom::bindings::str::DOMString;
14use crate::dom::document::Document;
15use crate::dom::element::AttributeMutation;
16use crate::dom::node::{Node, NodeTraits};
17use crate::dom::svggraphicselement::SVGGraphicsElement;
18use crate::dom::virtualmethods::VirtualMethods;
19use crate::script_runtime::CanGc;
20
21const DEFAULT_WIDTH: u32 = 300;
23const DEFAULT_HEIGHT: u32 = 150;
24
25#[dom_struct]
26pub(crate) struct SVGImageElement {
27 svggraphicselement: SVGGraphicsElement,
28}
29
30impl SVGImageElement {
31 fn new_inherited(
32 local_name: LocalName,
33 prefix: Option<Prefix>,
34 document: &Document,
35 ) -> SVGImageElement {
36 SVGImageElement {
37 svggraphicselement: SVGGraphicsElement::new_inherited(local_name, prefix, document),
38 }
39 }
40
41 #[cfg_attr(crown, allow(crown::unrooted_must_root))]
42 pub(crate) fn new(
43 local_name: LocalName,
44 prefix: Option<Prefix>,
45 document: &Document,
46 proto: Option<HandleObject>,
47 can_gc: CanGc,
48 ) -> DomRoot<SVGImageElement> {
49 Node::reflect_node_with_proto(
50 Box::new(SVGImageElement::new_inherited(local_name, prefix, document)),
51 document,
52 proto,
53 can_gc,
54 )
55 }
56
57 fn fetch_image_resource(&self) {
59 self.owner_global()
62 .task_manager()
63 .dom_manipulation_task_source()
64 .queue_simple_event(self.upcast(), atom!("error"));
65 }
66}
67
68impl VirtualMethods for SVGImageElement {
69 fn super_type(&self) -> Option<&dyn VirtualMethods> {
70 Some(self.upcast::<SVGGraphicsElement>() as &dyn VirtualMethods)
71 }
72
73 fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation, can_gc: CanGc) {
74 self.super_type()
75 .unwrap()
76 .attribute_mutated(attr, mutation, can_gc);
77 if attr.local_name() == &local_name!("href") &&
78 matches!(attr.namespace(), &ns!() | &ns!(xlink))
79 {
80 if let AttributeMutation::Set(_) = mutation {
81 self.fetch_image_resource();
82 }
83 }
84 }
85
86 fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
87 match *name {
88 local_name!("width") => AttrValue::from_u32(value.into(), DEFAULT_WIDTH),
89 local_name!("height") => AttrValue::from_u32(value.into(), DEFAULT_HEIGHT),
90 _ => self
91 .super_type()
92 .unwrap()
93 .parse_plain_attribute(name, value),
94 }
95 }
96}