Implement referrer policy for follow_hyperlink

This commit is contained in:
Peijun Ma 2019-01-30 00:23:01 -05:00
parent fe7f3ab262
commit 0bafa61f7c
No known key found for this signature in database
GPG key ID: EC4EE0BAC1F72B02
2 changed files with 11 additions and 22 deletions

View file

@ -3,9 +3,9 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
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::DOMTokenListBinding::DOMTokenListMethods;
use crate::dom::bindings::codegen::Bindings::HTMLAnchorElementBinding;
use crate::dom::bindings::codegen::Bindings::HTMLAnchorElementBinding::HTMLAnchorElementMethods;
use crate::dom::bindings::codegen::Bindings::MouseEventBinding::MouseEventMethods;
@ -13,6 +13,7 @@ use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::root::{DomRoot, MutNullableDom};
use crate::dom::bindings::str::{DOMString, USVString};
use crate::dom::document::determine_policy_for_token;
use crate::dom::document::Document;
use crate::dom::domtokenlist::DOMTokenList;
use crate::dom::element::Element;
@ -26,7 +27,6 @@ use crate::dom::urlhelper::UrlHelper;
use crate::dom::virtualmethods::VirtualMethods;
use dom_struct::dom_struct;
use html5ever::{LocalName, Prefix};
use net_traits::ReferrerPolicy;
use num_traits::ToPrimitive;
use servo_url::ServoUrl;
use std::default::Default;
@ -571,15 +571,9 @@ impl Activatable for HTMLAnchorElement {
}
}
// https://w3c.github.io/webappsec-referrer-policy/#referrer-policy-delivery
let referrer_policy = match self.RelList().Contains("noreferrer".into()) {
true => Some(ReferrerPolicy::NoReferrer),
false => None,
};
// Step 2.
//TODO: Download the link is `download` attribute is set.
follow_hyperlink(element, ismap_suffix, referrer_policy);
follow_hyperlink(element, ismap_suffix);
}
//TODO:https://html.spec.whatwg.org/multipage/#the-a-element
@ -594,11 +588,7 @@ impl Activatable for HTMLAnchorElement {
}
/// <https://html.spec.whatwg.org/multipage/#following-hyperlinks-2>
pub fn follow_hyperlink(
subject: &Element,
hyperlink_suffix: Option<String>,
referrer_policy: Option<ReferrerPolicy>,
) {
pub fn follow_hyperlink(subject: &Element, hyperlink_suffix: Option<String>) {
// Step 1.
if subject.cannot_navigate() {
return;
@ -645,7 +635,7 @@ pub fn follow_hyperlink(
// Step 10, 11, 12, 13. 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 12: append a hyperlink suffix.
// 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);
@ -655,6 +645,11 @@ pub fn follow_hyperlink(
Err(_) => return,
};
// Step 12.
let referrer_policy = subject
.get_attribute_by_name(DOMString::from_string(String::from("referrerpolicy")))
.and_then(|attribute: DomRoot<Attr>| (determine_policy_for_token(&attribute.Value())));
// Step 13, 14.
debug!("following hyperlink to {}", url);
target_window.load_url(url, replace, false, referrer_policy);

View file

@ -3,7 +3,6 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use crate::dom::activation::Activatable;
use crate::dom::bindings::codegen::Bindings::DOMTokenListBinding::DOMTokenListMethods;
use crate::dom::bindings::codegen::Bindings::HTMLAreaElementBinding;
use crate::dom::bindings::codegen::Bindings::HTMLAreaElementBinding::HTMLAreaElementMethods;
use crate::dom::bindings::inheritance::Castable;
@ -21,7 +20,6 @@ use crate::dom::virtualmethods::VirtualMethods;
use dom_struct::dom_struct;
use euclid::Point2D;
use html5ever::{LocalName, Prefix};
use net_traits::ReferrerPolicy;
use std::default::Default;
use std::f32;
use std::str;
@ -332,10 +330,6 @@ impl Activatable for HTMLAreaElement {
}
fn activation_behavior(&self, _event: &Event, _target: &EventTarget) {
let referrer_policy = match self.RelList().Contains("noreferrer".into()) {
true => Some(ReferrerPolicy::NoReferrer),
false => None,
};
follow_hyperlink(self.as_element(), None, referrer_policy);
follow_hyperlink(self.as_element(), None);
}
}