use std::default::Default;
use dom_struct::dom_struct;
use html5ever::{local_name, namespace_url, ns, LocalName, Prefix};
use js::rust::HandleObject;
use net_traits::request::Referrer;
use num_traits::ToPrimitive;
use script_traits::{HistoryEntryReplacement, LoadData, LoadOrigin};
use servo_atoms::Atom;
use servo_url::ServoUrl;
use style::attr::AttrValue;
use crate::dom::activation::Activatable;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::AttrBinding::AttrMethods;
use crate::dom::bindings::codegen::Bindings::HTMLAnchorElementBinding::HTMLAnchorElementMethods;
use crate::dom::bindings::codegen::Bindings::MouseEventBinding::MouseEventMethods;
use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::bindings::root::{DomRoot, MutNullableDom};
use crate::dom::bindings::str::{DOMString, USVString};
use crate::dom::document::Document;
use crate::dom::domtokenlist::DOMTokenList;
use crate::dom::element::{referrer_policy_for_element, Element};
use crate::dom::event::Event;
use crate::dom::eventtarget::EventTarget;
use crate::dom::globalscope::GlobalScope;
use crate::dom::htmlareaelement::HTMLAreaElement;
use crate::dom::htmlelement::HTMLElement;
use crate::dom::htmlformelement::HTMLFormElement;
use crate::dom::htmlimageelement::HTMLImageElement;
use crate::dom::mouseevent::MouseEvent;
use crate::dom::node::{document_from_node, Node};
use crate::dom::urlhelper::UrlHelper;
use crate::dom::virtualmethods::VirtualMethods;
use crate::task_source::TaskSource;
#[dom_struct]
pub struct HTMLAnchorElement {
htmlelement: HTMLElement,
rel_list: MutNullableDom<DOMTokenList>,
#[no_trace]
url: DomRefCell<Option<ServoUrl>>,
}
impl HTMLAnchorElement {
fn new_inherited(
local_name: LocalName,
prefix: Option<Prefix>,
document: &Document,
) -> HTMLAnchorElement {
HTMLAnchorElement {
htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
rel_list: Default::default(),
url: DomRefCell::new(None),
}
}
#[allow(crown::unrooted_must_root)]
pub fn new(
local_name: LocalName,
prefix: Option<Prefix>,
document: &Document,
proto: Option<HandleObject>,
) -> DomRoot<HTMLAnchorElement> {
Node::reflect_node_with_proto(
Box::new(HTMLAnchorElement::new_inherited(
local_name, prefix, document,
)),
document,
proto,
)
}
fn set_url(&self) {
let attribute = self
.upcast::<Element>()
.get_attribute(&ns!(), &local_name!("href"));
*self.url.borrow_mut() = attribute.and_then(|attribute| {
let document = document_from_node(self);
document.base_url().join(&attribute.value()).ok()
});
}
fn reinitialize_url(&self) {
match *self.url.borrow() {
Some(ref url) if url.scheme() == "blob" && url.cannot_be_a_base() => return,
_ => (),
}
self.set_url();
}
fn update_href(&self, url: DOMString) {
self.upcast::<Element>()
.set_string_attribute(&local_name!("href"), url);
}
}
impl VirtualMethods for HTMLAnchorElement {
fn super_type(&self) -> Option<&dyn VirtualMethods> {
Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
}
fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
match name {
&local_name!("rel") => AttrValue::from_serialized_tokenlist(value.into()),
_ => self
.super_type()
.unwrap()
.parse_plain_attribute(name, value),
}
}
}
impl HTMLAnchorElementMethods for HTMLAnchorElement {
fn Text(&self) -> DOMString {
self.upcast::<Node>().GetTextContent().unwrap()
}
fn SetText(&self, value: DOMString) {
self.upcast::<Node>().SetTextContent(Some(value))
}
make_getter!(Rel, "rel");
fn SetRel(&self, rel: DOMString) {
self.upcast::<Element>()
.set_tokenlist_attribute(&local_name!("rel"), rel);
}
fn RelList(&self) -> DomRoot<DOMTokenList> {
self.rel_list.or_init(|| {
DOMTokenList::new(
self.upcast(),
&local_name!("rel"),
Some(vec![
Atom::from("noopener"),
Atom::from("noreferrer"),
Atom::from("opener"),
]),
)
})
}
make_getter!(Coords, "coords");
make_setter!(SetCoords, "coords");
make_getter!(Name, "name");
make_atomic_setter!(SetName, "name");
make_getter!(Rev, "rev");
make_setter!(SetRev, "rev");
make_getter!(Shape, "shape");
make_setter!(SetShape, "shape");
make_getter!(Target, "target");
make_setter!(SetTarget, "target");
fn Hash(&self) -> USVString {
self.reinitialize_url();
match *self.url.borrow() {
None => USVString(String::new()),
Some(ref url) => {
UrlHelper::Hash(url)
},
}
}
fn SetHash(&self, value: USVString) {
self.reinitialize_url();
let url = match self.url.borrow_mut().as_mut() {
Some(ref url) if url.scheme() == "javascript" => return,
None => return,
Some(url) => {
UrlHelper::SetHash(url, value);
DOMString::from(url.as_str())
},
};
self.update_href(url);
}
fn Host(&self) -> USVString {
self.reinitialize_url();
match *self.url.borrow() {
None => USVString(String::new()),
Some(ref url) => {
if url.host().is_none() {
USVString(String::new())
} else {
UrlHelper::Host(url)
}
},
}
}
fn SetHost(&self, value: USVString) {
self.reinitialize_url();
let url = match self.url.borrow_mut().as_mut() {
Some(ref url) if url.cannot_be_a_base() => return,
None => return,
Some(url) => {
UrlHelper::SetHost(url, value);
DOMString::from(url.as_str())
},
};
self.update_href(url);
}
fn Hostname(&self) -> USVString {
self.reinitialize_url();
match *self.url.borrow() {
None => USVString(String::new()),
Some(ref url) => {
UrlHelper::Hostname(url)
},
}
}
fn SetHostname(&self, value: USVString) {
self.reinitialize_url();
let url = match self.url.borrow_mut().as_mut() {
Some(ref url) if url.cannot_be_a_base() => return,
None => return,
Some(url) => {
UrlHelper::SetHostname(url, value);
DOMString::from(url.as_str())
},
};
self.update_href(url);
}
fn Href(&self) -> USVString {
self.reinitialize_url();
USVString(match *self.url.borrow() {
None => {
match self
.upcast::<Element>()
.get_attribute(&ns!(), &local_name!("href"))
{
None => String::new(),
Some(attribute) => (**attribute.value()).to_owned(),
}
},
Some(ref url) => url.as_str().to_owned(),
})
}
fn SetHref(&self, value: USVString) {
self.upcast::<Element>()
.set_string_attribute(&local_name!("href"), DOMString::from_string(value.0));
self.set_url();
}
fn Origin(&self) -> USVString {
self.reinitialize_url();
USVString(match *self.url.borrow() {
None => {
"".to_owned()
},
Some(ref url) => {
url.origin().ascii_serialization()
},
})
}
fn Password(&self) -> USVString {
self.reinitialize_url();
match *self.url.borrow() {
None => USVString(String::new()),
Some(ref url) => UrlHelper::Password(url),
}
}
fn SetPassword(&self, value: USVString) {
self.reinitialize_url();
let url = match self.url.borrow_mut().as_mut() {
Some(ref url) if url.host().is_none() || url.cannot_be_a_base() => return,
None => return,
Some(url) => {
UrlHelper::SetPassword(url, value);
DOMString::from(url.as_str())
},
};
self.update_href(url);
}
fn Pathname(&self) -> USVString {
self.reinitialize_url();
match *self.url.borrow() {
None => USVString(String::new()),
Some(ref url) => UrlHelper::Pathname(url),
}
}
fn SetPathname(&self, value: USVString) {
self.reinitialize_url();
let url = match self.url.borrow_mut().as_mut() {
Some(ref url) if url.cannot_be_a_base() => return,
None => return,
Some(url) => {
UrlHelper::SetPathname(url, value);
DOMString::from(url.as_str())
},
};
self.update_href(url);
}
fn Port(&self) -> USVString {
self.reinitialize_url();
match *self.url.borrow() {
None => USVString(String::new()),
Some(ref url) => UrlHelper::Port(url),
}
}
fn SetPort(&self, value: USVString) {
self.reinitialize_url();
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,
Some(url) => {
UrlHelper::SetPort(url, value);
DOMString::from(url.as_str())
},
};
self.update_href(url);
}
fn Protocol(&self) -> USVString {
self.reinitialize_url();
match *self.url.borrow() {
None => USVString(":".to_owned()),
Some(ref url) => UrlHelper::Protocol(url),
}
}
fn SetProtocol(&self, value: USVString) {
self.reinitialize_url();
let url = match self.url.borrow_mut().as_mut() {
None => return,
Some(url) => {
UrlHelper::SetProtocol(url, value);
DOMString::from(url.as_str())
},
};
self.update_href(url);
}
fn Search(&self) -> USVString {
self.reinitialize_url();
match *self.url.borrow() {
None => USVString(String::new()),
Some(ref url) => UrlHelper::Search(url),
}
}
fn SetSearch(&self, value: USVString) {
self.reinitialize_url();
let url = match self.url.borrow_mut().as_mut() {
None => return,
Some(url) => {
UrlHelper::SetSearch(url, value);
DOMString::from(url.as_str())
},
};
self.update_href(url);
}
fn Username(&self) -> USVString {
self.reinitialize_url();
match *self.url.borrow() {
None => USVString(String::new()),
Some(ref url) => UrlHelper::Username(url),
}
}
fn SetUsername(&self, value: USVString) {
self.reinitialize_url();
let url = match self.url.borrow_mut().as_mut() {
Some(ref url) if url.host().is_none() || url.cannot_be_a_base() => return,
None => return,
Some(url) => {
UrlHelper::SetUsername(url, value);
DOMString::from(url.as_str())
},
};
self.update_href(url);
}
}
impl Activatable for HTMLAnchorElement {
fn as_element(&self) -> &Element {
self.upcast::<Element>()
}
fn is_instance_activatable(&self) -> bool {
self.as_element().has_attribute(&local_name!("href"))
}
fn activation_behavior(&self, event: &Event, target: &EventTarget) {
let element = self.as_element();
let mouse_event = event.downcast::<MouseEvent>().unwrap();
let mut ismap_suffix = None;
if let Some(element) = target.downcast::<Element>() {
if target.is::<HTMLImageElement>() && element.has_attribute(&local_name!("ismap")) {
let target_node = element.upcast::<Node>();
let rect = target_node.bounding_content_box_or_zero();
ismap_suffix = Some(format!(
"?{},{}",
mouse_event.ClientX().to_f32().unwrap() - rect.origin.x.to_f32_px(),
mouse_event.ClientY().to_f32().unwrap() - rect.origin.y.to_f32_px()
))
}
}
follow_hyperlink(element, ismap_suffix);
}
}
pub fn get_element_target(subject: &Element) -> Option<DOMString> {
if !(subject.is::<HTMLAreaElement>() ||
subject.is::<HTMLAnchorElement>() ||
subject.is::<HTMLFormElement>())
{
return None;
}
if subject.has_attribute(&local_name!("target")) {
return Some(subject.get_string_attribute(&local_name!("target")));
}
let doc = document_from_node(subject).base_element();
match doc {
Some(doc) => {
let element = doc.upcast::<Element>();
if element.has_attribute(&local_name!("target")) {
Some(element.get_string_attribute(&local_name!("target")))
} else {
None
}
},
None => None,
}
}
pub fn get_element_noopener(subject: &Element, target_attribute_value: Option<DOMString>) -> bool {
if !(subject.is::<HTMLAreaElement>() ||
subject.is::<HTMLAnchorElement>() ||
subject.is::<HTMLFormElement>())
{
return false;
}
let target_is_blank = target_attribute_value
.as_ref()
.map_or(false, |target| target.to_lowercase() == "_blank");
let link_types = match subject.get_attribute(&ns!(), &local_name!("rel")) {
Some(rel) => rel.Value(),
None => return target_is_blank,
};
link_types.contains("noreferrer") ||
link_types.contains("noopener") ||
(!link_types.contains("opener") && target_is_blank)
}
pub fn follow_hyperlink(subject: &Element, hyperlink_suffix: Option<String>) {
if subject.cannot_navigate() {
return;
}
let document = document_from_node(subject);
let window = document.window();
let source = document.browsing_context().unwrap();
let target_attribute_value =
if subject.is::<HTMLAreaElement>() || subject.is::<HTMLAnchorElement>() {
get_element_target(subject)
} else {
None
};
let noopener = get_element_noopener(subject, target_attribute_value.clone());
let (maybe_chosen, replace) = match target_attribute_value {
Some(name) => {
let (maybe_chosen, new) = source.choose_browsing_context(name, noopener);
let replace = if new {
HistoryEntryReplacement::Enabled
} else {
HistoryEntryReplacement::Disabled
};
(maybe_chosen, replace)
},
None => (
Some(window.window_proxy()),
HistoryEntryReplacement::Disabled,
),
};
let chosen = match maybe_chosen {
Some(proxy) => proxy,
None => return,
};
if let Some(target_document) = chosen.document() {
let target_window = target_document.window();
let attribute = subject.get_attribute(&ns!(), &local_name!("href")).unwrap();
let mut href = attribute.Value();
if let Some(suffix) = hyperlink_suffix {
href.push_str(&suffix);
}
let url = match document.base_url().join(&href) {
Ok(url) => url,
Err(_) => return,
};
let referrer_policy = referrer_policy_for_element(subject);
let referrer = match subject.get_attribute(&ns!(), &local_name!("rel")) {
Some(ref link_types) if link_types.Value().contains("noreferrer") => {
Referrer::NoReferrer
},
_ => target_window.upcast::<GlobalScope>().get_referrer(),
};
let pipeline_id = target_window.upcast::<GlobalScope>().pipeline_id();
let secure = target_window.upcast::<GlobalScope>().is_secure_context();
let load_data = LoadData::new(
LoadOrigin::Script(document.origin().immutable().clone()),
url,
Some(pipeline_id),
referrer,
referrer_policy,
Some(secure),
);
let target = Trusted::new(target_window);
let task = task!(navigate_follow_hyperlink: move || {
debug!("following hyperlink to {}", load_data.url);
target.root().load_url(replace, false, load_data);
});
target_window
.task_manager()
.dom_manipulation_task_source()
.queue(task, target_window.upcast())
.unwrap();
};
}