Skip to main content

script/dom/svg/
svgaelement.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 std::cell::Cell;
6
7use dom_struct::dom_struct;
8use html5ever::{LocalName, Prefix, local_name};
9use js::context::{JSContext, NoGC};
10use js::rust::HandleObject;
11use net_traits::blob_url_store::UrlWithBlobClaim;
12use script_bindings::cell::DomRefCell;
13use style::attr::AttrValue;
14use stylo_atoms::Atom;
15use stylo_dom::ElementState;
16
17use crate::dom::bindings::codegen::Bindings::SVGAElementBinding::SVGAElementMethods;
18use crate::dom::bindings::inheritance::Castable;
19use crate::dom::bindings::root::{DomRoot, MutNullableDom};
20use crate::dom::bindings::str::{DOMString, USVString};
21use crate::dom::document::Document;
22use crate::dom::domtokenlist::DOMTokenList;
23use crate::dom::element::storage::AttrRef;
24use crate::dom::element::{AttributeMutation, Element, reflect_referrer_policy_attribute};
25use crate::dom::links::htmlhyperlinkelementutils::{HyperlinkElement, HyperlinkElementTraits};
26use crate::dom::links::relations::LinkRelations;
27use crate::dom::node::Node;
28use crate::dom::node::virtualmethods::VirtualMethods;
29use crate::dom::svg::svggraphicselement::SVGGraphicsElement;
30
31#[dom_struct]
32pub(crate) struct SVGAElement {
33    svggraphicselement: SVGGraphicsElement,
34    rel_list: MutNullableDom<DOMTokenList>,
35    #[no_trace]
36    relations: Cell<LinkRelations>,
37    #[no_trace]
38    url: DomRefCell<Option<UrlWithBlobClaim>>,
39}
40
41impl SVGAElement {
42    fn new_inherited(
43        local_name: LocalName,
44        prefix: Option<Prefix>,
45        document: &Document,
46    ) -> SVGAElement {
47        SVGAElement {
48            svggraphicselement: SVGGraphicsElement::new_inherited(local_name, prefix, document),
49            rel_list: Default::default(),
50            relations: Cell::new(LinkRelations::empty()),
51            url: Default::default(),
52        }
53    }
54
55    pub(crate) fn new(
56        cx: &mut JSContext,
57        local_name: LocalName,
58        prefix: Option<Prefix>,
59        document: &Document,
60        proto: Option<HandleObject>,
61    ) -> DomRoot<SVGAElement> {
62        Node::reflect_node_with_proto(
63            cx,
64            Box::new(SVGAElement::new_inherited(local_name, prefix, document)),
65            document,
66            proto,
67        )
68    }
69}
70
71impl SVGAElementMethods<crate::DomTypeHolder> for SVGAElement {
72    // https://svgwg.org/svg2-draft/linking.html#__svg__SVGAElement__rel
73    make_getter!(Rel, "rel");
74
75    /// <https://svgwg.org/svg2-draft/linking.html#__svg__SVGAElement__rel>
76    fn SetRel(&self, cx: &mut JSContext, rel: DOMString) {
77        self.upcast::<Element>()
78            .set_tokenlist_attribute(cx, &local_name!("rel"), rel);
79    }
80
81    /// <https://svgwg.org/svg2-draft/linking.html#__svg__SVGAElement__relList>
82    fn RelList(&self, cx: &mut JSContext) -> DomRoot<DOMTokenList> {
83        self.rel_list.or_init(|| {
84            DOMTokenList::new(
85                cx,
86                self.upcast(),
87                &local_name!("rel"),
88                Some(vec![
89                    Atom::from("noopener"),
90                    Atom::from("noreferrer"),
91                    Atom::from("opener"),
92                ]),
93            )
94        })
95    }
96
97    // https://svgwg.org/svg2-draft/linking.html#__svg__SVGAElement__hreflang
98    make_getter!(Hreflang, "hreflang");
99
100    // https://svgwg.org/svg2-draft/linking.html#__svg__SVGAElement__hreflang
101    make_setter!(SetHreflang, "hreflang");
102
103    // https://svgwg.org/svg2-draft/linking.html#__svg__SVGAElement__type
104    make_getter!(Type, "type");
105
106    // https://svgwg.org/svg2-draft/linking.html#__svg__SVGAElement__type
107    make_setter!(SetType, "type");
108
109    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-href>
110    fn Href(&self, no_gc: &NoGC) -> USVString {
111        self.get_href(no_gc)
112    }
113
114    // https://html.spec.whatwg.org/multipage/#dom-hyperlink-href
115    make_url_setter!(SetHref, "href");
116
117    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-origin>
118    fn Origin(&self, no_gc: &NoGC) -> USVString {
119        self.get_origin(no_gc)
120    }
121
122    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-protocol>
123    fn Protocol(&self, no_gc: &NoGC) -> USVString {
124        self.get_protocol(no_gc)
125    }
126
127    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-protocol>
128    fn SetProtocol(&self, cx: &mut JSContext, value: USVString) {
129        self.set_protocol(cx, value);
130    }
131
132    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-password>
133    fn Password(&self, no_gc: &NoGC) -> USVString {
134        self.get_password(no_gc)
135    }
136
137    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-password>
138    fn SetPassword(&self, cx: &mut JSContext, value: USVString) {
139        self.set_password(cx, value);
140    }
141
142    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-hash>
143    fn Hash(&self, no_gc: &NoGC) -> USVString {
144        self.get_hash(no_gc)
145    }
146
147    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-hash>
148    fn SetHash(&self, cx: &mut JSContext, value: USVString) {
149        self.set_hash(cx, value);
150    }
151
152    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-host>
153    fn Host(&self, no_gc: &NoGC) -> USVString {
154        self.get_host(no_gc)
155    }
156
157    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-host>
158    fn SetHost(&self, cx: &mut JSContext, value: USVString) {
159        self.set_host(cx, value);
160    }
161
162    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-hostname>
163    fn Hostname(&self, no_gc: &NoGC) -> USVString {
164        self.get_hostname(no_gc)
165    }
166
167    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-hostname>
168    fn SetHostname(&self, cx: &mut JSContext, value: USVString) {
169        self.set_hostname(cx, value);
170    }
171
172    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-port>
173    fn Port(&self, no_gc: &NoGC) -> USVString {
174        self.get_port(no_gc)
175    }
176
177    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-port>
178    fn SetPort(&self, cx: &mut JSContext, value: USVString) {
179        self.set_port(cx, value);
180    }
181
182    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-pathname>
183    fn Pathname(&self, no_gc: &NoGC) -> USVString {
184        self.get_pathname(no_gc)
185    }
186
187    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-pathname>
188    fn SetPathname(&self, cx: &mut JSContext, value: USVString) {
189        self.set_pathname(cx, value);
190    }
191
192    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-search>
193    fn Search(&self, no_gc: &NoGC) -> USVString {
194        self.get_search(no_gc)
195    }
196
197    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-search>
198    fn SetSearch(&self, cx: &mut JSContext, value: USVString) {
199        self.set_search(cx, value);
200    }
201
202    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-username>
203    fn Username(&self, no_gc: &NoGC) -> USVString {
204        self.get_username(no_gc)
205    }
206
207    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-username>
208    fn SetUsername(&self, cx: &mut JSContext, value: USVString) {
209        self.set_username(cx, value);
210    }
211
212    /// <https://svgwg.org/svg2-draft/linking.html#__svg__SVGAElement__referrerPolicy>
213    fn ReferrerPolicy(&self) -> DOMString {
214        reflect_referrer_policy_attribute(self.upcast::<Element>())
215    }
216
217    // https://svgwg.org/svg2-draft/linking.html#__svg__SVGAElement__referrerPolicy
218    make_setter!(SetReferrerPolicy, "referrerpolicy");
219}
220
221impl VirtualMethods for SVGAElement {
222    fn super_type(&self) -> Option<&dyn VirtualMethods> {
223        Some(self.upcast::<SVGGraphicsElement>() as &dyn VirtualMethods)
224    }
225
226    fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
227        match name {
228            &local_name!("rel") => AttrValue::from_serialized_tokenlist(value.into()),
229            _ => self
230                .super_type()
231                .unwrap()
232                .parse_plain_attribute(name, value),
233        }
234    }
235
236    fn attribute_mutated(
237        &self,
238        cx: &mut JSContext,
239        attr: AttrRef<'_>,
240        mutation: AttributeMutation,
241    ) {
242        self.super_type()
243            .unwrap()
244            .attribute_mutated(cx, attr, mutation);
245
246        self.attribute_mutated_for_hyperlinks(cx.no_gc(), attr, mutation);
247
248        // https://html.spec.whatwg.org/multipage/#introduction-2
249        // > Similarly, for a and area elements with an href attribute and a rel attribute,
250        // > links must be created for the keywords of the rel attribute
251        // > as defined for those keywords in the link types section.
252        match *attr.local_name() {
253            local_name!("href") => self.upcast::<Element>().set_state(
254                cx.no_gc(),
255                ElementState::UNVISITED,
256                !mutation.is_removal(),
257            ),
258            local_name!("rel") | local_name!("rev") => {
259                self.relations
260                    .set(LinkRelations::for_element(self.upcast()));
261            },
262            _ => {},
263        }
264    }
265}
266
267impl HyperlinkElement for SVGAElement {
268    fn get_url(&self) -> &DomRefCell<Option<UrlWithBlobClaim>> {
269        &self.url
270    }
271}