From 3acb7453d8ab3234cbf005176d4c36e19cdc2cc4 Mon Sep 17 00:00:00 2001 From: "jianli@chromium.org" Date: Wed, 3 Mar 2010 18:26:08 +0000 Subject: Fix bug 17333: Can't copy-paste text from OpenOffice to a contenteditable DIV BUG=17333 TEST=ClipboardTest.HtmlTest Review URL: http://codereview.chromium.org/661429 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@40523 0039d316-1c4b-4281-b951-d872f2087c98 --- app/clipboard/clipboard_unittest.cc | 45 +++++++++++++++++++++++++++++++++++++ app/clipboard/clipboard_util_win.cc | 32 +++++++++++++++++++++----- 2 files changed, 72 insertions(+), 5 deletions(-) (limited to 'app/clipboard') diff --git a/app/clipboard/clipboard_unittest.cc b/app/clipboard/clipboard_unittest.cc index 034e3a3..a980679 100644 --- a/app/clipboard/clipboard_unittest.cc +++ b/app/clipboard/clipboard_unittest.cc @@ -5,6 +5,9 @@ #include #include "app/clipboard/clipboard.h" +#if defined(OS_WIN) +#include "app/clipboard/clipboard_util_win.h" +#endif #include "app/clipboard/scoped_clipboard_writer.h" #include "base/basictypes.h" #include "base/gfx/size.h" @@ -334,6 +337,48 @@ TEST_F(ClipboardTest, BitmapTest) { EXPECT_TRUE(clipboard.IsFormatAvailable(Clipboard::GetBitmapFormatType(), Clipboard::BUFFER_STANDARD)); } + +void HtmlTestHelper(const std::string& cf_html, + const std::string& expected_html) { + std::string html; + ClipboardUtil::CFHtmlToHtml(cf_html, &html, NULL); + EXPECT_EQ(html, expected_html); +} + +TEST_F(ClipboardTest, HtmlTest) { + // Test converting from CF_HTML format data with and + // comments, like from MS Word. + HtmlTestHelper("Version:1.0\r\n" + "StartHTML:0000000105\r\n" + "EndHTML:0000000199\r\n" + "StartFragment:0000000123\r\n" + "EndFragment:0000000161\r\n" + "\r\n" + "\r\n" + "\r\n" + "\r\n" + "\r\n" + "

Foo

\r\n" + "\r\n" + "\r\n" + "\r\n" + "\r\n\r\n", + "

Foo

"); + + // Test converting from CF_HTML format data without and + // comments, like from OpenOffice Writer. + HtmlTestHelper("Version:1.0\r\n" + "StartHTML:0000000105\r\n" + "EndHTML:0000000151\r\n" + "StartFragment:0000000121\r\n" + "EndFragment:0000000131\r\n" + "\r\n" + "\r\n" + "

Foo

\r\n" + "\r\n" + "\r\n\r\n", + "

Foo

"); +} #endif // defined(OS_WIN) // Test writing all formats we have simultaneously. diff --git a/app/clipboard/clipboard_util_win.cc b/app/clipboard/clipboard_util_win.cc index 870d789..37b78f7 100644 --- a/app/clipboard/clipboard_util_win.cc +++ b/app/clipboard/clipboard_util_win.cc @@ -488,14 +488,36 @@ void ClipboardUtil::CFHtmlToHtml(const std::string& cf_html, } } - // Find the markup between "" and "". + // Find the markup between "" and "". + // If the comments cannot be found, like copying from OpenOffice Writer, + // we simply fall back to using StartFragment/EndFragment bytecount values + // to get the markup. if (html) { + size_t fragment_start = std::string::npos; + size_t fragment_end = std::string::npos; + std::string cf_html_lower = StringToLowerASCII(cf_html); size_t markup_start = cf_html_lower.find("', tag_start) + 1; - size_t tag_end = cf_html.rfind("EndFragment", std::string::npos); - size_t fragment_end = cf_html.rfind('<', tag_end); + size_t tag_start = cf_html.find("