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