1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-09 17:44:56 +09:00

LibWeb: Implement DataTransfer.prototype.getData()

This is used by Google Lens. It will always return the empty string when
dragging an image onto the Google Lens search field.
This commit is contained in:
Timothy Flynn 2024-08-20 18:29:00 -04:00 committed by Andreas Kling
parent 0b0d44da27
commit 535f03b6cf
Notes: github-actions[bot] 2024-08-22 12:23:15 +00:00
3 changed files with 54 additions and 2 deletions

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <AK/Find.h>
#include <LibJS/Runtime/Realm.h> #include <LibJS/Runtime/Realm.h>
#include <LibWeb/Bindings/DataTransferPrototype.h> #include <LibWeb/Bindings/DataTransferPrototype.h>
#include <LibWeb/Bindings/Intrinsics.h> #include <LibWeb/Bindings/Intrinsics.h>
@ -11,6 +12,7 @@
#include <LibWeb/FileAPI/File.h> #include <LibWeb/FileAPI/File.h>
#include <LibWeb/FileAPI/FileList.h> #include <LibWeb/FileAPI/FileList.h>
#include <LibWeb/HTML/DataTransfer.h> #include <LibWeb/HTML/DataTransfer.h>
#include <LibWeb/Infra/Strings.h>
namespace Web::HTML { namespace Web::HTML {
@ -87,6 +89,56 @@ ReadonlySpan<String> DataTransfer::types() const
return m_types; return m_types;
} }
// https://html.spec.whatwg.org/multipage/dnd.html#dom-datatransfer-getdata
String DataTransfer::get_data(String const& format_argument) const
{
// 1. If the DataTransfer object is no longer associated with a drag data store, then return the empty string.
if (!m_associated_drag_data_store.has_value())
return {};
// 2. If the drag data store's mode is the protected mode, then return the empty string.
if (m_associated_drag_data_store->mode() == DragDataStore::Mode::Protected)
return {};
// 3. Let format be the first argument, converted to ASCII lowercase.
auto format = MUST(Infra::to_ascii_lowercase(format_argument));
// 4. Let convert-to-URL be false.
[[maybe_unused]] bool convert_to_url = false;
// 5. If format equals "text", change it to "text/plain".
if (format == "text"sv) {
format = "text/plain"_string;
}
// 6. If format equals "url", change it to "text/uri-list" and set convert-to-URL to true.
else if (format == "url"sv) {
format = "text/uri-list"_string;
convert_to_url = true;
}
// 7. If there is no item in the drag data store item list whose kind is text and whose type string is equal to
// format, return the empty string.
auto item_list = m_associated_drag_data_store->item_list();
auto it = find_if(item_list.begin(), item_list.end(), [&](auto const& item) {
return item.kind == DragDataStoreItem::Kind::Text && item.type_string == format;
});
if (it == item_list.end())
return {};
// 8. Let result be the data of the item in the drag data store item list whose kind is Plain Unicode string and
// whose type string is equal to format.
auto const& result = it->data;
// FIXME: 9. If convert-to-URL is true, then parse result as appropriate for text/uri-list data, and then set result to
// the first URL from the list, if any, or the empty string otherwise.
// 10. Return result.
return MUST(String::from_utf8(result));
}
// https://html.spec.whatwg.org/multipage/dnd.html#dom-datatransfer-files // https://html.spec.whatwg.org/multipage/dnd.html#dom-datatransfer-files
JS::NonnullGCPtr<FileAPI::FileList> DataTransfer::files() const JS::NonnullGCPtr<FileAPI::FileList> DataTransfer::files() const
{ {

View file

@ -50,7 +50,7 @@ public:
void set_effect_allowed_internal(FlyString); void set_effect_allowed_internal(FlyString);
ReadonlySpan<String> types() const; ReadonlySpan<String> types() const;
String get_data(String const& format) const;
JS::NonnullGCPtr<FileAPI::FileList> files() const; JS::NonnullGCPtr<FileAPI::FileList> files() const;
void associate_with_drag_data_store(DragDataStore& drag_data_store); void associate_with_drag_data_store(DragDataStore& drag_data_store);

View file

@ -14,7 +14,7 @@ interface DataTransfer {
// old interface // old interface
readonly attribute sequence<DOMString> types; // FIXME: This should be FrozenArray<DOMString> readonly attribute sequence<DOMString> types; // FIXME: This should be FrozenArray<DOMString>
[FIXME] DOMString getData(DOMString format); DOMString getData(DOMString format);
[FIXME] undefined setData(DOMString format, DOMString data); [FIXME] undefined setData(DOMString format, DOMString data);
[FIXME] undefined clearData(optional DOMString format); [FIXME] undefined clearData(optional DOMString format);
[SameObject] readonly attribute FileList files; [SameObject] readonly attribute FileList files;