mirror of
https://github.com/servo/servo.git
synced 2025-08-06 06:00:15 +01:00
Properly track rel
keywords for <a>
/<area>
/<form>
elements (#33462)
* Use linkrelations for all linkable elements Signed-off-by: Simon Wülker <simon.wuelker@arcor.de> * Document LinkRelations Signed-off-by: Simon Wülker <simon.wuelker@arcor.de> * Rename linkrelations.rs -> links.rs This module is supposed to include general-purpose link related stuff. Signed-off-by: Simon Wülker <simon.wuelker@arcor.de> * Parse all "rel" keywords Signed-off-by: Simon Wülker <simon.wuelker@arcor.de> --------- Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>
This commit is contained in:
parent
d3d6a22d27
commit
a165982622
7 changed files with 540 additions and 300 deletions
|
@ -2,48 +2,46 @@
|
|||
* 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 std::cell::Cell;
|
||||
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::attr::Attr;
|
||||
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::element::{AttributeMutation, 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::node::{document_from_node, BindContext, Node};
|
||||
use crate::dom::urlhelper::UrlHelper;
|
||||
use crate::dom::virtualmethods::VirtualMethods;
|
||||
use crate::task_source::TaskSource;
|
||||
use crate::links::{follow_hyperlink, LinkRelations};
|
||||
|
||||
#[dom_struct]
|
||||
pub struct HTMLAnchorElement {
|
||||
htmlelement: HTMLElement,
|
||||
rel_list: MutNullableDom<DOMTokenList>,
|
||||
|
||||
#[no_trace]
|
||||
relations: Cell<LinkRelations>,
|
||||
#[no_trace]
|
||||
url: DomRefCell<Option<ServoUrl>>,
|
||||
}
|
||||
|
@ -57,6 +55,7 @@ impl HTMLAnchorElement {
|
|||
HTMLAnchorElement {
|
||||
htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
|
||||
rel_list: Default::default(),
|
||||
relations: Cell::new(LinkRelations::empty()),
|
||||
url: DomRefCell::new(None),
|
||||
}
|
||||
}
|
||||
|
@ -121,6 +120,27 @@ impl VirtualMethods for HTMLAnchorElement {
|
|||
.parse_plain_attribute(name, value),
|
||||
}
|
||||
}
|
||||
|
||||
fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation) {
|
||||
self.super_type().unwrap().attribute_mutated(attr, mutation);
|
||||
|
||||
match *attr.local_name() {
|
||||
local_name!("rel") | local_name!("rev") => {
|
||||
self.relations
|
||||
.set(LinkRelations::for_element(self.upcast()));
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
|
||||
fn bind_to_tree(&self, context: &BindContext) {
|
||||
if let Some(s) = self.super_type() {
|
||||
s.bind_to_tree(context);
|
||||
}
|
||||
|
||||
self.relations
|
||||
.set(LinkRelations::for_element(self.upcast()));
|
||||
}
|
||||
}
|
||||
|
||||
impl HTMLAnchorElementMethods for HTMLAnchorElement {
|
||||
|
@ -580,153 +600,6 @@ impl Activatable for HTMLAnchorElement {
|
|||
|
||||
// Step 2.
|
||||
//TODO: Download the link is `download` attribute is set.
|
||||
follow_hyperlink(element, ismap_suffix);
|
||||
follow_hyperlink(element, self.relations.get(), ismap_suffix);
|
||||
}
|
||||
}
|
||||
|
||||
/// <https://html.spec.whatwg.org/multipage/#get-an-element's-target>
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
/// <https://html.spec.whatwg.org/multipage/#get-an-element's-noopener>
|
||||
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()
|
||||
.is_some_and(|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)
|
||||
}
|
||||
|
||||
/// <https://html.spec.whatwg.org/multipage/#following-hyperlinks-2>
|
||||
pub fn follow_hyperlink(subject: &Element, hyperlink_suffix: Option<String>) {
|
||||
// Step 1.
|
||||
if subject.cannot_navigate() {
|
||||
return;
|
||||
}
|
||||
// Step 2, done in Step 7.
|
||||
|
||||
let document = document_from_node(subject);
|
||||
let window = document.window();
|
||||
|
||||
// Step 3: source browsing context.
|
||||
let source = document.browsing_context().unwrap();
|
||||
|
||||
// Step 4-5: target attribute.
|
||||
let target_attribute_value =
|
||||
if subject.is::<HTMLAreaElement>() || subject.is::<HTMLAnchorElement>() {
|
||||
get_element_target(subject)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
// Step 6.
|
||||
let noopener = get_element_noopener(subject, target_attribute_value.clone());
|
||||
|
||||
// Step 7.
|
||||
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,
|
||||
),
|
||||
};
|
||||
|
||||
// Step 8.
|
||||
let chosen = match maybe_chosen {
|
||||
Some(proxy) => proxy,
|
||||
None => return,
|
||||
};
|
||||
|
||||
if let Some(target_document) = chosen.document() {
|
||||
let target_window = target_document.window();
|
||||
// Step 9, dis-owning target's opener, if necessary
|
||||
// will have been done as part of Step 7 above
|
||||
// in choose_browsing_context/create_auxiliary_browsing_context.
|
||||
|
||||
// Step 10, 11. TODO: if parsing the URL failed, navigate to error page.
|
||||
let attribute = subject.get_attribute(&ns!(), &local_name!("href")).unwrap();
|
||||
let mut href = attribute.Value();
|
||||
// Step 11: append a hyperlink suffix.
|
||||
// https://www.w3.org/Bugs/Public/show_bug.cgi?id=28925
|
||||
if let Some(suffix) = hyperlink_suffix {
|
||||
href.push_str(&suffix);
|
||||
}
|
||||
let url = match document.base_url().join(&href) {
|
||||
Ok(url) => url,
|
||||
Err(_) => return,
|
||||
};
|
||||
|
||||
// Step 12.
|
||||
let referrer_policy = referrer_policy_for_element(subject);
|
||||
|
||||
// Step 13
|
||||
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(),
|
||||
};
|
||||
|
||||
// Step 14
|
||||
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();
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue