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;
19use crate::script_runtime::CanGc;
20
21/// <https://svgwg.org/svg2-draft/embedded.html#Placement>
22const 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    pub(crate) fn new(
42        local_name: LocalName,
43        prefix: Option<Prefix>,
44        document: &Document,
45        proto: Option<HandleObject>,
46        can_gc: CanGc,
47    ) -> DomRoot<SVGImageElement> {
48        Node::reflect_node_with_proto(
49            Box::new(SVGImageElement::new_inherited(local_name, prefix, document)),
50            document,
51            proto,
52            can_gc,
53        )
54    }
55
56    /// <https://svgwg.org/svg2-draft/linking.html#processingURL>
57    fn fetch_image_resource(&self) {
58        // TODO: Process and fetch the image resource (as HTMLImageElement).
59        // Reject any resource fetching request immediately.
60        self.owner_global()
61            .task_manager()
62            .dom_manipulation_task_source()
63            .queue_simple_event(self.upcast(), atom!("error"));
64    }
65}
66
67impl VirtualMethods for SVGImageElement {
68    fn super_type(&self) -> Option<&dyn VirtualMethods> {
69        Some(self.upcast::<SVGGraphicsElement>() as &dyn VirtualMethods)
70    }
71
72    fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation, can_gc: CanGc) {
73        self.super_type()
74            .unwrap()
75            .attribute_mutated(attr, mutation, can_gc);
76        if attr.local_name() == &local_name!("href") &&
77            matches!(attr.namespace(), &ns!() | &ns!(xlink))
78        {
79            if let AttributeMutation::Set(..) = mutation {
80                self.fetch_image_resource();
81            }
82        }
83    }
84
85    fn attribute_affects_presentational_hints(&self, attr: &Attr) -> bool {
86        match attr.local_name() {
87            &local_name!("width") | &local_name!("height") => true,
88            _ => self
89                .super_type()
90                .unwrap()
91                .attribute_affects_presentational_hints(attr),
92        }
93    }
94
95    fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
96        match *name {
97            local_name!("width") => AttrValue::from_u32(value.into(), DEFAULT_WIDTH),
98            local_name!("height") => AttrValue::from_u32(value.into(), DEFAULT_HEIGHT),
99            _ => self
100                .super_type()
101                .unwrap()
102                .parse_plain_attribute(name, value),
103        }
104    }
105}