Auto merge of #20182 - paulrouget:ctrl_l, r=jdm

Prompt URL on Cmd/Ctrl-L

<!-- Please describe your changes on the following line: -->

The `sanitize_url` code is very naive. I'm sure we can do better.
This ServoShell issue describes the problem: https://github.com/paulrouget/servoshell/issues/59

I can fix that now if someone can help me figure out how to tell if a string is a valid url which is just missing a scheme. Or we can do that in a follow up.

---
<!-- 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 #20165

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- 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="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/20182)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2018-03-04 12:48:30 -05:00 committed by GitHub
commit ec3aa8bd7a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 46 additions and 8 deletions

View file

@ -40,6 +40,7 @@ servo_geometry = {path = "../../components/geometry"}
servo_config = {path = "../../components/config"}
servo_url = {path = "../../components/url"}
style_traits = {path = "../../components/style_traits"}
tinyfiledialogs = "3.0"
webrender_api = {git = "https://github.com/servo/webrender", features = ["ipc"]}
[target.'cfg(not(target_os = "android"))'.dependencies]

View file

@ -21,6 +21,7 @@ use glutin::os::macos::{ActivationPolicy, WindowBuilderExt};
use msg::constellation_msg::{self, Key, TopLevelBrowsingContextId as BrowserId};
use msg::constellation_msg::{KeyModifiers, KeyState, TraversalDirection};
use net_traits::net_error_list::NetError;
use net_traits::pub_domains::is_reg_domain;
#[cfg(any(target_os = "linux", target_os = "macos"))]
use osmesa_sys;
use script_traits::{LoadData, TouchEventType};
@ -43,6 +44,7 @@ use std::time;
use style_traits::DevicePixel;
use style_traits::cursor::CursorKind;
use super::NestedEventLoopListener;
use tinyfiledialogs;
#[cfg(target_os = "windows")]
use user32;
use webrender_api::{DeviceUintRect, DeviceUintSize, ScrollLocation};
@ -1321,6 +1323,21 @@ impl WindowMethods for Window {
self.event_queue.borrow_mut().push(WindowEvent::Reload(browser_id));
}
}
(CMD_OR_CONTROL, Some('l'), _) => {
if let Some(true) = PREFS.get("shell.builtin-key-shortcuts.enabled").as_boolean() {
let url: String = if let Some(ref url) = *self.current_url.borrow() {
url.to_string()
} else {
String::from("")
};
let title = "URL or search query";
if let Some(input) = tinyfiledialogs::input_box(title, title, &url) {
if let Some(url) = sanitize_url(&input) {
self.event_queue.borrow_mut().push(WindowEvent::LoadUrl(browser_id, url));
}
}
}
}
(CMD_OR_CONTROL, Some('q'), _) => {
if let Some(true) = PREFS.get("shell.builtin-key-shortcuts.enabled").as_boolean() {
self.event_queue.borrow_mut().push(WindowEvent::Quit);
@ -1448,3 +1465,20 @@ fn filter_nonprintable(ch: char, key_code: VirtualKeyCode) -> Option<char> {
None
}
}
fn sanitize_url(request: &str) -> Option<ServoUrl> {
let request = request.trim();
ServoUrl::parse(&request).ok()
.or_else(|| {
if request.contains('/') || is_reg_domain(request) {
ServoUrl::parse(&format!("http://{}", request)).ok()
} else {
None
}
}).or_else(|| {
PREFS.get("shell.searchpage").as_string().and_then(|s: &str| {
let url = s.replace("%s", request);
ServoUrl::parse(&url).ok()
})
})
}

View file

@ -40,6 +40,7 @@ extern crate servo_url;
#[macro_use]
extern crate sig;
extern crate style_traits;
extern crate tinyfiledialogs;
extern crate webrender_api;
#[cfg(target_os = "windows")] extern crate winapi;
#[cfg(target_os = "windows")] extern crate user32;