/* -*- 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/. */ #include "mozilla/TextEditor.h" #include "mozilla/ArrayUtils.h" #include "mozilla/MouseEvents.h" #include "mozilla/SelectionState.h" #include "mozilla/dom/DataTransfer.h" #include "mozilla/dom/DragEvent.h" #include "mozilla/dom/Selection.h" #include "nsAString.h" #include "nsCOMPtr.h" #include "nsComponentManagerUtils.h" #include "nsContentUtils.h" #include "nsDebug.h" #include "nsError.h" #include "nsIClipboard.h" #include "nsIContent.h" #include "mozilla/dom/Document.h" #include "nsIDragService.h" #include "nsIDragSession.h" #include "nsIDocShell.h" #include "nsIDocShellTreeItem.h" #include "nsIPrincipal.h" #include "nsIFormControl.h" #include "nsISupportsPrimitives.h" #include "nsITransferable.h" #include "nsIVariant.h" #include "nsLiteralString.h" #include "nsRange.h" #include "nsServiceManagerUtils.h" #include "nsString.h" #include "nsXPCOM.h" #include "nscore.h" class nsILoadContext; class nsISupports; namespace mozilla { using namespace dom; nsresult TextEditor::PrepareTransferable(nsITransferable** transferable) { // Create generic Transferable for getting the data nsresult rv = CallCreateInstance("@mozilla.org/widget/transferable;1", transferable); NS_ENSURE_SUCCESS(rv, rv); // Get the nsITransferable interface for getting the data from the clipboard if (transferable) { RefPtr destdoc = GetDocument(); nsILoadContext* loadContext = destdoc ? destdoc->GetLoadContext() : nullptr; (*transferable)->Init(loadContext); (*transferable)->AddDataFlavor(kUnicodeMime); (*transferable)->AddDataFlavor(kMozTextInternal); }; return NS_OK; } nsresult TextEditor::PrepareToInsertContent( const EditorDOMPoint& aPointToInsert, bool aDoDeleteSelection) { MOZ_ASSERT(IsEditActionDataAvailable()); MOZ_ASSERT(aPointToInsert.IsSet()); EditorDOMPoint pointToInsert(aPointToInsert); if (aDoDeleteSelection) { AutoTrackDOMPoint tracker(RangeUpdaterRef(), &pointToInsert); nsresult rv = DeleteSelectionAsSubAction(eNone, eStrip); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } } ErrorResult error; SelectionRefPtr()->Collapse(pointToInsert, error); if (NS_WARN_IF(Destroyed())) { return NS_ERROR_EDITOR_DESTROYED; } if (NS_WARN_IF(error.Failed())) { return error.StealNSResult(); } return NS_OK; } nsresult TextEditor::InsertTextAt(const nsAString& aStringToInsert, const EditorDOMPoint& aPointToInsert, bool aDoDeleteSelection) { MOZ_ASSERT(IsEditActionDataAvailable()); MOZ_ASSERT(aPointToInsert.IsSet()); nsresult rv = PrepareToInsertContent(aPointToInsert, aDoDeleteSelection); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } rv = InsertTextAsSubAction(aStringToInsert); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } return NS_OK; } nsresult TextEditor::InsertTextFromTransferable( nsITransferable* aTransferable) { MOZ_ASSERT(IsEditActionDataAvailable()); MOZ_ASSERT(!AsHTMLEditor()); nsAutoCString bestFlavor; nsCOMPtr genericDataObj; if (NS_SUCCEEDED(aTransferable->GetAnyTransferData( bestFlavor, getter_AddRefs(genericDataObj))) && (bestFlavor.EqualsLiteral(kUnicodeMime) || bestFlavor.EqualsLiteral(kMozTextInternal))) { AutoTransactionsConserveSelection dontChangeMySelection(*this); nsAutoString stuffToPaste; if (nsCOMPtr 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); AutoPlaceholderBatch treatAsOneTransaction(*this); nsresult rv = InsertTextAsSubAction(stuffToPaste); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } } } // Try to scroll the selection into view if the paste/drop succeeded nsresult rv = ScrollSelectionFocusIntoView(); NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "ScrollSelectionFocusIntoView() failed"); return rv; } nsresult TextEditor::OnDrop(DragEvent* aDropEvent) { if (NS_WARN_IF(!aDropEvent)) { return NS_ERROR_INVALID_ARG; } CommitComposition(); AutoEditActionDataSetter editActionData(*this, EditAction::eDrop); if (NS_WARN_IF(!editActionData.CanHandle())) { return NS_ERROR_NOT_INITIALIZED; } RefPtr dataTransfer = aDropEvent->GetDataTransfer(); if (NS_WARN_IF(!dataTransfer)) { return NS_ERROR_FAILURE; } nsCOMPtr dragSession = nsContentUtils::GetDragSession(); if (NS_WARN_IF(!dragSession)) { return NS_ERROR_FAILURE; } nsCOMPtr sourceNode = dataTransfer->GetMozSourceNode(); RefPtr srcdoc; if (sourceNode) { srcdoc = sourceNode->OwnerDoc(); } if (nsContentUtils::CheckForSubFrameDrop( dragSession, aDropEvent->WidgetEventPtr()->AsDragEvent())) { // Don't allow drags from subframe documents with different origins than // the drop destination. if (srcdoc && !IsSafeToInsertData(srcdoc)) { return NS_OK; } } // Current doc is destination Document* destdoc = GetDocument(); if (NS_WARN_IF(!destdoc)) { return NS_ERROR_NOT_INITIALIZED; } uint32_t numItems = dataTransfer->MozItemCount(); if (NS_WARN_IF(!numItems)) { return NS_ERROR_FAILURE; // Nothing to drop? } // We have to figure out whether to delete and relocate caret only once // Parent and offset are under the mouse cursor. EditorDOMPoint droppedAt(aDropEvent->GetRangeParent(), aDropEvent->RangeOffset()); if (NS_WARN_IF(!droppedAt.IsSet())) { return NS_ERROR_FAILURE; } // Check if dropping into a selected range. If so and the source comes from // same document, jump through some hoops to determine if mouse is over // selection (bail) and whether user wants to copy selection or delete it. bool deleteSelection = false; if (!SelectionRefPtr()->IsCollapsed() && srcdoc == destdoc) { uint32_t rangeCount = SelectionRefPtr()->RangeCount(); for (uint32_t j = 0; j < rangeCount; j++) { nsRange* range = SelectionRefPtr()->GetRangeAt(j); if (NS_WARN_IF(!range)) { // don't bail yet, iterate through them all continue; } IgnoredErrorResult errorIgnored; if (range->IsPointInRange(*droppedAt.GetContainer(), droppedAt.Offset(), errorIgnored) && !errorIgnored.Failed()) { // If source document and destination document is same and dropping // into one of selected ranges, we don't need to do nothing. // XXX If the source comes from outside of this editor, this check // means that we don't allow to drop the item in the selected // range. However, the selection is hidden until the or //