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 servo_url::ServoUrl;
7
8use crate::dom::bindings::cell::DomRefCell;
9use crate::dom::bindings::conversions::DerivedFrom;
10use crate::dom::bindings::inheritance::Castable;
11use crate::dom::bindings::str::{DOMString, USVString};
12use crate::dom::element::Element;
13use crate::dom::node::NodeTraits;
14use crate::dom::urlhelper::UrlHelper;
15use crate::script_runtime::CanGc;
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, value: USVString, can_gc: CanGc);
25 fn get_host(&self) -> USVString;
26 fn set_host(&self, value: USVString, can_gc: CanGc);
27 fn get_hostname(&self) -> USVString;
28 fn set_hostname(&self, value: USVString, can_gc: CanGc);
29 fn get_href(&self) -> USVString;
30 fn set_href(&self, value: USVString, can_gc: CanGc);
31 fn get_origin(&self) -> USVString;
32 fn get_password(&self) -> USVString;
33 fn set_password(&self, value: USVString, can_gc: CanGc);
34 fn get_pathname(&self) -> USVString;
35 fn set_pathname(&self, value: USVString, can_gc: CanGc);
36 fn get_port(&self) -> USVString;
37 fn set_port(&self, value: USVString, can_gc: CanGc);
38 fn get_protocol(&self) -> USVString;
39 fn set_protocol(&self, value: USVString, can_gc: CanGc);
40 fn get_search(&self) -> USVString;
41 fn set_search(&self, value: USVString, can_gc: CanGc);
42 fn get_username(&self) -> USVString;
43 fn set_url(&self);
44 fn set_username(&self, value: USVString, can_gc: CanGc);
45 fn update_href(&self, url: &ServoUrl, can_gc: CanGc);
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, value: USVString, can_gc: CanGc) {
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(url, can_gc);
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, value: USVString, can_gc: CanGc) {
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(url, can_gc);
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, value: USVString, can_gc: CanGc) {
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(url, can_gc);
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 match self.upcast::<Element>().get_attribute(&local_name!("href")) {
189 // Step 3. If url is null and this has no href content attribute, return the
190 // empty string.
191 None => String::new(),
192
193 // Step 4. Otherwise, if url is null, return this's href content attribute's value.
194 Some(attribute) => (**attribute.value()).to_owned(),
195 }
196 },
197 // Step 5. Return url, serialized.
198 Some(ref url) => url.as_str().to_owned(),
199 })
200 }
201
202 /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-href
203 fn set_href(&self, value: USVString, can_gc: CanGc) {
204 self.upcast::<Element>()
205 .set_string_attribute(&local_name!("href"), value.into(), can_gc);
206
207 self.set_url();
208 }
209
210 /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-origin>
211 fn get_origin(&self) -> USVString {
212 // Step 1. Reinitialize url.
213 self.reinitialize_url();
214
215 USVString(match *self.get_url().borrow() {
216 // Step 2. If this's url is null, return the empty string.
217 None => "".to_owned(),
218 // Step 3. Return the serialization of this's url's origin.
219 Some(ref url) => url.origin().ascii_serialization(),
220 })
221 }
222
223 /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-password>
224 fn get_password(&self) -> USVString {
225 // Step 1. Reinitialize url.
226 self.reinitialize_url();
227
228 // Step 2. Let url be this's url.
229 match *self.get_url().borrow() {
230 // Step 3. If url is null, then return the empty string.
231 None => USVString(String::new()),
232 // Steps 4. Return url's password.
233 Some(ref url) => UrlHelper::Password(url),
234 }
235 }
236
237 /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-password>
238 fn set_password(&self, value: USVString, can_gc: CanGc) {
239 // Step 1. Reinitialize url.
240 self.reinitialize_url();
241
242 // Step 2. Let url be this's url.
243 let mut url = self.get_url().borrow_mut();
244 let url = match url.as_mut() {
245 // Step 3. If url is null or url cannot have a username/password/port, then return.
246 None => return,
247 Some(ref url) if url.host().is_none() || url.cannot_be_a_base() => return,
248 Some(url) => url,
249 };
250
251 // Step 4. Set the password, given url and the given value.
252 UrlHelper::SetPassword(url, value);
253
254 // Step 5. Update href.
255 self.update_href(url, can_gc);
256 }
257
258 /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-pathname>
259 fn get_pathname(&self) -> USVString {
260 // Step 1. Reinitialize url.
261 self.reinitialize_url();
262
263 // Step 2. Let url be this's url.
264 match *self.get_url().borrow() {
265 // Step 3. If url is null, then return the empty string.
266 None => USVString(String::new()),
267 // Steps 4. Return the result of URL path serializing url.
268 Some(ref url) => UrlHelper::Pathname(url),
269 }
270 }
271
272 /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-pathname>
273 fn set_pathname(&self, value: USVString, can_gc: CanGc) {
274 // Step 1. Reinitialize url.
275 self.reinitialize_url();
276
277 // Step 2. Let url be this's url.
278 let mut url = self.get_url().borrow_mut();
279 let url = match url.as_mut() {
280 // Step 3. If url is null or url has an opaque path, then return.
281 None => return,
282 Some(ref url) if url.cannot_be_a_base() => return,
283 Some(url) => url,
284 };
285
286 // Step 4. Set url's path to the empty list.
287 // Step 5. Basic URL parse the given value, with url as url and path start state as state override.
288 UrlHelper::SetPathname(url, value);
289
290 // Step 6. Update href.
291 self.update_href(url, can_gc);
292 }
293
294 /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-port>
295 fn get_port(&self) -> USVString {
296 // Step 1. Reinitialize url.
297 self.reinitialize_url();
298
299 // Step 2. Let url be this's url.
300 match *self.get_url().borrow() {
301 // Step 3. If url or url's port is null, return the empty string.
302 None => USVString(String::new()),
303 // Step 4. Return url's port, serialized.
304 Some(ref url) => UrlHelper::Port(url),
305 }
306 }
307
308 /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-port>
309 fn set_port(&self, value: USVString, can_gc: CanGc) {
310 // Step 1. Reinitialize url.
311 self.reinitialize_url();
312
313 // Step 2. Let url be this's url.
314 let mut url = self.get_url().borrow_mut();
315 let url = match url.as_mut() {
316 // Step 3. If url is null or url cannot have a username/password/port, then return.
317 None => return,
318 Some(ref url)
319 // https://url.spec.whatwg.org/#cannot-have-a-username-password-port
320 if url.host().is_none() || url.cannot_be_a_base() || url.scheme() == "file" =>
321 {
322 return;
323 },
324 Some(url) => url,
325 };
326
327 // Step 4. If the given value is the empty string, then set url's port to null.
328 // Step 5. Otherwise, basic URL parse the given value, with url as url and port state as
329 // state override.
330 UrlHelper::SetPort(url, value);
331
332 // Step 6. Update href.
333 self.update_href(url, can_gc);
334 }
335
336 /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-protocol>
337 fn get_protocol(&self) -> USVString {
338 // Step 1. Reinitialize url.
339 self.reinitialize_url();
340
341 match *self.get_url().borrow() {
342 // Step 2. If this's url is null, return ":".
343 None => USVString(":".to_owned()),
344 // Step 3. Return this's url's scheme, followed by ":".
345 Some(ref url) => UrlHelper::Protocol(url),
346 }
347 }
348
349 /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-protocol>
350 fn set_protocol(&self, value: USVString, can_gc: CanGc) {
351 // Step 1. Reinitialize url.
352 self.reinitialize_url();
353
354 let mut url = self.get_url().borrow_mut();
355 let url = match url.as_mut() {
356 // Step 2. If this's url is null, then return.
357 None => return,
358 Some(url) => url,
359 };
360
361 // Step 3. Basic URL parse the given value, followed by ":", with this's url as url and
362 // scheme start state as state override.
363 UrlHelper::SetProtocol(url, value);
364
365 // Step 4. Update href.
366 self.update_href(url, can_gc);
367 }
368
369 /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-search>
370 fn get_search(&self) -> USVString {
371 // Step 1. Reinitialize url.
372 self.reinitialize_url();
373
374 // Step 2. Let url be this's url.
375 match *self.get_url().borrow() {
376 // Step 3. If url is null, or url's query is either null or the empty string, return the
377 // empty string.
378 // Step 4. Return "?", followed by url's query.
379 // Note: This is handled in UrlHelper::Search
380 None => USVString(String::new()),
381 Some(ref url) => UrlHelper::Search(url),
382 }
383 }
384
385 /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-search>
386 fn set_search(&self, value: USVString, can_gc: CanGc) {
387 // Step 1. Reinitialize url.
388 self.reinitialize_url();
389
390 // Step 2. Let url be this's url.
391 let mut url = self.get_url().borrow_mut();
392 let url = match url.as_mut() {
393 // Step 3. If url is null, terminate these steps.
394 None => return,
395 Some(url) => url,
396 };
397
398 // Step 4. If the given value is the empty string, set url's query to null.
399 // Step 5. Otherwise:
400 // Note: Inner steps are handled by UrlHelper::SetSearch
401 UrlHelper::SetSearch(url, value);
402
403 // Step 6. Update href.
404 self.update_href(url, can_gc);
405 }
406
407 /// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-username>
408 fn get_username(&self) -> USVString {
409 // Step 1. Reinitialize url.
410 self.reinitialize_url();
411
412 match *self.get_url().borrow() {
413 // Step 2. If this's url is null, return the empty string.
414 None => USVString(String::new()),
415 // Step 3. Return this's url's username.
416 Some(ref url) => UrlHelper::Username(url),
417 }
418 }
419
420 /// <https://html.spec.whatwg.org/multipage/#concept-hyperlink-url-set>
421 fn set_url(&self) {
422 // Step 1. Set this element's url to null.
423 *self.get_url().borrow_mut() = None;
424
425 let attribute = self.upcast::<Element>().get_attribute(&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.value());
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, value: USVString, can_gc: CanGc) {
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(url, can_gc);
463 }
464
465 /// <https://html.spec.whatwg.org/multipage/#update-href>
466 fn update_href(&self, url: &ServoUrl, can_gc: CanGc) {
467 self.upcast::<Element>().set_string_attribute(
468 &local_name!("href"),
469 DOMString::from(url.as_str()),
470 can_gc,
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}