FormData should allow setting filename to empty
authorap@apple.com <ap@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 7 Mar 2013 18:25:10 +0000 (18:25 +0000)
committerap@apple.com <ap@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 7 Mar 2013 18:25:10 +0000 (18:25 +0000)
        https://bugs.webkit.org/show_bug.cgi?id=111687

        Reviewed by Brady Eidson.

        Tests: http/tests/local/formdata/send-form-data-with-empty-blob-filename.html
               http/tests/local/formdata/send-form-data-with-empty-file-filename.html

        * html/FileInputType.cpp: (WebCore::FileInputType::appendFormData):
        Use a Blob for empty file input, not a hacky File with empty path.

        * platform/network/FormData.cpp: (WebCore::FormData::appendKeyValuePairItems):
        Missing value is a null string. If the string is empty, we should treat is as
        authoritative.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@145097 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/http/tests/local/formdata/send-form-data-with-empty-blob-filename-expected.txt [new file with mode: 0644]
LayoutTests/http/tests/local/formdata/send-form-data-with-empty-blob-filename.html [new file with mode: 0644]
LayoutTests/http/tests/local/formdata/send-form-data-with-empty-file-filename-expected.txt [new file with mode: 0644]
LayoutTests/http/tests/local/formdata/send-form-data-with-empty-file-filename.html [new file with mode: 0644]
LayoutTests/http/tests/xmlhttprequest/resources/multipart-post-echo-filenames.php [new file with mode: 0644]
LayoutTests/platform/wk2/TestExpectations
Source/WebCore/ChangeLog
Source/WebCore/html/FileInputType.cpp
Source/WebCore/platform/network/FormData.cpp

index 9fc3c53..4c45e9b 100644 (file)
@@ -1,3 +1,18 @@
+2013-03-07  Alexey Proskuryakov  <ap@apple.com>
+
+        FormData should allow setting filename to empty
+        https://bugs.webkit.org/show_bug.cgi?id=111687
+
+        Reviewed by Brady Eidson.
+
+        * http/tests/local/formdata/send-form-data-with-empty-blob-filename-expected.txt: Added.
+        * http/tests/local/formdata/send-form-data-with-empty-blob-filename.html: Added.
+        * http/tests/local/formdata/send-form-data-with-empty-file-filename-expected.txt: Added.
+        * http/tests/local/formdata/send-form-data-with-empty-file-filename.html: Added.
+        * http/tests/xmlhttprequest/resources/multipart-post-echo-filenames.php: Added.
+
+        * platform/wk2/TestExpectations: Skip one of the tests, as it uses beginDragWithFiles.
+
 2013-03-07  Dean Jackson  <dino@apple.com>
 
         inspector/debugger/debugger-expand-scope.html sometimes fails on Apple MountainLion Release WK1 (Tests)
diff --git a/LayoutTests/http/tests/local/formdata/send-form-data-with-empty-blob-filename-expected.txt b/LayoutTests/http/tests/local/formdata/send-form-data-with-empty-blob-filename-expected.txt
new file mode 100644 (file)
index 0000000..f44261e
--- /dev/null
@@ -0,0 +1,11 @@
+Test that filename passed to FormData.append() takes precedence over default Blob filename 'blob', even if empty.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+Sending FormData containing one blob with custom empty filename:
+PASS filename is ''
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/http/tests/local/formdata/send-form-data-with-empty-blob-filename.html b/LayoutTests/http/tests/local/formdata/send-form-data-with-empty-blob-filename.html
new file mode 100644 (file)
index 0000000..a01b0e0
--- /dev/null
@@ -0,0 +1,37 @@
+<!DOCTYPE html>
+<html>
+<head>
+<script src="../../../../fast/js/resources/js-test-pre.js"></script>
+</head>
+<body>
+<p id="description"></p>
+<div id="console"></div>
+<script>
+description("Test that filename passed to FormData.append() takes precedence over default Blob filename 'blob', even if empty.");
+
+self.jsTestIsAsync = true;
+
+function runTest()
+{
+    debug("Sending FormData containing one blob with custom empty filename:");
+
+    var formData = new FormData;
+    formData.append("blob", new Blob([""]), "");
+    var xhr = new XMLHttpRequest();
+    xhr.open("POST", "http://127.0.0.1:8000/xmlhttprequest/resources/multipart-post-echo-filenames.php", true);
+    xhr.onload = function() {
+        filename = xhr.responseText;
+        shouldBe("filename", "''");
+        finishJSTest();
+    }
+    xhr.send(formData);
+}
+
+runTest();
+
+var successfullyParsed = true;
+</script>
+<script src="../../../../fast/js/resources/js-test-post.js"></script>
+</body>
+</html>
+
diff --git a/LayoutTests/http/tests/local/formdata/send-form-data-with-empty-file-filename-expected.txt b/LayoutTests/http/tests/local/formdata/send-form-data-with-empty-file-filename-expected.txt
new file mode 100644 (file)
index 0000000..37ea9af
--- /dev/null
@@ -0,0 +1,11 @@
+Test that filename passed to FormData.append() takes precedence over filename in File, even if empty.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+Sending FormData containing one file with custom empty filename:
+PASS filename is ''
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/http/tests/local/formdata/send-form-data-with-empty-file-filename.html b/LayoutTests/http/tests/local/formdata/send-form-data-with-empty-file-filename.html
new file mode 100644 (file)
index 0000000..2ba9486
--- /dev/null
@@ -0,0 +1,52 @@
+<!DOCTYPE html>
+<html>
+<head>
+<script src="../../../../fast/js/resources/js-test-pre.js"></script>
+</head>
+<body>
+<p id="description"></p>
+<div id="console"></div>
+<script src="resources/send-form-data-common.js"></script>
+<script>
+description("Test that filename passed to FormData.append() takes precedence over filename in File, even if empty.");
+
+self.jsTestIsAsync = true;
+
+function runTest()
+{
+    debug("Sending FormData containing one file with custom empty filename:");
+    
+    var input = document.getElementsByTagName("input")[0];
+    if (window.eventSender) {
+        eventSender.beginDragWithFiles(['../resources/file-for-drag-to-send.txt']);
+        moveMouseToCenterOfElement(input);
+        eventSender.mouseUp();
+    }
+
+    var formData = new FormData;
+    formData.append("file", input.files[0], "");
+    var xhr = new XMLHttpRequest();
+    xhr.open("POST", "http://127.0.0.1:8000/xmlhttprequest/resources/multipart-post-echo-filenames.php", true);
+    xhr.onload = function() {
+        filename = xhr.responseText;
+        shouldBe("filename", "''");
+        formDataTestingCleanup();
+        finishJSTest();
+    }
+
+    xhr.send(formData);
+}
+
+if (window.eventSender) {
+    runTest();
+} else {
+    debug("To run this test manually, please drag a file onto file input above");
+    document.getElementsByTagName("input")[0].onchange = runTest;
+}
+
+var successfullyParsed = true;
+</script>
+<script src="../../../../fast/js/resources/js-test-post.js"></script>
+</body>
+</html>
+
diff --git a/LayoutTests/http/tests/xmlhttprequest/resources/multipart-post-echo-filenames.php b/LayoutTests/http/tests/xmlhttprequest/resources/multipart-post-echo-filenames.php
new file mode 100644 (file)
index 0000000..c79cce9
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+$first = True;
+foreach ($_FILES as $file) {
+    if (!$first)
+        echo ",";
+    echo $file['name'];
+    $first = False;
+}
+?>
index 5ea176f..b5f6864 100644 (file)
@@ -868,6 +868,7 @@ http/tests/local/formdata/send-form-data-constructed-from-form.html
 http/tests/local/formdata/send-form-data-with-filename.html
 http/tests/local/formdata/send-form-data-with-sliced-file.html
 http/tests/local/formdata/send-form-data.html
+http/tests/local/formdata/send-form-data-with-empty-file-filename.html
 http/tests/local/formdata/upload-events.html
 http/tests/security/clipboard/clipboard-file-access.html
 media/video-src-blob.html
index e77fe8c..108e22d 100644 (file)
@@ -1,3 +1,20 @@
+2013-03-07  Alexey Proskuryakov  <ap@apple.com>
+
+        FormData should allow setting filename to empty
+        https://bugs.webkit.org/show_bug.cgi?id=111687
+
+        Reviewed by Brady Eidson.
+
+        Tests: http/tests/local/formdata/send-form-data-with-empty-blob-filename.html
+               http/tests/local/formdata/send-form-data-with-empty-file-filename.html
+
+        * html/FileInputType.cpp: (WebCore::FileInputType::appendFormData):
+        Use a Blob for empty file input, not a hacky File with empty path.
+
+        * platform/network/FormData.cpp: (WebCore::FormData::appendKeyValuePairItems):
+        Missing value is a null string. If the string is empty, we should treat is as
+        authoritative.
+
 2013-03-07  Andrey Lushnikov  <lushnikov@chromium.org>
 
         Web Inspector: [CodeMirror] dublicated horizontal scroll
index 8bcc101..061ccec 100644 (file)
@@ -40,6 +40,7 @@
 #include "RenderFileUploadControl.h"
 #include "ScriptController.h"
 #include "ShadowRoot.h"
+#include "WebKitBlobBuilder.h"
 #include <wtf/PassOwnPtr.h>
 #include <wtf/text/StringBuilder.h>
 #include <wtf/text/WTFString.h>
@@ -154,7 +155,7 @@ bool FileInputType::appendFormData(FormDataList& encoding, bool multipart) const
     // If no filename at all is entered, return successful but empty.
     // Null would be more logical, but Netscape posts an empty file. Argh.
     if (!numFiles) {
-        encoding.appendBlob(element()->name(), File::create(""));
+        encoding.appendBlob(element()->name(), BlobBuilder().getBlob(ASCIILiteral("application/octet-stream")), emptyString());
         return true;
     }
 
index 381d17b..f6d4588 100644 (file)
@@ -244,11 +244,11 @@ void FormData::appendKeyValuePairItems(const FormDataList& list, const TextEncod
                     }
 
                     // If a filename is passed in FormData.append(), use it instead of the file blob's name.
-                    if (!value.filename().isEmpty())
+                    if (!value.filename().isNull())
                         name = value.filename();
                 } else {
                     // For non-file blob, use the filename if it is passed in FormData.append().
-                    if (!value.filename().isEmpty())
+                    if (!value.filename().isNull())
                         name = value.filename();
                     else
                         name = "blob";