Auto merge of #11542 - asajeffrey:mozbrowser-send-opentab-event, r=paulrouget

Fire a mozbrowseropenwindow event when an html anchor has a non-self target

<!-- Please describe your changes on the following line: -->
When an html anchor has a non-self target, fire a `mozbrowseropenwindow` event.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #11539.
- [X] These changes do not require tests because we don't have the infrastructure for mozbrowser testing yet.

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/11542)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-06-02 09:45:26 -05:00
commit bdecfa13d2
9 changed files with 115 additions and 8 deletions

View file

@ -26,9 +26,11 @@ use dom::node::{Node, document_from_node, window_from_node};
use dom::urlhelper::UrlHelper;
use dom::virtualmethods::VirtualMethods;
use num_traits::ToPrimitive;
use script_traits::MozBrowserEvent;
use std::default::Default;
use string_cache::Atom;
use url::Url;
use util::prefs::mozbrowser_enabled;
#[dom_struct]
pub struct HTMLAnchorElement {
@ -542,30 +544,48 @@ impl Activatable for HTMLAnchorElement {
}
}
/// https://html.spec.whatwg.org/multipage/#the-rules-for-choosing-a-browsing-context-given-a-browsing-context-name
fn is_current_browsing_context(target: DOMString) -> bool {
target.is_empty() || target == "_self"
}
/// https://html.spec.whatwg.org/multipage/#following-hyperlinks-2
fn follow_hyperlink(subject: &Element, hyperlink_suffix: Option<String>) {
// Step 1: replace.
// Step 2: source browsing context.
// Step 3: target browsing context.
let target = subject.get_attribute(&ns!(), &atom!("target"));
// Step 4.
// Step 4: disown target's opener if needed.
let attribute = subject.get_attribute(&ns!(), &atom!("href")).unwrap();
let mut href = attribute.Value();
// Step 6.
// Step 7: 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);
}
// Step 4-5.
// Step 5: parse the URL.
// Step 6: navigate to an error document if parsing failed.
let document = document_from_node(subject);
let url = match document.url().join(&href) {
Ok(url) => url,
Err(_) => return,
};
// Step 7.
// Step 8: navigate to the URL.
if let Some(target) = target {
if mozbrowser_enabled() && !is_current_browsing_context(target.Value()) {
// https://developer.mozilla.org/en-US/docs/Web/Events/mozbrowseropenwindow
// TODO: referrer and opener
// TODO: should we send the normalized url or the non-normalized href?
let event = MozBrowserEvent::OpenWindow(url.into_string(), Some(String::from(target.Value())), None);
document.trigger_mozbrowser_event(event);
return
}
}
debug!("following hyperlink to {}", url);
let window = document.window();
window.load_url(url);

View file

@ -8,6 +8,8 @@ use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::BrowserElementBinding::BrowserElementErrorEventDetail;
use dom::bindings::codegen::Bindings::BrowserElementBinding::BrowserElementIconChangeEventDetail;
use dom::bindings::codegen::Bindings::BrowserElementBinding::BrowserElementLocationChangeEventDetail;
use dom::bindings::codegen::Bindings::BrowserElementBinding::BrowserElementOpenTabEventDetail;
use dom::bindings::codegen::Bindings::BrowserElementBinding::BrowserElementOpenWindowEventDetail;
use dom::bindings::codegen::Bindings::BrowserElementBinding::BrowserElementSecurityChangeDetail;
use dom::bindings::codegen::Bindings::BrowserElementBinding::BrowserShowModalPromptEventDetail;
use dom::bindings::codegen::Bindings::HTMLIFrameElementBinding;
@ -310,7 +312,7 @@ impl MozBrowserEventDetailBuilder for HTMLIFrameElement {
match event {
MozBrowserEvent::AsyncScroll | MozBrowserEvent::Close | MozBrowserEvent::ContextMenu |
MozBrowserEvent::LoadEnd | MozBrowserEvent::LoadStart |
MozBrowserEvent::Connected | MozBrowserEvent::OpenWindow | MozBrowserEvent::OpenSearch |
MozBrowserEvent::Connected | MozBrowserEvent::OpenSearch |
MozBrowserEvent::UsernameAndPasswordRequired => {
rval.set(NullValue());
}
@ -347,6 +349,18 @@ impl MozBrowserEventDetailBuilder for HTMLIFrameElement {
canGoForward: Some(can_go_forward),
}.to_jsval(cx, rval);
}
MozBrowserEvent::OpenTab(url) => {
BrowserElementOpenTabEventDetail {
url: Some(DOMString::from(url)),
}.to_jsval(cx, rval);
}
MozBrowserEvent::OpenWindow(url, target, features) => {
BrowserElementOpenWindowEventDetail {
url: Some(DOMString::from(url)),
target: target.map(DOMString::from),
features: features.map(DOMString::from),
}.to_jsval(cx, rval);
}
MozBrowserEvent::IconChange(rel, href, sizes) => {
BrowserElementIconChangeEventDetail {
rel: Some(DOMString::from(rel)),

View file

@ -83,6 +83,19 @@ dictionary BrowserShowModalPromptEventDetail {
// TODO(simartin) unblock() callback
};
dictionary BrowserElementOpenTabEventDetail {
// https://developer.mozilla.org/en-US/docs/Web/Events/mozbrowseropentab
DOMString url;
};
dictionary BrowserElementOpenWindowEventDetail {
// https://developer.mozilla.org/en-US/docs/Web/Events/mozbrowseropenwindow
DOMString url;
DOMString target;
DOMString features;
// Element frameElement;
};
BrowserElement implements BrowserElementCommon;
BrowserElement implements BrowserElementPrivileged;