diff --git a/Userland/Libraries/LibWeb/HTML/DataTransfer.cpp b/Userland/Libraries/LibWeb/HTML/DataTransfer.cpp index 6baa5b7f0a2..b109537dc58 100644 --- a/Userland/Libraries/LibWeb/HTML/DataTransfer.cpp +++ b/Userland/Libraries/LibWeb/HTML/DataTransfer.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include @@ -11,6 +12,7 @@ #include #include #include +#include namespace Web::HTML { @@ -87,6 +89,56 @@ ReadonlySpan DataTransfer::types() const 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 JS::NonnullGCPtr DataTransfer::files() const { diff --git a/Userland/Libraries/LibWeb/HTML/DataTransfer.h b/Userland/Libraries/LibWeb/HTML/DataTransfer.h index 1ab610df538..cc71f3b2f1b 100644 --- a/Userland/Libraries/LibWeb/HTML/DataTransfer.h +++ b/Userland/Libraries/LibWeb/HTML/DataTransfer.h @@ -50,7 +50,7 @@ public: void set_effect_allowed_internal(FlyString); ReadonlySpan types() const; - + String get_data(String const& format) const; JS::NonnullGCPtr files() const; void associate_with_drag_data_store(DragDataStore& drag_data_store); diff --git a/Userland/Libraries/LibWeb/HTML/DataTransfer.idl b/Userland/Libraries/LibWeb/HTML/DataTransfer.idl index 41921951f58..bb96818413b 100644 --- a/Userland/Libraries/LibWeb/HTML/DataTransfer.idl +++ b/Userland/Libraries/LibWeb/HTML/DataTransfer.idl @@ -14,7 +14,7 @@ interface DataTransfer { // old interface readonly attribute sequence types; // FIXME: This should be FrozenArray - [FIXME] DOMString getData(DOMString format); + DOMString getData(DOMString format); [FIXME] undefined setData(DOMString format, DOMString data); [FIXME] undefined clearData(optional DOMString format); [SameObject] readonly attribute FileList files;