From 5da560a3b6fa77574be12ff1dfc9f3c8768a56ea Mon Sep 17 00:00:00 2001 From: Jonathan Kew Date: Thu, 1 Jan 2009 15:26:16 +0100 Subject: [PATCH] Bug 466599 - wrap HTML on system pasteboard to provide charset metadata, fixes paste/drag into rich-text NSTextView destinations; r=(joshmoz + roc) sr=roc --- widget/src/cocoa/nsClipboard.h | 1 + widget/src/cocoa/nsClipboard.mm | 26 +++++++- widget/src/cocoa/nsDragService.mm | 5 +- widget/tests/Makefile.in | 4 +- widget/tests/test_bug466599.xul | 102 ++++++++++++++++++++++++++++++ 5 files changed, 133 insertions(+), 5 deletions(-) create mode 100644 widget/tests/test_bug466599.xul diff --git a/widget/src/cocoa/nsClipboard.h b/widget/src/cocoa/nsClipboard.h index 411025436df..17841fc11d6 100644 --- a/widget/src/cocoa/nsClipboard.h +++ b/widget/src/cocoa/nsClipboard.h @@ -61,6 +61,7 @@ public: // Helper methods, used also by nsDragService static NSDictionary* PasteboardDictFromTransferable(nsITransferable *aTransferable); static PRBool IsStringType(const nsCString& aMIMEType, const NSString** aPasteboardType); + static NSString* WrapHtmlForSystemPasteboard(NSString* aString); protected: diff --git a/widget/src/cocoa/nsClipboard.mm b/widget/src/cocoa/nsClipboard.mm index b800fa6ef8d..1d7e8307b57 100644 --- a/widget/src/cocoa/nsClipboard.mm +++ b/widget/src/cocoa/nsClipboard.mm @@ -115,13 +115,16 @@ nsClipboard::SetNativeClipboardData(PRInt32 aWhichClipboard) NSString* currentKey = [outputKeys objectAtIndex:i]; id currentValue = [pasteboardOutputDict valueForKey:currentKey]; if (currentKey == NSStringPboardType || - currentKey == NSHTMLPboardType || currentKey == kCorePboardType_url || currentKey == kCorePboardType_urld || - currentKey == kCorePboardType_urln) + currentKey == kCorePboardType_urln) { [generalPBoard setString:currentValue forType:currentKey]; - else + } else if (currentKey == NSHTMLPboardType) { + [generalPBoard setString:(nsClipboard::WrapHtmlForSystemPasteboard(currentValue)) + forType:currentKey]; + } else { [generalPBoard setData:currentValue forType:currentKey]; + } } mChangeCount = [generalPBoard changeCount]; @@ -539,6 +542,7 @@ nsClipboard::PasteboardDictFromTransferable(nsITransferable* aTransferable) NS_OBJC_END_TRY_ABORT_BLOCK_NIL; } + PRBool nsClipboard::IsStringType(const nsCString& aMIMEType, const NSString** aPasteboardType) { if (aMIMEType.EqualsLiteral(kUnicodeMime) || @@ -552,3 +556,19 @@ PRBool nsClipboard::IsStringType(const nsCString& aMIMEType, const NSString** aP return PR_FALSE; } } + + +NSString* nsClipboard::WrapHtmlForSystemPasteboard(NSString* aString) +{ + NSString* wrapped = + [NSString stringWithFormat: + @"" + "" + "" + "" + "" + "%@" + "" + "", aString]; + return wrapped; +} diff --git a/widget/src/cocoa/nsDragService.mm b/widget/src/cocoa/nsDragService.mm index f28dabc8f14..dd4734bf782 100644 --- a/widget/src/cocoa/nsDragService.mm +++ b/widget/src/cocoa/nsDragService.mm @@ -138,12 +138,15 @@ static nsresult SetUpDragClipboard(nsISupportsArray* aTransferableArray) NSString* currentKey = [types objectAtIndex:i]; id currentValue = [pasteboardOutputDict valueForKey:currentKey]; if (currentKey == NSStringPboardType || - currentKey == NSHTMLPboardType || currentKey == kCorePboardType_url || currentKey == kCorePboardType_urld || currentKey == kCorePboardType_urln) { [dragPBoard setString:currentValue forType:currentKey]; } + else if (currentKey == NSHTMLPboardType) { + [dragPBoard setString:(nsClipboard::WrapHtmlForSystemPasteboard(currentValue)) + forType:currentKey]; + } else if (currentKey == NSTIFFPboardType) { [dragPBoard setData:currentValue forType:currentKey]; } diff --git a/widget/tests/Makefile.in b/widget/tests/Makefile.in index 17340aaec26..9b2643edf3e 100644 --- a/widget/tests/Makefile.in +++ b/widget/tests/Makefile.in @@ -52,7 +52,9 @@ _TEST_FILES = test_bug343416.xul \ ifeq ($(MOZ_WIDGET_TOOLKIT),cocoa) _TEST_FILES += native_menus_window.xul \ test_native_menus.xul \ - test_bug428405.xul + test_bug428405.xul \ + test_bug466599.xul \ + $(NULL) endif libs:: $(_TEST_FILES) diff --git a/widget/tests/test_bug466599.xul b/widget/tests/test_bug466599.xul new file mode 100644 index 00000000000..d50350269a5 --- /dev/null +++ b/widget/tests/test_bug466599.xul @@ -0,0 +1,102 @@ + + + + + + +