DataTransferItemList.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/Realm.h>
  7. #include <LibWeb/Bindings/DataTransferItemListPrototype.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/FileAPI/File.h>
  10. #include <LibWeb/HTML/DataTransfer.h>
  11. #include <LibWeb/HTML/DataTransferItem.h>
  12. #include <LibWeb/HTML/DataTransferItemList.h>
  13. #include <LibWeb/Infra/Strings.h>
  14. namespace Web::HTML {
  15. GC_DEFINE_ALLOCATOR(DataTransferItemList);
  16. GC::Ref<DataTransferItemList> DataTransferItemList::create(JS::Realm& realm, GC::Ref<DataTransfer> data_transfer)
  17. {
  18. return realm.create<DataTransferItemList>(realm, data_transfer);
  19. }
  20. DataTransferItemList::DataTransferItemList(JS::Realm& realm, GC::Ref<DataTransfer> data_transfer)
  21. : PlatformObject(realm)
  22. , m_data_transfer(data_transfer)
  23. {
  24. m_legacy_platform_object_flags = LegacyPlatformObjectFlags { .supports_indexed_properties = true };
  25. }
  26. DataTransferItemList::~DataTransferItemList() = default;
  27. void DataTransferItemList::initialize(JS::Realm& realm)
  28. {
  29. Base::initialize(realm);
  30. WEB_SET_PROTOTYPE_FOR_INTERFACE(DataTransferItemList);
  31. }
  32. void DataTransferItemList::visit_edges(JS::Cell::Visitor& visitor)
  33. {
  34. Base::visit_edges(visitor);
  35. visitor.visit(m_data_transfer);
  36. }
  37. // https://html.spec.whatwg.org/multipage/dnd.html#dom-datatransferitemlist-length
  38. WebIDL::UnsignedLong DataTransferItemList::length() const
  39. {
  40. // The length attribute must return zero if the object is in the disabled mode; otherwise it must return the number
  41. // of items in the drag data store item list.
  42. return m_data_transfer->length();
  43. }
  44. // https://html.spec.whatwg.org/multipage/dnd.html#dom-datatransferitemlist-add
  45. WebIDL::ExceptionOr<GC::Ptr<DataTransferItem>> DataTransferItemList::add(String const& data, String const& type)
  46. {
  47. auto& realm = this->realm();
  48. // 1. If the DataTransferItemList object is not in the read/write mode, return null.
  49. if (m_data_transfer->mode() != DragDataStore::Mode::ReadWrite)
  50. return nullptr;
  51. // 2. Jump to the appropriate set of steps from the following list:
  52. // -> If the first argument to the method is a string
  53. // If there is already an item in the drag data store item list whose kind is text and whose type string is equal
  54. // to the value of the method's second argument, converted to ASCII lowercase, then throw a "NotSupportedError"
  55. // DOMException.
  56. if (m_data_transfer->contains_item_with_type(DragDataStoreItem::Kind::Text, type)) {
  57. auto error = MUST(String::formatted("There is already a DataTransferItem with type {}", type));
  58. return WebIDL::NotSupportedError::create(realm, error);
  59. }
  60. // Otherwise, add an item to the drag data store item list whose kind is text, whose type string is equal to the
  61. // value of the method's second argument, converted to ASCII lowercase, and whose data is the string given by the
  62. // method's first argument.
  63. auto item = m_data_transfer->add_item({
  64. .kind = HTML::DragDataStoreItem::Kind::Text,
  65. .type_string = type.to_ascii_lowercase(),
  66. .data = MUST(ByteBuffer::copy(data.bytes())),
  67. .file_name = {},
  68. });
  69. // 3. Determine the value of the indexed property corresponding to the newly added item, and return that value (a
  70. // newly created DataTransferItem object).
  71. return item;
  72. }
  73. // https://html.spec.whatwg.org/multipage/dnd.html#dom-datatransferitemlist-add
  74. GC::Ptr<DataTransferItem> DataTransferItemList::add(GC::Ref<FileAPI::File> file)
  75. {
  76. // 1. If the DataTransferItemList object is not in the read/write mode, return null.
  77. if (m_data_transfer->mode() != DragDataStore::Mode::ReadWrite)
  78. return nullptr;
  79. // 2. Jump to the appropriate set of steps from the following list:
  80. // -> If the first argument to the method is a File
  81. // Add an item to the drag data store item list whose kind is File, whose type string is the type of the File,
  82. // converted to ASCII lowercase, and whose data is the same as the File's data.
  83. auto item = m_data_transfer->add_item({
  84. .kind = HTML::DragDataStoreItem::Kind::File,
  85. .type_string = file->type().to_ascii_lowercase(),
  86. .data = MUST(ByteBuffer::copy(file->raw_bytes())),
  87. .file_name = file->name().to_byte_string(),
  88. });
  89. // 3. Determine the value of the indexed property corresponding to the newly added item, and return that value (a
  90. // newly created DataTransferItem object).
  91. return item;
  92. }
  93. // https://html.spec.whatwg.org/multipage/dnd.html#dom-datatransferitemlist-item
  94. Optional<JS::Value> DataTransferItemList::item_value(size_t index) const
  95. {
  96. // To determine the value of an indexed property i of a DataTransferItemList object, the user agent must return a
  97. // DataTransferItem object representing the ith item in the drag data store. The same object must be returned each
  98. // time a particular item is obtained from this DataTransferItemList object. The DataTransferItem object must be
  99. // associated with the same DataTransfer object as the DataTransferItemList object when it is first created.
  100. if (index < m_data_transfer->length())
  101. return m_data_transfer->item(index);
  102. return {};
  103. }
  104. }