Skip to main content

script/dom/html/
htmlhyperlinkelementutils.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 html5ever::local_name;
6use js::context::JSContext;
7use script_bindings::cell::DomRefCell;
8use servo_url::ServoUrl;
9
10use crate::dom::bindings::conversions::DerivedFrom;
11use crate::dom::bindings::inheritance::Castable;
12use crate::dom::bindings::str::{DOMString, USVString};
13use crate::dom::element::Element;
14use crate::dom::node::NodeTraits;
15use crate::dom::urlhelper::UrlHelper;
16
17pub(crate) trait HyperlinkElement {
18    fn get_url(&self) -> &DomRefCell<Option<ServoUrl>>;
19}
20
21/// <https://html.spec.whatwg.org/multipage/#htmlhyperlinkelementutils>
22pub(crate) trait HyperlinkElementTraits {
23    fn get_hash(&self) -> USVString;
24    fn set_hash(&self, cx: &mut JSContext, value: USVString);
25    fn get_host(&self) -> USVString;
26    fn set_host(&self, cx: &mut JSContext, value: USVString);
27    fn get_hostname(&self) -> USVString;
28    fn set_hostname(&self, cx: &mut JSContext, value: USVString);
29    fn get_href(&self) -> USVString;
30    fn set_href(&self, cx: &mut JSContext, value: USVString);
31    fn get_origin(&self) -> USVString;
32    fn get_password(&self) -> USVString;
33    fn set_password(&self, cx: &mut JSContext, value: USVString);
34    fn get_pathname(&self) -> USVString;
35    fn set_pathname(&self, cx: &mut JSContext, value: USVString);
36    fn get_port(&self) -> USVString;
37    fn set_port(&self, cx: &mut JSContext, value: USVString);
38    fn get_protocol(&self) -> USVString;
39    fn set_protocol(&self, cx: &mut JSContext, value: USVString);
40    fn get_search(&self) -> USVString;
41    fn set_search(&self, cx: &mut JSContext, value: USVString);
42    fn get_username(&self) -> USVString;
43    fn set_url(&self);
44    fn set_username(&self, cx: &mut JSContext, value: USVString);
45    fn update_href(&self, cx: &mut JSContext, url: &ServoUrl);
46    fn reinitialize_url(&self);
47}
48
49impl<T: HyperlinkElement + DerivedFrom<Element> + Castable + NodeTraits> HyperlinkElementTraits
50    for T
51{
52    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-hash>
53    fn get_hash(&self) -> USVString {
54        // Step 1. Reinitialize url.
55        self.reinitialize_url();
56
57        // Step 2. Let url be this's url.
58        match *self.get_url().borrow() {
59            // Step 3. If url is null, or url's fragment is either null or the empty string, return
60            // the empty string.
61            None => USVString(String::new()),
62            Some(ref url) if url.fragment().is_none() || url.fragment() == Some("") => {
63                USVString(String::new())
64            },
65            Some(ref url) => {
66                // Step 4. Return "#", followed by url's fragment.
67                UrlHelper::Hash(url)
68            },
69        }
70    }
71
72    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-hash>
73    fn set_hash(&self, cx: &mut JSContext, value: USVString) {
74        // Step 1. Reinitialize url.
75        self.reinitialize_url();
76
77        // Step 2. Let url be this's url.
78        // Step 3. If url is null, then return.
79        {
80            let mut url = self.get_url().borrow_mut();
81            let Some(url) = url.as_mut() else {
82                return;
83            };
84
85            // Step 4. If the given value is the empty string, set url's fragment to null.
86            // Note this step is taken care of by UrlHelper::SetHash when the value is Some
87            // Steps 5. Otherwise:
88            // Step 5.1. Let input be the given value with a single leading "#" removed, if any.
89            // Step 5.2. Set url's fragment to the empty string.
90            // Note these steps are taken care of by UrlHelper::SetHash
91            // Step 5.4.  Basic URL parse input, with url as url and fragment state as state
92            // override.
93            UrlHelper::SetHash(url, value);
94        }
95
96        // Step 6. Update href.
97        let url = self.get_url().borrow();
98        // Above we checked for not None
99        self.update_href(cx, url.as_ref().unwrap());
100    }
101
102    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-host>
103    fn get_host(&self) -> USVString {
104        // Step 1. Reinitialize url.
105        self.reinitialize_url();
106
107        // Step 2. Let url be this's url.
108        match *self.get_url().borrow() {
109            // Step 3. If url or url's host is null, return the empty string.
110            None => USVString(String::new()),
111            Some(ref url) => {
112                if url.host().is_none() {
113                    USVString(String::new())
114                } else {
115                    // Step 4. If url's port is null, return url's host, serialized.
116                    // Step 5. Return url's host, serialized, followed by ":" and url's port,
117                    // serialized.
118                    UrlHelper::Host(url)
119                }
120            },
121        }
122    }
123
124    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-host>
125    fn set_host(&self, cx: &mut JSContext, value: USVString) {
126        // Step 1. Reinitialize url.
127        self.reinitialize_url();
128
129        // Step 2. Let url be this's url.
130        {
131            let mut url = self.get_url().borrow_mut();
132            let url = match url.as_mut() {
133                // Step 3. If url or url's host is null, return the empty string.
134                Some(ref url) if url.cannot_be_a_base() => return,
135                None => return,
136                Some(url) => url,
137            };
138
139            // Step 4. Basic URL parse the given value, with url as url and host state as state
140            // override.
141            UrlHelper::SetHost(url, value);
142        }
143
144        // Step 5. Update href.
145        let url = self.get_url().borrow();
146        // Tested above for not None
147        self.update_href(cx, url.as_ref().unwrap());
148    }
149
150    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-hostname>
151    fn get_hostname(&self) -> USVString {
152        // Step 1. Reinitialize url.
153        self.reinitialize_url();
154
155        // Step 2. Let url be this's url.
156        match *self.get_url().borrow() {
157            // Step 3. If url or url's host is null, return the empty string.
158            None => USVString(String::new()),
159            Some(ref url) => {
160                // Step 4. Return url's host, serialized.
161                UrlHelper::Hostname(url)
162            },
163        }
164    }
165
166    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-hostname>
167    fn set_hostname(&self, cx: &mut JSContext, value: USVString) {
168        // Step 1. Reinitialize url.
169        self.reinitialize_url();
170
171        // Step 2. Let url be this's url.
172        {
173            let mut url = self.get_url().borrow_mut();
174            let url = match url.as_mut() {
175                // Step 3. If url is null or url has an opaque path, then return.
176                None => return,
177                Some(ref url) if url.cannot_be_a_base() => return,
178                Some(url) => url,
179            };
180
181            // Step 4. Basic URL parse the given value, with url as url and hostname state as state
182            // override.
183            UrlHelper::SetHostname(url, value);
184        }
185
186        // Step 5. Update href.
187        let url = self.get_url().borrow();
188        // tested above for not None
189        self.update_href(cx, url.as_ref().unwrap());
190    }
191
192    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-href>
193    fn get_href(&self) -> USVString {
194        // Step 1. Reinitialize url.
195        self.reinitialize_url();
196
197        // Step 2. Let url be this's url.
198        USVString(match *self.get_url().borrow() {
199            None => {
200                // Step 3. If url is null and this has no href content attribute, return the
201                // empty string.
202                // Step 4. Otherwise, if url is null, return this's href content attribute's value.
203                self.upcast::<Element>()
204                    .get_attribute_string_value(&local_name!("href"))
205                    .unwrap_or_default()
206            },
207            // Step 5. Return url, serialized.
208            Some(ref url) => url.as_str().to_owned(),
209        })
210    }
211
212    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-href
213    fn set_href(&self, cx: &mut JSContext, value: USVString) {
214        self.upcast::<Element>()
215            .set_string_attribute(cx, &local_name!("href"), value.into());
216
217        self.set_url();
218    }
219
220    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-origin>
221    fn get_origin(&self) -> USVString {
222        // Step 1. Reinitialize url.
223        self.reinitialize_url();
224
225        USVString(match *self.get_url().borrow() {
226            // Step 2. If this's url is null, return the empty string.
227            None => "".to_owned(),
228            // Step 3. Return the serialization of this's url's origin.
229            Some(ref url) => url.origin().ascii_serialization(),
230        })
231    }
232
233    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-password>
234    fn get_password(&self) -> USVString {
235        // Step 1. Reinitialize url.
236        self.reinitialize_url();
237
238        // Step 2. Let url be this's url.
239        match *self.get_url().borrow() {
240            // Step 3. If url is null, then return the empty string.
241            None => USVString(String::new()),
242            // Steps 4. Return url's password.
243            Some(ref url) => UrlHelper::Password(url),
244        }
245    }
246
247    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-password>
248    fn set_password(&self, cx: &mut JSContext, value: USVString) {
249        // Step 1. Reinitialize url.
250        self.reinitialize_url();
251
252        {
253            // Step 2. Let url be this's url.
254            let mut url = self.get_url().borrow_mut();
255            let url = match url.as_mut() {
256                // Step 3. If url is null or url cannot have a username/password/port, then return.
257                None => return,
258                Some(ref url) if url.host().is_none() || url.cannot_be_a_base() => return,
259                Some(url) => url,
260            };
261
262            // Step 4. Set the password, given url and the given value.
263            UrlHelper::SetPassword(url, value);
264        }
265
266        // Step 5. Update href.
267        let url = self.get_url().borrow();
268        // Tested above for not None
269        self.update_href(cx, url.as_ref().unwrap());
270    }
271
272    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-pathname>
273    fn get_pathname(&self) -> USVString {
274        // Step 1. Reinitialize url.
275        self.reinitialize_url();
276
277        // Step 2. Let url be this's url.
278        match *self.get_url().borrow() {
279            // Step 3. If url is null, then return the empty string.
280            None => USVString(String::new()),
281            // Steps 4. Return the result of URL path serializing url.
282            Some(ref url) => UrlHelper::Pathname(url),
283        }
284    }
285
286    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-pathname>
287    fn set_pathname(&self, cx: &mut JSContext, value: USVString) {
288        // Step 1. Reinitialize url.
289        self.reinitialize_url();
290
291        // Step 2. Let url be this's url.
292        {
293            let mut url = self.get_url().borrow_mut();
294            let url = match url.as_mut() {
295                // Step 3. If url is null or url has an opaque path, then return.
296                None => return,
297                Some(ref url) if url.cannot_be_a_base() => return,
298                Some(url) => url,
299            };
300
301            // Step 4. Set url's path to the empty list.
302            // Step 5. Basic URL parse the given value, with url as url and path start state as state override.
303            UrlHelper::SetPathname(url, value);
304        }
305
306        // Step 6. Update href.
307        let url = self.get_url().borrow();
308        self.update_href(cx, url.as_ref().unwrap());
309    }
310
311    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-port>
312    fn get_port(&self) -> USVString {
313        // Step 1. Reinitialize url.
314        self.reinitialize_url();
315
316        // Step 2. Let url be this's url.
317        match *self.get_url().borrow() {
318            // Step 3. If url or url's port is null, return the empty string.
319            None => USVString(String::new()),
320            // Step 4. Return url's port, serialized.
321            Some(ref url) => UrlHelper::Port(url),
322        }
323    }
324
325    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-port>
326    fn set_port(&self, cx: &mut JSContext, value: USVString) {
327        // Step 1. Reinitialize url.
328        self.reinitialize_url();
329
330        // Step 2. Let url be this's url.
331        {
332            let mut url = self.get_url().borrow_mut();
333            let url = match url.as_mut() {
334            // Step 3. If url is null or url cannot have a username/password/port, then return.
335            None => return,
336            Some(ref url)
337                // https://url.spec.whatwg.org/#cannot-have-a-username-password-port
338                if url.host().is_none() || url.cannot_be_a_base() || url.scheme() == "file" =>
339            {
340                return;
341            },
342            Some(url) => url,
343        };
344
345            // Step 4. If the given value is the empty string, then set url's port to null.
346            // Step 5. Otherwise, basic URL parse the given value, with url as url and port state as
347            // state override.
348            UrlHelper::SetPort(url, value);
349        }
350
351        // Step 6. Update href.
352        let url = self.get_url().borrow();
353        // Tested above for not None
354        self.update_href(cx, url.as_ref().unwrap());
355    }
356
357    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-protocol>
358    fn get_protocol(&self) -> USVString {
359        // Step 1. Reinitialize url.
360        self.reinitialize_url();
361
362        match *self.get_url().borrow() {
363            // Step 2. If this's url is null, return ":".
364            None => USVString(":".to_owned()),
365            // Step 3. Return this's url's scheme, followed by ":".
366            Some(ref url) => UrlHelper::Protocol(url),
367        }
368    }
369
370    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-protocol>
371    fn set_protocol(&self, cx: &mut JSContext, value: USVString) {
372        // Step 1. Reinitialize url.
373        self.reinitialize_url();
374
375        {
376            let mut url = self.get_url().borrow_mut();
377            let url = match url.as_mut() {
378                // Step 2. If this's url is null, then return.
379                None => return,
380                Some(url) => url,
381            };
382
383            // Step 3. Basic URL parse the given value, followed by ":", with this's url as url and
384            // scheme start state as state override.
385            UrlHelper::SetProtocol(url, value);
386        }
387
388        // Step 4. Update href.
389        let url = self.get_url().borrow();
390        // Tested above for not None
391        self.update_href(cx, url.as_ref().unwrap());
392    }
393
394    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-search>
395    fn get_search(&self) -> USVString {
396        // Step 1. Reinitialize url.
397        self.reinitialize_url();
398
399        // Step 2. Let url be this's url.
400        match *self.get_url().borrow() {
401            // Step 3. If url is null, or url's query is either null or the empty string, return the
402            // empty string.
403            // Step 4. Return "?", followed by url's query.
404            // Note: This is handled in UrlHelper::Search
405            None => USVString(String::new()),
406            Some(ref url) => UrlHelper::Search(url),
407        }
408    }
409
410    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-search>
411    fn set_search(&self, cx: &mut JSContext, value: USVString) {
412        // Step 1. Reinitialize url.
413        self.reinitialize_url();
414
415        // Step 2. Let url be this's url.
416        {
417            let mut url = self.get_url().borrow_mut();
418            let url = match url.as_mut() {
419                // Step 3. If url is null, terminate these steps.
420                None => return,
421                Some(url) => url,
422            };
423
424            // Step 4. If the given value is the empty string, set url's query to null.
425            // Step 5. Otherwise:
426            // Note: Inner steps are handled by UrlHelper::SetSearch
427            UrlHelper::SetSearch(url, value);
428        }
429
430        // Step 6. Update href.
431        let url = self.get_url().borrow();
432        self.update_href(cx, url.as_ref().unwrap());
433    }
434
435    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-username>
436    fn get_username(&self) -> USVString {
437        // Step 1. Reinitialize url.
438        self.reinitialize_url();
439
440        match *self.get_url().borrow() {
441            // Step 2. If this's url is null, return the empty string.
442            None => USVString(String::new()),
443            // Step 3. Return this's url's username.
444            Some(ref url) => UrlHelper::Username(url),
445        }
446    }
447
448    /// <https://html.spec.whatwg.org/multipage/#concept-hyperlink-url-set>
449    fn set_url(&self) {
450        // Step 1. Set this element's url to null.
451        *self.get_url().borrow_mut() = None;
452
453        let attribute = self
454            .upcast::<Element>()
455            .get_attribute_string_value(&local_name!("href"));
456
457        // Step 2. If this element's href content attribute is absent, then return.
458        let Some(attribute) = attribute else {
459            return;
460        };
461
462        let document = self.owner_document();
463
464        // Step 3. Let url be the result of encoding-parsing a URL given this element's href content
465        // attribute's value, relative to this element's node document.
466        let url = document.encoding_parse_a_url(&attribute);
467
468        // Step 4. If url is not failure, then set this element's url to url.
469        if let Ok(url) = url {
470            *self.get_url().borrow_mut() = Some(url);
471        }
472    }
473
474    /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-username>
475    fn set_username(&self, cx: &mut JSContext, value: USVString) {
476        // Step 1. Reinitialize url.
477        self.reinitialize_url();
478
479        // Step 2. Let url be this's url.
480        {
481            let mut url = self.get_url().borrow_mut();
482            let url = match url.as_mut() {
483                // Step 3. If url is null or url cannot have a username/password/port, then return.
484                None => return,
485                Some(ref url) if url.host().is_none() || url.cannot_be_a_base() => return,
486                Some(url) => url,
487            };
488
489            // Step 4. Set the username, given url and the given value.
490            UrlHelper::SetUsername(url, value);
491        }
492
493        // Step 5. Update href.
494        let url = self.get_url().borrow();
495        self.update_href(cx, url.as_ref().unwrap());
496    }
497
498    /// <https://html.spec.whatwg.org/multipage/#update-href>
499    fn update_href(&self, cx: &mut JSContext, url: &ServoUrl) {
500        self.upcast::<Element>().set_string_attribute(
501            cx,
502            &local_name!("href"),
503            DOMString::from(url.as_str()),
504        );
505    }
506
507    /// <https://html.spec.whatwg.org/multipage/#reinitialise-url>
508    fn reinitialize_url(&self) {
509        // Step 1. If the element's url is non-null, its scheme is "blob", and it has an opaque
510        // path, then terminate these steps.
511        match *self.get_url().borrow() {
512            Some(ref url) if url.scheme() == "blob" && url.cannot_be_a_base() => return,
513            _ => (),
514        }
515
516        // Step 2. Set the url.
517        self.set_url();
518    }
519}