script/dom/svg/
svgimageelement.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use 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::svg::svggraphicselement::SVGGraphicsElement;
18use crate::dom::virtualmethods::VirtualMethods;
19
20/// <https://svgwg.org/svg2-draft/embedded.html#Placement>
21const DEFAULT_WIDTH: u32 = 300;
22const DEFAULT_HEIGHT: u32 = 150;
23
24#[dom_struct]
25pub(crate) struct SVGImageElement {
26    svggraphicselement: SVGGraphicsElement,
27}
28
29impl SVGImageElement {
30    fn new_inherited(
31        local_name: LocalName,
32        prefix: Option<Prefix>,
33        document: &Document,
34    ) -> SVGImageElement {
35        SVGImageElement {
36            svggraphicselement: SVGGraphicsElement::new_inherited(local_name, prefix, document),
37        }
38    }
39
40    pub(crate) fn new(
41        cx: &mut js::context::JSContext,
42        local_name: LocalName,
43        prefix: Option<Prefix>,
44        document: &Document,
45        proto: Option<HandleObject>,
46    ) -> DomRoot<SVGImageElement> {
47        Node::reflect_node_with_proto(
48            cx,
49            Box::new(SVGImageElement::new_inherited(local_name, prefix, document)),
50            document,
51            proto,
52        )
53    }
54
55    /// <https://svgwg.org/svg2-draft/linking.html#processingURL>
56    fn fetch_image_resource(&self) {
57        // TODO: Process and fetch the image resource (as HTMLImageElement).
58        // Reject any resource fetching request immediately.
59        self.owner_global()
60            .task_manager()
61            .dom_manipulation_task_source()
62            .queue_simple_event(self.upcast(), atom!("error"));
63    }
64}
65
66impl VirtualMethods for SVGImageElement {
67    fn super_type(&self) -> Option<&dyn VirtualMethods> {
68        Some(self.upcast::<SVGGraphicsElement>() as &dyn VirtualMethods)
69    }
70
71    fn attribute_mutated(
72        &self,
73        cx: &mut js::context::JSContext,
74        attr: &Attr,
75        mutation: AttributeMutation,
76    ) {
77        self.super_type()
78            .unwrap()
79            .attribute_mutated(cx, attr, mutation);
80        if attr.local_name() == &local_name!("href") &&
81            matches!(attr.namespace(), &ns!() | &ns!(xlink))
82        {
83            if let AttributeMutation::Set(..) = mutation {
84                self.fetch_image_resource();
85            }
86        }
87    }
88
89    fn attribute_affects_presentational_hints(&self, attr: &Attr) -> bool {
90        match attr.local_name() {
91            &local_name!("width") | &local_name!("height") => true,
92            _ => self
93                .super_type()
94                .unwrap()
95                .attribute_affects_presentational_hints(attr),
96        }
97    }
98
99    fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
100        match *name {
101            local_name!("width") => AttrValue::from_u32(value.into(), DEFAULT_WIDTH),
102            local_name!("height") => AttrValue::from_u32(value.into(), DEFAULT_HEIGHT),
103            _ => self
104                .super_type()
105                .unwrap()
106                .parse_plain_attribute(name, value),
107        }
108    }
109}