diff options
author | Peter Varga <pvarga@inf.u-szeged.hu> | 2021-11-11 14:24:48 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-11-19 09:00:15 +0000 |
commit | 5a2db0883cc6acadebf3fa0197c36d81af167492 (patch) | |
tree | 0d6823483133c95e7df11f2af7ca4840b7da8662 /src/core/clipboard_qt.cpp | |
parent | 52cf91622a628ababfd39514c0e1b465d1a70b89 (diff) | |
download | qtwebengine-5a2db0883cc6acadebf3fa0197c36d81af167492.tar.gz |
Add CF_HTML clipboard format handling
Fixes: QTBUG-92539
Change-Id: Iece974e7b045bd793ceb8870f370803bf2524c33
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
(cherry picked from commit 74021bfcb8f3937e960e76c39d06f5a2a6304673)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src/core/clipboard_qt.cpp')
-rw-r--r-- | src/core/clipboard_qt.cpp | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/core/clipboard_qt.cpp b/src/core/clipboard_qt.cpp index 026653cbb..6549a19da 100644 --- a/src/core/clipboard_qt.cpp +++ b/src/core/clipboard_qt.cpp @@ -46,6 +46,7 @@ #include "type_conversion.h" #include "base/logging.h" +#include "base/strings/utf_offset_string_conversions.h" #include "base/strings/utf_string_conversions.h" #include "third_party/skia/include/core/SkBitmap.h" #include "ui/base/clipboard/custom_data_helper.h" @@ -107,6 +108,12 @@ Clipboard *Clipboard::Create() namespace QtWebEngineCore { +#if defined(Q_OS_WIN) +extern std::string HtmlToCFHtml(const std::string &html, const std::string &base_url); +extern void CFHtmlExtractMetadata(const std::string &cf_html, std::string *base_url, + size_t *html_start, size_t *fragment_start, size_t *fragment_end); +#endif // defined(Q_OS_WIN) + void ClipboardQt::WritePortableRepresentations(ui::ClipboardBuffer type, const ObjectMap &objects, std::unique_ptr<ui::DataTransferEndpoint> data_src) { DCHECK(CalledOnValidThread()); @@ -157,7 +164,17 @@ void ClipboardQt::WriteHTML(const char *markup_data, size_t markup_len, const ch // Mirrors the behavior in ui/base/clipboard/clipboard_mac.mm in Chromium. markup_string.prepend(QLatin1String("<meta charset='utf-8'>")); #endif + +#if !defined(Q_OS_WIN) getUncommittedData()->setHtml(markup_string); +#else + std::string url; + if (url_len > 0) + url.assign(url_data, url_len); + + std::string cf_html = HtmlToCFHtml(markup_string.toStdString(), url); + getUncommittedData()->setHtml(QString::fromStdString(cf_html)); +#endif // !defined(Q_OS_WIN) } void ClipboardQt::WriteRTF(const char *rtf_data, size_t data_len) @@ -269,8 +286,35 @@ void ClipboardQt::ReadHTML(ui::ClipboardBuffer type, type == ui::ClipboardBuffer::kCopyPaste ? QClipboard::Clipboard : QClipboard::Selection); if (!mimeData) return; + +#if !defined(Q_OS_WIN) *markup = toString16(mimeData->html()); *fragment_end = static_cast<uint32_t>(markup->length()); +#else + const std::string cf_html = mimeData->html().toStdString(); + size_t html_start = std::string::npos; + size_t start_index = std::string::npos; + size_t end_index = std::string::npos; + CFHtmlExtractMetadata(cf_html, src_url, &html_start, &start_index, &end_index); + + // This might happen if the contents of the clipboard changed and CF_HTML is + // no longer available. + if (start_index == std::string::npos || end_index == std::string::npos + || html_start == std::string::npos) + return; + + if (start_index < html_start || end_index < start_index) + return; + + std::vector<size_t> offsets; + offsets.push_back(start_index - html_start); + offsets.push_back(end_index - html_start); + markup->assign(base::UTF8ToUTF16AndAdjustOffsets(cf_html.data() + html_start, &offsets)); + // Ensure the Fragment points within the string; see https://crbug.com/607181. + size_t end = std::min(offsets[1], markup->length()); + *fragment_start = base::checked_cast<uint32_t>(std::min(offsets[0], end)); + *fragment_end = base::checked_cast<uint32_t>(end); +#endif // !defined(Q_OS_WIN) } void ClipboardQt::ReadRTF(ui::ClipboardBuffer type, |