Bug 1303638 - file.webkitRelativePath must contain the leafName of the parent directory. r=smaug, a=ritu
authorAndrea Marchesini <amarchesini@mozilla.com>
Thu, 22 Sep 2016 13:48:46 +0200
changeset 348329 c8d78d3fa2018955b230a21c6824cb66ab3fc328
parent 348328 73430b7a19da9fd34d3622992c6401c866e0e2bf
child 348330 5d734d96dc24cd7107bfeda30451f93d963ee9a5
push id6417
push userryanvm@gmail.com
push dateFri, 23 Sep 2016 22:01:50 +0000
treeherdermozilla-beta@0c2226b65a2f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug, ritu
bugs1303638
milestone50.0
Bug 1303638 - file.webkitRelativePath must contain the leafName of the parent directory. r=smaug, a=ritu
dom/filesystem/GetFilesHelper.cpp
dom/filesystem/tests/script_fileList.js
dom/filesystem/tests/test_basic.html
dom/filesystem/tests/test_webkitdirectory.html
dom/html/HTMLInputElement.cpp
--- a/dom/filesystem/GetFilesHelper.cpp
+++ b/dom/filesystem/GetFilesHelper.cpp
@@ -273,17 +273,24 @@ GetFilesHelper::RunIO()
   nsCOMPtr<nsIFile> file;
   mErrorResult = NS_NewNativeLocalFile(NS_ConvertUTF16toUTF8(mDirectoryPath), true,
                                        getter_AddRefs(file));
   if (NS_WARN_IF(NS_FAILED(mErrorResult))) {
     return;
   }
 
   nsAutoString path;
-  path.AssignLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL);
+  mErrorResult = file->GetLeafName(path);
+  if (NS_WARN_IF(NS_FAILED(mErrorResult))) {
+    return;
+  }
+
+  if (path.IsEmpty()) {
+    path.AppendLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL);
+  }
 
   mErrorResult = ExploreDirectory(path, file);
 }
 
 void
 GetFilesHelper::RunMainThread()
 {
   MOZ_ASSERT(NS_IsMainThread());
--- a/dom/filesystem/tests/script_fileList.js
+++ b/dom/filesystem/tests/script_fileList.js
@@ -106,17 +106,18 @@ addMessageListener("dir.open", function 
       break;
 
     case 'tree':
       testFile = createTreeFile(3);
       break;
   }
 
   sendAsyncMessage("dir.opened", {
-    dir: testFile.path
+    dir: testFile.path,
+    name: testFile.leafName
   });
 });
 
 addMessageListener("file.open", function (e) {
   var testFile = Cc["@mozilla.org/file/directory_service;1"]
                    .getService(Ci.nsIDirectoryService)
                    .QueryInterface(Ci.nsIProperties)
                    .get("ProfD", Ci.nsIFile);
--- a/dom/filesystem/tests/test_basic.html
+++ b/dom/filesystem/tests/test_basic.html
@@ -18,16 +18,17 @@ function create_fileList(aPath) {
   fileList.setAttribute('type', 'file');
   document.body.appendChild(fileList);
 
   var url = SimpleTest.getTestFileURL("script_fileList.js");
   var script = SpecialPowers.loadChromeScript(url);
 
   function onOpened(message) {
     SpecialPowers.wrap(fileList).mozSetDirectory(message.dir);
+    fileList.setAttribute('data-name', message.name);
 
     fileList.getFilesAndDirectories().then(function(array) {
       is(array.length, 1, "We want just 1 directory.");
       ok(array[0] instanceof Directory, "We want just 1 directory.");
 
       directory = array[0];
       script.destroy();
       next();
@@ -79,42 +80,43 @@ function test_duplicateGetFilesAndDirect
 }
 
 function test_inputGetFiles() {
   var url = SimpleTest.getTestFileURL("script_fileList.js");
   var script = SpecialPowers.loadChromeScript(url);
 
   function onOpened(message) {
     SpecialPowers.wrap(fileList).mozSetDirectory(message.dir);
+    fileList.setAttribute('data-name', message.name);
 
     fileList.getFilesAndDirectories()
     .then(function(result) {
        is(result.length, 1, "getFilesAndDirectories should return 1 element");
        ok(result[0] instanceof Directory, "getFilesAndDirectories should return 1 directory");
 
       return fileList.getFiles(false);
     })
     .then(function(result) {
       is(result.length, 1, "getFiles should return 1 element");
       ok(result[0] instanceof File, "getFile should return 1 file");
       is(result[0].name, 'foo.txt', "getFiles()[0].name should be 'foo.txt'");
-      is(result[0].webkitRelativePath, '/foo.txt', "getFiles()[0].webkitRelativePath should be '/foo.txt'");
+      is(result[0].webkitRelativePath, fileList.dataset.name + '/foo.txt', "getFiles()[0].webkitRelativePath should be '/foo.txt'");
 
       return fileList.getFiles(true);
     })
     .then(function(result) {
       is(result.length, 2, "getFiles should return 2 elements");
 
       function checkFile(file) {
         ok(file instanceof File, "getFile[x] should return a file");
         if (file.name == 'foo.txt') {
-          is(file.webkitRelativePath, '/foo.txt', "getFiles()[x].webkitRelativePath should be '/foo.txt'");
+          is(file.webkitRelativePath, fileList.dataset.name + '/foo.txt', "getFiles()[x].webkitRelativePath should be '/foo.txt'");
         } else {
           is(file.name, 'bar.txt', "getFiles()[x].name should be 'bar.txt'");
-          is(file.webkitRelativePath, '/subdir/bar.txt', "getFiles()[x].webkitRelativePath should be '/subdir/bar.txt'");
+          is(file.webkitRelativePath, fileList.dataset.name + '/subdir/bar.txt', "getFiles()[x].webkitRelativePath should be '/subdir/bar.txt'");
         }
       }
 
       checkFile(result[0]);
       checkFile(result[1]);
     })
     .then(function() {
       script.destroy();
--- a/dom/filesystem/tests/test_webkitdirectory.html
+++ b/dom/filesystem/tests/test_webkitdirectory.html
@@ -20,35 +20,36 @@ function populateInputFile(aInputFile) {
 
   var MockFilePicker = SpecialPowers.MockFilePicker;
   MockFilePicker.init(window, "A Mock File Picker", SpecialPowers.Ci.nsIFilePicker.modeOpen);
 
   function onOpened(message) {
     MockFilePicker.useDirectory(message.dir);
 
     var input = document.getElementById(aInputFile);
+    input.setAttribute('data-name', message.name);
     input.addEventListener('change', function change() {
       input.removeEventListener('change', change);
       MockFilePicker.cleanup();
       script.destroy();
       next();
     });
 
     input.click();
   }
 
   script.addMessageListener("dir.opened", onOpened);
   script.sendAsyncMessage("dir.open", { path: 'test' });
 }
 
-function checkFile(file, fileList) {
+function checkFile(file, fileList, dirName) {
   for (var i = 0; i < fileList.length; ++i) {
     ok(fileList[i] instanceof File, "We want just files.");
     if (fileList[i].name == file.name) {
-      is(fileList[i].webkitRelativePath, file.path, "Path matches");
+      is(fileList[i].webkitRelativePath, dirName + file.path, "Path matches");
       return;
     }
   }
 
   ok(false, "File not found.");
 }
 
 function test_fileList(aInputFile, aWhat) {
@@ -58,17 +59,17 @@ function test_fileList(aInputFile, aWhat
   if (aWhat == null) {
     is(fileList, null, "We want a null fileList for " + aInputFile);
     next();
     return;
   }
 
   is(fileList.length, aWhat.length, "We want just " + aWhat.length + " elements for " + aInputFile);
   for (var i = 0; i < aWhat.length; ++i) {
-    checkFile(aWhat[i], fileList);
+    checkFile(aWhat[i], fileList, input.dataset.name);
   }
 
   next();
 }
 
 function test_webkitdirectory_attribute() {
   var a = document.createElement("input");
   a.setAttribute("type", "file");
--- a/dom/html/HTMLInputElement.cpp
+++ b/dom/html/HTMLInputElement.cpp
@@ -5527,17 +5527,17 @@ HTMLInputElement::GetFiles(bool aRecursi
   if (mType != NS_FORM_INPUT_FILE) {
     aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
     return nullptr;
   }
 
   GetFilesHelper* helper = GetOrCreateGetFilesHelper(aRecursiveFlag, aRv);
   if (NS_WARN_IF(aRv.Failed())) {
     return nullptr;
-   }
+  }
   MOZ_ASSERT(helper);
 
   nsCOMPtr<nsIGlobalObject> global = OwnerDoc()->GetScopeObject();
   MOZ_ASSERT(global);
   if (!global) {
     return nullptr;
   }