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