Implement filter for file-type input's accept attribute

This commit is contained in:
Zhen Zhang 2016-06-16 08:35:30 +08:00
parent 881c02ec89
commit 74eb80dbd8
9 changed files with 78 additions and 45 deletions

View file

@ -22,8 +22,8 @@ ipc-channel = {git = "https://github.com/servo/ipc-channel"}
lazy_static = "0.2" lazy_static = "0.2"
log = "0.3.5" log = "0.3.5"
matches = "0.1" matches = "0.1"
mime = "0.2.0" mime = "0.2.1"
mime_guess = "1.6.0" mime_guess = "1.8.0"
msg = {path = "../msg"} msg = {path = "../msg"}
net_traits = {path = "../net_traits"} net_traits = {path = "../net_traits"}
openssl = "0.7.6" openssl = "0.7.6"

View file

@ -25,37 +25,51 @@ pub trait FileManagerThreadFactory<UI: 'static + UIProvider> {
} }
pub trait UIProvider where Self: Sync { pub trait UIProvider where Self: Sync {
fn open_file_dialog(&self, path: &str, fn open_file_dialog(&self, path: &str, patterns: Vec<FilterPattern>) -> Option<String>;
filter: Option<(&[&str], &str)>) -> Option<String>;
fn open_file_dialog_multi(&self, path: &str, fn open_file_dialog_multi(&self, path: &str, patterns: Vec<FilterPattern>) -> Option<Vec<String>>;
filter: Option<(&[&str], &str)>) -> Option<Vec<String>>;
} }
pub struct TFDProvider; pub struct TFDProvider;
impl UIProvider for TFDProvider { impl UIProvider for TFDProvider {
#[cfg(any(target_os = "macos", target_os = "linux"))] #[cfg(any(target_os = "macos", target_os = "linux"))]
fn open_file_dialog(&self, path: &str, fn open_file_dialog(&self, path: &str, patterns: Vec<FilterPattern>) -> Option<String> {
filter: Option<(&[&str], &str)>) -> Option<String> { let mut filter = vec![];
tinyfiledialogs::open_file_dialog("Pick a file", path, filter) for p in patterns {
let s = "*.".to_string() + &p.0;
filter.push(s)
}
let filter_ref = &(filter.iter().map(|s| s.as_str()).collect::<Vec<&str>>()[..]);
let filter_opt = if filter.len() > 0 { Some((filter_ref, "")) } else { None };
tinyfiledialogs::open_file_dialog("Pick a file", path, filter_opt)
} }
#[cfg(any(target_os = "macos", target_os = "linux"))] #[cfg(any(target_os = "macos", target_os = "linux"))]
fn open_file_dialog_multi(&self, path: &str, fn open_file_dialog_multi(&self, path: &str, patterns: Vec<FilterPattern>) -> Option<Vec<String>> {
filter: Option<(&[&str], &str)>) -> Option<Vec<String>> { let mut filter = vec![];
tinyfiledialogs::open_file_dialog_multi("Pick files", path, filter) for p in patterns {
let s = "*.".to_string() + &p.0;
filter.push(s)
}
let filter_ref = &(filter.iter().map(|s| s.as_str()).collect::<Vec<&str>>()[..]);
let filter_opt = if filter.len() > 0 { Some((filter_ref, "")) } else { None };
tinyfiledialogs::open_file_dialog_multi("Pick files", path, filter_opt)
} }
#[cfg(not(any(target_os = "macos", target_os = "linux")))] #[cfg(not(any(target_os = "macos", target_os = "linux")))]
fn open_file_dialog(&self, _path: &str, fn open_file_dialog(&self, path: &str, patterns: Vec<FilterPattern>) -> Option<String> {
_filter: Option<(&[&str], &str)>) -> Option<String> {
None None
} }
#[cfg(not(any(target_os = "macos", target_os = "linux")))] #[cfg(not(any(target_os = "macos", target_os = "linux")))]
fn open_file_dialog_multi(&self, _path: &str, fn open_file_dialog_multi(&self, path: &str, patterns: Vec<FilterPattern>) -> Option<Vec<String>> {
_filter: Option<(&[&str], &str)>) -> Option<Vec<String>> {
None None
} }
} }
@ -116,9 +130,9 @@ impl<UI: 'static + UIProvider> FileManager<UI> {
} }
} }
fn select_file(&mut self, _filter: Vec<FilterPattern>, fn select_file(&mut self, patterns: Vec<FilterPattern>,
sender: IpcSender<FileManagerResult<SelectedFile>>) { sender: IpcSender<FileManagerResult<SelectedFile>>) {
match self.ui.open_file_dialog("", None) { match self.ui.open_file_dialog("", patterns) {
Some(s) => { Some(s) => {
let selected_path = Path::new(&s); let selected_path = Path::new(&s);
@ -134,9 +148,9 @@ impl<UI: 'static + UIProvider> FileManager<UI> {
} }
} }
fn select_files(&mut self, _filter: Vec<FilterPattern>, fn select_files(&mut self, patterns: Vec<FilterPattern>,
sender: IpcSender<FileManagerResult<Vec<SelectedFile>>>) { sender: IpcSender<FileManagerResult<Vec<SelectedFile>>>) {
match self.ui.open_file_dialog_multi("", None) { match self.ui.open_file_dialog_multi("", patterns) {
Some(v) => { Some(v) => {
let mut selected_paths = vec![]; let mut selected_paths = vec![];

View file

@ -19,6 +19,8 @@ pub struct SelectedFile {
pub type_string: String, pub type_string: String,
} }
/// Filter for file selection
/// the content is expected to be extension (e.g, "doc", without the prefixing ".")
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct FilterPattern(pub String); pub struct FilterPattern(pub String);

View file

@ -37,7 +37,8 @@ ipc-channel = {git = "https://github.com/servo/ipc-channel"}
js = {git = "https://github.com/servo/rust-mozjs"} js = {git = "https://github.com/servo/rust-mozjs"}
libc = "0.2" libc = "0.2"
log = "0.3.5" log = "0.3.5"
mime = "0.2.0" mime = "0.2.1"
mime_guess = "1.8.0"
msg = {path = "../msg"} msg = {path = "../msg"}
net_traits = {path = "../net_traits"} net_traits = {path = "../net_traits"}
num-traits = "0.1.32" num-traits = "0.1.32"

View file

@ -32,6 +32,7 @@ use dom::nodelist::NodeList;
use dom::validation::Validatable; use dom::validation::Validatable;
use dom::virtualmethods::VirtualMethods; use dom::virtualmethods::VirtualMethods;
use ipc_channel::ipc::{self, IpcSender}; use ipc_channel::ipc::{self, IpcSender};
use mime_guess;
use net_traits::IpcSend; use net_traits::IpcSend;
use net_traits::filemanager_thread::{FileManagerThreadMsg, FilterPattern}; use net_traits::filemanager_thread::{FileManagerThreadMsg, FilterPattern};
use script_traits::ScriptMsg as ConstellationMsg; use script_traits::ScriptMsg as ConstellationMsg;
@ -44,6 +45,7 @@ use style::element_state::*;
use textinput::KeyReaction::{DispatchInput, Nothing, RedrawSelection, TriggerDefaultAction}; use textinput::KeyReaction::{DispatchInput, Nothing, RedrawSelection, TriggerDefaultAction};
use textinput::Lines::Single; use textinput::Lines::Single;
use textinput::{TextInput, SelectionDirection}; use textinput::{TextInput, SelectionDirection};
use util::str::split_commas;
const DEFAULT_SUBMIT_VALUE: &'static str = "Submit"; const DEFAULT_SUBMIT_VALUE: &'static str = "Submit";
const DEFAULT_RESET_VALUE: &'static str = "Reset"; const DEFAULT_RESET_VALUE: &'static str = "Reset";
@ -1140,7 +1142,7 @@ impl Activatable for HTMLInputElement {
let mut files: Vec<Root<File>> = vec![]; let mut files: Vec<Root<File>> = vec![];
let mut error = None; let mut error = None;
let filter = filter_from_accept(self.Accept()); let filter = filter_from_accept(&self.Accept());
if self.Multiple() { if self.Multiple() {
let (chan, recv) = ipc::channel().expect("Error initializing channel"); let (chan, recv) = ipc::channel().expect("Error initializing channel");
@ -1231,9 +1233,20 @@ impl Activatable for HTMLInputElement {
} }
} }
fn filter_from_accept(_s: DOMString) -> Vec<FilterPattern> { // https://html.spec.whatwg.org/multipage/#attr-input-accept
/// TODO: it means not pattern restriction now fn filter_from_accept(s: &DOMString) -> Vec<FilterPattern> {
/// Blocked by https://github.com/cybergeek94/mime_guess/issues/19 let mut filter = vec![];
vec![] for p in split_commas(s) {
} if let Some('.') = p.chars().nth(0) {
filter.push(FilterPattern(p[1..].to_string()));
} else {
if let Some(exts) = mime_guess::get_mime_extensions_str(p) {
for ext in exts {
filter.push(FilterPattern(ext.to_string()));
}
}
}
}
filter
}

View file

@ -55,6 +55,7 @@ extern crate libc;
extern crate log; extern crate log;
#[macro_use] #[macro_use]
extern crate mime; extern crate mime;
extern crate mime_guess;
extern crate msg; extern crate msg;
extern crate net_traits; extern crate net_traits;
extern crate num_traits; extern crate num_traits;

View file

@ -770,7 +770,7 @@ dependencies = [
"lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"mime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "mime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"msg 0.0.1", "msg 0.0.1",
"net_traits 0.0.1", "net_traits 0.0.1",
"plugins 0.0.1", "plugins 0.0.1",
@ -981,7 +981,7 @@ dependencies = [
"httparse 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "httparse 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"mime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "mime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)",
"openssl 0.7.13 (registry+https://github.com/rust-lang/crates.io-index)", "openssl 0.7.13 (registry+https://github.com/rust-lang/crates.io-index)",
"openssl-verify 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "openssl-verify 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1303,7 +1303,7 @@ dependencies = [
[[package]] [[package]]
name = "mime" name = "mime"
version = "0.2.0" version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1312,10 +1312,10 @@ dependencies = [
[[package]] [[package]]
name = "mime_guess" name = "mime_guess"
version = "1.6.1" version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"mime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "mime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"phf 0.7.15 (registry+https://github.com/rust-lang/crates.io-index)", "phf 0.7.15 (registry+https://github.com/rust-lang/crates.io-index)",
"phf_codegen 0.7.15 (registry+https://github.com/rust-lang/crates.io-index)", "phf_codegen 0.7.15 (registry+https://github.com/rust-lang/crates.io-index)",
"unicase 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "unicase 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1377,8 +1377,8 @@ dependencies = [
"lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"mime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "mime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"mime_guess 1.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "mime_guess 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"msg 0.0.1", "msg 0.0.1",
"net_traits 0.0.1", "net_traits 0.0.1",
"openssl 0.7.13 (registry+https://github.com/rust-lang/crates.io-index)", "openssl 0.7.13 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1891,7 +1891,8 @@ dependencies = [
"js 0.1.3 (git+https://github.com/servo/rust-mozjs)", "js 0.1.3 (git+https://github.com/servo/rust-mozjs)",
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"mime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "mime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"mime_guess 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"msg 0.0.1", "msg 0.0.1",
"net_traits 0.0.1", "net_traits 0.0.1",
"num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",

17
ports/cef/Cargo.lock generated
View file

@ -692,7 +692,7 @@ dependencies = [
"lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"mime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "mime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"msg 0.0.1", "msg 0.0.1",
"net_traits 0.0.1", "net_traits 0.0.1",
"plugins 0.0.1", "plugins 0.0.1",
@ -894,7 +894,7 @@ dependencies = [
"httparse 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "httparse 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"mime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "mime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)",
"openssl 0.7.13 (registry+https://github.com/rust-lang/crates.io-index)", "openssl 0.7.13 (registry+https://github.com/rust-lang/crates.io-index)",
"openssl-verify 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "openssl-verify 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1209,7 +1209,7 @@ dependencies = [
[[package]] [[package]]
name = "mime" name = "mime"
version = "0.2.0" version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1218,10 +1218,10 @@ dependencies = [
[[package]] [[package]]
name = "mime_guess" name = "mime_guess"
version = "1.6.1" version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"mime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "mime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"phf 0.7.15 (registry+https://github.com/rust-lang/crates.io-index)", "phf 0.7.15 (registry+https://github.com/rust-lang/crates.io-index)",
"phf_codegen 0.7.15 (registry+https://github.com/rust-lang/crates.io-index)", "phf_codegen 0.7.15 (registry+https://github.com/rust-lang/crates.io-index)",
"unicase 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "unicase 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1283,8 +1283,8 @@ dependencies = [
"lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"mime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "mime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"mime_guess 1.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "mime_guess 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"msg 0.0.1", "msg 0.0.1",
"net_traits 0.0.1", "net_traits 0.0.1",
"openssl 0.7.13 (registry+https://github.com/rust-lang/crates.io-index)", "openssl 0.7.13 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1749,7 +1749,8 @@ dependencies = [
"js 0.1.3 (git+https://github.com/servo/rust-mozjs)", "js 0.1.3 (git+https://github.com/servo/rust-mozjs)",
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"mime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "mime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"mime_guess 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"msg 0.0.1", "msg 0.0.1",
"net_traits 0.0.1", "net_traits 0.0.1",
"num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",

View file

@ -14,11 +14,11 @@ const TEST_PROVIDER: &'static TestProvider = &TestProvider;
struct TestProvider; struct TestProvider;
impl UIProvider for TestProvider { impl UIProvider for TestProvider {
fn open_file_dialog(&self, _: &str, _: Option<(&[&str], &str)>) -> Option<String> { fn open_file_dialog(&self, _path: &str, _patterns: Vec<FilterPattern>) -> Option<String> {
Some("test.txt".to_string()) Some("test.txt".to_string())
} }
fn open_file_dialog_multi(&self, _: &str, _: Option<(&[&str], &str)>) -> Option<Vec<String>> { fn open_file_dialog_multi(&self, _path: &str, _patterns: Vec<FilterPattern>) -> Option<Vec<String>> {
Some(vec!["test.txt".to_string()]) Some(vec!["test.txt".to_string()])
} }
} }