FileList.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/Realm.h>
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/Bindings/PlatformObject.h>
  9. #include <LibWeb/FileAPI/FileList.h>
  10. namespace Web::FileAPI {
  11. JS_DEFINE_ALLOCATOR(FileList);
  12. JS::NonnullGCPtr<FileList> FileList::create(JS::Realm& realm, Vector<JS::NonnullGCPtr<File>>&& files)
  13. {
  14. return realm.heap().allocate<FileList>(realm, realm, move(files));
  15. }
  16. FileList::FileList(JS::Realm& realm, Vector<JS::NonnullGCPtr<File>>&& files)
  17. : Bindings::PlatformObject(realm)
  18. , m_files(move(files))
  19. {
  20. m_legacy_platform_object_flags = LegacyPlatformObjectFlags { .supports_indexed_properties = 1 };
  21. }
  22. FileList::~FileList() = default;
  23. void FileList::initialize(JS::Realm& realm)
  24. {
  25. Base::initialize(realm);
  26. set_prototype(&Bindings::ensure_web_prototype<Bindings::FileListPrototype>(realm, "FileList"_fly_string));
  27. }
  28. // https://w3c.github.io/FileAPI/#dfn-item
  29. bool FileList::is_supported_property_index(u32 index) const
  30. {
  31. // Supported property indices are the numbers in the range zero to one less than the number of File objects represented by the FileList object.
  32. // If there are no such File objects, then there are no supported property indices.
  33. if (m_files.is_empty())
  34. return false;
  35. return m_files.size() < index;
  36. }
  37. WebIDL::ExceptionOr<JS::Value> FileList::item_value(size_t index) const
  38. {
  39. if (index >= m_files.size())
  40. return JS::js_undefined();
  41. return m_files[index].ptr();
  42. }
  43. void FileList::visit_edges(Cell::Visitor& visitor)
  44. {
  45. Base::visit_edges(visitor);
  46. for (auto file : m_files)
  47. visitor.visit(file);
  48. }
  49. }