diff options
9 files changed, 69 insertions, 82 deletions
diff --git a/app/clipboard/clipboard_mac.mm b/app/clipboard/clipboard_mac.mm index 1596eba..f546f84 100644 --- a/app/clipboard/clipboard_mac.mm +++ b/app/clipboard/clipboard_mac.mm @@ -64,7 +64,9 @@ void Clipboard::WriteHTML(const char* markup_data, size_t markup_len, const char* url_data, size_t url_len) { - std::string html_fragment_str(markup_data, markup_len); + // We need to mark it as utf-8. (see crbug.com/11957) + std::string html_fragment_str("<meta charset='utf-8'>"); + html_fragment_str.append(markup_data, markup_len); NSString *html_fragment = base::SysUTF8ToNSString(html_fragment_str); // TODO(avi): url_data? diff --git a/app/clipboard/clipboard_unittest.cc b/app/clipboard/clipboard_unittest.cc index 8203bac..8a2bd62 100644 --- a/app/clipboard/clipboard_unittest.cc +++ b/app/clipboard/clipboard_unittest.cc @@ -34,7 +34,7 @@ namespace { bool ClipboardContentsIsExpected(const string16& copied_markup, const string16& pasted_markup) { -#if defined(OS_LINUX) +#if defined(OS_POSIX) return pasted_markup.find(copied_markup) != string16::npos; #else return copied_markup == pasted_markup; @@ -353,4 +353,3 @@ TEST_F(ClipboardTest, WriteEverything) { // Passes if we don't crash. } - diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_mac.h b/chrome/browser/autocomplete/autocomplete_edit_view_mac.h index 066a353..b544ae7 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view_mac.h +++ b/chrome/browser/autocomplete/autocomplete_edit_view_mac.h @@ -79,6 +79,7 @@ class AutocompleteEditViewMac : public AutocompleteEditView, // Implement the AutocompleteTextFieldObserver interface. virtual void OnControlKeyChanged(bool pressed); + virtual void OnCopy(); virtual void OnPaste(); virtual bool CanPasteAndGo(); virtual int GetPasteActionStringId(); diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm b/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm index 0ee2f7b..852b393 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm +++ b/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm @@ -7,6 +7,7 @@ #include <Carbon/Carbon.h> // kVK_Return #include "app/clipboard/clipboard.h" +#include "app/clipboard/scoped_clipboard_writer.h" #include "app/resource_bundle.h" #include "base/string_util.h" #include "base/sys_string_conversions.h" @@ -18,6 +19,7 @@ #include "chrome/browser/tab_contents/tab_contents.h" #include "chrome/browser/toolbar_model.h" #include "grit/generated_resources.h" +#include "net/base/escape.h" // Focus-handling between |field_| and |model_| is a bit subtle. // Other platforms detect change of focus, which is inconvenient @@ -679,6 +681,34 @@ void AutocompleteEditViewMac::OnDidResignKey() { ClosePopup(); } +void AutocompleteEditViewMac::OnCopy() { + const NSRange selection = GetSelectedRange(); + if (selection.length == 0) + return; + + const std::wstring& text = GetText(); + const string16 text16 = WideToUTF16(text); + + ScopedClipboardWriter scw(g_browser_process->clipboard()); + // If the entire contents are being copied and it looks like an URL, + // copy as a hyperlink. + if (IsSelectAll()) { + GURL url; + if (model_->GetURLForText(text, &url)) { + if ((url.SchemeIs("http") || url.SchemeIs("https")) && + !model_->user_input_in_progress()) + scw.WriteText(UTF8ToUTF16(url.spec())); + else + scw.WriteText(text16); + scw.WriteBookmark(text16, url.spec()); + scw.WriteHyperlink(EscapeForHTML(WideToUTF8(text)), url.spec()); + return; + } + } + + scw.WriteText(text16.substr(selection.location, selection.length)); +} + void AutocompleteEditViewMac::OnPaste() { // This code currently expects |field_| to be focussed. DCHECK([field_ currentEditor]); diff --git a/chrome/browser/cocoa/autocomplete_text_field.h b/chrome/browser/cocoa/autocomplete_text_field.h index 0f29f1e..fa3668d 100644 --- a/chrome/browser/cocoa/autocomplete_text_field.h +++ b/chrome/browser/cocoa/autocomplete_text_field.h @@ -39,6 +39,9 @@ class AutocompleteTextFieldObserver { // Called when the user pastes into the field. virtual void OnPaste() = 0; + // Called when the user copies the text. + virtual void OnCopy() = 0; + // Returns true if the current clipboard text supports paste and go // (or paste and search). virtual bool CanPasteAndGo() = 0; diff --git a/chrome/browser/cocoa/autocomplete_text_field_editor.h b/chrome/browser/cocoa/autocomplete_text_field_editor.h index b7f0457..d7a5afe7 100644 --- a/chrome/browser/cocoa/autocomplete_text_field_editor.h +++ b/chrome/browser/cocoa/autocomplete_text_field_editor.h @@ -24,14 +24,6 @@ class AutocompleteTextFieldObserver; // start-up time regression. scoped_nsobject<URLDropTargetHandler> dropHandler_; } - -// Copy contents of the TextView to the designated clipboard as plain -// text. -- (void)performCopy:(NSPasteboard*)pb; - -// Same as above, note that this calls through to performCopy. -- (void)performCut:(NSPasteboard*)pb; - @end @interface AutocompleteTextFieldEditor(PrivateTestMethods) diff --git a/chrome/browser/cocoa/autocomplete_text_field_editor.mm b/chrome/browser/cocoa/autocomplete_text_field_editor.mm index 0973418..96bbfa8 100644 --- a/chrome/browser/cocoa/autocomplete_text_field_editor.mm +++ b/chrome/browser/cocoa/autocomplete_text_field_editor.mm @@ -34,23 +34,15 @@ class Extension; } - (void)copy:(id)sender { - NSPasteboard* pb = [NSPasteboard generalPasteboard]; - [self performCopy:pb]; + AutocompleteTextFieldObserver* observer = [self observer]; + DCHECK(observer); + if (observer) { + observer->OnCopy(); + } } - (void)cut:(id)sender { - NSPasteboard* pb = [NSPasteboard generalPasteboard]; - [self performCut:pb]; -} - -- (void)performCopy:(NSPasteboard*)pb { - [pb declareTypes:[NSArray array] owner:nil]; - [self writeSelectionToPasteboard:pb types: - [NSArray arrayWithObject:NSStringPboardType]]; -} - -- (void)performCut:(NSPasteboard*)pb { - [self performCopy:pb]; + [self copy:sender]; [self delete:nil]; } diff --git a/chrome/browser/cocoa/autocomplete_text_field_editor_unittest.mm b/chrome/browser/cocoa/autocomplete_text_field_editor_unittest.mm index 27545bb..40a1838 100644 --- a/chrome/browser/cocoa/autocomplete_text_field_editor_unittest.mm +++ b/chrome/browser/cocoa/autocomplete_text_field_editor_unittest.mm @@ -27,26 +27,11 @@ void ClearPasteboard(NSPasteboard* pb) { [pb declareTypes:[NSArray array] owner:nil]; } -bool NoRichTextOnClipboard(NSPasteboard* pb) { - return ([pb dataForType:NSRTFPboardType] == nil) && - ([pb dataForType:NSRTFDPboardType] == nil) && - ([pb dataForType:NSHTMLPboardType] == nil); -} - -bool ClipboardContainsText(NSPasteboard* pb, NSString* cmp) { - NSString* clipboard_text = [pb stringForType:NSStringPboardType]; - return [clipboard_text isEqualToString:cmp]; -} - // TODO(shess): Very similar to AutocompleteTextFieldTest. Maybe // those can be shared. class AutocompleteTextFieldEditorTest : public CocoaTest { public: - AutocompleteTextFieldEditorTest() - : pb_([NSPasteboard pasteboardWithUniqueName]) { - } - virtual void SetUp() { CocoaTest::SetUp(); NSRect frame = NSMakeRect(0, 0, 50, 30); @@ -69,21 +54,9 @@ class AutocompleteTextFieldEditorTest : public CocoaTest { EXPECT_TRUE([editor_ isKindOfClass:[AutocompleteTextFieldEditor class]]); } - virtual ~AutocompleteTextFieldEditorTest() { - [pb_ releaseGlobally]; - } - - NSPasteboard *clipboard() { - DCHECK(pb_); - return pb_; - } - AutocompleteTextFieldEditor* editor_; AutocompleteTextField* field_; scoped_nsobject<AutocompleteTextFieldWindowTestDelegate> window_delegate_; - - private: - NSPasteboard* pb_; }; TEST_VIEW(AutocompleteTextFieldEditorTest, field_); @@ -117,36 +90,6 @@ TEST_F(AutocompleteTextFieldEditorTest, FirstResponder) { EXPECT_EQ([editor_ observer], [field_ observer]); } -TEST_F(AutocompleteTextFieldEditorTest, CutCopyTest) { - // Make sure pasteboard is empty before we start. - ASSERT_EQ(NumTypesOnPasteboard(clipboard()), 0); - - NSString* test_string_1 = @"astring"; - NSString* test_string_2 = @"another string"; - - [editor_ setRichText:YES]; - - // Put some text on the clipboard. - [editor_ setString:test_string_1]; - [editor_ selectAll:nil]; - [editor_ alignRight:nil]; // Add a rich text attribute. - ASSERT_TRUE(NoRichTextOnClipboard(clipboard())); - - // Check that copying it works and we only get plain text. - [editor_ performCopy:clipboard()]; - ASSERT_TRUE(NoRichTextOnClipboard(clipboard())); - ASSERT_TRUE(ClipboardContainsText(clipboard(), test_string_1)); - - // Check that cutting it works and we only get plain text. - [editor_ setString:test_string_2]; - [editor_ selectAll:nil]; - [editor_ alignLeft:nil]; // Add a rich text attribute. - [editor_ performCut:clipboard()]; - ASSERT_TRUE(NoRichTextOnClipboard(clipboard())); - ASSERT_TRUE(ClipboardContainsText(clipboard(), test_string_2)); - ASSERT_EQ([[editor_ textStorage] length], 0U); -} - // Test drawing, mostly to ensure nothing leaks or crashes. TEST_F(AutocompleteTextFieldEditorTest, Display) { [field_ display]; @@ -159,6 +102,30 @@ TEST_F(AutocompleteTextFieldEditorObserverTest, Paste) { [editor_ paste:nil]; } +// Test that -copy: is correctly delegated to the observer. +TEST_F(AutocompleteTextFieldEditorObserverTest, Copy) { + EXPECT_CALL(field_observer_, OnCopy()); + [editor_ copy:nil]; +} + +// Test that -cut: is correctly delegated to the observer and clears +// the text field. +TEST_F(AutocompleteTextFieldEditorObserverTest, Cut) { + // Sets a string in the field. + NSString* test_string = @"astring"; + EXPECT_CALL(field_observer_, OnDidBeginEditing()); + EXPECT_CALL(field_observer_, OnDidChange()); + [editor_ setString:test_string]; + [editor_ selectAll:nil]; + + // Calls cut. + EXPECT_CALL(field_observer_, OnCopy()); + [editor_ cut:nil]; + + // Check if the field is cleared. + ASSERT_EQ([[editor_ textStorage] length], 0U); +} + // Test that -pasteAndGo: is correctly delegated to the observer. TEST_F(AutocompleteTextFieldEditorObserverTest, PasteAndGo) { EXPECT_CALL(field_observer_, OnPasteAndGo()); diff --git a/chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h b/chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h index b055086..4997171 100644 --- a/chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h +++ b/chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h @@ -35,6 +35,7 @@ namespace { class MockAutocompleteTextFieldObserver : public AutocompleteTextFieldObserver { public: MOCK_METHOD1(OnControlKeyChanged, void(bool pressed)); + MOCK_METHOD0(OnCopy, void()); MOCK_METHOD0(OnPaste, void()); MOCK_METHOD0(CanPasteAndGo, bool()); MOCK_METHOD0(GetPasteActionStringId, int()); |