/* -*- 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/TextEditRules.h" #include "HTMLEditRules.h" #include "TextEditUtils.h" #include "mozilla/Assertions.h" #include "mozilla/EditAction.h" #include "mozilla/EditorDOMPoint.h" #include "mozilla/EditorUtils.h" #include "mozilla/LookAndFeel.h" #include "mozilla/Preferences.h" #include "mozilla/TextComposition.h" #include "mozilla/TextEditor.h" #include "mozilla/dom/Element.h" #include "mozilla/dom/NodeFilterBinding.h" #include "mozilla/dom/NodeIterator.h" #include "mozilla/dom/Selection.h" #include "nsAString.h" #include "nsCOMPtr.h" #include "nsCRT.h" #include "nsCRTGlue.h" #include "nsComponentManagerUtils.h" #include "nsContentUtils.h" #include "nsDebug.h" #include "nsError.h" #include "nsGkAtoms.h" #include "nsIContent.h" #include "nsIDocumentEncoder.h" #include "nsNameSpaceManager.h" #include "nsINode.h" #include "nsIPlaintextEditor.h" #include "nsISupportsBase.h" #include "nsLiteralString.h" #include "nsTextNode.h" #include "nsUnicharUtils.h" #include "nsIHTMLCollection.h" #include "nsPrintfCString.h" namespace mozilla { using namespace dom; #define CANCEL_OPERATION_IF_READONLY_OR_DISABLED \ if (IsReadonly() || IsDisabled()) \ { \ *aCancel = true; \ return NS_OK; \ }; /******************************************************** * mozilla::TextEditRules ********************************************************/ TextEditRules::TextEditRules() : mTextEditor(nullptr) , mPasswordIMEIndex(0) , mCachedSelectionOffset(0) , mActionNesting(0) , mLockRulesSniffing(false) , mDidExplicitlySetInterline(false) , mDeleteBidiImmediately(false) , mIsHTMLEditRules(false) , mTheAction(EditAction::none) , mLastStart(0) , mLastLength(0) { InitFields(); } void TextEditRules::InitFields() { mTextEditor = nullptr; mPasswordText.Truncate(); mPasswordIMEText.Truncate(); mPasswordIMEIndex = 0; mBogusNode = nullptr; mCachedSelectionNode = nullptr; mCachedSelectionOffset = 0; mActionNesting = 0; mLockRulesSniffing = false; mDidExplicitlySetInterline = false; mDeleteBidiImmediately = false; mTheAction = EditAction::none; mTimer = nullptr; mLastStart = 0; mLastLength = 0; } TextEditRules::~TextEditRules() { // do NOT delete mTextEditor here. We do not hold a ref count to // mTextEditor. mTextEditor owns our lifespan. if (mTimer) { mTimer->Cancel(); } } HTMLEditRules* TextEditRules::AsHTMLEditRules() { return mIsHTMLEditRules ? static_cast(this) : nullptr; } const HTMLEditRules* TextEditRules::AsHTMLEditRules() const { return mIsHTMLEditRules ? static_cast(this) : nullptr; } NS_IMPL_CYCLE_COLLECTION(TextEditRules, mBogusNode, mCachedSelectionNode) NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TextEditRules) NS_INTERFACE_MAP_ENTRY(nsITimerCallback) NS_INTERFACE_MAP_ENTRY(nsINamed) NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsITimerCallback) NS_INTERFACE_MAP_END NS_IMPL_CYCLE_COLLECTING_ADDREF(TextEditRules) NS_IMPL_CYCLE_COLLECTING_RELEASE(TextEditRules) nsresult TextEditRules::Init(TextEditor* aTextEditor) { if (!aTextEditor) { return NS_ERROR_NULL_POINTER; } InitFields(); // We hold a non-refcounted reference back to our editor. mTextEditor = aTextEditor; RefPtr selection = mTextEditor->GetSelection(); NS_WARNING_ASSERTION(selection, "editor cannot get selection"); // Put in a magic br if needed. This method handles null selection, // which should never happen anyway nsresult rv = CreateBogusNodeIfNeeded(selection); NS_ENSURE_SUCCESS(rv, rv); // If the selection hasn't been set up yet, set it up collapsed to the end of // our editable content. if (!selection->RangeCount()) { rv = mTextEditor->CollapseSelectionToEnd(selection); NS_ENSURE_SUCCESS(rv, rv); } if (IsPlaintextEditor()) { // ensure trailing br node rv = CreateTrailingBRIfNeeded(); NS_ENSURE_SUCCESS(rv, rv); } mDeleteBidiImmediately = Preferences::GetBool("bidi.edit.delete_immediately", false); return NS_OK; } nsresult TextEditRules::SetInitialValue(const nsAString& aValue) { if (IsPasswordEditor()) { mPasswordText = aValue; } return NS_OK; } nsresult TextEditRules::DetachEditor() { if (mTimer) { mTimer->Cancel(); } mTextEditor = nullptr; return NS_OK; } nsresult TextEditRules::BeforeEdit(EditAction aAction, nsIEditor::EDirection aDirection) { if (mLockRulesSniffing) { return NS_OK; } AutoLockRulesSniffing lockIt(this); mDidExplicitlySetInterline = false; if (!mActionNesting) { // let rules remember the top level action mTheAction = aAction; } mActionNesting++; // get the selection and cache the position before editing if (NS_WARN_IF(!mTextEditor)) { return NS_ERROR_FAILURE; } RefPtr textEditor = mTextEditor; RefPtr selection = textEditor->GetSelection(); NS_ENSURE_STATE(selection); if (aAction == EditAction::setText) { // setText replaces all text, so mCachedSelectionNode might be invalid on // AfterEdit. // Since this will be used as start position of spellchecker, we should // use root instead. mCachedSelectionNode = textEditor->GetRoot(); mCachedSelectionOffset = 0; } else { mCachedSelectionNode = selection->GetAnchorNode(); mCachedSelectionOffset = selection->AnchorOffset(); } return NS_OK; } nsresult TextEditRules::AfterEdit(EditAction aAction, nsIEditor::EDirection aDirection) { if (mLockRulesSniffing) { return NS_OK; } AutoLockRulesSniffing lockIt(this); NS_PRECONDITION(mActionNesting>0, "bad action nesting!"); if (!--mActionNesting) { NS_ENSURE_STATE(mTextEditor); RefPtr selection = mTextEditor->GetSelection(); NS_ENSURE_STATE(selection); NS_ENSURE_STATE(mTextEditor); nsresult rv = mTextEditor->HandleInlineSpellCheck(aAction, selection, mCachedSelectionNode, mCachedSelectionOffset, nullptr, 0, nullptr, 0); NS_ENSURE_SUCCESS(rv, rv); // no longer uses mCachedSelectionNode, so release it. mCachedSelectionNode = nullptr; // if only trailing
remaining remove it rv = RemoveRedundantTrailingBR(); if (NS_FAILED(rv)) { return rv; } // detect empty doc rv = CreateBogusNodeIfNeeded(selection); NS_ENSURE_SUCCESS(rv, rv); // ensure trailing br node rv = CreateTrailingBRIfNeeded(); NS_ENSURE_SUCCESS(rv, rv); // collapse the selection to the trailing BR if it's at the end of our text node CollapseSelectionToTrailingBRIfNeeded(selection); } return NS_OK; } nsresult TextEditRules::WillDoAction(Selection* aSelection, RulesInfo* aInfo, bool* aCancel, bool* aHandled) { // null selection is legal MOZ_ASSERT(aInfo && aCancel && aHandled); *aCancel = false; *aHandled = false; // my kingdom for dynamic cast switch (aInfo->action) { case EditAction::insertBreak: UndefineCaretBidiLevel(aSelection); return WillInsertBreak(aSelection, aCancel, aHandled, aInfo->maxLength); case EditAction::insertText: case EditAction::insertIMEText: UndefineCaretBidiLevel(aSelection); return WillInsertText(aInfo->action, aSelection, aCancel, aHandled, aInfo->inString, aInfo->outString, aInfo->maxLength); case EditAction::setText: UndefineCaretBidiLevel(aSelection); return WillSetText(*aSelection, aCancel, aHandled, aInfo->inString, aInfo->maxLength); case EditAction::deleteSelection: return WillDeleteSelection(aSelection, aInfo->collapsedAction, aCancel, aHandled); case EditAction::undo: return WillUndo(aSelection, aCancel, aHandled); case EditAction::redo: return WillRedo(aSelection, aCancel, aHandled); case EditAction::setTextProperty: return WillSetTextProperty(aSelection, aCancel, aHandled); case EditAction::removeTextProperty: return WillRemoveTextProperty(aSelection, aCancel, aHandled); case EditAction::outputText: return WillOutputText(aSelection, aInfo->outputFormat, aInfo->outString, aInfo->flags, aCancel, aHandled); case EditAction::insertElement: // i had thought this would be html rules only. but we put pre elements // into plaintext mail when doing quoting for reply! doh! WillInsert(*aSelection, aCancel); return NS_OK; default: return NS_ERROR_FAILURE; } } nsresult TextEditRules::DidDoAction(Selection* aSelection, RulesInfo* aInfo, nsresult aResult) { NS_ENSURE_STATE(mTextEditor); // don't let any txns in here move the selection around behind our back. // Note that this won't prevent explicit selection setting from working. AutoTransactionsConserveSelection dontChangeMySelection(mTextEditor); NS_ENSURE_TRUE(aSelection && aInfo, NS_ERROR_NULL_POINTER); switch (aInfo->action) { case EditAction::insertBreak: return DidInsertBreak(aSelection, aResult); case EditAction::insertText: case EditAction::insertIMEText: return DidInsertText(aSelection, aResult); case EditAction::setText: return DidSetText(*aSelection, aResult); case EditAction::deleteSelection: return DidDeleteSelection(aSelection, aInfo->collapsedAction, aResult); case EditAction::undo: return DidUndo(aSelection, aResult); case EditAction::redo: return DidRedo(aSelection, aResult); case EditAction::setTextProperty: return DidSetTextProperty(aSelection, aResult); case EditAction::removeTextProperty: return DidRemoveTextProperty(aSelection, aResult); case EditAction::outputText: return DidOutputText(aSelection, aResult); default: // Don't fail on transactions we don't handle here! return NS_OK; } } /** * Return false if the editor has non-empty text nodes or non-text * nodes. Otherwise, i.e., there is no meaningful content, * return true. */ bool TextEditRules::DocumentIsEmpty() { bool retVal = false; if (!mTextEditor || NS_FAILED(mTextEditor->DocumentIsEmpty(&retVal))) { retVal = true; } return retVal; } void TextEditRules::WillInsert(Selection& aSelection, bool* aCancel) { MOZ_ASSERT(aCancel); if (IsReadonly() || IsDisabled()) { *aCancel = true; return; } // initialize out param *aCancel = false; // check for the magic content node and delete it if it exists if (mBogusNode) { if (NS_WARN_IF(!mTextEditor)) { return; // XXX Shouldn't we release mBogusNode now? } RefPtr textEditor(mTextEditor); DebugOnly rv = textEditor->DeleteNodeWithTransaction(*mBogusNode); NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to remove the bogus node"); mBogusNode = nullptr; } } nsresult TextEditRules::DidInsert(Selection* aSelection, nsresult aResult) { return NS_OK; } nsresult TextEditRules::WillInsertBreak(Selection* aSelection, bool* aCancel, bool* aHandled, int32_t aMaxLength) { if (!aSelection || !aCancel || !aHandled) { return NS_ERROR_NULL_POINTER; } CANCEL_OPERATION_IF_READONLY_OR_DISABLED *aHandled = false; if (IsSingleLineEditor()) { *aCancel = true; } else { // handle docs with a max length // NOTE, this function copies inString into outString for us. NS_NAMED_LITERAL_STRING(inString, "\n"); nsAutoString outString; bool didTruncate; nsresult rv = TruncateInsertionIfNeeded(aSelection, &inString.AsString(), &outString, aMaxLength, &didTruncate); NS_ENSURE_SUCCESS(rv, rv); if (didTruncate) { *aCancel = true; return NS_OK; } *aCancel = false; // if the selection isn't collapsed, delete it. if (!aSelection->IsCollapsed()) { NS_ENSURE_STATE(mTextEditor); rv = mTextEditor->DeleteSelectionAsAction(nsIEditor::eNone, nsIEditor::eStrip); NS_ENSURE_SUCCESS(rv, rv); } WillInsert(*aSelection, aCancel); // initialize out param // we want to ignore result of WillInsert() *aCancel = false; } return NS_OK; } nsresult TextEditRules::DidInsertBreak(Selection* aSelection, nsresult aResult) { return NS_OK; } nsresult TextEditRules::CollapseSelectionToTrailingBRIfNeeded(Selection* aSelection) { // we only need to execute the stuff below if we are a plaintext editor. // html editors have a different mechanism for putting in mozBR's // (because there are a bunch more places you have to worry about it in html) if (!IsPlaintextEditor()) { return NS_OK; } if (NS_WARN_IF(!mTextEditor)) { return NS_ERROR_NOT_AVAILABLE; } RefPtr textEditor(mTextEditor); // If there is no selection ranges, we should set to the end of the editor. // This is usually performed in TextEditRules::Init(), however, if the // editor is reframed, this may be called by AfterEdit(). if (!aSelection->RangeCount()) { textEditor->CollapseSelectionToEnd(aSelection); if (!mTextEditor) { // The editor has been destroyed. return NS_ERROR_FAILURE; } } // If we are at the end of the