script: implement HTMLHyperlinkElementUtils for HTMLAreaElement (#35482)

This makes some common functionality in HTMLAreaElement and
HTMLAnchorElement shared code.

Signed-off-by: Shane Handley <shanehandley@fastmail.com>
This commit is contained in:
shanehandley 2025-02-16 20:54:31 +11:00 committed by GitHub
parent 6d6070242b
commit 5ab3641a8e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 701 additions and 1924 deletions

View file

@ -6,7 +6,7 @@ use std::cell::Cell;
use std::default::Default;
use dom_struct::dom_struct;
use html5ever::{local_name, namespace_url, ns, LocalName, Prefix};
use html5ever::{local_name, LocalName, Prefix};
use js::rust::HandleObject;
use num_traits::ToPrimitive;
use servo_atoms::Atom;
@ -28,10 +28,10 @@ use crate::dom::element::{reflect_referrer_policy_attribute, AttributeMutation,
use crate::dom::event::Event;
use crate::dom::eventtarget::EventTarget;
use crate::dom::htmlelement::HTMLElement;
use crate::dom::htmlhyperlinkelementutils::{HyperlinkElement, HyperlinkElementTraits};
use crate::dom::htmlimageelement::HTMLImageElement;
use crate::dom::mouseevent::MouseEvent;
use crate::dom::node::{BindContext, Node, NodeTraits};
use crate::dom::urlhelper::UrlHelper;
use crate::dom::node::{BindContext, Node};
use crate::dom::virtualmethods::VirtualMethods;
use crate::links::{follow_hyperlink, LinkRelations};
use crate::script_runtime::CanGc;
@ -40,7 +40,6 @@ use crate::script_runtime::CanGc;
pub(crate) struct HTMLAnchorElement {
htmlelement: HTMLElement,
rel_list: MutNullableDom<DOMTokenList>,
#[no_trace]
relations: Cell<LinkRelations>,
#[no_trace]
@ -78,48 +77,11 @@ impl HTMLAnchorElement {
can_gc,
)
}
}
/// <https://html.spec.whatwg.org/multipage/#concept-hyperlink-url-set>
fn set_url(&self) {
// Step 1. Set this element's url to null.
*self.url.borrow_mut() = None;
// Step 2. If this element's href content attribute is absent, then return.
let attribute = self
.upcast::<Element>()
.get_attribute(&ns!(), &local_name!("href"));
let Some(attribute) = attribute else {
return;
};
// 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 document = self.owner_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.url.borrow_mut() = Some(url);
}
}
// https://html.spec.whatwg.org/multipage/#reinitialise-url
fn reinitialize_url(&self) {
// Step 1.
match *self.url.borrow() {
Some(ref url) if url.scheme() == "blob" && url.cannot_be_a_base() => return,
_ => (),
}
// Step 2.
self.set_url();
}
// 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);
impl HyperlinkElement for HTMLAnchorElement {
fn get_url(&self) -> &DomRefCell<Option<ServoUrl>> {
&self.url
}
}
@ -225,362 +187,109 @@ impl HTMLAnchorElementMethods<crate::DomTypeHolder> for HTMLAnchorElement {
// https://html.spec.whatwg.org/multipage/#attr-hyperlink-target
make_setter!(SetTarget, "target");
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-hash
fn Hash(&self) -> USVString {
// Step 1.
self.reinitialize_url();
match *self.url.borrow() {
// Step 3.
None => USVString(String::new()),
Some(ref url) => {
// Steps 3-4.
UrlHelper::Hash(url)
},
}
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-hash
fn SetHash(&self, value: USVString, can_gc: CanGc) {
// Step 1.
self.reinitialize_url();
// Step 2.
let url = match self.url.borrow_mut().as_mut() {
// Step 3.
Some(ref url) if url.scheme() == "javascript" => return,
None => return,
// Steps 4-5.
Some(url) => {
UrlHelper::SetHash(url, value);
DOMString::from(url.as_str())
},
};
// Step 6.
self.update_href(url, can_gc);
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-host
fn Host(&self) -> USVString {
// Step 1.
self.reinitialize_url();
match *self.url.borrow() {
// Step 3.
None => USVString(String::new()),
Some(ref url) => {
if url.host().is_none() {
USVString(String::new())
} else {
// Steps 4-5.
UrlHelper::Host(url)
}
},
}
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-host
fn SetHost(&self, value: USVString, can_gc: CanGc) {
// Step 1.
self.reinitialize_url();
// Step 2.
let url = match self.url.borrow_mut().as_mut() {
// Step 3.
Some(ref url) if url.cannot_be_a_base() => return,
None => return,
// Step 4.
Some(url) => {
UrlHelper::SetHost(url, value);
DOMString::from(url.as_str())
},
};
// Step 5.
self.update_href(url, can_gc);
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-hostname
fn Hostname(&self) -> USVString {
// Step 1.
self.reinitialize_url();
match *self.url.borrow() {
// Step 3.
None => USVString(String::new()),
Some(ref url) => {
// Step 4.
UrlHelper::Hostname(url)
},
}
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-hostname
fn SetHostname(&self, value: USVString, can_gc: CanGc) {
// Step 1.
self.reinitialize_url();
// Step 2.
let url = match self.url.borrow_mut().as_mut() {
// Step 3.
Some(ref url) if url.cannot_be_a_base() => return,
None => return,
// Step 4.
Some(url) => {
UrlHelper::SetHostname(url, value);
DOMString::from(url.as_str())
},
};
// Step 5.
self.update_href(url, can_gc);
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-href
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-href>
fn Href(&self) -> USVString {
// Step 1.
self.reinitialize_url();
USVString(match *self.url.borrow() {
None => {
match self
.upcast::<Element>()
.get_attribute(&ns!(), &local_name!("href"))
{
// Step 3.
None => String::new(),
// Step 4.
Some(attribute) => (**attribute.value()).to_owned(),
}
},
// Step 5.
Some(ref url) => url.as_str().to_owned(),
})
self.get_href()
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-href
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-href>
fn SetHref(&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();
self.set_href(value, can_gc);
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-origin
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-origin>
fn Origin(&self) -> USVString {
// Step 1.
self.reinitialize_url();
USVString(match *self.url.borrow() {
None => {
// Step 2.
"".to_owned()
},
Some(ref url) => {
// Step 3.
url.origin().ascii_serialization()
},
})
self.get_origin()
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-password
fn Password(&self) -> USVString {
// Step 1.
self.reinitialize_url();
match *self.url.borrow() {
// Step 3.
None => USVString(String::new()),
// Steps 3-4.
Some(ref url) => UrlHelper::Password(url),
}
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-password
fn SetPassword(&self, value: USVString, can_gc: CanGc) {
// Step 1.
self.reinitialize_url();
// Step 2.
let url = match self.url.borrow_mut().as_mut() {
// Step 3.
Some(ref url) if url.host().is_none() || url.cannot_be_a_base() => return,
None => return,
// Step 4.
Some(url) => {
UrlHelper::SetPassword(url, value);
DOMString::from(url.as_str())
},
};
// Step 5.
self.update_href(url, can_gc);
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-pathname
fn Pathname(&self) -> USVString {
// Step 1.
self.reinitialize_url();
match *self.url.borrow() {
// Step 3.
None => USVString(String::new()),
// Steps 4-5.
Some(ref url) => UrlHelper::Pathname(url),
}
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-pathname
fn SetPathname(&self, value: USVString, can_gc: CanGc) {
// Step 1.
self.reinitialize_url();
// Step 2.
let url = match self.url.borrow_mut().as_mut() {
// Step 3.
Some(ref url) if url.cannot_be_a_base() => return,
None => return,
// Step 5.
Some(url) => {
UrlHelper::SetPathname(url, value);
DOMString::from(url.as_str())
},
};
// Step 6.
self.update_href(url, can_gc);
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-port
fn Port(&self) -> USVString {
// Step 1.
self.reinitialize_url();
match *self.url.borrow() {
// Step 3.
None => USVString(String::new()),
// Step 4.
Some(ref url) => UrlHelper::Port(url),
}
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-port
fn SetPort(&self, value: USVString, can_gc: CanGc) {
// Step 1.
self.reinitialize_url();
// Step 3.
let url = match self.url.borrow_mut().as_mut() {
Some(ref url)
if url.host().is_none() || url.cannot_be_a_base() || url.scheme() == "file" =>
{
return;
},
None => return,
// Step 4.
Some(url) => {
UrlHelper::SetPort(url, value);
DOMString::from(url.as_str())
},
};
// Step 5.
self.update_href(url, can_gc);
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-protocol
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-protocol>
fn Protocol(&self) -> USVString {
// Step 1.
self.reinitialize_url();
match *self.url.borrow() {
// Step 2.
None => USVString(":".to_owned()),
// Step 3.
Some(ref url) => UrlHelper::Protocol(url),
}
self.get_protocol()
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-protocol
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-protocol>
fn SetProtocol(&self, value: USVString, can_gc: CanGc) {
// Step 1.
self.reinitialize_url();
let url = match self.url.borrow_mut().as_mut() {
// Step 2.
None => return,
// Step 3.
Some(url) => {
UrlHelper::SetProtocol(url, value);
DOMString::from(url.as_str())
},
};
// Step 4.
self.update_href(url, can_gc);
self.set_protocol(value, can_gc);
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-search
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-password>
fn Password(&self) -> USVString {
self.get_password()
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-password>
fn SetPassword(&self, value: USVString, can_gc: CanGc) {
self.set_password(value, can_gc);
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-hash>
fn Hash(&self) -> USVString {
self.get_hash()
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-hash>
fn SetHash(&self, value: USVString, can_gc: CanGc) {
self.set_hash(value, can_gc);
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-host>
fn Host(&self) -> USVString {
self.get_host()
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-host>
fn SetHost(&self, value: USVString, can_gc: CanGc) {
self.set_host(value, can_gc);
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-hostname>
fn Hostname(&self) -> USVString {
self.get_hostname()
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-hostname>
fn SetHostname(&self, value: USVString, can_gc: CanGc) {
self.set_hostname(value, can_gc);
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-port>
fn Port(&self) -> USVString {
self.get_port()
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-port>
fn SetPort(&self, value: USVString, can_gc: CanGc) {
self.set_port(value, can_gc);
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-pathname>
fn Pathname(&self) -> USVString {
self.get_pathname()
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-pathname>
fn SetPathname(&self, value: USVString, can_gc: CanGc) {
self.set_pathname(value, can_gc);
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-search>
fn Search(&self) -> USVString {
// Step 1.
self.reinitialize_url();
match *self.url.borrow() {
// Step 2.
None => USVString(String::new()),
// Step 3.
Some(ref url) => UrlHelper::Search(url),
}
self.get_search()
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-search
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-search>
fn SetSearch(&self, value: USVString, can_gc: CanGc) {
// Step 1.
self.reinitialize_url();
// Step 2.
let url = match self.url.borrow_mut().as_mut() {
// Step 3.
None => return,
// Steps 4-5.
// TODO add this element's node document character encoding as
// encoding override (as described in the spec)
Some(url) => {
UrlHelper::SetSearch(url, value);
DOMString::from(url.as_str())
},
};
// Step 6.
self.update_href(url, can_gc);
self.set_search(value, can_gc);
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-username
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-username>
fn Username(&self) -> USVString {
// Step 1.
self.reinitialize_url();
match *self.url.borrow() {
// Step 2.
None => USVString(String::new()),
// Step 3.
Some(ref url) => UrlHelper::Username(url),
}
self.get_username()
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-username
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-username>
fn SetUsername(&self, value: USVString, can_gc: CanGc) {
// Step 1.
self.reinitialize_url();
// Step 2.
let url = match self.url.borrow_mut().as_mut() {
// Step 3.
Some(ref url) if url.host().is_none() || url.cannot_be_a_base() => return,
None => return,
// Step 4.
Some(url) => {
UrlHelper::SetUsername(url, value);
DOMString::from(url.as_str())
},
};
// Step 5.
self.update_href(url, can_gc);
self.set_username(value, can_gc);
}
// https://html.spec.whatwg.org/multipage/#dom-a-referrerpolicy

View file

@ -12,20 +12,23 @@ use euclid::default::Point2D;
use html5ever::{local_name, LocalName, Prefix};
use js::rust::HandleObject;
use servo_atoms::Atom;
use servo_url::ServoUrl;
use style::attr::AttrValue;
use crate::dom::activation::Activatable;
use crate::dom::attr::Attr;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::HTMLAreaElementBinding::HTMLAreaElementMethods;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::root::{DomRoot, MutNullableDom};
use crate::dom::bindings::str::DOMString;
use crate::dom::bindings::str::{DOMString, USVString};
use crate::dom::document::Document;
use crate::dom::domtokenlist::DOMTokenList;
use crate::dom::element::{reflect_referrer_policy_attribute, AttributeMutation, Element};
use crate::dom::event::Event;
use crate::dom::eventtarget::EventTarget;
use crate::dom::htmlelement::HTMLElement;
use crate::dom::htmlhyperlinkelementutils::{HyperlinkElement, HyperlinkElementTraits};
use crate::dom::node::{BindContext, Node};
use crate::dom::virtualmethods::VirtualMethods;
use crate::links::{follow_hyperlink, LinkRelations};
@ -107,7 +110,7 @@ impl Area {
index += 1;
}
// The input does not consist any valid charecters
// The input does not consist any valid characters
if array.is_empty() {
break;
}
@ -240,9 +243,10 @@ impl Area {
pub(crate) struct HTMLAreaElement {
htmlelement: HTMLElement,
rel_list: MutNullableDom<DOMTokenList>,
#[no_trace]
relations: Cell<LinkRelations>,
#[no_trace]
url: DomRefCell<Option<ServoUrl>>,
}
impl HTMLAreaElement {
@ -255,6 +259,7 @@ impl HTMLAreaElement {
htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
rel_list: Default::default(),
relations: Cell::new(LinkRelations::empty()),
url: DomRefCell::new(None),
}
}
@ -295,6 +300,12 @@ impl HTMLAreaElement {
}
}
impl HyperlinkElement for HTMLAreaElement {
fn get_url(&self) -> &DomRefCell<Option<ServoUrl>> {
&self.url
}
}
impl VirtualMethods for HTMLAreaElement {
fn super_type(&self) -> Option<&dyn VirtualMethods> {
Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
@ -348,7 +359,7 @@ impl HTMLAreaElementMethods<crate::DomTypeHolder> for HTMLAreaElement {
.set_tokenlist_attribute(&local_name!("rel"), rel, can_gc);
}
// https://html.spec.whatwg.org/multipage/#dom-area-rellist
/// <https://html.spec.whatwg.org/multipage/#dom-area-rellist>
fn RelList(&self) -> DomRoot<DOMTokenList> {
self.rel_list.or_init(|| {
DOMTokenList::new(
@ -370,6 +381,111 @@ impl HTMLAreaElementMethods<crate::DomTypeHolder> for HTMLAreaElement {
// https://html.spec.whatwg.org/multipage/#attr-iframe-referrerpolicy
make_setter!(SetReferrerPolicy, "referrerpolicy");
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-href>
fn Href(&self) -> USVString {
self.get_href()
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-href>
fn SetHref(&self, value: USVString, can_gc: CanGc) {
self.set_href(value, can_gc);
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-origin>
fn Origin(&self) -> USVString {
self.get_origin()
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-protocol>
fn Protocol(&self) -> USVString {
self.get_protocol()
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-protocol>
fn SetProtocol(&self, value: USVString, can_gc: CanGc) {
self.set_protocol(value, can_gc);
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-password>
fn Password(&self) -> USVString {
self.get_password()
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-password>
fn SetPassword(&self, value: USVString, can_gc: CanGc) {
self.set_password(value, can_gc);
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-hash>
fn Hash(&self) -> USVString {
self.get_hash()
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-hash>
fn SetHash(&self, value: USVString, can_gc: CanGc) {
self.set_hash(value, can_gc);
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-host>
fn Host(&self) -> USVString {
self.get_host()
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-host>
fn SetHost(&self, value: USVString, can_gc: CanGc) {
self.set_host(value, can_gc);
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-hostname>
fn Hostname(&self) -> USVString {
self.get_hostname()
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-hostname>
fn SetHostname(&self, value: USVString, can_gc: CanGc) {
self.set_hostname(value, can_gc);
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-port>
fn Port(&self) -> USVString {
self.get_port()
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-port>
fn SetPort(&self, value: USVString, can_gc: CanGc) {
self.set_port(value, can_gc);
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-pathname>
fn Pathname(&self) -> USVString {
self.get_pathname()
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-pathname>
fn SetPathname(&self, value: USVString, can_gc: CanGc) {
self.set_pathname(value, can_gc);
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-search>
fn Search(&self) -> USVString {
self.get_search()
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-search>
fn SetSearch(&self, value: USVString, can_gc: CanGc) {
self.set_search(value, can_gc);
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-username>
fn Username(&self) -> USVString {
self.get_username()
}
/// <https://html.spec.whatwg.org/multipage/#dom-hyperlink-username>
fn SetUsername(&self, value: USVString, can_gc: CanGc) {
self.set_username(value, can_gc);
}
}
impl Activatable for HTMLAreaElement {

View file

@ -0,0 +1,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();
}
}

View file

@ -370,6 +370,7 @@ pub(crate) mod htmlheadelement;
pub(crate) mod htmlheadingelement;
pub(crate) mod htmlhrelement;
pub(crate) mod htmlhtmlelement;
pub(crate) mod htmlhyperlinkelementutils;
pub(crate) mod htmliframeelement;
pub(crate) mod htmlimageelement;
pub(crate) mod htmlinputelement;