Repository: LadybirdBrowser/ladybird
File: Libraries/LibWeb/HTML/DataTransfer.cpp (C++)

Code before:
    if (m_associated_drag_data_store->mode() != DragDataStore::Mode::ReadWrite)
        return;

    // 3. Let format be the first argument, converted to ASCII lowercase.
    auto format = format_argument.to_ascii_lowercase();

    // 4. If format equals "text", change it to "text/plain".
    //    If format equals "url", change it to "text/uri-list".
    if (format == "text"sv) {
        format = "text/plain"_string;
    } else if (format == "url"sv) {
        format = "text/uri-list"_string;
    }

    // 5. Remove the item in the drag data store item list whose kind is text and whose type string is equal to format, if there is one.
    for (auto const& [i, item] : enumerate(m_associated_drag_data_store->item_list())) {
        if (item.kind == DragDataStoreItem::Kind::Text && item.type_string == format)
            m_associated_drag_data_store->remove_item_at(i);
    }

    // 6. Add an item to the drag data store item list
    // whose kind is text, whose type string is equal to format, and whose data is the string given by the method's second argument.
    DragDataStoreItem item {};
    item.kind = DragDataStoreItem::Kind::Text;
    item.type_string = format;
    item.data = value.to_byte_string().to_byte_buffer();
    m_associated_drag_data_store->add_item(move(item));

    update_data_transfer_types_list();
}

// https://html.spec.whatwg.org/multipage/dnd.html#dom-datatransfer-files
GC::Ref<FileAPI::FileList> DataTransfer::files() const
{
    auto& realm = this->realm();

    // 1. Start with an empty list L.
    auto files = FileAPI::FileList::create(realm);

    // 2. If the DataTransfer object is no longer associated with a drag data store, the FileList is empty. Return
    //    the empty list L.
    if (!m_associated_drag_data_store)
        return files;

    // 3. If the drag data store's mode is the protected mode, return the empty list L.
    if (m_associated_drag_data_store->mode() == DragDataStore::Mode::Protected)
        return files;

    // 4. For each item in the drag data store item list whose kind is File, add the item's data (the file, in
    //    particular its name and contents, as well as its type) to the list L.
    for (auto const& item : m_associated_drag_data_store->item_list()) {

Review comment (suggestion):
This creates a new `ByteString` object before copying it into a `ByteBuffer`. You should be able to do `MUST(ByteBuffer::copy(value.bytes()));` to avoid creating the intermediate `ByteString` object

Code after:

    // 2. If the drag data store's mode is not the read/write mode, return. Nothing happens.
    if (m_associated_drag_data_store->mode() != DragDataStore::Mode::ReadWrite)
        return;

    // 3. Let format be the first argument, converted to ASCII lowercase.
    auto format = format_argument.to_ascii_lowercase();

    // 4. If format equals "text", change it to "text/plain".
    //    If format equals "url", change it to "text/uri-list".
    if (format == "text"sv) {
        format = "text/plain"_string;
    } else if (format == "url"sv) {
        format = "text/uri-list"_string;
    }

    // 5. Remove the item in the drag data store item list whose kind is text and whose type string is equal to format, if there is one.
    for (auto const& [i, item] : enumerate(m_associated_drag_data_store->item_list())) {
        if (item.kind == DragDataStoreItem::Kind::Text && item.type_string == format) {
            remove_item(i);
            break;
        }
    }

    // 6. Add an item to the drag data store item list
    // whose kind is text, whose type string is equal to format, and whose data is the string given by the method's second argument.
    DragDataStoreItem item {};
    item.kind = DragDataStoreItem::Kind::Text;
    item.type_string = format;
    item.data = MUST(ByteBuffer::copy(value.bytes()));
    add_item(move(item));

    update_data_transfer_types_list();
}

// https://html.spec.whatwg.org/multipage/dnd.html#dom-datatransfer-files
GC::Ref<FileAPI::FileList> DataTransfer::files() const
{
    auto& realm = this->realm();

    // 1. Start with an empty list L.
    auto files = FileAPI::FileList::create(realm);

    // 2. If the DataTransfer object is no longer associated with a drag data store, the FileList is empty. Return
    //    the empty list L.
    if (!m_associated_drag_data_store)
        return files;

    // 3. If the drag data store's mode is the protected mode, return the empty list L.
    if (m_associated_drag_data_store->mode() == DragDataStore::Mode::Protected)
        return files;