Bug 377624 - "Implement the accept attribute for the form and file upload controls form "image/*"" [r=enndeakin,roc]
authorMounir Lamouri <mounir.lamouri@gmail.com>
Wed, 19 May 2010 22:28:01 -0400
changeset 42484 103d84b4b399
parent 42483 64e1cfd8ba01
child 42485 cec477f51801
push id13361
push userCallek@gmail.com
push dateThu, 20 May 2010 02:31:41 +0000
treeherdermozilla-central@c46b625c886e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersenndeakin, roc
bugs377624
milestone1.9.3a5pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
Bug 377624 - "Implement the accept attribute for the form and file upload controls form "image/*"" [r=enndeakin,roc]
layout/forms/nsFileControlFrame.cpp
layout/forms/nsFileControlFrame.h
toolkit/content/filepicker.properties
widget/public/nsIFilePicker.idl
--- a/layout/forms/nsFileControlFrame.cpp
+++ b/layout/forms/nsFileControlFrame.cpp
@@ -367,18 +367,28 @@ nsFileControlFrame::MouseListener::Mouse
   NS_ENSURE_SUCCESS(result, result);
 
   result = filePicker->Init(win, title, multi ?
                             (PRInt16)nsIFilePicker::modeOpenMultiple :
                             (PRInt16)nsIFilePicker::modeOpen);
   if (NS_FAILED(result))
     return result;
 
-  // Set filter "All Files"
-  filePicker->AppendFilters(nsIFilePicker::filterAll);
+  // We want to get the file filter from the accept attribute and we add the
+  // |filterAll| filter to be sure the user has a valid fallback.
+  PRUint32 filter = mFrame->GetFileFilterFromAccept();
+  filePicker->AppendFilters(filter | nsIFilePicker::filterAll);
+
+  // If the accept attribute asks for a filter, it has to be the default one.
+  if (filter) {
+    // We have two filters: |filterAll| and another one. |filterAll| is
+    // always the first one (index=0) so we can assume the one we want to be
+    // the default is at index 1.
+    filePicker->SetFilterIndex(1);
+  }
 
   // Set default directry and filename
   nsAutoString defaultName;
 
   nsCOMArray<nsIFile> oldFiles;
   fileControl->GetFileArray(oldFiles);
 
   if (oldFiles.Count()) {
@@ -846,14 +856,26 @@ NS_IMETHODIMP nsFileControlFrame::GetAcc
   nsCOMPtr<nsIAccessibilityService> accService = do_GetService("@mozilla.org/accessibilityService;1");
   if (!accService)
     return NS_ERROR_FAILURE;
 
   return accService->CreateHTMLGenericAccessible(static_cast<nsIFrame*>(this), aAccessible);
 }
 #endif
 
+PRInt32
+nsFileControlFrame::GetFileFilterFromAccept() const
+{
+  nsAutoString accept;
+  mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::accept, accept);
+
+  if (accept.EqualsLiteral("image/*")) {
+    return nsIFilePicker::filterImages;
+  }
+
+  return 0;
+}
 ////////////////////////////////////////////////////////////
 // Mouse listener implementation
 
 NS_IMPL_ISUPPORTS2(nsFileControlFrame::MouseListener,
                    nsIDOMMouseListener,
                    nsIDOMEventListener)
--- a/layout/forms/nsFileControlFrame.h
+++ b/layout/forms/nsFileControlFrame.h
@@ -98,16 +98,29 @@ public:
 #ifdef ACCESSIBILITY
   NS_IMETHOD GetAccessible(nsIAccessible** aAccessible);
 #endif
 
   // create and destroy the static UploadLastDir object for remembering
   // which directory was last used on a site-by-site basis
   static void InitUploadLastDir();
   static void DestroyUploadLastDir();
+
+  /**
+   * This methods return the file filter mask requested by the HTML5 accept
+   * attribute. If the accept attribute isn't present or the value isn't valid,
+   * the returned value will be 0.
+   *
+   * See:
+   * http://dev.w3.org/html5/spec/forms.html#attr-input-accept
+   *
+   * @return the file picker filter mask or 0 if there is no filter.
+   */
+  PRInt32 GetFileFilterFromAccept() const;
+
 protected:
   class MouseListener;
   friend class MouseListener;
   class MouseListener : public nsIDOMMouseListener {
   public:
     NS_DECL_ISUPPORTS
     
     MouseListener(nsFileControlFrame* aFrame) :
--- a/toolkit/content/filepicker.properties
+++ b/toolkit/content/filepicker.properties
@@ -1,6 +1,6 @@
 allFilter=*
 htmlFilter=*.html; *.htm; *.shtml; *.xhtml
 textFilter=*.txt; *.text
-imageFilter=*.jpg; *.jpeg; *.gif; *.png; *.bmp; *.ico
+imageFilter=*.jpe; *.jpg; *.jpeg; *.gif; *.png; *.bmp; *.ico; *.svg; *.svgz; *.tif; *.tiff; *.ai; *.drw; *.pct; *.psp; *.xcf; *.psd; *.raw
 xmlFilter=*.xml
 xulFilter=*.xul
--- a/widget/public/nsIFilePicker.idl
+++ b/widget/public/nsIFilePicker.idl
@@ -54,17 +54,21 @@ interface nsIFilePicker : nsISupports
 
   const short returnOK        = 0;              // User hit Ok, process selection
   const short returnCancel    = 1;              // User hit cancel, ignore selection
   const short returnReplace   = 2;              // User acknowledged file already exists so ok to replace, process selection
 
   const long filterAll        = 0x01;           // *.*
   const long filterHTML       = 0x02;           // *.html; *.htm
   const long filterText       = 0x04;           // *.txt
-  const long filterImages     = 0x08;           // *.png; *.gif; *.jpg; *.jpeg
+  const long filterImages     = 0x08;           // *.jpe; *.jpg; *.jpeg; *.gif;
+                                                // *.png; *.bmp; *.ico; *.svg;
+                                                // *.svgz; *.tif; *.tiff; *.ai;
+                                                // *.drw; *.pct; *.psp; *.xcf;
+                                                // *.psd; *.raw
   const long filterXML        = 0x10;           // *.xml
   const long filterXUL        = 0x20;           // *.xul
   const long filterApps       = 0x40;           // Applications (per-platform implementation)
   const long filterAllowURLs  = 0x80;           // Allow URLs
 
  /**
   * Initialize the file picker widget.  The file picker is not valid until this
   * method is called.