DataTransferItemList.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/DataTransferItemList.h>
  12. #include <LibWeb/Infra/Strings.h>
  13. namespace Web::HTML {
  14. JS_DEFINE_ALLOCATOR(DataTransferItemList);
  15. JS::NonnullGCPtr<DataTransferItemList> DataTransferItemList::create(JS::Realm& realm, JS::NonnullGCPtr<DataTransfer> data_transfer)
  16. {
  17. return realm.heap().allocate<DataTransferItemList>(realm, realm, data_transfer);
  18. }
  19. DataTransferItemList::DataTransferItemList(JS::Realm& realm, JS::NonnullGCPtr<DataTransfer> data_transfer)
  20. : PlatformObject(realm)
  21. , m_data_transfer(data_transfer)
  22. {
  23. }
  24. DataTransferItemList::~DataTransferItemList() = default;
  25. void DataTransferItemList::initialize(JS::Realm& realm)
  26. {
  27. Base::initialize(realm);
  28. WEB_SET_PROTOTYPE_FOR_INTERFACE(DataTransferItemList);
  29. }
  30. void DataTransferItemList::visit_edges(JS::Cell::Visitor& visitor)
  31. {
  32. Base::visit_edges(visitor);
  33. visitor.visit(m_data_transfer);
  34. }
  35. // https://html.spec.whatwg.org/multipage/dnd.html#dom-datatransferitemlist-length
  36. WebIDL::UnsignedLong DataTransferItemList::length() const
  37. {
  38. // The length attribute must return zero if the object is in the disabled mode; otherwise it must return the number
  39. // of items in the drag data store item list.
  40. return m_data_transfer->length();
  41. }
  42. // https://html.spec.whatwg.org/multipage/dnd.html#dom-datatransferitemlist-add
  43. WebIDL::ExceptionOr<JS::GCPtr<DataTransferItem>> DataTransferItemList::add(String const& data, String const& type)
  44. {
  45. auto& realm = this->realm();
  46. // 1. If the DataTransferItemList object is not in the read/write mode, return null.
  47. if (m_data_transfer->mode() != DragDataStore::Mode::ReadWrite)
  48. return nullptr;
  49. // 2. Jump to the appropriate set of steps from the following list:
  50. // -> If the first argument to the method is a string
  51. // If there is already an item in the drag data store item list whose kind is text and whose type string is equal
  52. // to the value of the method's second argument, converted to ASCII lowercase, then throw a "NotSupportedError"
  53. // DOMException.
  54. if (m_data_transfer->contains_item_with_type(DragDataStoreItem::Kind::Text, type)) {
  55. auto error = MUST(String::formatted("There is already a DataTransferItem with type {}", type));
  56. return WebIDL::NotSupportedError::create(realm, error);
  57. }
  58. // Otherwise, add an item to the drag data store item list whose kind is text, whose type string is equal to the
  59. // value of the method's second argument, converted to ASCII lowercase, and whose data is the string given by the
  60. // method's first argument.
  61. auto item = m_data_transfer->add_item({
  62. .kind = HTML::DragDataStoreItem::Kind::Text,
  63. .type_string = MUST(Infra::to_ascii_lowercase(type)),
  64. .data = MUST(ByteBuffer::copy(data.bytes())),
  65. .file_name = {},
  66. });
  67. // 3. Determine the value of the indexed property corresponding to the newly added item, and return that value (a
  68. // newly created DataTransferItem object).
  69. return item;
  70. }
  71. // https://html.spec.whatwg.org/multipage/dnd.html#dom-datatransferitemlist-add
  72. JS::GCPtr<DataTransferItem> DataTransferItemList::add(JS::NonnullGCPtr<FileAPI::File> file)
  73. {
  74. // 1. If the DataTransferItemList object is not in the read/write mode, return null.
  75. if (m_data_transfer->mode() != DragDataStore::Mode::ReadWrite)
  76. return nullptr;
  77. // 2. Jump to the appropriate set of steps from the following list:
  78. // -> If the first argument to the method is a File
  79. // Add an item to the drag data store item list whose kind is File, whose type string is the type of the File,
  80. // converted to ASCII lowercase, and whose data is the same as the File's data.
  81. auto item = m_data_transfer->add_item({
  82. .kind = HTML::DragDataStoreItem::Kind::File,
  83. .type_string = MUST(Infra::to_ascii_lowercase(file->type())),
  84. .data = MUST(ByteBuffer::copy(file->raw_bytes())),
  85. .file_name = file->name().to_byte_string(),
  86. });
  87. // 3. Determine the value of the indexed property corresponding to the newly added item, and return that value (a
  88. // newly created DataTransferItem object).
  89. return item;
  90. }
  91. }