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