Bug 938991 - text/rtf support for clipboard data. r=enndeakin

This commit is contained in:
Jorg K 2015-11-23 11:04:26 -08:00
parent ffb1c5dd5b
commit 88642f71dc
7 changed files with 40 additions and 13 deletions

View File

@ -1237,7 +1237,7 @@ DataTransfer::CacheExternalDragFormats()
// there isn't a way to get a list of the formats that might be available on
// all platforms, so just check for the types that can actually be imported
// XXXndeakin there are some other formats but those are platform specific.
const char* formats[] = { kFileMime, kHTMLMime, kURLMime, kURLDataMime, kUnicodeMime };
const char* formats[] = { kFileMime, kHTMLMime, kRTFMime, kURLMime, kURLDataMime, kUnicodeMime };
uint32_t count;
dragSession->GetNumDropItems(&count);
@ -1279,7 +1279,7 @@ DataTransfer::CacheExternalClipboardFormats()
// there isn't a way to get a list of the formats that might be available on
// all platforms, so just check for the types that can actually be imported
const char* formats[] = { kFileMime, kHTMLMime, kURLMime, kURLDataMime, kUnicodeMime };
const char* formats[] = { kFileMime, kHTMLMime, kRTFMime, kURLMime, kURLDataMime, kUnicodeMime };
for (uint32_t f = 0; f < mozilla::ArrayLength(formats); ++f) {
// check each format one at a time

View File

@ -153,7 +153,12 @@ nsClipboard::TransferableFromPasteboard(nsITransferable *aTransferable, NSPasteb
if (!pString)
continue;
NSData* stringData = [pString dataUsingEncoding:NSUnicodeStringEncoding];
NSData* stringData;
if ([pboardType isEqualToString:NSRTFPboardType]) {
stringData = [pString dataUsingEncoding:NSASCIIStringEncoding];
} else {
stringData = [pString dataUsingEncoding:NSUnicodeStringEncoding];
}
unsigned int dataLength = [stringData length];
void* clipboardDataPtr = malloc(dataLength);
if (!clipboardDataPtr)
@ -581,12 +586,14 @@ nsClipboard::PasteboardDictFromTransferable(nsITransferable* aTransferable)
bool nsClipboard::IsStringType(const nsCString& aMIMEType, NSString** aPasteboardType)
{
if (aMIMEType.EqualsLiteral(kUnicodeMime) ||
aMIMEType.EqualsLiteral(kHTMLMime)) {
if (aMIMEType.EqualsLiteral(kUnicodeMime))
*aPasteboardType = NSStringPboardType;
else
*aPasteboardType = NSHTMLPboardType;
if (aMIMEType.EqualsLiteral(kUnicodeMime)) {
*aPasteboardType = NSStringPboardType;
return true;
} else if (aMIMEType.EqualsLiteral(kRTFMime)) {
*aPasteboardType = NSRTFPboardType;
return true;
} else if (aMIMEType.EqualsLiteral(kHTMLMime)) {
*aPasteboardType = NSHTMLPboardType;
return true;
} else {
return false;

View File

@ -493,9 +493,16 @@ nsDragService::GetData(nsITransferable* aTransferable, uint32_t aItemIndex)
pString = GetStringForType(item, (const NSString*)kUTTypeURL);
} else if (flavorStr.EqualsLiteral(kURLDescriptionMime)) {
pString = GetTitleForURL(item);
} else if (flavorStr.EqualsLiteral(kRTFMime)) {
pString = GetStringForType(item, (const NSString*)kUTTypeRTF);
}
if (pString) {
NSData* stringData = [pString dataUsingEncoding:NSUnicodeStringEncoding];
NSData* stringData;
if (flavorStr.EqualsLiteral(kRTFMime)) {
stringData = [pString dataUsingEncoding:NSASCIIStringEncoding];
} else {
stringData = [pString dataUsingEncoding:NSUnicodeStringEncoding];
}
unsigned int dataLength = [stringData length];
void* clipboardDataPtr = malloc(dataLength);
if (!clipboardDataPtr)
@ -601,6 +608,8 @@ nsDragService::IsDataFlavorSupported(const char *aDataFlavor, bool *_retval)
type = (const NSString*)kUTTypeURL;
} else if (dataFlavor.EqualsLiteral(kURLDescriptionMime)) {
type = (const NSString*)kUTTypeURLName;
} else if (dataFlavor.EqualsLiteral(kRTFMime)) {
type = (const NSString*)kUTTypeRTF;
}
NSString* availableType = [globalDragPboard availableTypeFromArray:[NSArray arrayWithObjects:(id)type, nil]];
if (availableType && IsValidType(availableType, allowFileURL)) {

View File

@ -92,7 +92,8 @@ nsClipboardProxy::GetData(nsITransferable *aTransferable, int32_t aWhichClipboar
rv = aTransferable->SetTransferData(flavor.get(), stream, sizeof(nsISupports*));
NS_ENSURE_SUCCESS(rv, rv);
} else if (flavor.EqualsLiteral(kNativeHTMLMime)) {
} else if (flavor.EqualsLiteral(kNativeHTMLMime) ||
flavor.EqualsLiteral(kRTFMime)) {
nsCOMPtr<nsISupportsCString> dataWrapper =
do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);

View File

@ -15,6 +15,7 @@ interface nsIDOMNode;
// these probably shouldn't live here, but in some central repository shared
// by the entire app.
#define kTextMime "text/plain"
#define kRTFMime "text/rtf"
#define kUnicodeMime "text/unicode"
#define kMozTextInternal "text/x-moz-text-internal" // text data which isn't suppoed to be parsed by other apps.
#define kHTMLMime "text/html"

View File

@ -45,7 +45,8 @@ nsPrimitiveHelpers :: CreatePrimitiveForData ( const char* aFlavor, const void*
if ( !aPrimitive )
return;
if ( strcmp(aFlavor,kTextMime) == 0 || strcmp(aFlavor,kNativeHTMLMime) == 0 ) {
if ( strcmp(aFlavor,kTextMime) == 0 || strcmp(aFlavor,kNativeHTMLMime) == 0 ||
strcmp(aFlavor,kRTFMime) == 0) {
nsCOMPtr<nsISupportsCString> primitive =
do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID);
if ( primitive ) {
@ -170,7 +171,7 @@ nsLinebreakHelpers :: ConvertPlatformToDOMLinebreaks ( const char* inFlavor, voi
nsresult retVal = NS_OK;
if ( strcmp(inFlavor, "text/plain") == 0 ) {
if ( strcmp(inFlavor, kTextMime) == 0 || strcmp(inFlavor, kRTFMime) == 0) {
char* buffAsChars = reinterpret_cast<char*>(*ioData);
char* oldBuffer = buffAsChars;
retVal = nsLinebreakConverter::ConvertLineBreaksInSitu ( &buffAsChars, nsLinebreakConverter::eLinebreakAny,

View File

@ -95,6 +95,8 @@ UINT nsClipboard::GetFormat(const char* aMimeStr)
if (strcmp(aMimeStr, kTextMime) == 0 ||
strcmp(aMimeStr, kUnicodeMime) == 0)
format = CF_UNICODETEXT;
else if (strcmp(aMimeStr, kRTFMime) == 0)
format = ::RegisterClipboardFormat(L"Rich Text Format");
else if (strcmp(aMimeStr, kJPEGImageMime) == 0 ||
strcmp(aMimeStr, kJPGImageMime) == 0 ||
strcmp(aMimeStr, kPNGImageMime) == 0)
@ -670,6 +672,12 @@ nsresult nsClipboard::GetDataFromDataObject(IDataObject * aDataObject,
nsLinebreakHelpers::ConvertPlatformToDOMLinebreaks ( flavorStr, &data, &signedLen );
dataLen = signedLen;
if (strcmp(flavorStr, kRTFMime) == 0) {
// RTF on Windows is known to sometimes deliver an extra null byte.
if (dataLen > 0 && static_cast<char*>(data)[dataLen - 1] == '\0')
dataLen--;
}
nsPrimitiveHelpers::CreatePrimitiveForData ( flavorStr, data, dataLen, getter_AddRefs(genericDataWrapper) );
free(data);
}