Bug 1459859 - Treat an empty <input type=file> as an empty File in FormData. r=smaug,robwu

This patch changes the value of an empty `<input type=file>` control as obtained from the `FormData` API, which used to be an empty string, to be a `File` object with empty contents, whose name is the empty string and whose type is `"application/octet-stream"`.

Differential Revision: https://phabricator.services.mozilla.com/D106605
This commit is contained in:
Andreu Botella
2021-03-24 18:43:58 +00:00
parent 3cc063b808
commit f778253bfe
9 changed files with 116 additions and 110 deletions

View File

@@ -5668,13 +5668,21 @@ HTMLInputElement::SubmitNamesValues(HTMLFormSubmission* aFormSubmission) {
GetFilesOrDirectoriesInternal();
if (files.IsEmpty()) {
aFormSubmission->AddNameBlobOrNullPair(name, nullptr);
return NS_OK;
ErrorResult rv;
RefPtr<Blob> blob = Blob::CreateStringBlob(
GetOwnerGlobal(), ""_ns, u"application/octet-stream"_ns);
RefPtr<File> file = blob->ToFile(u""_ns, rv);
if (!rv.Failed()) {
aFormSubmission->AddNameBlobPair(name, file);
}
return rv.StealNSResult();
}
for (uint32_t i = 0; i < files.Length(); ++i) {
if (files[i].IsFile()) {
aFormSubmission->AddNameBlobOrNullPair(name, files[i].GetAsFile());
aFormSubmission->AddNameBlobPair(name, files[i].GetAsFile());
} else {
MOZ_ASSERT(files[i].IsDirectory());
aFormSubmission->AddNameDirectoryPair(name, files[i].GetAsDirectory());