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

Code before:
    // 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);
            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()));
    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()) {
        if (item.kind != DragDataStoreItem::Kind::File)

Review comment (suggestion):
I think this needs to be just `add_item(move(item))` instead. This ensures that `DataTransfer::m_item_list` and `DragDataStore::m_item_list` remain in sync.

Code after:
        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;

    // 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.