Skip to main content

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::bindings::inheritance::Castable;
11use crate::dom::bindings::root::DomRoot;
12use crate::dom::bindings::str::DOMString;
13use crate::dom::document::Document;
14use crate::dom::element::AttributeMutation;
15use crate::dom::element::attributes::storage::AttrRef;
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: AttrRef<'_>,
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            let AttributeMutation::Set(..) = mutation
83        {
84            self.fetch_image_resource();
85        }
86    }
87
88    fn attribute_affects_presentational_hints(&self, attr: AttrRef<'_>) -> bool {
89        match attr.local_name() {
90            &local_name!("width") | &local_name!("height") => true,
91            _ => self
92                .super_type()
93                .unwrap()
94                .attribute_affects_presentational_hints(attr),
95        }
96    }
97
98    fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
99        match *name {
100            local_name!("width") => AttrValue::from_u32(value.into(), DEFAULT_WIDTH),
101            local_name!("height") => AttrValue::from_u32(value.into(), DEFAULT_HEIGHT),
102            _ => self
103                .super_type()
104                .unwrap()
105                .parse_plain_attribute(name, value),
106        }
107    }
108}