/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
nsAutoString stuffToPaste; if (nsCOMPtr<nsISupportsString> text = do_QueryInterface(genericDataObj)) {
text->GetData(stuffToPaste);
}
MOZ_ASSERT(GetEditAction() == EditAction::ePaste); // Use native line breaks for compatibility with Chrome. // XXX Although, somebody has already converted native line breaks to // XP line breaks.
UpdateEditActionData(stuffToPaste);
if (!stuffToPaste.IsEmpty()) { // Sanitize possible carriage returns in the string to be inserted
nsContentUtils::PlatformToDOMLineBreaks(stuffToPaste);
// Try to scroll the selection into view if the paste/drop succeeded
rv = ScrollSelectionFocusIntoView();
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "EditorBase::ScrollSelectionFocusIntoView() failed"); return rv;
}
uint32_t numItems = aDataTransfer.MozItemCount();
AutoTArray<nsString, 5> textArray;
textArray.SetCapacity(numItems);
uint32_t textLength = 0; for (uint32_t i = 0; i < numItems; ++i) {
nsCOMPtr<nsIVariant> data;
aDataTransfer.GetDataAtNoSecurityCheck(u"text/plain"_ns, i,
getter_AddRefs(data)); if (!data) { continue;
} // Use nsString to avoid copying its storage to textArray.
nsString insertText;
data->GetAsAString(insertText); if (insertText.IsEmpty()) { continue;
}
textArray.AppendElement(insertText);
textLength += insertText.Length();
} // Use nsString to avoid copying its storage to aEditActionData.
nsString data;
data.SetCapacity(textLength); // Join the text array from end to start because we insert each items // in the aDataTransfer at same point from start to end. Although I // don't know whether this is intentional behavior. for (nsString& text : Reversed(textArray)) {
data.Append(text);
} // Use native line breaks for compatibility with Chrome. // XXX Although, somebody has already converted native line breaks to // XP line breaks.
aEditActionData.SetData(data);
// Then, insert the text. Note that we shouldn't need to walk the array // anymore because nobody should listen to mutation events of anonymous // text node in <input>/<textarea>.
nsContentUtils::PlatformToDOMLineBreaks(data);
rv = InsertTextAt(data, aDroppedAt, DeleteSelectedContent::No); if (NS_WARN_IF(Destroyed())) { return NS_ERROR_EDITOR_DESTROYED;
}
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "EditorBase::InsertTextAt(DeleteSelectedContent::No) " "failed, but ignored"); return rv;
}
// The data will be initialized in InsertTextFromTransferable() if we're not // an HTMLEditor. Therefore, we cannot dispatch "beforeinput" here.
// Get Clipboard Service
nsresult rv;
nsCOMPtr<nsIClipboard> clipboard =
do_GetService("@mozilla.org/widget/clipboard;1", &rv); if (NS_FAILED(rv)) {
NS_WARNING("Failed to get nsIClipboard service"); return rv;
}
// Get the nsITransferable interface for getting the data from the clipboard
Result<nsCOMPtr<nsITransferable>, nsresult> maybeTransferable =
EditorUtils::CreateTransferableForPlainText(*GetDocument()); if (maybeTransferable.isErr()) {
NS_WARNING("EditorUtils::CreateTransferableForPlainText() failed"); return maybeTransferable.unwrapErr();
}
nsCOMPtr<nsITransferable> transferable(maybeTransferable.unwrap()); if (NS_WARN_IF(!transferable)) {
NS_WARNING( "EditorUtils::CreateTransferableForPlainText() returned nullptr, but " "ignored"); return NS_OK; // XXX Why?
} // Get the Data from the clipboard.
rv = GetDataFromDataTransferOrClipboard(aDataTransfer, transferable,
aClipboardType); if (NS_FAILED(rv)) {
NS_WARNING("EditorBase::GetDataFromDataTransferOrClipboard() failed"); return rv;
}
// XXX Why don't we check this first? if (!IsModifiable()) { return NS_OK;
}
rv = InsertTextFromTransferable(transferable);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "TextEditor::InsertTextFromTransferable() failed"); return rv;
}
// FYI: The data of beforeinput will be initialized in // InsertTextFromTransferable(). Therefore, here does not touch // aEditActionData.
nsresult rv = InsertTextFromTransferable(&aTransferable);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "TextEditor::InsertTextFromTransferable() failed"); return rv;
}
bool TextEditor::CanPaste(nsIClipboard::ClipboardType aClipboardType) const { if (AreClipboardCommandsUnconditionallyEnabled()) { returntrue;
}
// can't paste if readonly if (!IsModifiable()) { returnfalse;
}
nsresult rv;
nsCOMPtr<nsIClipboard> clipboard(
do_GetService("@mozilla.org/widget/clipboard;1", &rv)); if (NS_FAILED(rv)) {
NS_WARNING("Failed to get nsIClipboard service"); returnfalse;
}
// the flavors that we can deal with
AutoTArray<nsCString, 1> textEditorFlavors = {nsDependentCString(kTextMime)};
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.