Bug 1312936 - Part 2. Use nsAutoCString for nsITranserable.getAnyTransferData into editor. r?masayuki
Also, replace nsCRT::strcmp with EqualsLiteral. nsCRT::strcmp is deprecated API.
MozReview-Commit-ID: KZ4xfk7bhH3
--- a/editor/libeditor/HTMLEditor.h
+++ b/editor/libeditor/HTMLEditor.h
@@ -643,17 +643,18 @@ protected:
* have already been added.)
* @return aNodeInserted The node spanning the insertion, if applicable.
* If aAddCites is false, this will be null.
*/
NS_IMETHOD InsertAsPlaintextQuotation(const nsAString& aQuotedText,
bool aAddCites,
nsIDOMNode** aNodeInserted);
- nsresult InsertObject(const char* aType, nsISupports* aObject, bool aIsSafe,
+ nsresult InsertObject(const nsACString& aType, nsISupports* aObject,
+ bool aIsSafe,
nsIDOMDocument* aSourceDoc,
nsIDOMNode* aDestinationNode,
int32_t aDestOffset,
bool aDoDeleteSelection);
// factored methods for handling insertion of data from transferables
// (drag&drop or clipboard)
NS_IMETHOD PrepareTransferable(nsITransferable** transferable) override;
--- a/editor/libeditor/HTMLEditorDataTransfer.cpp
+++ b/editor/libeditor/HTMLEditorDataTransfer.cpp
@@ -19,18 +19,17 @@
#include "mozilla/Base64.h"
#include "mozilla/BasicEvents.h"
#include "mozilla/EditorUtils.h"
#include "mozilla/OwningNonNull.h"
#include "mozilla/Preferences.h"
#include "mozilla/SelectionState.h"
#include "nsAString.h"
#include "nsCOMPtr.h"
-#include "nsCRT.h"
-#include "nsCRTGlue.h"
+#include "nsCRTGlue.h" // for CRLF
#include "nsComponentManagerUtils.h"
#include "nsIScriptError.h"
#include "nsContentUtils.h"
#include "nsDebug.h"
#include "nsDependentSubstring.h"
#include "nsError.h"
#include "nsGkAtoms.h"
#include "nsIClipboard.h"
@@ -1053,17 +1052,17 @@ HTMLEditor::BlobReader::OnError(const ns
destNode->OwnerDoc(),
nsContentUtils::eDOM_PROPERTIES,
"EditorFileDropFailed",
&error, 1);
return NS_OK;
}
nsresult
-HTMLEditor::InsertObject(const char* aType,
+HTMLEditor::InsertObject(const nsACString& aType,
nsISupports* aObject,
bool aIsSafe,
nsIDOMDocument* aSourceDoc,
nsIDOMNode* aDestinationNode,
int32_t aDestOffset,
bool aDoDeleteSelection)
{
nsresult rv;
@@ -1145,37 +1144,37 @@ HTMLEditor::InsertFromTransferable(nsITr
const nsAString& aContextStr,
const nsAString& aInfoStr,
bool havePrivateHTMLFlavor,
nsIDOMNode* aDestinationNode,
int32_t aDestOffset,
bool aDoDeleteSelection)
{
nsresult rv = NS_OK;
- nsXPIDLCString bestFlavor;
+ nsAutoCString bestFlavor;
nsCOMPtr<nsISupports> genericDataObj;
uint32_t len = 0;
if (NS_SUCCEEDED(
- transferable->GetAnyTransferData(getter_Copies(bestFlavor),
+ transferable->GetAnyTransferData(bestFlavor,
getter_AddRefs(genericDataObj),
&len))) {
AutoTransactionsConserveSelection dontSpazMySelection(this);
nsAutoString flavor;
flavor.AssignWithConversion(bestFlavor);
nsAutoString stuffToPaste;
bool isSafe = IsSafeToInsertData(aSourceDoc);
- if (!nsCRT::strcmp(bestFlavor, kFileMime) ||
- !nsCRT::strcmp(bestFlavor, kJPEGImageMime) ||
- !nsCRT::strcmp(bestFlavor, kJPGImageMime) ||
- !nsCRT::strcmp(bestFlavor, kPNGImageMime) ||
- !nsCRT::strcmp(bestFlavor, kGIFImageMime)) {
+ if (bestFlavor.EqualsLiteral(kFileMime) ||
+ bestFlavor.EqualsLiteral(kJPEGImageMime) ||
+ bestFlavor.EqualsLiteral(kJPGImageMime) ||
+ bestFlavor.EqualsLiteral(kPNGImageMime) ||
+ bestFlavor.EqualsLiteral(kGIFImageMime)) {
rv = InsertObject(bestFlavor, genericDataObj, isSafe,
aSourceDoc, aDestinationNode, aDestOffset, aDoDeleteSelection);
- } else if (!nsCRT::strcmp(bestFlavor, kNativeHTMLMime)) {
+ } else if (bestFlavor.EqualsLiteral(kNativeHTMLMime)) {
// note cf_html uses utf8, hence use length = len, not len/2 as in flavors below
nsCOMPtr<nsISupportsCString> textDataObj = do_QueryInterface(genericDataObj);
if (textDataObj && len > 0) {
nsAutoCString cfhtml;
textDataObj->GetData(cfhtml);
NS_ASSERTION(cfhtml.Length() <= (len), "Invalid length!");
nsXPIDLString cfcontext, cffragment, cfselection; // cfselection left emtpy for now
@@ -1206,19 +1205,19 @@ HTMLEditor::InsertFromTransferable(nsITr
// application/x-moz-nativehtml). In this case, treat the data
// to be pasted as mere HTML to get the best chance of pasting it
// correctly.
bestFlavor.AssignLiteral(kHTMLMime);
// Fall through the next case
}
}
}
- if (!nsCRT::strcmp(bestFlavor, kHTMLMime) ||
- !nsCRT::strcmp(bestFlavor, kUnicodeMime) ||
- !nsCRT::strcmp(bestFlavor, kMozTextInternal)) {
+ if (bestFlavor.EqualsLiteral(kHTMLMime) ||
+ bestFlavor.EqualsLiteral(kUnicodeMime) ||
+ bestFlavor.EqualsLiteral(kMozTextInternal)) {
nsCOMPtr<nsISupportsString> textDataObj = do_QueryInterface(genericDataObj);
if (textDataObj && len > 0) {
nsAutoString text;
textDataObj->GetData(text);
NS_ASSERTION(text.Length() <= (len/2), "Invalid length!");
stuffToPaste.Assign(text.get(), len / 2);
} else {
nsCOMPtr<nsISupportsCString> textDataObj(do_QueryInterface(genericDataObj));
@@ -1227,17 +1226,17 @@ HTMLEditor::InsertFromTransferable(nsITr
textDataObj->GetData(text);
NS_ASSERTION(text.Length() <= len, "Invalid length!");
stuffToPaste.Assign(NS_ConvertUTF8toUTF16(Substring(text, 0, len)));
}
}
if (!stuffToPaste.IsEmpty()) {
AutoEditBatch beginBatching(this);
- if (!nsCRT::strcmp(bestFlavor, kHTMLMime)) {
+ if (bestFlavor.EqualsLiteral(kHTMLMime)) {
rv = DoInsertHTMLWithContext(stuffToPaste,
aContextStr, aInfoStr, flavor,
aSourceDoc,
aDestinationNode, aDestOffset,
aDoDeleteSelection,
isSafe);
} else {
rv = InsertTextAt(stuffToPaste, aDestinationNode, aDestOffset, aDoDeleteSelection);
@@ -1296,17 +1295,17 @@ HTMLEditor::InsertFromDataTransfer(DataT
type.EqualsLiteral(kJPGImageMime) ||
type.EqualsLiteral(kPNGImageMime) ||
type.EqualsLiteral(kGIFImageMime)) {
nsCOMPtr<nsIVariant> variant;
DataTransfer::Cast(aDataTransfer)->GetDataAtNoSecurityCheck(type, aIndex, getter_AddRefs(variant));
if (variant) {
nsCOMPtr<nsISupports> object;
variant->GetAsISupports(getter_AddRefs(object));
- return InsertObject(NS_ConvertUTF16toUTF8(type).get(), object, isSafe,
+ return InsertObject(NS_ConvertUTF16toUTF8(type), object, isSafe,
aSourceDoc, aDestinationNode, aDestOffset, aDoDeleteSelection);
}
} else if (type.EqualsLiteral(kNativeHTMLMime)) {
// Windows only clipboard parsing.
nsAutoString text;
GetStringFromDataTransfer(aDataTransfer, type, aIndex, text);
NS_ConvertUTF16toUTF8 cfhtml(text);
@@ -1692,31 +1691,30 @@ HTMLEditor::PasteAsPlaintextQuotation(in
// Get the Data from the clipboard
clipboard->GetData(trans, aSelectionType);
// Now we ask the transferable for the data
// it still owns the data, we just have a pointer to it.
// If it can't support a "text" output of the data the call will fail
nsCOMPtr<nsISupports> genericDataObj;
uint32_t len = 0;
- char* flav = 0;
- rv = trans->GetAnyTransferData(&flav, getter_AddRefs(genericDataObj), &len);
+ nsAutoCString flav;
+ rv = trans->GetAnyTransferData(flav, getter_AddRefs(genericDataObj), &len);
NS_ENSURE_SUCCESS(rv, rv);
- if (flav && !nsCRT::strcmp(flav, kUnicodeMime)) {
+ if (flav.EqualsLiteral(kUnicodeMime)) {
nsCOMPtr<nsISupportsString> textDataObj = do_QueryInterface(genericDataObj);
if (textDataObj && len > 0) {
nsAutoString stuffToPaste;
textDataObj->GetData(stuffToPaste);
NS_ASSERTION(stuffToPaste.Length() <= (len/2), "Invalid length!");
AutoEditBatch beginBatching(this);
rv = InsertAsPlaintextQuotation(stuffToPaste, true, 0);
}
}
- free(flav);
return rv;
}
NS_IMETHODIMP
HTMLEditor::InsertTextWithQuotations(const nsAString& aStringToInsert)
{
// The whole operation should be undoable in one transaction:
--- a/editor/libeditor/TextEditor.cpp
+++ b/editor/libeditor/TextEditor.cpp
@@ -1365,34 +1365,33 @@ TextEditor::PasteAsQuotation(int32_t aSe
// Get the Data from the clipboard
clipboard->GetData(trans, aSelectionType);
// Now we ask the transferable for the data
// it still owns the data, we just have a pointer to it.
// If it can't support a "text" output of the data the call will fail
nsCOMPtr<nsISupports> genericDataObj;
uint32_t len;
- char* flav = nullptr;
- rv = trans->GetAnyTransferData(&flav, getter_AddRefs(genericDataObj),
+ nsAutoCString flav;
+ rv = trans->GetAnyTransferData(flav, getter_AddRefs(genericDataObj),
&len);
- if (NS_FAILED(rv) || !flav) {
+ if (NS_FAILED(rv)) {
return rv;
}
- if (!nsCRT::strcmp(flav, kUnicodeMime) ||
- !nsCRT::strcmp(flav, kMozTextInternal)) {
+ if (flav.EqualsLiteral(kUnicodeMime) ||
+ flav.EqualsLiteral(kMozTextInternal)) {
nsCOMPtr<nsISupportsString> textDataObj ( do_QueryInterface(genericDataObj) );
if (textDataObj && len > 0) {
nsAutoString stuffToPaste;
textDataObj->GetData ( stuffToPaste );
AutoEditBatch beginBatching(this);
rv = InsertAsQuotation(stuffToPaste, 0);
}
}
- free(flav);
}
return rv;
}
NS_IMETHODIMP
TextEditor::InsertAsQuotation(const nsAString& aQuotedText,
nsIDOMNode** aNodeInserted)
--- a/editor/libeditor/TextEditorDataTransfer.cpp
+++ b/editor/libeditor/TextEditorDataTransfer.cpp
@@ -7,17 +7,16 @@
#include "mozilla/ArrayUtils.h"
#include "mozilla/EditorUtils.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/SelectionState.h"
#include "mozilla/dom/Selection.h"
#include "nsAString.h"
#include "nsCOMPtr.h"
-#include "nsCRT.h"
#include "nsComponentManagerUtils.h"
#include "nsContentUtils.h"
#include "nsDebug.h"
#include "nsError.h"
#include "nsIClipboard.h"
#include "nsIContent.h"
#include "nsIDOMDataTransfer.h"
#include "nsIDOMDocument.h"
@@ -101,40 +100,39 @@ TextEditor::InsertTextAt(const nsAString
NS_IMETHODIMP
TextEditor::InsertTextFromTransferable(nsITransferable* aTransferable,
nsIDOMNode* aDestinationNode,
int32_t aDestOffset,
bool aDoDeleteSelection)
{
nsresult rv = NS_OK;
- char* bestFlavor = nullptr;
+ nsAutoCString bestFlavor;
nsCOMPtr<nsISupports> genericDataObj;
uint32_t len = 0;
if (NS_SUCCEEDED(
- aTransferable->GetAnyTransferData(&bestFlavor,
+ aTransferable->GetAnyTransferData(bestFlavor,
getter_AddRefs(genericDataObj),
&len)) &&
- bestFlavor && (!nsCRT::strcmp(bestFlavor, kUnicodeMime) ||
- !nsCRT::strcmp(bestFlavor, kMozTextInternal))) {
+ (bestFlavor.EqualsLiteral(kUnicodeMime) ||
+ bestFlavor.EqualsLiteral(kMozTextInternal))) {
AutoTransactionsConserveSelection dontSpazMySelection(this);
nsCOMPtr<nsISupportsString> textDataObj ( do_QueryInterface(genericDataObj) );
if (textDataObj && len > 0) {
nsAutoString stuffToPaste;
textDataObj->GetData(stuffToPaste);
NS_ASSERTION(stuffToPaste.Length() <= (len/2), "Invalid length!");
// Sanitize possible carriage returns in the string to be inserted
nsContentUtils::PlatformToDOMLineBreaks(stuffToPaste);
AutoEditBatch beginBatching(this);
rv = InsertTextAt(stuffToPaste, aDestinationNode, aDestOffset, aDoDeleteSelection);
}
}
- free(bestFlavor);
// Try to scroll the selection into view if the paste/drop succeeded
if (NS_SUCCEEDED(rv)) {
ScrollSelectionIntoView(false);
}
return rv;