FileList.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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(Bindings::cached_web_prototype(realm, "FileList"))
  17. , m_files(move(files))
  18. {
  19. }
  20. FileList::~FileList() = default;
  21. // https://w3c.github.io/FileAPI/#dfn-item
  22. bool FileList::is_supported_property_index(u32 index) const
  23. {
  24. // Supported property indices are the numbers in the range zero to one less than the number of File objects represented by the FileList object.
  25. // If there are no such File objects, then there are no supported property indices.
  26. if (m_files.is_empty())
  27. return false;
  28. return m_files.size() < index;
  29. }
  30. JS::Value FileList::item_value(size_t index) const
  31. {
  32. if (index >= m_files.size())
  33. return JS::js_undefined();
  34. return m_files[index].ptr();
  35. }
  36. void FileList::visit_edges(Cell::Visitor& visitor)
  37. {
  38. Base::visit_edges(visitor);
  39. for (auto file : m_files)
  40. visitor.visit(file);
  41. }
  42. }