2022-10-04 00:10:39 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <LibJS/Runtime/Realm.h>
|
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2024-01-09 20:05:03 -03:00
|
|
|
#include <LibWeb/Bindings/PlatformObject.h>
|
2022-10-04 00:10:39 -03:00
|
|
|
#include <LibWeb/FileAPI/FileList.h>
|
|
|
|
|
|
|
|
|
|
namespace Web::FileAPI {
|
|
|
|
|
|
2023-12-23 11:15:27 -03:00
|
|
|
JS_DEFINE_ALLOCATOR(FileList);
|
|
|
|
|
|
2023-08-13 08:05:26 -03:00
|
|
|
JS::NonnullGCPtr<FileList> FileList::create(JS::Realm& realm, Vector<JS::NonnullGCPtr<File>>&& files)
|
2022-10-04 00:10:39 -03:00
|
|
|
{
|
2023-08-13 08:05:26 -03:00
|
|
|
return realm.heap().allocate<FileList>(realm, realm, move(files));
|
2022-10-04 00:10:39 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FileList::FileList(JS::Realm& realm, Vector<JS::NonnullGCPtr<File>>&& files)
|
2024-01-09 20:05:03 -03:00
|
|
|
: Bindings::PlatformObject(realm)
|
2022-10-04 00:10:39 -03:00
|
|
|
, m_files(move(files))
|
|
|
|
|
{
|
2024-01-09 20:05:03 -03:00
|
|
|
m_legacy_platform_object_flags = LegacyPlatformObjectFlags { .supports_indexed_properties = 1 };
|
2022-10-04 00:10:39 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FileList::~FileList() = default;
|
|
|
|
|
|
2023-08-07 03:41:28 -03:00
|
|
|
void FileList::initialize(JS::Realm& realm)
|
2023-01-10 08:56:59 -03:00
|
|
|
{
|
2023-08-07 03:41:28 -03:00
|
|
|
Base::initialize(realm);
|
2023-11-21 20:55:21 -03:00
|
|
|
set_prototype(&Bindings::ensure_web_prototype<Bindings::FileListPrototype>(realm, "FileList"_fly_string));
|
2023-01-10 08:56:59 -03:00
|
|
|
}
|
|
|
|
|
|
2022-10-04 00:10:39 -03:00
|
|
|
// https://w3c.github.io/FileAPI/#dfn-item
|
|
|
|
|
bool FileList::is_supported_property_index(u32 index) const
|
|
|
|
|
{
|
|
|
|
|
// Supported property indices are the numbers in the range zero to one less than the number of File objects represented by the FileList object.
|
|
|
|
|
// If there are no such File objects, then there are no supported property indices.
|
|
|
|
|
if (m_files.is_empty())
|
|
|
|
|
return false;
|
|
|
|
|
|
2024-03-13 16:29:32 -03:00
|
|
|
return index < m_files.size();
|
2022-10-04 00:10:39 -03:00
|
|
|
}
|
|
|
|
|
|
2023-02-27 21:05:39 -03:00
|
|
|
WebIDL::ExceptionOr<JS::Value> FileList::item_value(size_t index) const
|
2022-10-04 00:10:39 -03:00
|
|
|
{
|
|
|
|
|
if (index >= m_files.size())
|
|
|
|
|
return JS::js_undefined();
|
|
|
|
|
|
|
|
|
|
return m_files[index].ptr();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FileList::visit_edges(Cell::Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
|
for (auto file : m_files)
|
|
|
|
|
visitor.visit(file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|