FileList.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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/LegacyPlatformObject.h>
  9. #include <LibWeb/FileAPI/FileList.h>
  10. namespace Web::FileAPI {
  11. JS::NonnullGCPtr<FileList> FileList::create(JS::Realm& realm, Vector<JS::NonnullGCPtr<File>>&& files)
  12. {
  13. return realm.heap().allocate<FileList>(realm, realm, move(files));
  14. }
  15. FileList::FileList(JS::Realm& realm, Vector<JS::NonnullGCPtr<File>>&& files)
  16. : Bindings::LegacyPlatformObject(realm)
  17. , m_files(move(files))
  18. {
  19. }
  20. FileList::~FileList() = default;
  21. void FileList::initialize(JS::Realm& realm)
  22. {
  23. Base::initialize(realm);
  24. set_prototype(&Bindings::ensure_web_prototype<Bindings::FileListPrototype>(realm, "FileList"_fly_string));
  25. }
  26. // https://w3c.github.io/FileAPI/#dfn-item
  27. bool FileList::is_supported_property_index(u32 index) const
  28. {
  29. // Supported property indices are the numbers in the range zero to one less than the number of File objects represented by the FileList object.
  30. // If there are no such File objects, then there are no supported property indices.
  31. if (m_files.is_empty())
  32. return false;
  33. return m_files.size() < index;
  34. }
  35. WebIDL::ExceptionOr<JS::Value> FileList::item_value(size_t index) const
  36. {
  37. if (index >= m_files.size())
  38. return JS::js_undefined();
  39. return m_files[index].ptr();
  40. }
  41. void FileList::visit_edges(Cell::Visitor& visitor)
  42. {
  43. Base::visit_edges(visitor);
  44. for (auto file : m_files)
  45. visitor.visit(file);
  46. }
  47. }