diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2016-06-20 12:06:52 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-06-20 12:06:52 -0500 |
commit | 33bda9c207001b1f4f0676cbab3222218e94e987 (patch) | |
tree | 55a8cf52444a2850c9532f3f1c263b0bd2217846 /components/script | |
parent | c270622bfdd7a409c1a940322f630fd6cbd985c0 (diff) | |
parent | 74eb80dbd8a9d83a3339399cd036087702eb80ea (diff) | |
download | servo-33bda9c207001b1f4f0676cbab3222218e94e987.tar.gz servo-33bda9c207001b1f4f0676cbab3222218e94e987.zip |
Auto merge of #11757 - izgzhen:file-acccept-mime-filter, r=Manishearth
Implement filter for file-type input's accept attribute
Now the two sides are pasted together with the new version of `mime_guess` landed.
I tested this thing locally with stuff like this:
```html
<input id="input_file" type="file" accept=".doc,.docx,.xml,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document"></input>
<a onclick="open_file();">Click</a>
<script type="text/javascript">
function open_file() {
console.log("Open file");
document.getElementById("input_file").click();
}
</script>
```
Will WPT be able to handle this automatically?
---
<!-- 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 is related to #11131
<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because _____
<!-- 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/11757)
<!-- Reviewable:end -->
Diffstat (limited to 'components/script')
-rw-r--r-- | components/script/Cargo.toml | 3 | ||||
-rw-r--r-- | components/script/dom/htmlinputelement.rs | 25 | ||||
-rw-r--r-- | components/script/lib.rs | 1 |
3 files changed, 22 insertions, 7 deletions
diff --git a/components/script/Cargo.toml b/components/script/Cargo.toml index 911f1b9cc67..a9daf1a2adf 100644 --- a/components/script/Cargo.toml +++ b/components/script/Cargo.toml @@ -37,7 +37,8 @@ ipc-channel = {git = "https://github.com/servo/ipc-channel"} js = {git = "https://github.com/servo/rust-mozjs"} libc = "0.2" log = "0.3.5" -mime = "0.2.0" +mime = "0.2.1" +mime_guess = "1.8.0" msg = {path = "../msg"} net_traits = {path = "../net_traits"} num-traits = "0.1.32" diff --git a/components/script/dom/htmlinputelement.rs b/components/script/dom/htmlinputelement.rs index 561ccc647f2..71edf5c7abb 100644 --- a/components/script/dom/htmlinputelement.rs +++ b/components/script/dom/htmlinputelement.rs @@ -32,6 +32,7 @@ use dom::nodelist::NodeList; use dom::validation::Validatable; use dom::virtualmethods::VirtualMethods; use ipc_channel::ipc::{self, IpcSender}; +use mime_guess; use net_traits::IpcSend; use net_traits::filemanager_thread::{FileManagerThreadMsg, FilterPattern}; use script_traits::ScriptMsg as ConstellationMsg; @@ -44,6 +45,7 @@ use style::element_state::*; use textinput::KeyReaction::{DispatchInput, Nothing, RedrawSelection, TriggerDefaultAction}; use textinput::Lines::Single; use textinput::{TextInput, SelectionDirection}; +use util::str::split_commas; const DEFAULT_SUBMIT_VALUE: &'static str = "Submit"; const DEFAULT_RESET_VALUE: &'static str = "Reset"; @@ -1140,7 +1142,7 @@ impl Activatable for HTMLInputElement { let mut files: Vec<Root<File>> = vec![]; let mut error = None; - let filter = filter_from_accept(self.Accept()); + let filter = filter_from_accept(&self.Accept()); if self.Multiple() { 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> { - /// TODO: it means not pattern restriction now - /// Blocked by https://github.com/cybergeek94/mime_guess/issues/19 - vec![] -} +// https://html.spec.whatwg.org/multipage/#attr-input-accept +fn filter_from_accept(s: &DOMString) -> Vec<FilterPattern> { + let mut filter = 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 +} diff --git a/components/script/lib.rs b/components/script/lib.rs index 307376854ca..208e48811f6 100644 --- a/components/script/lib.rs +++ b/components/script/lib.rs @@ -55,6 +55,7 @@ extern crate libc; extern crate log; #[macro_use] extern crate mime; +extern crate mime_guess; extern crate msg; extern crate net_traits; extern crate num_traits; |