FileList.cpp 1.7 KB

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