Bug 1779348 - Clear FileList after files changed. r=smaug

This matches Chrome and Safari. Also add a test for FileList being
shareable between DataTransfer and input, which seems to be in the
intention of the spec and matches our pre-existing behavior, the
behavior of input-to-input FileList handling, and Safari, but not
Chrome.

Differential Revision: https://phabricator.services.mozilla.com/D152367
This commit is contained in:
Emilio Cobos Álvarez
2022-07-21 11:06:18 +00:00
parent 1360794ddc
commit b7aeba4a07
3 changed files with 24 additions and 30 deletions

View File

@@ -2524,7 +2524,11 @@ void HTMLInputElement::AfterSetFilesOrDirectories(bool aSetValueChanged) {
}
}
UpdateFileList();
// Null out |mFileData->mFileList| to return a new file list when asked for.
// Don't clear it since the file list might come from the user via SetFiles.
if (mFileData->mFileList) {
mFileData->mFileList = nullptr;
}
if (aSetValueChanged) {
SetValueChanged(true);
@@ -2557,7 +2561,11 @@ FileList* HTMLInputElement::GetFiles() {
if (!mFileData->mFileList) {
mFileData->mFileList = new FileList(static_cast<nsIContent*>(this));
UpdateFileList();
for (const OwningFileOrDirectory& item : GetFilesOrDirectoriesInternal()) {
if (item.IsFile()) {
mFileData->mFileList->Append(item.GetAsFile());
}
}
}
return mFileData->mFileList;
@@ -2568,15 +2576,11 @@ void HTMLInputElement::SetFiles(FileList* aFiles) {
return;
}
// Clear |mFileData->mFileList| to omit |UpdateFileList|
if (mFileData->mFileList) {
mFileData->mFileList->Clear();
mFileData->mFileList = nullptr;
}
// Update |mFileData->mFilesOrDirectories|
SetFiles(aFiles, true);
MOZ_ASSERT(!mFileData->mFileList, "Should've cleared the existing file list");
// Update |mFileData->mFileList| without copy
mFileData->mFileList = aFiles;
}
@@ -2601,23 +2605,6 @@ void HTMLInputElement::HandleNumberControlSpin(void* aData) {
}
}
void HTMLInputElement::UpdateFileList() {
MOZ_ASSERT(mFileData);
if (mFileData->mFileList) {
mFileData->mFileList->Clear();
const nsTArray<OwningFileOrDirectory>& array =
GetFilesOrDirectoriesInternal();
for (uint32_t i = 0; i < array.Length(); ++i) {
if (array[i].IsFile()) {
mFileData->mFileList->Append(array[i].GetAsFile());
}
}
}
}
nsresult HTMLInputElement::SetValueInternal(
const nsAString& aValue, const nsAString* aOldValue,
const ValueSetterOptions& aOptions) {