2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-12-14 15:58:45 -08:00
|
|
|
#include "mozilla/DebugOnly.h" // for DebugOnly
|
|
|
|
|
2013-04-02 17:13:10 -07:00
|
|
|
#include <stdio.h> // for nullptr, stdout
|
2012-07-12 23:33:42 -07:00
|
|
|
#include <string.h> // for strcmp
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "ChangeAttributeTxn.h" // for ChangeAttributeTxn
|
|
|
|
#include "CreateElementTxn.h" // for CreateElementTxn
|
|
|
|
#include "DeleteNodeTxn.h" // for DeleteNodeTxn
|
|
|
|
#include "DeleteRangeTxn.h" // for DeleteRangeTxn
|
|
|
|
#include "DeleteTextTxn.h" // for DeleteTextTxn
|
|
|
|
#include "EditAggregateTxn.h" // for EditAggregateTxn
|
|
|
|
#include "EditTxn.h" // for EditTxn
|
|
|
|
#include "IMETextTxn.h" // for IMETextTxn
|
|
|
|
#include "InsertElementTxn.h" // for InsertElementTxn
|
|
|
|
#include "InsertTextTxn.h" // for InsertTextTxn
|
|
|
|
#include "JoinElementTxn.h" // for JoinElementTxn
|
|
|
|
#include "PlaceholderTxn.h" // for PlaceholderTxn
|
|
|
|
#include "SplitElementTxn.h" // for SplitElementTxn
|
2014-02-12 05:02:55 -08:00
|
|
|
#include "TextComposition.h" // for TextComposition
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "mozFlushType.h" // for mozFlushType::Flush_Frames
|
|
|
|
#include "mozISpellCheckingEngine.h"
|
|
|
|
#include "mozInlineSpellChecker.h" // for mozInlineSpellChecker
|
2014-03-07 17:20:07 -08:00
|
|
|
#include "mozilla/IMEStateManager.h" // for IMEStateManager
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "mozilla/Preferences.h" // for Preferences
|
|
|
|
#include "mozilla/Selection.h" // for Selection, etc
|
|
|
|
#include "mozilla/Services.h" // for GetObserverService
|
2013-09-25 04:21:19 -07:00
|
|
|
#include "mozilla/TextEvents.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "mozilla/dom/Element.h" // for Element, nsINode::AsElement
|
|
|
|
#include "mozilla/mozalloc.h" // for operator new, etc
|
|
|
|
#include "nsAString.h" // for nsAString_internal::Length, etc
|
|
|
|
#include "nsCCUncollectableMarker.h" // for nsCCUncollectableMarker
|
2012-10-28 21:31:26 -07:00
|
|
|
#include "nsCaret.h" // for nsCaret
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsCaseTreatment.h"
|
|
|
|
#include "nsCharTraits.h" // for NS_IS_HIGH_SURROGATE, etc
|
|
|
|
#include "nsComponentManagerUtils.h" // for do_CreateInstance
|
|
|
|
#include "nsComputedDOMStyle.h" // for nsComputedDOMStyle
|
|
|
|
#include "nsContentUtils.h" // for nsContentUtils
|
|
|
|
#include "nsDOMString.h" // for DOMStringIsNull
|
|
|
|
#include "nsDebug.h" // for NS_ENSURE_TRUE, etc
|
|
|
|
#include "nsEditProperty.h" // for nsEditProperty, etc
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsEditor.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsEditorEventListener.h" // for nsEditorEventListener
|
|
|
|
#include "nsEditorUtils.h" // for nsAutoRules, etc
|
|
|
|
#include "nsError.h" // for NS_OK, etc
|
|
|
|
#include "nsFocusManager.h" // for nsFocusManager
|
|
|
|
#include "nsFrameSelection.h" // for nsFrameSelection
|
|
|
|
#include "nsGkAtoms.h" // for nsGkAtoms, nsGkAtoms::dir
|
|
|
|
#include "nsIAbsorbingTransaction.h" // for nsIAbsorbingTransaction
|
|
|
|
#include "nsIAtom.h" // for nsIAtom
|
|
|
|
#include "nsIContent.h" // for nsIContent
|
|
|
|
#include "nsIDOMAttr.h" // for nsIDOMAttr
|
|
|
|
#include "nsIDOMCharacterData.h" // for nsIDOMCharacterData
|
|
|
|
#include "nsIDOMDocument.h" // for nsIDOMDocument
|
|
|
|
#include "nsIDOMElement.h" // for nsIDOMElement
|
|
|
|
#include "nsIDOMEvent.h" // for nsIDOMEvent
|
|
|
|
#include "nsIDOMEventListener.h" // for nsIDOMEventListener
|
|
|
|
#include "nsIDOMEventTarget.h" // for nsIDOMEventTarget
|
|
|
|
#include "nsIDOMHTMLElement.h" // for nsIDOMHTMLElement
|
|
|
|
#include "nsIDOMKeyEvent.h" // for nsIDOMKeyEvent, etc
|
2013-03-10 00:00:33 -08:00
|
|
|
#include "nsIDOMMozNamedAttrMap.h" // for nsIDOMMozNamedAttrMap
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsIDOMMouseEvent.h" // for nsIDOMMouseEvent
|
|
|
|
#include "nsIDOMNode.h" // for nsIDOMNode, etc
|
|
|
|
#include "nsIDOMNodeList.h" // for nsIDOMNodeList
|
|
|
|
#include "nsIDOMRange.h" // for nsIDOMRange
|
|
|
|
#include "nsIDOMText.h" // for nsIDOMText
|
|
|
|
#include "nsIDocument.h" // for nsIDocument
|
|
|
|
#include "nsIDocumentStateListener.h" // for nsIDocumentStateListener
|
|
|
|
#include "nsIEditActionListener.h" // for nsIEditActionListener
|
2012-11-14 11:23:35 -08:00
|
|
|
#include "nsIEditorObserver.h" // for nsIEditorObserver
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsIEditorSpellCheck.h" // for nsIEditorSpellCheck
|
|
|
|
#include "nsIFrame.h" // for nsIFrame
|
2013-06-19 13:54:53 -07:00
|
|
|
#include "nsIHTMLDocument.h" // for nsIHTMLDocument
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsIInlineSpellChecker.h" // for nsIInlineSpellChecker, etc
|
2014-02-27 15:04:46 -08:00
|
|
|
#include "nsNameSpaceManager.h" // for kNameSpaceID_None, etc
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsINode.h" // for nsINode, etc
|
|
|
|
#include "nsIObserverService.h" // for nsIObserverService
|
|
|
|
#include "nsIPlaintextEditor.h" // for nsIPlaintextEditor, etc
|
|
|
|
#include "nsIPresShell.h" // for nsIPresShell
|
|
|
|
#include "nsISelection.h" // for nsISelection, etc
|
|
|
|
#include "nsISelectionController.h" // for nsISelectionController, etc
|
|
|
|
#include "nsISelectionDisplay.h" // for nsISelectionDisplay, etc
|
|
|
|
#include "nsISelectionPrivate.h" // for nsISelectionPrivate, etc
|
|
|
|
#include "nsISupportsBase.h" // for nsISupports
|
|
|
|
#include "nsISupportsUtils.h" // for NS_ADDREF, NS_IF_ADDREF
|
|
|
|
#include "nsITransaction.h" // for nsITransaction
|
|
|
|
#include "nsITransactionManager.h"
|
|
|
|
#include "nsIWeakReference.h" // for nsISupportsWeakReference
|
|
|
|
#include "nsIWidget.h" // for nsIWidget, IMEState, etc
|
|
|
|
#include "nsPIDOMWindow.h" // for nsPIDOMWindow
|
|
|
|
#include "nsPresContext.h" // for nsPresContext
|
|
|
|
#include "nsRange.h" // for nsRange
|
|
|
|
#include "nsReadableUtils.h" // for EmptyString, ToNewCString
|
|
|
|
#include "nsString.h" // for nsAutoString, nsString, etc
|
|
|
|
#include "nsStringFwd.h" // for nsAFlatString
|
|
|
|
#include "nsStyleConsts.h" // for NS_STYLE_DIRECTION_RTL, etc
|
|
|
|
#include "nsStyleContext.h" // for nsStyleContext
|
|
|
|
#include "nsStyleSheetTxns.h" // for AddStyleSheetTxn, etc
|
|
|
|
#include "nsStyleStruct.h" // for nsStyleDisplay, nsStyleText, etc
|
2013-02-16 13:51:02 -08:00
|
|
|
#include "nsStyleStructFwd.h" // for nsIFrame::StyleUIReset, etc
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsTextEditUtils.h" // for nsTextEditUtils
|
2013-04-04 05:02:24 -07:00
|
|
|
#include "nsTextNode.h" // for nsTextNode
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsThreadUtils.h" // for nsRunnable
|
|
|
|
#include "nsTransactionManager.h" // for nsTransactionManager
|
|
|
|
#include "prtime.h" // for PR_Now
|
2010-01-10 17:45:45 -08:00
|
|
|
|
2012-07-12 23:33:42 -07:00
|
|
|
class nsIOutputStream;
|
|
|
|
class nsIParserService;
|
|
|
|
class nsITransferable;
|
2010-05-19 16:22:19 -07:00
|
|
|
|
2010-06-16 22:29:40 -07:00
|
|
|
#ifdef DEBUG
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsIDOMHTMLDocument.h" // for nsIDOMHTMLDocument
|
2010-06-16 22:29:40 -07:00
|
|
|
#endif
|
|
|
|
|
2011-06-16 17:59:29 -07:00
|
|
|
using namespace mozilla;
|
2013-01-09 15:05:08 -08:00
|
|
|
using namespace mozilla::dom;
|
2011-11-27 03:51:53 -08:00
|
|
|
using namespace mozilla::widget;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Defined in nsEditorRegistration.cpp
|
|
|
|
extern nsIParserService *sParserService;
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// nsEditor: base editor class implementation
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsEditor::nsEditor()
|
2012-07-30 07:20:58 -07:00
|
|
|
: mPlaceHolderName(nullptr)
|
|
|
|
, mSelState(nullptr)
|
|
|
|
, mPhonetic(nullptr)
|
2012-04-23 21:54:11 -07:00
|
|
|
, mModCount(0)
|
2010-08-22 17:35:37 -07:00
|
|
|
, mFlags(0)
|
2007-03-22 10:30:00 -07:00
|
|
|
, mUpdateCount(0)
|
|
|
|
, mPlaceHolderBatch(0)
|
2012-08-12 11:28:26 -07:00
|
|
|
, mAction(EditAction::none)
|
2007-03-22 10:30:00 -07:00
|
|
|
, mIMETextOffset(0)
|
2012-04-23 21:54:11 -07:00
|
|
|
, mDirection(eNone)
|
|
|
|
, mDocDirtyState(-1)
|
|
|
|
, mSpellcheckCheckboxState(eTriUnset)
|
2011-10-17 07:59:28 -07:00
|
|
|
, mShouldTxnSetSelection(true)
|
|
|
|
, mDidPreDestroy(false)
|
|
|
|
, mDidPostCreate(false)
|
2012-03-26 18:36:44 -07:00
|
|
|
, mDispatchInputEvent(true)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEditor::~nsEditor()
|
|
|
|
{
|
2010-04-11 23:13:17 -07:00
|
|
|
NS_ASSERTION(!mDocWeak || mDidPreDestroy, "Why PreDestroy hasn't been called?");
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mTxnMgr = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
delete mPhonetic;
|
|
|
|
}
|
|
|
|
|
2013-08-01 18:29:05 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsEditor)
|
|
|
|
|
2009-05-08 21:59:25 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsEditor)
|
2012-11-14 23:32:40 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mRootElement)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mInlineSpellChecker)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mTxnMgr)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mIMETextNode)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mActionListeners)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mEditorObservers)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDocStateListeners)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mEventTarget)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mEventListener)
|
2009-05-08 21:59:25 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsEditor)
|
2011-12-30 09:16:10 -08:00
|
|
|
nsIDocument* currentDoc =
|
2012-07-30 07:20:58 -07:00
|
|
|
tmp->mRootElement ? tmp->mRootElement->GetCurrentDoc() : nullptr;
|
2011-12-30 09:16:10 -08:00
|
|
|
if (currentDoc &&
|
|
|
|
nsCCUncollectableMarker::InGeneration(cb, currentDoc->GetMarkedCCGeneration())) {
|
|
|
|
return NS_SUCCESS_INTERRUPTED_TRAVERSE;
|
|
|
|
}
|
2012-11-14 23:32:40 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRootElement)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mInlineSpellChecker)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTxnMgr)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mIMETextNode)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mActionListeners)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEditorObservers)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDocStateListeners)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEventTarget)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEventListener)
|
2009-05-08 21:59:25 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsEditor)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIPhonetic)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIEditorIMESupport)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIEditor)
|
2011-09-13 11:58:54 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIObserver)
|
2009-05-08 21:59:25 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIEditor)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2011-03-06 03:11:31 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsEditor)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsEditor)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsEditor::Init(nsIDOMDocument *aDoc, nsIContent *aRoot, nsISelectionController *aSelCon, uint32_t aFlags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-03-10 21:40:31 -08:00
|
|
|
NS_PRECONDITION(aDoc, "bad arg");
|
|
|
|
if (!aDoc)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2010-05-04 10:40:39 -07:00
|
|
|
// First only set flags, but other stuff shouldn't be initialized now.
|
2011-03-10 21:40:31 -08:00
|
|
|
// Don't move this call after initializing mDocWeak.
|
2010-05-04 10:40:39 -07:00
|
|
|
// SetFlags() can check whether it's called during initialization or not by
|
|
|
|
// them. Note that SetFlags() will be called by PostCreate().
|
2010-11-04 13:44:46 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
nsresult rv =
|
|
|
|
#endif
|
|
|
|
SetFlags(aFlags);
|
2010-05-04 10:40:39 -07:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "SetFlags() failed");
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
mDocWeak = do_GetWeakReference(aDoc); // weak reference to doc
|
2011-03-10 21:40:30 -08:00
|
|
|
// HTML editors currently don't have their own selection controller,
|
|
|
|
// so they'll pass null as aSelCon, and we'll get the selection controller
|
|
|
|
// off of the presshell.
|
|
|
|
nsCOMPtr<nsISelectionController> selCon;
|
|
|
|
if (aSelCon) {
|
|
|
|
mSelConWeak = do_GetWeakReference(aSelCon); // weak reference to selectioncontroller
|
|
|
|
selCon = aSelCon;
|
|
|
|
} else {
|
2011-05-22 05:43:13 -07:00
|
|
|
nsCOMPtr<nsIPresShell> presShell = GetPresShell();
|
2011-03-10 21:40:31 -08:00
|
|
|
selCon = do_QueryInterface(presShell);
|
2011-03-10 21:40:30 -08:00
|
|
|
}
|
|
|
|
NS_ASSERTION(selCon, "Selection controller should be available at this point");
|
2010-04-11 19:35:18 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//set up root element if we are passed one.
|
|
|
|
if (aRoot)
|
|
|
|
mRootElement = do_QueryInterface(aRoot);
|
|
|
|
|
|
|
|
mUpdateCount=0;
|
|
|
|
|
|
|
|
/* initialize IME stuff */
|
2012-07-30 07:20:58 -07:00
|
|
|
mIMETextNode = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
mIMETextOffset = 0;
|
|
|
|
/* Show the caret */
|
2011-10-17 07:59:28 -07:00
|
|
|
selCon->SetCaretReadOnly(false);
|
2011-03-10 21:40:30 -08:00
|
|
|
selCon->SetDisplaySelection(nsISelectionController::SELECTION_ON);
|
|
|
|
|
|
|
|
selCon->SetSelectionFlags(nsISelectionDisplay::DISPLAY_ALL);//we want to see all the selection reflected to user
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-03-10 21:40:31 -08:00
|
|
|
NS_POSTCONDITION(mDocWeak, "bad state");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-21 13:17:41 -07:00
|
|
|
// Make sure that the editor will be destroyed properly
|
2011-10-17 07:59:28 -07:00
|
|
|
mDidPreDestroy = false;
|
2011-03-11 15:45:20 -08:00
|
|
|
// Make sure that the ediotr will be created properly
|
2011-10-17 07:59:28 -07:00
|
|
|
mDidPostCreate = false;
|
2010-04-21 13:17:41 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::PostCreate()
|
|
|
|
{
|
2010-08-19 04:27:04 -07:00
|
|
|
// Synchronize some stuff for the flags. SetFlags() will initialize
|
|
|
|
// something by the flag difference. This is first time of that, so, all
|
|
|
|
// initializations must be run. For such reason, we need to invert mFlags
|
|
|
|
// value first.
|
|
|
|
mFlags = ~mFlags;
|
|
|
|
nsresult rv = SetFlags(~mFlags);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-03-11 15:45:20 -08:00
|
|
|
// These operations only need to happen on the first PostCreate call
|
|
|
|
if (!mDidPostCreate) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mDidPostCreate = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-03-11 15:45:20 -08:00
|
|
|
// Set up listeners
|
2011-07-15 03:18:35 -07:00
|
|
|
CreateEventListeners();
|
2011-03-11 15:45:20 -08:00
|
|
|
rv = InstallEventListeners();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// nuke the modification count, so the doc appears unmodified
|
|
|
|
// do this before we notify listeners
|
|
|
|
ResetModificationCount();
|
|
|
|
|
|
|
|
// update the UI with our state
|
|
|
|
NotifyDocumentListeners(eDocumentCreated);
|
|
|
|
NotifyDocumentListeners(eDocumentStateChanged);
|
2011-09-13 11:58:54 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
obs->AddObserver(this,
|
|
|
|
SPELLCHECK_DICTIONARY_UPDATE_NOTIFICATION,
|
2011-10-17 07:59:28 -07:00
|
|
|
false);
|
2011-09-13 11:58:54 -07:00
|
|
|
}
|
2011-03-11 15:45:20 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-10-10 15:07:01 -07:00
|
|
|
// update nsTextStateManager and caret if we have focus
|
2010-10-29 09:30:52 -07:00
|
|
|
nsCOMPtr<nsIContent> focusedContent = GetFocusedContent();
|
|
|
|
if (focusedContent) {
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> target = do_QueryInterface(focusedContent);
|
|
|
|
if (target) {
|
|
|
|
InitializeSelection(target);
|
2010-09-23 20:34:28 -07:00
|
|
|
}
|
2012-03-19 09:21:56 -07:00
|
|
|
|
|
|
|
// If the text control gets reframed during focus, Focus() would not be
|
|
|
|
// called, so take a chance here to see if we need to spell check the text
|
|
|
|
// control.
|
|
|
|
nsEditorEventListener* listener =
|
|
|
|
reinterpret_cast<nsEditorEventListener*> (mEventListener.get());
|
|
|
|
listener->SpellCheckIfNeeded();
|
2012-11-09 00:40:39 -08:00
|
|
|
|
|
|
|
IMEState newState;
|
|
|
|
rv = GetPreferredIMEState(&newState);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_OK);
|
|
|
|
nsCOMPtr<nsIContent> content = GetFocusedContentForIME();
|
2014-03-07 17:20:07 -08:00
|
|
|
IMEStateManager::UpdateIMEState(newState, content);
|
2010-09-23 20:34:28 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-07-15 03:18:35 -07:00
|
|
|
/* virtual */
|
|
|
|
void
|
2010-03-05 20:01:28 -08:00
|
|
|
nsEditor::CreateEventListeners()
|
|
|
|
{
|
2010-04-21 13:17:41 -07:00
|
|
|
// Don't create the handler twice
|
2011-06-27 16:34:54 -07:00
|
|
|
if (!mEventListener) {
|
|
|
|
mEventListener = new nsEditorEventListener();
|
|
|
|
}
|
2010-03-05 20:01:28 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
|
|
|
nsEditor::InstallEventListeners()
|
|
|
|
{
|
2011-03-10 21:40:30 -08:00
|
|
|
NS_ENSURE_TRUE(mDocWeak && mEventListener,
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ERROR_NOT_INITIALIZED);
|
2010-06-16 22:30:10 -07:00
|
|
|
|
|
|
|
// Initialize the event target.
|
2012-06-10 05:39:21 -07:00
|
|
|
nsCOMPtr<nsIContent> rootContent = GetRoot();
|
2010-06-16 22:30:10 -07:00
|
|
|
NS_ENSURE_TRUE(rootContent, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
mEventTarget = do_QueryInterface(rootContent->GetParent());
|
|
|
|
NS_ENSURE_TRUE(mEventTarget, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
2010-04-19 05:20:42 -07:00
|
|
|
nsEditorEventListener* listener =
|
|
|
|
reinterpret_cast<nsEditorEventListener*>(mEventListener.get());
|
|
|
|
return listener->Connect(this);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsEditor::RemoveEventListeners()
|
|
|
|
{
|
2010-04-19 05:20:42 -07:00
|
|
|
if (!mDocWeak || !mEventListener) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return;
|
|
|
|
}
|
2010-04-19 05:20:42 -07:00
|
|
|
reinterpret_cast<nsEditorEventListener*>(mEventListener.get())->Disconnect();
|
2014-02-12 05:02:56 -08:00
|
|
|
if (mComposition) {
|
|
|
|
mComposition->EndHandlingComposition(this);
|
|
|
|
mComposition = nullptr;
|
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
mEventTarget = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsEditor::GetDesiredSpellCheckState()
|
|
|
|
{
|
|
|
|
// Check user override on this element
|
|
|
|
if (mSpellcheckCheckboxState != eTriUnset) {
|
|
|
|
return (mSpellcheckCheckboxState == eTriTrue);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check user preferences
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t spellcheckLevel = Preferences::GetInt("layout.spellcheckDefault", 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (spellcheckLevel == 0) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false; // Spellchecking forced off globally
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-08-19 04:27:04 -07:00
|
|
|
if (!CanEnableSpellCheck()) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-05-22 05:43:13 -07:00
|
|
|
nsCOMPtr<nsIPresShell> presShell = GetPresShell();
|
|
|
|
if (presShell) {
|
2007-03-22 10:30:00 -07:00
|
|
|
nsPresContext* context = presShell->GetPresContext();
|
|
|
|
if (context && !context->IsDynamic()) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check DOM state
|
2014-01-17 07:31:03 -08:00
|
|
|
nsCOMPtr<nsIContent> content = GetExposedRoot();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!content) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-10-29 13:03:55 -07:00
|
|
|
nsCOMPtr<nsIDOMHTMLElement> element = do_QueryInterface(content);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!element) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-06-19 13:54:53 -07:00
|
|
|
if (!IsPlaintextEditor()) {
|
|
|
|
// Some of the page content might be editable and some not, if spellcheck=
|
|
|
|
// is explicitly set anywhere, so if there's anything editable on the page,
|
|
|
|
// return true and let the spellchecker figure it out.
|
|
|
|
nsCOMPtr<nsIHTMLDocument> doc = do_QueryInterface(content->GetCurrentDoc());
|
|
|
|
return doc && doc->IsEditingOn();
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool enable;
|
2007-03-22 10:30:00 -07:00
|
|
|
element->GetSpellcheck(&enable);
|
|
|
|
|
|
|
|
return enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsEditor::PreDestroy(bool aDestroyingFrames)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mDidPreDestroy)
|
|
|
|
return NS_OK;
|
|
|
|
|
2011-09-13 11:58:54 -07:00
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
obs->RemoveObserver(this,
|
|
|
|
SPELLCHECK_DICTIONARY_UPDATE_NOTIFICATION);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Let spellchecker clean up its observers etc. It is important not to
|
|
|
|
// actually free the spellchecker here, since the spellchecker could have
|
|
|
|
// caused flush notifications, which could have gotten here if a textbox
|
2013-04-02 17:13:10 -07:00
|
|
|
// is being removed. Setting the spellchecker to nullptr could free the
|
2007-03-22 10:30:00 -07:00
|
|
|
// object that is still in use! It will be freed when the editor is
|
|
|
|
// destroyed.
|
|
|
|
if (mInlineSpellChecker)
|
2008-12-12 00:34:43 -08:00
|
|
|
mInlineSpellChecker->Cleanup(aDestroyingFrames);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// tell our listeners that the doc is going away
|
|
|
|
NotifyDocumentListeners(eDocumentToBeDestroyed);
|
|
|
|
|
|
|
|
// Unregister event listeners
|
|
|
|
RemoveEventListeners();
|
|
|
|
mActionListeners.Clear();
|
2012-11-14 11:23:35 -08:00
|
|
|
mEditorObservers.Clear();
|
2007-03-22 10:30:00 -07:00
|
|
|
mDocStateListeners.Clear();
|
2012-07-30 07:20:58 -07:00
|
|
|
mInlineSpellChecker = nullptr;
|
2011-03-01 15:04:42 -08:00
|
|
|
mSpellcheckCheckboxState = eTriUnset;
|
2012-07-30 07:20:58 -07:00
|
|
|
mRootElement = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mDidPreDestroy = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsEditor::GetFlags(uint32_t *aFlags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
*aFlags = mFlags;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsEditor::SetFlags(uint32_t aFlags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-08-19 04:27:04 -07:00
|
|
|
if (mFlags == aFlags) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool spellcheckerWasEnabled = CanEnableSpellCheck();
|
2007-03-22 10:30:00 -07:00
|
|
|
mFlags = aFlags;
|
|
|
|
|
2011-03-10 21:40:30 -08:00
|
|
|
if (!mDocWeak) {
|
2010-05-04 10:40:39 -07:00
|
|
|
// If we're initializing, we shouldn't do anything now.
|
|
|
|
// SetFlags() will be called by PostCreate(),
|
|
|
|
// we should synchronize some stuff for the flags at that time.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-08-19 04:27:04 -07:00
|
|
|
// The flag change may cause the spellchecker state change
|
|
|
|
if (CanEnableSpellCheck() != spellcheckerWasEnabled) {
|
|
|
|
nsresult rv = SyncRealTimeSpell();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2010-05-04 10:40:39 -07:00
|
|
|
|
2012-11-09 00:40:39 -08:00
|
|
|
// If this is called from PostCreate(), it will update the IME state if it's
|
|
|
|
// necessary.
|
|
|
|
if (!mDidPostCreate) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-05-04 10:40:39 -07:00
|
|
|
// Might be changing editable state, so, we need to reset current IME state
|
2010-05-26 19:04:14 -07:00
|
|
|
// if we're focused and the flag change causes IME state change.
|
2010-10-29 09:30:52 -07:00
|
|
|
nsCOMPtr<nsIContent> focusedContent = GetFocusedContent();
|
|
|
|
if (focusedContent) {
|
2011-11-27 03:51:53 -08:00
|
|
|
IMEState newState;
|
2010-08-19 04:27:04 -07:00
|
|
|
nsresult rv = GetPreferredIMEState(&newState);
|
2010-05-04 10:40:39 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2010-05-26 19:04:14 -07:00
|
|
|
// NOTE: When the enabled state isn't going to be modified, this method
|
|
|
|
// is going to do nothing.
|
2012-11-09 00:40:39 -08:00
|
|
|
nsCOMPtr<nsIContent> content = GetFocusedContentForIME();
|
2014-03-07 17:20:07 -08:00
|
|
|
IMEStateManager::UpdateIMEState(newState, content);
|
2010-05-04 10:40:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-08-17 10:28:03 -07:00
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsEditor::GetIsSelectionEditable(bool *aIsSelectionEditable)
|
2011-08-17 10:28:03 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aIsSelectionEditable);
|
|
|
|
|
|
|
|
// get current selection
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
nsresult res = GetSelection(getter_AddRefs(selection));
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
// XXX we just check that the anchor node is editable at the moment
|
|
|
|
// we should check that all nodes in the selection are editable
|
|
|
|
nsCOMPtr<nsIDOMNode> anchorNode;
|
|
|
|
selection->GetAnchorNode(getter_AddRefs(anchorNode));
|
|
|
|
*aIsSelectionEditable = anchorNode && IsEditable(anchorNode);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsEditor::GetIsDocumentEditable(bool *aIsDocumentEditable)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aIsDocumentEditable);
|
2012-03-24 01:19:14 -07:00
|
|
|
nsCOMPtr<nsIDOMDocument> doc = GetDOMDocument();
|
|
|
|
*aIsDocumentEditable = !!doc;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-24 01:19:14 -07:00
|
|
|
already_AddRefed<nsIDocument>
|
|
|
|
nsEditor::GetDocument()
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(mDocWeak, "bad state, mDocWeak weak pointer not initialized");
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocWeak);
|
|
|
|
return doc.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIDOMDocument>
|
|
|
|
nsEditor::GetDOMDocument()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(mDocWeak, "bad state, mDocWeak weak pointer not initialized");
|
|
|
|
nsCOMPtr<nsIDOMDocument> doc = do_QueryReferent(mDocWeak);
|
2012-03-24 01:19:14 -07:00
|
|
|
return doc.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::GetDocument(nsIDOMDocument **aDoc)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMDocument> doc = GetDOMDocument();
|
|
|
|
doc.forget(aDoc);
|
|
|
|
return *aDoc ? NS_OK : NS_ERROR_NOT_INITIALIZED;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-05-22 05:43:13 -07:00
|
|
|
already_AddRefed<nsIPresShell>
|
|
|
|
nsEditor::GetPresShell()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-03-10 21:40:31 -08:00
|
|
|
NS_PRECONDITION(mDocWeak, "bad state, null mDocWeak");
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocWeak);
|
2013-04-02 17:13:10 -07:00
|
|
|
NS_ENSURE_TRUE(doc, nullptr);
|
2011-05-23 09:54:57 -07:00
|
|
|
nsCOMPtr<nsIPresShell> ps = doc->GetShell();
|
2011-05-22 05:43:13 -07:00
|
|
|
return ps.forget();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* attribute string contentsMIMEType; */
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::GetContentsMIMEType(char * *aContentsMIMEType)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aContentsMIMEType);
|
|
|
|
*aContentsMIMEType = ToNewCString(mContentMIMEType);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::SetContentsMIMEType(const char * aContentsMIMEType)
|
|
|
|
{
|
|
|
|
mContentMIMEType.Assign(aContentsMIMEType ? aContentsMIMEType : "");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::GetSelectionController(nsISelectionController **aSel)
|
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aSel, NS_ERROR_NULL_POINTER);
|
2012-07-30 07:20:58 -07:00
|
|
|
*aSel = nullptr; // init out param
|
2011-03-10 21:40:30 -08:00
|
|
|
nsCOMPtr<nsISelectionController> selCon;
|
|
|
|
if (mSelConWeak) {
|
|
|
|
selCon = do_QueryReferent(mSelConWeak);
|
|
|
|
} else {
|
2011-05-22 05:43:13 -07:00
|
|
|
nsCOMPtr<nsIPresShell> presShell = GetPresShell();
|
2011-03-10 21:40:30 -08:00
|
|
|
selCon = do_QueryInterface(presShell);
|
|
|
|
}
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(selCon, NS_ERROR_NOT_INITIALIZED);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ADDREF(*aSel = selCon);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-05-10 07:54:33 -07:00
|
|
|
nsEditor::DeleteSelection(EDirection aAction, EStripWrappers aStripWrappers)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-05-10 07:54:33 -07:00
|
|
|
MOZ_ASSERT(aStripWrappers == eStrip || aStripWrappers == eNoStrip);
|
|
|
|
return DeleteSelectionImpl(aAction, aStripWrappers);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::GetSelection(nsISelection **aSelection)
|
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aSelection, NS_ERROR_NULL_POINTER);
|
2012-07-30 07:20:58 -07:00
|
|
|
*aSelection = nullptr;
|
2011-03-10 21:40:30 -08:00
|
|
|
nsCOMPtr<nsISelectionController> selcon;
|
|
|
|
GetSelectionController(getter_AddRefs(selcon));
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(selcon, NS_ERROR_NOT_INITIALIZED);
|
2007-03-22 10:30:00 -07:00
|
|
|
return selcon->GetSelection(nsISelectionController::SELECTION_NORMAL, aSelection); // does an addref
|
|
|
|
}
|
|
|
|
|
2012-06-08 07:20:55 -07:00
|
|
|
Selection*
|
|
|
|
nsEditor::GetSelection()
|
2012-05-10 07:54:33 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsISelection> sel;
|
|
|
|
nsresult res = GetSelection(getter_AddRefs(sel));
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, nullptr);
|
2012-05-10 07:54:33 -07:00
|
|
|
|
2013-02-15 12:09:28 -08:00
|
|
|
return static_cast<Selection*>(sel.get());
|
2012-05-10 07:54:33 -07:00
|
|
|
}
|
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::DoTransaction(nsITransaction* aTxn)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-06-19 06:23:36 -07:00
|
|
|
if (mPlaceHolderBatch && !mPlaceHolderTxn) {
|
2012-07-09 01:41:47 -07:00
|
|
|
nsCOMPtr<nsIAbsorbingTransaction> plcTxn = new PlaceholderTxn();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// save off weak reference to placeholder txn
|
|
|
|
mPlaceHolderTxn = do_GetWeakReference(plcTxn);
|
|
|
|
plcTxn->Init(mPlaceHolderName, mSelState, this);
|
2012-06-19 06:23:36 -07:00
|
|
|
// placeholder txn took ownership of this pointer
|
2012-07-30 07:20:58 -07:00
|
|
|
mSelState = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-09 01:41:47 -07:00
|
|
|
// QI to an nsITransaction since that's what DoTransaction() expects
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsITransaction> theTxn = do_QueryInterface(plcTxn);
|
2012-06-19 06:23:36 -07:00
|
|
|
// we will recurse, but will not hit this case in the nested call
|
|
|
|
DoTransaction(theTxn);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
if (mTxnMgr) {
|
2012-06-19 06:23:36 -07:00
|
|
|
nsCOMPtr<nsITransaction> topTxn = mTxnMgr->PeekUndoStack();
|
2012-06-19 06:23:36 -07:00
|
|
|
if (topTxn) {
|
2007-03-22 10:30:00 -07:00
|
|
|
plcTxn = do_QueryInterface(topTxn);
|
2012-06-19 06:23:36 -07:00
|
|
|
if (plcTxn) {
|
|
|
|
// there is a placeholder transaction on top of the undo stack. It
|
|
|
|
// is either the one we just created, or an earlier one that we are
|
|
|
|
// now merging into. From here on out remember this placeholder
|
|
|
|
// instead of the one we just created.
|
2007-03-22 10:30:00 -07:00
|
|
|
mPlaceHolderTxn = do_GetWeakReference(plcTxn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
if (aTxn) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// XXX: Why are we doing selection specific batching stuff here?
|
|
|
|
// XXX: Most entry points into the editor have auto variables that
|
|
|
|
// XXX: should trigger Begin/EndUpdateViewBatch() calls that will make
|
|
|
|
// XXX: these selection batch calls no-ops.
|
|
|
|
// XXX:
|
|
|
|
// XXX: I suspect that this was placed here to avoid multiple
|
|
|
|
// XXX: selection changed notifications from happening until after
|
|
|
|
// XXX: the transaction was done. I suppose that can still happen
|
|
|
|
// XXX: if an embedding application called DoTransaction() directly
|
|
|
|
// XXX: to pump its own transactions through the system, but in that
|
|
|
|
// XXX: case, wouldn't we want to use Begin/EndUpdateViewBatch() or
|
|
|
|
// XXX: its auto equivalent nsAutoUpdateViewBatch to ensure that
|
|
|
|
// XXX: selection listeners have access to accurate frame data?
|
|
|
|
// XXX:
|
|
|
|
// XXX: Note that if we did add Begin/EndUpdateViewBatch() calls
|
|
|
|
// XXX: we will need to make sure that they are disabled during
|
|
|
|
// XXX: the init of the editor for text widgets to avoid layout
|
|
|
|
// XXX: re-entry during initial reflow. - kin
|
|
|
|
|
|
|
|
// get the selection and start a batch change
|
2012-06-19 06:23:36 -07:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
2010-06-21 08:02:14 -07:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
selection->StartBatchChanges();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
nsresult res;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mTxnMgr) {
|
2012-06-19 06:23:36 -07:00
|
|
|
res = mTxnMgr->DoTransaction(aTxn);
|
|
|
|
} else {
|
|
|
|
res = aTxn->DoTransaction();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-06-19 06:23:36 -07:00
|
|
|
if (NS_SUCCEEDED(res)) {
|
2012-07-05 00:45:09 -07:00
|
|
|
DoAfterDoTransaction(aTxn);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
// no need to check res here, don't lose result of operation
|
|
|
|
selection->EndBatchChanges();
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsEditor::EnableUndo(bool aEnable)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-06-19 06:23:36 -07:00
|
|
|
if (aEnable) {
|
|
|
|
if (!mTxnMgr) {
|
|
|
|
mTxnMgr = new nsTransactionManager();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-07-14 23:11:47 -07:00
|
|
|
mTxnMgr->SetMaxTransactionCount(-1);
|
2012-06-19 06:23:36 -07:00
|
|
|
} else if (mTxnMgr) {
|
|
|
|
// disable the transaction manager if it is enabled
|
|
|
|
mTxnMgr->Clear();
|
|
|
|
mTxnMgr->SetMaxTransactionCount(0);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-02-23 12:47:37 -08:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsEditor::GetNumberOfUndoItems(int32_t* aNumItems)
|
2012-02-23 12:47:37 -08:00
|
|
|
{
|
|
|
|
*aNumItems = 0;
|
|
|
|
return mTxnMgr ? mTxnMgr->GetNumberOfUndoItems(aNumItems) : NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsEditor::GetNumberOfRedoItems(int32_t* aNumItems)
|
2012-02-23 12:47:37 -08:00
|
|
|
{
|
|
|
|
*aNumItems = 0;
|
|
|
|
return mTxnMgr ? mTxnMgr->GetNumberOfRedoItems(aNumItems) : NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::GetTransactionManager(nsITransactionManager* *aTxnManager)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aTxnManager);
|
|
|
|
|
2013-04-02 17:13:10 -07:00
|
|
|
*aTxnManager = nullptr;
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(mTxnMgr, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_ADDREF(*aTxnManager = mTxnMgr);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::SetTransactionManager(nsITransactionManager *aTxnManager)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(aTxnManager, NS_ERROR_FAILURE);
|
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
// nsITransactionManager is builtinclass, so this is safe
|
|
|
|
mTxnMgr = static_cast<nsTransactionManager*>(aTxnManager);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsEditor::Undo(uint32_t aCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
ForceCompositionEnd();
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasTxnMgr, hasTransaction = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
CanUndo(&hasTxnMgr, &hasTransaction);
|
2012-02-01 02:54:22 -08:00
|
|
|
NS_ENSURE_TRUE(hasTransaction, NS_OK);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-12 11:28:26 -07:00
|
|
|
nsAutoRules beginRulesSniffing(this, EditAction::undo, nsIEditor::eNone);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-01 02:54:22 -08:00
|
|
|
if (!mTxnMgr) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < aCount; ++i) {
|
2012-02-01 02:54:22 -08:00
|
|
|
nsresult rv = mTxnMgr->UndoTransaction();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-07-05 00:45:09 -07:00
|
|
|
DoAfterUndoTransaction();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-02-01 02:54:22 -08:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
NS_IMETHODIMP nsEditor::CanUndo(bool *aIsEnabled, bool *aCanUndo)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aIsEnabled && aCanUndo, NS_ERROR_NULL_POINTER);
|
2012-02-01 02:54:22 -08:00
|
|
|
*aIsEnabled = !!mTxnMgr;
|
|
|
|
if (*aIsEnabled) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t numTxns = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
mTxnMgr->GetNumberOfUndoItems(&numTxns);
|
2012-02-01 02:54:22 -08:00
|
|
|
*aCanUndo = !!numTxns;
|
|
|
|
} else {
|
2011-10-17 07:59:28 -07:00
|
|
|
*aCanUndo = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsEditor::Redo(uint32_t aCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasTxnMgr, hasTransaction = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
CanRedo(&hasTxnMgr, &hasTransaction);
|
2012-02-01 02:54:22 -08:00
|
|
|
NS_ENSURE_TRUE(hasTransaction, NS_OK);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-12 11:28:26 -07:00
|
|
|
nsAutoRules beginRulesSniffing(this, EditAction::redo, nsIEditor::eNone);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-01 02:54:22 -08:00
|
|
|
if (!mTxnMgr) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < aCount; ++i) {
|
2012-02-01 02:54:22 -08:00
|
|
|
nsresult rv = mTxnMgr->RedoTransaction();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-05 00:45:09 -07:00
|
|
|
DoAfterRedoTransaction();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-02-01 02:54:22 -08:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
NS_IMETHODIMP nsEditor::CanRedo(bool *aIsEnabled, bool *aCanRedo)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aIsEnabled && aCanRedo, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-01 02:54:22 -08:00
|
|
|
*aIsEnabled = !!mTxnMgr;
|
|
|
|
if (*aIsEnabled) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t numTxns = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
mTxnMgr->GetNumberOfRedoItems(&numTxns);
|
2012-02-01 02:54:22 -08:00
|
|
|
*aCanRedo = !!numTxns;
|
|
|
|
} else {
|
2011-10-17 07:59:28 -07:00
|
|
|
*aCanRedo = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::BeginTransaction()
|
|
|
|
{
|
|
|
|
BeginUpdateViewBatch();
|
|
|
|
|
2012-02-01 02:54:22 -08:00
|
|
|
if (mTxnMgr) {
|
2013-01-03 22:54:26 -08:00
|
|
|
mTxnMgr->BeginBatch(nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::EndTransaction()
|
|
|
|
{
|
2012-02-01 02:54:22 -08:00
|
|
|
if (mTxnMgr) {
|
2013-01-03 22:54:26 -08:00
|
|
|
mTxnMgr->EndBatch(false);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
EndUpdateViewBatch();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// These two routines are similar to the above, but do not use
|
|
|
|
// the transaction managers batching feature. Instead we use
|
|
|
|
// a placeholder transaction to wrap up any further transaction
|
|
|
|
// while the batch is open. The advantage of this is that
|
|
|
|
// placeholder transactions can later merge, if needed. Merging
|
|
|
|
// is unavailable between transaction manager batches.
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::BeginPlaceHolderTransaction(nsIAtom *aName)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(mPlaceHolderBatch >= 0, "negative placeholder batch count!");
|
|
|
|
if (!mPlaceHolderBatch)
|
|
|
|
{
|
|
|
|
// time to turn on the batch
|
|
|
|
BeginUpdateViewBatch();
|
2012-07-30 07:20:58 -07:00
|
|
|
mPlaceHolderTxn = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
mPlaceHolderName = aName;
|
2012-07-23 03:27:22 -07:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
|
|
|
if (selection) {
|
2010-07-20 06:18:54 -07:00
|
|
|
mSelState = new nsSelectionState();
|
2012-02-01 02:54:22 -08:00
|
|
|
mSelState->SaveSelection(selection);
|
2010-07-20 06:18:54 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
mPlaceHolderBatch++;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::EndPlaceHolderTransaction()
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(mPlaceHolderBatch > 0, "zero or negative placeholder batch count when ending batch!");
|
|
|
|
if (mPlaceHolderBatch == 1)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISelection>selection;
|
2009-01-06 11:25:58 -08:00
|
|
|
GetSelection(getter_AddRefs(selection));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsISelectionPrivate>selPrivate(do_QueryInterface(selection));
|
|
|
|
|
2012-02-01 02:54:22 -08:00
|
|
|
// By making the assumption that no reflow happens during the calls
|
|
|
|
// to EndUpdateViewBatch and ScrollSelectionIntoView, we are able to
|
|
|
|
// allow the selection to cache a frame offset which is used by the
|
|
|
|
// caret drawing code. We only enable this cache here; at other times,
|
|
|
|
// we have no way to know whether reflow invalidates it
|
|
|
|
// See bugs 35296 and 199412.
|
2007-03-22 10:30:00 -07:00
|
|
|
if (selPrivate) {
|
2011-10-17 07:59:28 -07:00
|
|
|
selPrivate->SetCanCacheFrameOffset(true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-02-28 07:28:37 -08:00
|
|
|
|
2010-07-21 13:08:52 -07:00
|
|
|
{
|
|
|
|
// Hide the caret here to avoid hiding it twice, once in EndUpdateViewBatch
|
|
|
|
// and once in ScrollSelectionIntoView.
|
|
|
|
nsRefPtr<nsCaret> caret;
|
2011-05-22 05:43:13 -07:00
|
|
|
nsCOMPtr<nsIPresShell> presShell = GetPresShell();
|
2010-07-21 13:08:52 -07:00
|
|
|
|
|
|
|
if (presShell)
|
|
|
|
caret = presShell->GetCaret();
|
|
|
|
|
|
|
|
// time to turn off the batch
|
|
|
|
EndUpdateViewBatch();
|
|
|
|
// make sure selection is in view
|
|
|
|
|
|
|
|
// After ScrollSelectionIntoView(), the pending notifications might be
|
|
|
|
// flushed and PresShell/PresContext/Frames may be dead. See bug 418470.
|
2011-10-17 07:59:28 -07:00
|
|
|
ScrollSelectionIntoView(false);
|
2010-07-21 13:08:52 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// cached for frame offset are Not available now
|
|
|
|
if (selPrivate) {
|
2011-10-17 07:59:28 -07:00
|
|
|
selPrivate->SetCanCacheFrameOffset(false);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mSelState)
|
|
|
|
{
|
|
|
|
// we saved the selection state, but never got to hand it to placeholder
|
|
|
|
// (else we ould have nulled out this pointer), so destroy it to prevent leaks.
|
|
|
|
delete mSelState;
|
2012-07-30 07:20:58 -07:00
|
|
|
mSelState = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
if (mPlaceHolderTxn) // we might have never made a placeholder if no action took place
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAbsorbingTransaction> plcTxn = do_QueryReferent(mPlaceHolderTxn);
|
|
|
|
if (plcTxn)
|
|
|
|
{
|
|
|
|
plcTxn->EndPlaceHolderBatch();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// in the future we will check to make sure undo is off here,
|
|
|
|
// since that is the only known case where the placeholdertxn would disappear on us.
|
|
|
|
// For now just removing the assert.
|
|
|
|
}
|
2011-12-31 06:04:37 -08:00
|
|
|
// notify editor observers of action but if composing, it's done by
|
|
|
|
// text event handler.
|
2014-02-12 05:02:55 -08:00
|
|
|
if (!mComposition) {
|
|
|
|
NotifyEditorObservers();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mPlaceHolderBatch--;
|
2012-11-14 11:23:35 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsEditor::ShouldTxnSetSelection(bool *aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(aResult, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
*aResult = mShouldTxnSetSelection;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsEditor::SetShouldTxnSetSelection(bool aShould)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mShouldTxnSetSelection = aShould;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsEditor::GetDocumentIsEmpty(bool *aDocumentIsEmpty)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
*aDocumentIsEmpty = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-01 02:54:22 -08:00
|
|
|
dom::Element* root = GetRoot();
|
|
|
|
NS_ENSURE_TRUE(root, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-01 02:54:22 -08:00
|
|
|
*aDocumentIsEmpty = !root->HasChildren();
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// XXX: the rule system should tell us which node to select all on (ie, the root, or the body)
|
|
|
|
NS_IMETHODIMP nsEditor::SelectAll()
|
|
|
|
{
|
2011-03-10 21:40:30 -08:00
|
|
|
if (!mDocWeak) { return NS_ERROR_NOT_INITIALIZED; }
|
2007-03-22 10:30:00 -07:00
|
|
|
ForceCompositionEnd();
|
|
|
|
|
2011-03-10 21:40:30 -08:00
|
|
|
nsCOMPtr<nsISelectionController> selCon;
|
|
|
|
GetSelectionController(getter_AddRefs(selCon));
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(selCon, NS_ERROR_NOT_INITIALIZED);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
nsresult result = selCon->GetSelection(nsISelectionController::SELECTION_NORMAL, getter_AddRefs(selection));
|
|
|
|
if (NS_SUCCEEDED(result) && selection)
|
|
|
|
{
|
|
|
|
result = SelectEntireDocument(selection);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsEditor::BeginningOfDocument()
|
|
|
|
{
|
2011-03-10 21:40:30 -08:00
|
|
|
if (!mDocWeak) { return NS_ERROR_NOT_INITIALIZED; }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// get the selection
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
nsresult result = GetSelection(getter_AddRefs(selection));
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NOT_INITIALIZED);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// get the root element
|
2012-02-01 02:54:22 -08:00
|
|
|
dom::Element* rootElement = GetRoot();
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(rootElement, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// find first editable thingy
|
2012-02-01 02:54:22 -08:00
|
|
|
nsCOMPtr<nsINode> firstNode = GetFirstEditableNode(rootElement);
|
|
|
|
if (!firstNode) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// just the root node, set selection to inside the root
|
2012-02-01 02:54:22 -08:00
|
|
|
return selection->CollapseNative(rootElement, 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-02-01 02:54:22 -08:00
|
|
|
|
|
|
|
if (firstNode->NodeType() == nsIDOMNode::TEXT_NODE) {
|
|
|
|
// If firstNode is text, set selection to beginning of the text node.
|
|
|
|
return selection->CollapseNative(firstNode, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, it's a leaf node and we set the selection just in front of it.
|
|
|
|
nsCOMPtr<nsIContent> parent = firstNode->GetParent();
|
|
|
|
if (!parent) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t offsetInParent = parent->IndexOf(firstNode);
|
2012-02-01 02:54:22 -08:00
|
|
|
return selection->CollapseNative(parent, offsetInParent);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-05-05 02:00:06 -07:00
|
|
|
nsEditor::EndOfDocument()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-05-05 02:00:06 -07:00
|
|
|
NS_ENSURE_TRUE(mDocWeak, NS_ERROR_NOT_INITIALIZED);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// get selection
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
2012-05-05 02:00:06 -07:00
|
|
|
nsresult rv = GetSelection(getter_AddRefs(selection));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// get the root element
|
2012-05-05 02:00:06 -07:00
|
|
|
nsINode* node = GetRoot();
|
2011-12-03 13:50:15 -08:00
|
|
|
NS_ENSURE_TRUE(node, NS_ERROR_NULL_POINTER);
|
2012-05-05 02:00:06 -07:00
|
|
|
nsINode* child = node->GetLastChild();
|
2010-11-11 13:40:55 -08:00
|
|
|
|
2012-05-05 02:00:06 -07:00
|
|
|
while (child && IsContainer(child->AsDOMNode())) {
|
|
|
|
node = child;
|
|
|
|
child = node->GetLastChild();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t length = node->Length();
|
|
|
|
return selection->CollapseNative(node, int32_t(length));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsEditor::GetDocumentModified(bool *outDocModified)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(outDocModified, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t modCount = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
GetModificationCount(&modCount);
|
|
|
|
|
|
|
|
*outDocModified = (modCount != 0);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::GetDocumentCharacterSet(nsACString &characterSet)
|
|
|
|
{
|
2011-03-10 21:40:30 -08:00
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocWeak);
|
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_UNEXPECTED);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-03-10 21:40:30 -08:00
|
|
|
characterSet = doc->GetDocumentCharacterSet();
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::SetDocumentCharacterSet(const nsACString& characterSet)
|
|
|
|
{
|
2011-03-10 21:40:30 -08:00
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocWeak);
|
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_UNEXPECTED);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-03-10 21:40:30 -08:00
|
|
|
doc->SetDocumentCharacterSet(characterSet);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::Cut()
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsEditor::CanCut(bool *aCanCut)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::Copy()
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsEditor::CanCopy(bool *aCanCut)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsEditor::Paste(int32_t aSelectionType)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2010-01-10 17:45:45 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::PasteTransferable(nsITransferable *aTransferable)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsEditor::CanPaste(int32_t aSelectionType, bool *aCanPaste)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2010-01-10 17:45:45 -08:00
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsEditor::CanPasteTransferable(nsITransferable *aTransferable, bool *aCanPaste)
|
2010-01-10 17:45:45 -08:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2013-01-09 15:05:08 -08:00
|
|
|
NS_IMETHODIMP
|
2007-03-22 10:30:00 -07:00
|
|
|
nsEditor::SetAttribute(nsIDOMElement *aElement, const nsAString & aAttribute, const nsAString & aValue)
|
|
|
|
{
|
2007-07-02 07:01:27 -07:00
|
|
|
nsRefPtr<ChangeAttributeTxn> txn;
|
2013-01-09 15:05:08 -08:00
|
|
|
nsCOMPtr<Element> element = do_QueryInterface(aElement);
|
|
|
|
nsresult result = CreateTxnForSetAttribute(element, aAttribute, aValue,
|
2007-07-02 07:01:27 -07:00
|
|
|
getter_AddRefs(txn));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
2013-01-09 15:05:08 -08:00
|
|
|
result = DoTransaction(txn);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::GetAttributeValue(nsIDOMElement *aElement,
|
|
|
|
const nsAString & aAttribute,
|
|
|
|
nsAString & aResultValue,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool *aResultIsSet)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aResultIsSet, NS_ERROR_NULL_POINTER);
|
2011-10-29 13:19:27 -07:00
|
|
|
*aResultIsSet = false;
|
|
|
|
if (!aElement) {
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-10-29 13:19:27 -07:00
|
|
|
nsAutoString value;
|
|
|
|
nsresult rv = aElement->GetAttribute(aAttribute, value);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!DOMStringIsNull(value)) {
|
|
|
|
*aResultIsSet = true;
|
|
|
|
aResultValue = value;
|
|
|
|
}
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-01-09 15:05:08 -08:00
|
|
|
NS_IMETHODIMP
|
2007-03-22 10:30:00 -07:00
|
|
|
nsEditor::RemoveAttribute(nsIDOMElement *aElement, const nsAString& aAttribute)
|
|
|
|
{
|
2007-07-02 07:01:27 -07:00
|
|
|
nsRefPtr<ChangeAttributeTxn> txn;
|
2013-01-09 15:05:08 -08:00
|
|
|
nsCOMPtr<Element> element = do_QueryInterface(aElement);
|
|
|
|
nsresult result = CreateTxnForRemoveAttribute(element, aAttribute,
|
2007-07-02 07:01:27 -07:00
|
|
|
getter_AddRefs(txn));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
2013-01-09 15:05:08 -08:00
|
|
|
result = DoTransaction(txn);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-23 23:06:11 -07:00
|
|
|
bool
|
|
|
|
nsEditor::OutputsMozDirty()
|
|
|
|
{
|
|
|
|
// Return true for Composer (!eEditorAllowInteraction) or mail
|
|
|
|
// (eEditorMailMask), but false for webpages.
|
|
|
|
return !(mFlags & nsIPlaintextEditor::eEditorAllowInteraction) ||
|
|
|
|
(mFlags & nsIPlaintextEditor::eEditorMailMask);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::MarkNodeDirty(nsIDOMNode* aNode)
|
2013-01-09 15:05:08 -08:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
return MarkNodeDirty(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsEditor::MarkNodeDirty(nsINode* aNode)
|
|
|
|
{
|
2012-04-23 23:06:11 -07:00
|
|
|
// Mark the node dirty, but not for webpages (bug 599983)
|
|
|
|
if (!OutputsMozDirty()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-01-09 15:05:08 -08:00
|
|
|
if (aNode->IsElement()) {
|
|
|
|
aNode->AsElement()->SetAttr(kNameSpaceID_None, nsEditProperty::mozdirty,
|
|
|
|
EmptyString(), false);
|
2012-04-23 23:06:11 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
NS_IMETHODIMP nsEditor::GetInlineSpellChecker(bool autoCreate,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIInlineSpellChecker ** aInlineSpellChecker)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aInlineSpellChecker);
|
|
|
|
|
|
|
|
if (mDidPreDestroy) {
|
|
|
|
// Don't allow people to get or create the spell checker once the editor
|
|
|
|
// is going away.
|
2012-07-30 07:20:58 -07:00
|
|
|
*aInlineSpellChecker = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
return autoCreate ? NS_ERROR_NOT_AVAILABLE : NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-13 11:58:54 -07:00
|
|
|
// We don't want to show the spell checking UI if there are no spell check dictionaries available.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool canSpell = mozInlineSpellChecker::CanEnableInlineSpellChecking();
|
2011-09-13 11:58:54 -07:00
|
|
|
if (!canSpell) {
|
2012-07-30 07:20:58 -07:00
|
|
|
*aInlineSpellChecker = nullptr;
|
2011-09-13 11:58:54 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2008-05-27 10:55:13 -07:00
|
|
|
nsresult rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!mInlineSpellChecker && autoCreate) {
|
|
|
|
mInlineSpellChecker = do_CreateInstance(MOZ_INLINESPELLCHECKER_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2008-05-27 10:55:13 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-05-27 10:55:13 -07:00
|
|
|
if (mInlineSpellChecker) {
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = mInlineSpellChecker->Init(this);
|
|
|
|
if (NS_FAILED(rv))
|
2012-07-30 07:20:58 -07:00
|
|
|
mInlineSpellChecker = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IF_ADDREF(*aInlineSpellChecker = mInlineSpellChecker);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-13 11:58:54 -07:00
|
|
|
NS_IMETHODIMP nsEditor::Observe(nsISupports* aSubj, const char *aTopic,
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t *aData)
|
2011-09-13 11:58:54 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!strcmp(aTopic,
|
|
|
|
SPELLCHECK_DICTIONARY_UPDATE_NOTIFICATION),
|
|
|
|
"Unexpected observer topic");
|
|
|
|
|
|
|
|
// When mozInlineSpellChecker::CanEnableInlineSpellChecking changes
|
|
|
|
SyncRealTimeSpell();
|
|
|
|
|
|
|
|
// When nsIEditorSpellCheck::GetCurrentDictionary changes
|
|
|
|
if (mInlineSpellChecker) {
|
|
|
|
// if the current dictionary is no longer available, find another one
|
|
|
|
nsCOMPtr<nsIEditorSpellCheck> editorSpellCheck;
|
|
|
|
mInlineSpellChecker->GetSpellChecker(getter_AddRefs(editorSpellCheck));
|
|
|
|
if (editorSpellCheck) {
|
|
|
|
// Note: This might change the current dictionary, which may call
|
|
|
|
// this observer recursively.
|
|
|
|
editorSpellCheck->CheckCurrentDictionary();
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the inline spell checker to reflect the new current dictionary
|
2012-07-30 07:20:58 -07:00
|
|
|
mInlineSpellChecker->SpellCheckRange(nullptr); // causes recheck
|
2011-09-13 11:58:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP nsEditor::SyncRealTimeSpell()
|
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool enable = GetDesiredSpellCheckState();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-13 11:58:54 -07:00
|
|
|
// Initializes mInlineSpellChecker
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIInlineSpellChecker> spellChecker;
|
|
|
|
GetInlineSpellChecker(enable, getter_AddRefs(spellChecker));
|
|
|
|
|
2011-09-13 11:58:54 -07:00
|
|
|
if (mInlineSpellChecker) {
|
|
|
|
// We might have a mInlineSpellChecker even if there are no dictionaries
|
|
|
|
// available since we don't destroy the mInlineSpellChecker when the last
|
|
|
|
// dictionariy is removed, but in that case spellChecker is null
|
|
|
|
mInlineSpellChecker->SetEnableRealTimeSpell(enable && spellChecker);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
NS_IMETHODIMP nsEditor::SetSpellcheckUserOverride(bool enable)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mSpellcheckCheckboxState = enable ? eTriTrue : eTriFalse;
|
|
|
|
|
|
|
|
return SyncRealTimeSpell();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsEditor::CreateNode(const nsAString& aTag,
|
|
|
|
nsIDOMNode * aParent,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aPosition,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIDOMNode ** aNewNode)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t i;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-12 11:28:26 -07:00
|
|
|
nsAutoRules beginRulesSniffing(this, EditAction::createNode, nsIEditor::eNext);
|
2013-01-09 15:05:08 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = 0; i < mActionListeners.Count(); i++)
|
|
|
|
mActionListeners[i]->WillCreateNode(aTag, aParent, aPosition);
|
|
|
|
|
2007-07-02 07:01:27 -07:00
|
|
|
nsRefPtr<CreateElementTxn> txn;
|
2013-01-09 15:05:08 -08:00
|
|
|
nsCOMPtr<nsINode> parent = do_QueryInterface(aParent);
|
|
|
|
nsresult result = CreateTxnForCreateElement(aTag, parent, aPosition,
|
2007-07-02 07:01:27 -07:00
|
|
|
getter_AddRefs(txn));
|
2013-01-09 15:05:08 -08:00
|
|
|
if (NS_SUCCEEDED(result))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-01-09 15:05:08 -08:00
|
|
|
result = DoTransaction(txn);
|
|
|
|
if (NS_SUCCEEDED(result))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-01-09 15:05:08 -08:00
|
|
|
nsCOMPtr<nsINode> newNode;
|
|
|
|
result = txn->GetNewNode(getter_AddRefs(newNode));
|
|
|
|
CallQueryInterface(newNode, aNewNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION((NS_SUCCEEDED(result)), "GetNewNode can't fail if txn::DoTransaction succeeded.");
|
|
|
|
}
|
|
|
|
}
|
2013-01-09 15:05:08 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
mRangeUpdater.SelAdjCreateNode(aParent, aPosition);
|
2013-01-09 15:05:08 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = 0; i < mActionListeners.Count(); i++)
|
|
|
|
mActionListeners[i]->DidCreateNode(aTag, *aNewNode, aParent, aPosition, result);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsEditor::InsertNode(nsIDOMNode * aNode,
|
|
|
|
nsIDOMNode * aParent,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aPosition)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t i;
|
2012-08-12 11:28:26 -07:00
|
|
|
nsAutoRules beginRulesSniffing(this, EditAction::insertNode, nsIEditor::eNext);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
for (i = 0; i < mActionListeners.Count(); i++)
|
|
|
|
mActionListeners[i]->WillInsertNode(aNode, aParent, aPosition);
|
|
|
|
|
2007-07-02 07:01:27 -07:00
|
|
|
nsRefPtr<InsertElementTxn> txn;
|
2013-01-09 15:05:08 -08:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
nsCOMPtr<nsINode> parent = do_QueryInterface(aParent);
|
|
|
|
nsresult result = CreateTxnForInsertElement(node, parent, aPosition,
|
2007-07-02 07:01:27 -07:00
|
|
|
getter_AddRefs(txn));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
2013-01-09 15:05:08 -08:00
|
|
|
result = DoTransaction(txn);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mRangeUpdater.SelAdjInsertNode(aParent, aPosition);
|
|
|
|
|
|
|
|
for (i = 0; i < mActionListeners.Count(); i++)
|
|
|
|
mActionListeners[i]->DidInsertNode(aNode, aParent, aPosition, result);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::SplitNode(nsIDOMNode * aNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aOffset,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIDOMNode **aNewLeftNode)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t i;
|
2012-08-12 11:28:26 -07:00
|
|
|
nsAutoRules beginRulesSniffing(this, EditAction::splitNode, nsIEditor::eNext);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
for (i = 0; i < mActionListeners.Count(); i++)
|
|
|
|
mActionListeners[i]->WillSplitNode(aNode, aOffset);
|
|
|
|
|
2007-07-02 07:01:27 -07:00
|
|
|
nsRefPtr<SplitElementTxn> txn;
|
2013-01-09 15:05:08 -08:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
nsresult result = CreateTxnForSplitNode(node, aOffset, getter_AddRefs(txn));
|
|
|
|
if (NS_SUCCEEDED(result))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
result = DoTransaction(txn);
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
2013-01-09 15:05:08 -08:00
|
|
|
nsCOMPtr<nsINode> leftNode;
|
|
|
|
result = txn->GetNewNode(getter_AddRefs(leftNode));
|
|
|
|
CallQueryInterface(leftNode, aNewLeftNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION((NS_SUCCEEDED(result)), "result must succeeded for GetNewNode");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mRangeUpdater.SelAdjSplitNode(aNode, aOffset, *aNewLeftNode);
|
|
|
|
|
|
|
|
for (i = 0; i < mActionListeners.Count(); i++)
|
|
|
|
{
|
|
|
|
nsIDOMNode *ptr = *aNewLeftNode;
|
|
|
|
mActionListeners[i]->DidSplitNode(aNode, aOffset, ptr, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-29 00:12:51 -07:00
|
|
|
nsresult
|
|
|
|
nsEditor::JoinNodes(nsINode* aNodeToKeep, nsIContent* aNodeToMove)
|
|
|
|
{
|
|
|
|
// We don't really need aNodeToMove's parent to be non-null -- we could just
|
|
|
|
// skip adjusting any ranges in aNodeToMove's parent if there is none. But
|
|
|
|
// the current implementation requires it.
|
2012-10-09 05:31:24 -07:00
|
|
|
MOZ_ASSERT(aNodeToKeep && aNodeToMove && aNodeToMove->GetParentNode());
|
2012-05-29 00:12:51 -07:00
|
|
|
nsresult res = JoinNodes(aNodeToKeep->AsDOMNode(), aNodeToMove->AsDOMNode(),
|
2012-10-09 05:31:24 -07:00
|
|
|
aNodeToMove->GetParentNode()->AsDOMNode());
|
2012-05-29 00:12:51 -07:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(res), "JoinNodes failed");
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::JoinNodes(nsIDOMNode * aLeftNode,
|
|
|
|
nsIDOMNode * aRightNode,
|
|
|
|
nsIDOMNode * aParent)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t i;
|
2012-08-12 11:28:26 -07:00
|
|
|
nsAutoRules beginRulesSniffing(this, EditAction::joinNode, nsIEditor::ePrevious);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// remember some values; later used for saved selection updating.
|
|
|
|
// find the offset between the nodes to be joined.
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t offset = GetChildOffset(aRightNode, aParent);
|
2007-03-22 10:30:00 -07:00
|
|
|
// find the number of children of the lefthand node
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t oldLeftNodeLen;
|
2012-07-02 04:42:19 -07:00
|
|
|
nsresult result = GetLengthOfDOMNode(aLeftNode, oldLeftNodeLen);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
for (i = 0; i < mActionListeners.Count(); i++)
|
|
|
|
mActionListeners[i]->WillJoinNodes(aLeftNode, aRightNode, aParent);
|
|
|
|
|
2007-07-02 07:01:27 -07:00
|
|
|
nsRefPtr<JoinElementTxn> txn;
|
2013-01-09 15:05:08 -08:00
|
|
|
nsCOMPtr<nsINode> leftNode = do_QueryInterface(aLeftNode);
|
|
|
|
nsCOMPtr<nsINode> rightNode = do_QueryInterface(aRightNode);
|
|
|
|
result = CreateTxnForJoinNode(leftNode, rightNode, getter_AddRefs(txn));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
2013-01-09 15:05:08 -08:00
|
|
|
result = DoTransaction(txn);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
mRangeUpdater.SelAdjJoinNodes(aLeftNode, aRightNode, aParent, offset, (int32_t)oldLeftNodeLen);
|
2013-01-09 15:05:08 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = 0; i < mActionListeners.Count(); i++)
|
|
|
|
mActionListeners[i]->DidJoinNodes(aLeftNode, aRightNode, aParent, result);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::DeleteNode(nsIDOMNode* aNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
NS_ENSURE_STATE(node);
|
|
|
|
return DeleteNode(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsEditor::DeleteNode(nsINode* aNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-12 11:28:26 -07:00
|
|
|
nsAutoRules beginRulesSniffing(this, EditAction::createNode, nsIEditor::ePrevious);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// save node location for selection updating code.
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0; i < mActionListeners.Count(); i++) {
|
2012-06-19 06:23:36 -07:00
|
|
|
mActionListeners[i]->WillDeleteNode(aNode->AsDOMNode());
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
nsRefPtr<DeleteNodeTxn> txn;
|
|
|
|
nsresult res = CreateTxnForDeleteNode(aNode, getter_AddRefs(txn));
|
2012-06-19 06:23:36 -07:00
|
|
|
if (NS_SUCCEEDED(res)) {
|
|
|
|
res = DoTransaction(txn);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0; i < mActionListeners.Count(); i++) {
|
2012-06-19 06:23:36 -07:00
|
|
|
mActionListeners[i]->DidDeleteNode(aNode->AsDOMNode(), res);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// ReplaceContainer: replace inNode with a new node (outNode) which is contructed
|
|
|
|
// to be of type aNodeType. Put inNodes children into outNode.
|
|
|
|
// Callers responsibility to make sure inNode's children can
|
|
|
|
// go in outNode.
|
|
|
|
nsresult
|
|
|
|
nsEditor::ReplaceContainer(nsIDOMNode *inNode,
|
|
|
|
nsCOMPtr<nsIDOMNode> *outNode,
|
|
|
|
const nsAString &aNodeType,
|
|
|
|
const nsAString *aAttribute,
|
|
|
|
const nsAString *aValue,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aCloneAttributes)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(inNode && outNode, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-06 00:36:33 -07:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(inNode);
|
|
|
|
NS_ENSURE_STATE(node);
|
|
|
|
|
|
|
|
nsCOMPtr<dom::Element> element;
|
|
|
|
nsresult rv = ReplaceContainer(node, getter_AddRefs(element), aNodeType,
|
|
|
|
aAttribute, aValue, aCloneAttributes);
|
2012-07-30 07:20:58 -07:00
|
|
|
*outNode = element ? element->AsDOMNode() : nullptr;
|
2012-06-06 00:36:33 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsEditor::ReplaceContainer(nsINode* aNode,
|
|
|
|
dom::Element** outNode,
|
|
|
|
const nsAString& aNodeType,
|
|
|
|
const nsAString* aAttribute,
|
|
|
|
const nsAString* aValue,
|
|
|
|
bool aCloneAttributes)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aNode);
|
|
|
|
MOZ_ASSERT(outNode);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
*outNode = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-06 00:36:33 -07:00
|
|
|
nsCOMPtr<nsIContent> parent = aNode->GetParent();
|
|
|
|
NS_ENSURE_STATE(parent);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t offset = parent->IndexOf(aNode);
|
2012-06-06 00:36:33 -07:00
|
|
|
|
|
|
|
// create new container
|
2007-03-22 10:30:00 -07:00
|
|
|
//new call to use instead to get proper HTML element, bug# 39919
|
2012-06-06 00:36:33 -07:00
|
|
|
nsresult res = CreateHTMLContent(aNodeType, outNode);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2012-06-06 00:36:33 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMElement> elem = do_QueryInterface(*outNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-06 00:36:33 -07:00
|
|
|
nsIDOMNode* inNode = aNode->AsDOMNode();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// set attribute if needed
|
2012-06-06 00:36:33 -07:00
|
|
|
if (aAttribute && aValue && !aAttribute->IsEmpty()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
res = elem->SetAttribute(*aAttribute, *aValue);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-06-06 00:36:33 -07:00
|
|
|
if (aCloneAttributes) {
|
|
|
|
res = CloneAttributes(elem, inNode);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// notify our internal selection state listener
|
|
|
|
// (Note: A nsAutoSelectionReset object must be created
|
|
|
|
// before calling this to initialize mRangeUpdater)
|
2012-06-06 00:36:33 -07:00
|
|
|
nsAutoReplaceContainerSelNotify selStateNotify(mRangeUpdater, inNode, elem);
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsAutoTxnsConserveSelection conserveSelection(this);
|
2012-06-06 00:36:33 -07:00
|
|
|
while (aNode->HasChildren()) {
|
|
|
|
nsCOMPtr<nsIDOMNode> child = aNode->GetFirstChild()->AsDOMNode();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
res = DeleteNode(child);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-06 00:36:33 -07:00
|
|
|
res = InsertNode(child, elem, -1);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2012-06-06 00:36:33 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// insert new container into tree
|
2012-06-06 00:36:33 -07:00
|
|
|
res = InsertNode(elem, parent->AsDOMNode(), offset);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// delete old container
|
|
|
|
return DeleteNode(inNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// RemoveContainer: remove inNode, reparenting its children into their
|
|
|
|
// the parent of inNode
|
|
|
|
//
|
2012-02-01 02:54:22 -08:00
|
|
|
nsresult
|
2012-06-06 00:35:54 -07:00
|
|
|
nsEditor::RemoveContainer(nsIDOMNode* aNode)
|
2012-02-01 02:54:22 -08:00
|
|
|
{
|
2012-06-06 00:35:54 -07:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
2012-02-01 02:54:22 -08:00
|
|
|
return RemoveContainer(node);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
2012-06-06 00:35:54 -07:00
|
|
|
nsEditor::RemoveContainer(nsINode* aNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-06-06 00:35:54 -07:00
|
|
|
NS_ENSURE_TRUE(aNode, NS_ERROR_NULL_POINTER);
|
|
|
|
|
2012-10-09 05:31:24 -07:00
|
|
|
nsCOMPtr<nsINode> parent = aNode->GetParentNode();
|
2012-06-06 00:35:54 -07:00
|
|
|
NS_ENSURE_STATE(parent);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t offset = parent->IndexOf(aNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// loop through the child nodes of inNode and promote them
|
|
|
|
// into inNode's parent.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t nodeOrigLen = aNode->GetChildCount();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// notify our internal selection state listener
|
2012-06-06 00:35:54 -07:00
|
|
|
nsAutoRemoveContainerSelNotify selNotify(mRangeUpdater, aNode, parent, offset, nodeOrigLen);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-06 00:35:54 -07:00
|
|
|
while (aNode->HasChildren()) {
|
2012-07-09 13:52:31 -07:00
|
|
|
nsCOMPtr<nsIContent> child = aNode->GetLastChild();
|
2012-06-06 00:35:54 -07:00
|
|
|
nsresult rv = DeleteNode(child->AsDOMNode());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = InsertNode(child->AsDOMNode(), parent->AsDOMNode(), offset);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-06-06 00:35:54 -07:00
|
|
|
|
|
|
|
return DeleteNode(aNode->AsDOMNode());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// InsertContainerAbove: insert a new parent for inNode, returned in outNode,
|
|
|
|
// which is contructed to be of type aNodeType. outNode becomes
|
|
|
|
// a child of inNode's earlier parent.
|
|
|
|
// Callers responsibility to make sure inNode's can be child
|
|
|
|
// of outNode, and outNode can be child of old parent.
|
|
|
|
nsresult
|
|
|
|
nsEditor::InsertContainerAbove( nsIDOMNode *inNode,
|
|
|
|
nsCOMPtr<nsIDOMNode> *outNode,
|
|
|
|
const nsAString &aNodeType,
|
|
|
|
const nsAString *aAttribute,
|
|
|
|
const nsAString *aValue)
|
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(inNode && outNode, NS_ERROR_NULL_POINTER);
|
2012-06-06 00:36:12 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> node = do_QueryInterface(inNode);
|
|
|
|
NS_ENSURE_STATE(node);
|
|
|
|
|
|
|
|
nsCOMPtr<dom::Element> element;
|
|
|
|
nsresult rv = InsertContainerAbove(node, getter_AddRefs(element), aNodeType,
|
|
|
|
aAttribute, aValue);
|
2012-07-30 07:20:58 -07:00
|
|
|
*outNode = element ? element->AsDOMNode() : nullptr;
|
2012-06-06 00:36:12 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsEditor::InsertContainerAbove(nsIContent* aNode,
|
|
|
|
dom::Element** aOutNode,
|
|
|
|
const nsAString& aNodeType,
|
|
|
|
const nsAString* aAttribute,
|
|
|
|
const nsAString* aValue)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aNode);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> parent = aNode->GetParent();
|
|
|
|
NS_ENSURE_STATE(parent);
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t offset = parent->IndexOf(aNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// create new container
|
2012-06-06 00:35:47 -07:00
|
|
|
nsCOMPtr<dom::Element> newContent;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//new call to use instead to get proper HTML element, bug# 39919
|
2012-06-06 00:36:12 -07:00
|
|
|
nsresult res = CreateHTMLContent(aNodeType, getter_AddRefs(newContent));
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2012-06-06 00:36:12 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// set attribute if needed
|
2012-06-06 00:36:12 -07:00
|
|
|
if (aAttribute && aValue && !aAttribute->IsEmpty()) {
|
|
|
|
nsIDOMNode* elem = newContent->AsDOMNode();
|
|
|
|
res = static_cast<nsIDOMElement*>(elem)->SetAttribute(*aAttribute, *aValue);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// notify our internal selection state listener
|
|
|
|
nsAutoInsertContainerSelNotify selNotify(mRangeUpdater);
|
|
|
|
|
|
|
|
// put inNode in new parent, outNode
|
2012-06-06 00:36:12 -07:00
|
|
|
res = DeleteNode(aNode->AsDOMNode());
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
nsAutoTxnsConserveSelection conserveSelection(this);
|
2012-06-06 00:36:12 -07:00
|
|
|
res = InsertNode(aNode->AsDOMNode(), newContent->AsDOMNode(), 0);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// put new parent in doc
|
2012-06-06 00:36:12 -07:00
|
|
|
res = InsertNode(newContent->AsDOMNode(), parent->AsDOMNode(), offset);
|
|
|
|
newContent.forget(aOutNode);
|
|
|
|
return res;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// MoveNode: move aNode to {aParent,aOffset}
|
2012-05-29 00:12:51 -07:00
|
|
|
nsresult
|
2013-07-10 03:03:38 -07:00
|
|
|
nsEditor::MoveNode(nsIDOMNode* aNode, nsIDOMNode* aParent, int32_t aOffset)
|
2012-05-29 00:12:51 -07:00
|
|
|
{
|
2013-07-10 03:03:38 -07:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
NS_ENSURE_STATE(node);
|
|
|
|
|
|
|
|
nsCOMPtr<nsINode> parent = do_QueryInterface(aParent);
|
|
|
|
NS_ENSURE_STATE(parent);
|
|
|
|
|
|
|
|
return MoveNode(node, parent, aOffset);
|
2012-05-29 00:12:51 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
2013-07-10 03:03:38 -07:00
|
|
|
nsEditor::MoveNode(nsINode* aNode, nsINode* aParent, int32_t aOffset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-07-10 03:03:38 -07:00
|
|
|
MOZ_ASSERT(aNode);
|
|
|
|
MOZ_ASSERT(aParent);
|
|
|
|
MOZ_ASSERT(aOffset == -1 ||
|
|
|
|
(0 <= aOffset && SafeCast<uint32_t>(aOffset) <= aParent->Length()));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t oldOffset;
|
2013-07-10 03:03:38 -07:00
|
|
|
nsCOMPtr<nsINode> oldParent = GetNodeLocation(aNode, &oldOffset);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-10 03:03:38 -07:00
|
|
|
if (aOffset == -1) {
|
|
|
|
// Magic value meaning "move to end of aParent".
|
|
|
|
aOffset = SafeCast<int32_t>(aParent->Length());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-07-10 03:03:38 -07:00
|
|
|
// Don't do anything if it's already in right place.
|
|
|
|
if (aParent == oldParent && aOffset == oldOffset) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-10 03:03:38 -07:00
|
|
|
// Notify our internal selection state listener.
|
|
|
|
nsAutoMoveNodeSelNotify selNotify(mRangeUpdater, oldParent, oldOffset,
|
|
|
|
aParent, aOffset);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-10 03:03:38 -07:00
|
|
|
// Need to adjust aOffset if we are moving aNode further along in its current
|
|
|
|
// parent.
|
|
|
|
if (aParent == oldParent && oldOffset < aOffset) {
|
|
|
|
// This is because when we delete aNode, it will make the offsets after it
|
|
|
|
// off by one.
|
|
|
|
aOffset--;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-07-10 03:03:38 -07:00
|
|
|
// Hold a reference so aNode doesn't go away when we remove it (bug 772282).
|
|
|
|
nsCOMPtr<nsINode> kungFuDeathGrip = aNode;
|
|
|
|
|
|
|
|
nsresult rv = DeleteNode(aNode);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return InsertNode(aNode->AsDOMNode(), aParent->AsDOMNode(), aOffset);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 11:23:35 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2012-11-14 11:23:35 -08:00
|
|
|
nsEditor::AddEditorObserver(nsIEditorObserver *aObserver)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-11-14 11:23:35 -08:00
|
|
|
// we don't keep ownership of the observers. They must
|
|
|
|
// remove themselves as observers before they are destroyed.
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aObserver, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-11-14 11:23:35 -08:00
|
|
|
// Make sure the listener isn't already on the list
|
|
|
|
if (mEditorObservers.IndexOf(aObserver) == -1)
|
|
|
|
{
|
|
|
|
if (!mEditorObservers.AppendObject(aObserver))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-11-14 11:23:35 -08:00
|
|
|
nsEditor::RemoveEditorObserver(nsIEditorObserver *aObserver)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-11-14 11:23:35 -08:00
|
|
|
NS_ENSURE_TRUE(aObserver, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
if (!mEditorObservers.RemoveObject(aObserver))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-26 18:36:44 -07:00
|
|
|
class EditorInputEventDispatcher : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
EditorInputEventDispatcher(nsEditor* aEditor,
|
|
|
|
nsIContent* aTarget) :
|
2013-01-11 17:49:29 -08:00
|
|
|
mEditor(aEditor), mTarget(aTarget)
|
2012-03-26 18:36:44 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
// Note that we don't need to check mDispatchInputEvent here. We need
|
|
|
|
// to check it only when the editor requests to dispatch the input event.
|
|
|
|
|
|
|
|
if (!mTarget->IsInDoc()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPresShell> ps = mEditor->GetPresShell();
|
|
|
|
if (!ps) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-11 17:49:29 -08:00
|
|
|
// Even if the change is caused by untrusted event, we need to dispatch
|
|
|
|
// trusted input event since it's a fact.
|
2013-10-01 20:46:04 -07:00
|
|
|
WidgetEvent inputEvent(true, NS_FORM_INPUT);
|
2012-12-15 17:26:03 -08:00
|
|
|
inputEvent.mFlags.mCancelable = false;
|
2012-08-22 08:56:38 -07:00
|
|
|
inputEvent.time = static_cast<uint64_t>(PR_Now() / 1000);
|
2012-03-26 18:36:44 -07:00
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
|
|
|
nsresult rv =
|
2012-07-30 07:20:58 -07:00
|
|
|
ps->HandleEventWithTarget(&inputEvent, nullptr, mTarget, &status);
|
2012-03-26 18:36:44 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_OK); // print the warning if error
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<nsEditor> mEditor;
|
|
|
|
nsCOMPtr<nsIContent> mTarget;
|
|
|
|
};
|
|
|
|
|
2012-11-14 11:23:35 -08:00
|
|
|
void nsEditor::NotifyEditorObservers(void)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-11-14 11:23:35 -08:00
|
|
|
for (int32_t i = 0; i < mEditorObservers.Count(); i++) {
|
|
|
|
mEditorObservers[i]->EditAction();
|
2012-03-26 18:36:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mDispatchInputEvent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-17 07:31:18 -08:00
|
|
|
FireInputEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsEditor::FireInputEvent()
|
|
|
|
{
|
2012-03-26 18:36:44 -07:00
|
|
|
// We don't need to dispatch multiple input events if there is a pending
|
|
|
|
// input event. However, it may have different event target. If we resolved
|
|
|
|
// this issue, we need to manage the pending events in an array. But it's
|
|
|
|
// overwork. We don't need to do it for the very rare case.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> target = GetInputEventTargetContent();
|
2012-09-14 03:00:31 -07:00
|
|
|
NS_ENSURE_TRUE_VOID(target);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-26 18:36:44 -07:00
|
|
|
nsContentUtils::AddScriptRunner(
|
2013-01-11 17:49:29 -08:00
|
|
|
new EditorInputEventDispatcher(this, target));
|
2012-03-26 18:36:44 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::AddEditActionListener(nsIEditActionListener *aListener)
|
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aListener, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Make sure the listener isn't already on the list
|
|
|
|
if (mActionListeners.IndexOf(aListener) == -1)
|
|
|
|
{
|
|
|
|
if (!mActionListeners.AppendObject(aListener))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::RemoveEditActionListener(nsIEditActionListener *aListener)
|
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aListener, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!mActionListeners.RemoveObject(aListener))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::AddDocumentStateListener(nsIDocumentStateListener *aListener)
|
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aListener, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (mDocStateListeners.IndexOf(aListener) == -1)
|
|
|
|
{
|
|
|
|
if (!mDocStateListeners.AppendObject(aListener))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::RemoveDocumentStateListener(nsIDocumentStateListener *aListener)
|
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aListener, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!mDocStateListeners.RemoveObject(aListener))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsEditor::OutputToString(const nsAString& aFormatType,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aFlags,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAString& aOutputString)
|
|
|
|
{
|
|
|
|
// these should be implemented by derived classes.
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::OutputToStream(nsIOutputStream* aOutputStream,
|
|
|
|
const nsAString& aFormatType,
|
|
|
|
const nsACString& aCharsetOverride,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aFlags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// these should be implemented by derived classes.
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::DumpContentTree()
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2012-06-10 05:39:21 -07:00
|
|
|
if (mRootElement) {
|
|
|
|
mRootElement->List(stdout);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::DebugDumpContent()
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsCOMPtr<nsIDOMHTMLDocument> doc = do_QueryReferent(mDocWeak);
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_NOT_INITIALIZED);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMHTMLElement>bodyElem;
|
|
|
|
doc->GetBody(getter_AddRefs(bodyElem));
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(bodyElem);
|
|
|
|
if (content)
|
|
|
|
content->List();
|
|
|
|
#endif
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsEditor::DebugUnitTests(int32_t *outNumTests, int32_t *outNumTestsFailed)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
NS_NOTREACHED("This should never get called. Overridden by subclasses");
|
|
|
|
#endif
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsEditor::ArePreservingSelection()
|
|
|
|
{
|
|
|
|
return !(mSavedSel.IsEmpty());
|
|
|
|
}
|
|
|
|
|
2012-06-01 04:03:17 -07:00
|
|
|
void
|
2012-07-23 03:27:22 -07:00
|
|
|
nsEditor::PreserveSelectionAcrossActions(Selection* aSel)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mSavedSel.SaveSelection(aSel);
|
|
|
|
mRangeUpdater.RegisterSelectionState(mSavedSel);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsEditor::RestorePreservedSelection(nsISelection *aSel)
|
|
|
|
{
|
|
|
|
if (mSavedSel.IsEmpty()) return NS_ERROR_FAILURE;
|
|
|
|
mSavedSel.RestoreSelection(aSel);
|
|
|
|
StopPreservingSelection();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsEditor::StopPreservingSelection()
|
|
|
|
{
|
|
|
|
mRangeUpdater.DropSelectionState(mSavedSel);
|
|
|
|
mSavedSel.MakeEmpty();
|
|
|
|
}
|
|
|
|
|
2014-02-12 05:02:55 -08:00
|
|
|
void
|
|
|
|
nsEditor::EnsureComposition(mozilla::WidgetGUIEvent* aEvent)
|
|
|
|
{
|
|
|
|
if (mComposition) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// The compositionstart event must cause creating new TextComposition
|
2014-03-07 17:20:07 -08:00
|
|
|
// instance at being dispatched by IMEStateManager.
|
|
|
|
mComposition = IMEStateManager::GetTextCompositionFor(aEvent);
|
2014-02-12 05:02:55 -08:00
|
|
|
if (!mComposition) {
|
2014-03-07 17:20:07 -08:00
|
|
|
MOZ_CRASH("IMEStateManager doesn't return proper composition");
|
2014-02-12 05:02:55 -08:00
|
|
|
}
|
2014-02-12 05:02:56 -08:00
|
|
|
mComposition->StartHandlingComposition(this);
|
2014-02-12 05:02:55 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-29 21:05:12 -07:00
|
|
|
nsresult
|
2014-02-12 05:02:55 -08:00
|
|
|
nsEditor::BeginIMEComposition(WidgetCompositionEvent* aCompositionEvent)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-02-12 05:02:55 -08:00
|
|
|
MOZ_ASSERT(!mComposition, "There is composition already");
|
|
|
|
EnsureComposition(aCompositionEvent);
|
2010-06-29 21:05:12 -07:00
|
|
|
if (mPhonetic) {
|
2007-03-22 10:30:00 -07:00
|
|
|
mPhonetic->Truncate(0);
|
2010-06-29 21:05:12 -07:00
|
|
|
}
|
2010-04-10 02:24:35 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
void
|
2010-06-29 21:05:12 -07:00
|
|
|
nsEditor::EndIMEComposition()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-02-12 05:02:55 -08:00
|
|
|
NS_ENSURE_TRUE_VOID(mComposition); // nothing to do
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// commit the IME transaction..we can get at it via the transaction mgr.
|
|
|
|
// Note that this means IME won't work without an undo stack!
|
2010-06-29 21:05:12 -07:00
|
|
|
if (mTxnMgr) {
|
2012-06-19 06:23:36 -07:00
|
|
|
nsCOMPtr<nsITransaction> txn = mTxnMgr->PeekUndoStack();
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIAbsorbingTransaction> plcTxn = do_QueryInterface(txn);
|
2010-06-29 21:05:12 -07:00
|
|
|
if (plcTxn) {
|
2012-06-19 06:23:36 -07:00
|
|
|
DebugOnly<nsresult> rv = plcTxn->Commit();
|
2010-06-29 21:05:12 -07:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"nsIAbsorbingTransaction::Commit() failed");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reset the data we need to construct a transaction */
|
2012-07-30 07:20:58 -07:00
|
|
|
mIMETextNode = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
mIMETextOffset = 0;
|
2014-02-12 05:02:56 -08:00
|
|
|
mComposition->EndHandlingComposition(this);
|
2014-02-12 05:02:55 -08:00
|
|
|
mComposition = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// notify editor observers of action
|
|
|
|
NotifyEditorObservers();
|
|
|
|
}
|
|
|
|
|
2010-06-29 21:05:12 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::GetPhonetic(nsAString& aPhonetic)
|
|
|
|
{
|
|
|
|
if (mPhonetic)
|
|
|
|
aPhonetic = *mPhonetic;
|
|
|
|
else
|
|
|
|
aPhonetic.Truncate(0);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::ForceCompositionEnd()
|
|
|
|
{
|
2012-09-25 22:47:51 -07:00
|
|
|
nsCOMPtr<nsIPresShell> ps = GetPresShell();
|
|
|
|
if (!ps) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
nsPresContext* pc = ps->GetPresContext();
|
|
|
|
if (!pc) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-02-12 05:02:55 -08:00
|
|
|
if (!mComposition) {
|
2012-09-25 22:47:51 -07:00
|
|
|
// XXXmnakano see bug 558976, ResetInputState() has two meaning which are
|
|
|
|
// "commit the composition" and "cursor is moved". This method name is
|
|
|
|
// "ForceCompositionEnd", so, ResetInputState() should be used only for the
|
|
|
|
// former here. However, ResetInputState() is also used for the latter here
|
|
|
|
// because even if we don't have composition, we call ResetInputState() on
|
|
|
|
// Linux. Currently, nsGtkIMModule can know the timing of the cursor move,
|
|
|
|
// so, the latter meaning should be gone.
|
|
|
|
// XXX This may commit a composition in another editor.
|
2014-03-07 17:20:07 -08:00
|
|
|
return IMEStateManager::NotifyIME(NOTIFY_IME_OF_CURSOR_POS_CHANGED, pc);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-03-07 17:20:07 -08:00
|
|
|
return IMEStateManager::NotifyIME(REQUEST_TO_COMMIT_COMPOSITION, pc);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-11-27 03:51:53 -08:00
|
|
|
nsEditor::GetPreferredIMEState(IMEState *aState)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aState);
|
2011-11-27 03:51:53 -08:00
|
|
|
aState->mEnabled = IMEState::ENABLED;
|
|
|
|
aState->mOpen = IMEState::DONT_CHANGE_OPEN_STATE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-11 19:35:18 -07:00
|
|
|
if (IsReadonly() || IsDisabled()) {
|
2011-11-27 03:51:53 -08:00
|
|
|
aState->mEnabled = IMEState::DISABLED;
|
2007-05-16 08:51:37 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-10 05:39:21 -07:00
|
|
|
nsCOMPtr<nsIContent> content = GetRoot();
|
2007-05-16 08:51:37 -07:00
|
|
|
NS_ENSURE_TRUE(content, NS_ERROR_FAILURE);
|
|
|
|
|
2009-12-24 13:20:06 -08:00
|
|
|
nsIFrame* frame = content->GetPrimaryFrame();
|
2007-05-16 08:51:37 -07:00
|
|
|
NS_ENSURE_TRUE(frame, NS_ERROR_FAILURE);
|
|
|
|
|
2013-02-16 13:51:02 -08:00
|
|
|
switch (frame->StyleUIReset()->mIMEMode) {
|
2007-05-16 08:51:37 -07:00
|
|
|
case NS_STYLE_IME_MODE_AUTO:
|
2010-04-11 19:35:18 -07:00
|
|
|
if (IsPasswordEditor())
|
2011-11-27 03:51:53 -08:00
|
|
|
aState->mEnabled = IMEState::PASSWORD;
|
2007-05-16 08:51:37 -07:00
|
|
|
break;
|
|
|
|
case NS_STYLE_IME_MODE_DISABLED:
|
|
|
|
// we should use password state for |ime-mode: disabled;|.
|
2011-11-27 03:51:53 -08:00
|
|
|
aState->mEnabled = IMEState::PASSWORD;
|
2007-05-16 08:51:37 -07:00
|
|
|
break;
|
|
|
|
case NS_STYLE_IME_MODE_ACTIVE:
|
2011-11-27 03:51:53 -08:00
|
|
|
aState->mOpen = IMEState::OPEN;
|
2007-05-16 08:51:37 -07:00
|
|
|
break;
|
|
|
|
case NS_STYLE_IME_MODE_INACTIVE:
|
2011-11-27 03:51:53 -08:00
|
|
|
aState->mOpen = IMEState::CLOSED;
|
2007-05-16 08:51:37 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-02-19 23:18:53 -08:00
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsEditor::GetComposing(bool* aResult)
|
2008-02-19 23:18:53 -08:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
|
|
|
*aResult = IsIMEComposing();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-05-10 11:24:48 -07:00
|
|
|
/* Non-interface, public methods */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-16 22:29:40 -07:00
|
|
|
NS_IMETHODIMP
|
2007-03-22 10:30:00 -07:00
|
|
|
nsEditor::GetRootElement(nsIDOMElement **aRootElement)
|
|
|
|
{
|
2010-06-16 22:29:40 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aRootElement);
|
|
|
|
NS_ENSURE_TRUE(mRootElement, NS_ERROR_NOT_AVAILABLE);
|
2011-12-03 13:50:15 -08:00
|
|
|
nsCOMPtr<nsIDOMElement> rootElement = do_QueryInterface(mRootElement);
|
|
|
|
rootElement.forget(aRootElement);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** All editor operations which alter the doc should be prefaced
|
|
|
|
* with a call to StartOperation, naming the action and direction */
|
|
|
|
NS_IMETHODIMP
|
2012-08-12 11:28:26 -07:00
|
|
|
nsEditor::StartOperation(EditAction opID, nsIEditor::EDirection aDirection)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mAction = opID;
|
|
|
|
mDirection = aDirection;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** All editor operations which alter the doc should be followed
|
|
|
|
* with a call to EndOperation */
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::EndOperation()
|
|
|
|
{
|
2012-08-12 11:28:26 -07:00
|
|
|
mAction = EditAction::none;
|
2007-03-22 10:30:00 -07:00
|
|
|
mDirection = eNone;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::CloneAttribute(const nsAString & aAttribute,
|
|
|
|
nsIDOMNode *aDestNode, nsIDOMNode *aSourceNode)
|
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aDestNode && aSourceNode, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMElement> destElement = do_QueryInterface(aDestNode);
|
|
|
|
nsCOMPtr<nsIDOMElement> sourceElement = do_QueryInterface(aSourceNode);
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(destElement && sourceElement, NS_ERROR_NO_INTERFACE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsAutoString attrValue;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isAttrSet;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = GetAttributeValue(sourceElement,
|
|
|
|
aAttribute,
|
|
|
|
attrValue,
|
|
|
|
&isAttrSet);
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (isAttrSet)
|
|
|
|
rv = SetAttribute(destElement, aAttribute, attrValue);
|
|
|
|
else
|
|
|
|
rv = RemoveAttribute(destElement, aAttribute);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Objects must be DOM elements
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::CloneAttributes(nsIDOMNode *aDestNode, nsIDOMNode *aSourceNode)
|
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aDestNode && aSourceNode, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMElement> destElement = do_QueryInterface(aDestNode);
|
|
|
|
nsCOMPtr<nsIDOMElement> sourceElement = do_QueryInterface(aSourceNode);
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(destElement && sourceElement, NS_ERROR_NO_INTERFACE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-03-10 00:00:33 -08:00
|
|
|
nsCOMPtr<nsIDOMMozNamedAttrMap> sourceAttributes;
|
2007-03-22 10:30:00 -07:00
|
|
|
sourceElement->GetAttributes(getter_AddRefs(sourceAttributes));
|
2013-03-10 00:00:33 -08:00
|
|
|
nsCOMPtr<nsIDOMMozNamedAttrMap> destAttributes;
|
2007-03-22 10:30:00 -07:00
|
|
|
destElement->GetAttributes(getter_AddRefs(destAttributes));
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(sourceAttributes && destAttributes, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsAutoEditBatch beginBatching(this);
|
|
|
|
|
|
|
|
// Use transaction system for undo only if destination
|
|
|
|
// is already in the document
|
|
|
|
nsCOMPtr<nsIDOMNode> p = aDestNode;
|
2011-12-03 13:50:15 -08:00
|
|
|
nsCOMPtr<nsIDOMNode> rootNode = do_QueryInterface(GetRoot());
|
|
|
|
NS_ENSURE_TRUE(rootNode, NS_ERROR_NULL_POINTER);
|
|
|
|
bool destInBody = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
while (p && p != rootNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
|
|
|
if (NS_FAILED(p->GetParentNode(getter_AddRefs(tmp))) || !tmp)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
destInBody = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
p = tmp;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t sourceCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
sourceAttributes->GetLength(&sourceCount);
|
2013-03-10 00:00:33 -08:00
|
|
|
uint32_t destCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
destAttributes->GetLength(&destCount);
|
2013-03-10 00:00:33 -08:00
|
|
|
nsCOMPtr<nsIDOMAttr> attr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Clear existing attributes
|
2013-03-10 00:00:33 -08:00
|
|
|
for (uint32_t i = 0; i < destCount; i++) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// always remove item number 0 (first item in list)
|
2013-03-10 00:00:33 -08:00
|
|
|
if (NS_SUCCEEDED(destAttributes->Item(0, getter_AddRefs(attr))) && attr) {
|
|
|
|
nsString str;
|
|
|
|
if (NS_SUCCEEDED(attr->GetName(str))) {
|
|
|
|
if (destInBody) {
|
|
|
|
RemoveAttribute(destElement, str);
|
|
|
|
} else {
|
|
|
|
destElement->RemoveAttribute(str);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
// Set just the attributes that the source element has
|
2013-03-10 00:00:33 -08:00
|
|
|
for (uint32_t i = 0; i < sourceCount; i++)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-03-10 00:00:33 -08:00
|
|
|
if (NS_SUCCEEDED(sourceAttributes->Item(i, getter_AddRefs(attr))) && attr) {
|
|
|
|
nsString sourceAttrName;
|
|
|
|
if (NS_SUCCEEDED(attr->GetName(sourceAttrName))) {
|
|
|
|
nsString sourceAttrValue;
|
|
|
|
/* Presence of an attribute in the named node map indicates that it was
|
|
|
|
* set on the element even if it has no value.
|
|
|
|
*/
|
|
|
|
if (NS_SUCCEEDED(attr->GetValue(sourceAttrValue))) {
|
|
|
|
if (destInBody) {
|
|
|
|
result = SetAttributeOrEquivalent(destElement, sourceAttrName, sourceAttrValue, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2013-03-10 00:00:33 -08:00
|
|
|
// the element is not inserted in the document yet, we don't want to put a
|
|
|
|
// transaction on the UndoStack
|
|
|
|
result = SetAttributeOrEquivalent(destElement, sourceAttrName, sourceAttrValue, true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Do we ever get here?
|
2007-03-22 10:30:00 -07:00
|
|
|
#if DEBUG_cmanske
|
2013-03-10 00:00:33 -08:00
|
|
|
printf("Attribute in sourceAttribute has empty value in nsEditor::CloneAttributes()\n");
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2013-03-10 00:00:33 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
NS_IMETHODIMP nsEditor::ScrollSelectionIntoView(bool aScrollToAnchor)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsISelectionController> selCon;
|
|
|
|
if (NS_SUCCEEDED(GetSelectionController(getter_AddRefs(selCon))) && selCon)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int16_t region = nsISelectionController::SELECTION_FOCUS_REGION;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (aScrollToAnchor)
|
|
|
|
region = nsISelectionController::SELECTION_ANCHOR_REGION;
|
|
|
|
|
|
|
|
selCon->ScrollSelectionIntoView(nsISelectionController::SELECTION_NORMAL,
|
2012-10-07 11:45:51 -07:00
|
|
|
region, nsISelectionController::SCROLL_OVERFLOW_HIDDEN);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-04 05:02:24 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::InsertTextImpl(const nsAString& aStringToInsert,
|
|
|
|
nsCOMPtr<nsIDOMNode>* aInOutNode,
|
|
|
|
int32_t* aInOutOffset,
|
|
|
|
nsIDOMDocument* aDoc)
|
|
|
|
{
|
|
|
|
// NOTE: caller *must* have already used nsAutoTxnsConserveSelection
|
|
|
|
// stack-based class to turn off txn selection updating. Caller also turned
|
|
|
|
// on rules sniffing if desired.
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(aInOutNode && *aInOutNode && aInOutOffset && aDoc,
|
|
|
|
NS_ERROR_NULL_POINTER);
|
2014-02-12 05:02:55 -08:00
|
|
|
if (!mComposition && aStringToInsert.IsEmpty()) {
|
2013-04-04 05:02:24 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(*aInOutNode);
|
|
|
|
NS_ENSURE_STATE(node);
|
|
|
|
uint32_t offset = static_cast<uint32_t>(*aInOutOffset);
|
|
|
|
|
|
|
|
if (!node->IsNodeOfType(nsINode::eTEXT) && IsPlaintextEditor()) {
|
|
|
|
nsCOMPtr<nsINode> root = GetRoot();
|
|
|
|
// In some cases, node is the anonymous DIV, and offset is 0. To avoid
|
|
|
|
// injecting unneeded text nodes, we first look to see if we have one
|
|
|
|
// available. In that case, we'll just adjust node and offset accordingly.
|
|
|
|
if (node == root && offset == 0 && node->HasChildren() &&
|
|
|
|
node->GetFirstChild()->IsNodeOfType(nsINode::eTEXT)) {
|
|
|
|
node = node->GetFirstChild();
|
2010-07-19 13:47:52 -07:00
|
|
|
}
|
2013-04-04 05:02:24 -07:00
|
|
|
// In some other cases, node is the anonymous DIV, and offset points to the
|
|
|
|
// terminating mozBR. In that case, we'll adjust aInOutNode and
|
|
|
|
// aInOutOffset to the preceding text node, if any.
|
|
|
|
if (node == root && offset > 0 && node->GetChildAt(offset - 1) &&
|
|
|
|
node->GetChildAt(offset - 1)->IsNodeOfType(nsINode::eTEXT)) {
|
|
|
|
node = node->GetChildAt(offset - 1);
|
|
|
|
offset = node->Length();
|
2010-07-19 13:47:52 -07:00
|
|
|
}
|
2013-04-04 05:02:24 -07:00
|
|
|
// Sometimes, node is the mozBR element itself. In that case, we'll adjust
|
|
|
|
// the insertion point to the previous text node, if one exists, or to the
|
|
|
|
// parent anonymous DIV.
|
|
|
|
if (nsTextEditUtils::IsMozBR(node) && offset == 0) {
|
|
|
|
if (node->GetPreviousSibling() &&
|
|
|
|
node->GetPreviousSibling()->IsNodeOfType(nsINode::eTEXT)) {
|
|
|
|
node = node->GetPreviousSibling();
|
|
|
|
offset = node->Length();
|
|
|
|
} else if (node->GetParentNode() && node->GetParentNode() == root) {
|
|
|
|
node = node->GetParentNode();
|
2010-08-13 15:58:24 -07:00
|
|
|
}
|
|
|
|
}
|
2010-07-19 13:47:52 -07:00
|
|
|
}
|
2013-04-04 05:02:24 -07:00
|
|
|
|
|
|
|
nsresult res;
|
2014-02-12 05:02:55 -08:00
|
|
|
if (mComposition) {
|
2013-04-04 05:02:24 -07:00
|
|
|
if (!node->IsNodeOfType(nsINode::eTEXT)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// create a text node
|
2013-04-04 05:02:24 -07:00
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryInterface(aDoc);
|
|
|
|
NS_ENSURE_STATE(doc);
|
|
|
|
nsRefPtr<nsTextNode> newNode = doc->CreateTextNode(EmptyString());
|
2007-03-22 10:30:00 -07:00
|
|
|
// then we insert it into the dom tree
|
2013-04-04 05:02:24 -07:00
|
|
|
res = InsertNode(newNode->AsDOMNode(), node->AsDOMNode(), offset);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2013-04-04 05:02:24 -07:00
|
|
|
node = newNode;
|
2007-03-22 10:30:00 -07:00
|
|
|
offset = 0;
|
|
|
|
}
|
2013-04-04 05:02:24 -07:00
|
|
|
nsCOMPtr<nsIDOMCharacterData> charDataNode = do_QueryInterface(node);
|
|
|
|
NS_ENSURE_STATE(charDataNode);
|
|
|
|
res = InsertTextIntoTextNodeImpl(aStringToInsert, charDataNode, offset);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2013-04-04 05:02:24 -07:00
|
|
|
offset += aStringToInsert.Length();
|
|
|
|
} else {
|
|
|
|
if (node->IsNodeOfType(nsINode::eTEXT)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// we are inserting text into an existing text node.
|
2013-04-04 05:02:24 -07:00
|
|
|
nsCOMPtr<nsIDOMCharacterData> charDataNode = do_QueryInterface(node);
|
|
|
|
NS_ENSURE_STATE(charDataNode);
|
|
|
|
res = InsertTextIntoTextNodeImpl(aStringToInsert, charDataNode, offset);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2013-04-04 05:02:24 -07:00
|
|
|
offset += aStringToInsert.Length();
|
|
|
|
} else {
|
|
|
|
// we are inserting text into a non-text node. first we have to create a
|
|
|
|
// textnode (this also populates it with the text)
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryInterface(aDoc);
|
|
|
|
NS_ENSURE_STATE(doc);
|
|
|
|
nsRefPtr<nsTextNode> newNode = doc->CreateTextNode(aStringToInsert);
|
2007-03-22 10:30:00 -07:00
|
|
|
// then we insert it into the dom tree
|
2013-04-04 05:02:24 -07:00
|
|
|
res = InsertNode(newNode->AsDOMNode(), node->AsDOMNode(), offset);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2013-04-04 05:02:24 -07:00
|
|
|
node = newNode;
|
|
|
|
offset = aStringToInsert.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2013-04-04 05:02:24 -07:00
|
|
|
|
|
|
|
*aInOutNode = node->AsDOMNode();
|
|
|
|
*aInOutOffset = static_cast<int32_t>(offset);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-12 18:35:50 -08:00
|
|
|
nsresult nsEditor::InsertTextIntoTextNodeImpl(const nsAString& aStringToInsert,
|
|
|
|
nsIDOMCharacterData *aTextNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aOffset,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aSuppressIME)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-07-02 07:01:27 -07:00
|
|
|
nsRefPtr<EditTxn> txn;
|
2008-10-09 16:30:48 -07:00
|
|
|
nsresult result = NS_OK;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isIMETransaction = false;
|
2010-01-12 18:35:50 -08:00
|
|
|
// aSuppressIME is used when editor must insert text, yet this text is not
|
2007-03-22 10:30:00 -07:00
|
|
|
// part of current ime operation. example: adjusting whitespace around an ime insertion.
|
2014-03-04 05:48:26 -08:00
|
|
|
if (mComposition && !aSuppressIME) {
|
|
|
|
if (!mIMETextNode) {
|
2007-03-22 10:30:00 -07:00
|
|
|
mIMETextNode = aTextNode;
|
|
|
|
mIMETextOffset = aOffset;
|
|
|
|
}
|
2014-03-04 05:48:26 -08:00
|
|
|
// Modify mPhonetic with raw text input clauses.
|
|
|
|
const TextRangeArray* ranges = mComposition->GetRanges();
|
|
|
|
for (uint32_t i = 0; i < (ranges ? ranges->Length() : 0); ++i) {
|
|
|
|
const TextRange& textRange = ranges->ElementAt(i);
|
|
|
|
if (!textRange.Length() ||
|
|
|
|
textRange.mRangeType != NS_TEXTRANGE_RAWINPUT) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!mPhonetic) {
|
|
|
|
mPhonetic = new nsString();
|
|
|
|
}
|
|
|
|
nsAutoString stringToInsert(aStringToInsert);
|
|
|
|
stringToInsert.Mid(*mPhonetic,
|
|
|
|
textRange.mStartOffset, textRange.Length());
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-02 07:01:27 -07:00
|
|
|
nsRefPtr<IMETextTxn> imeTxn;
|
|
|
|
result = CreateTxnForIMEText(aStringToInsert, getter_AddRefs(imeTxn));
|
|
|
|
txn = imeTxn;
|
2011-10-17 07:59:28 -07:00
|
|
|
isIMETransaction = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-07-02 07:01:27 -07:00
|
|
|
nsRefPtr<InsertTextTxn> insertTxn;
|
|
|
|
result = CreateTxnForInsertText(aStringToInsert, aTextNode, aOffset,
|
|
|
|
getter_AddRefs(insertTxn));
|
|
|
|
txn = insertTxn;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// let listeners know what's up
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t i;
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = 0; i < mActionListeners.Count(); i++)
|
|
|
|
mActionListeners[i]->WillInsertText(aTextNode, aOffset, aStringToInsert);
|
|
|
|
|
|
|
|
// XXX we may not need these view batches anymore. This is handled at a higher level now I believe
|
|
|
|
BeginUpdateViewBatch();
|
|
|
|
result = DoTransaction(txn);
|
|
|
|
EndUpdateViewBatch();
|
|
|
|
|
|
|
|
mRangeUpdater.SelAdjInsertText(aTextNode, aOffset, aStringToInsert);
|
|
|
|
|
|
|
|
// let listeners know what happened
|
|
|
|
for (i = 0; i < mActionListeners.Count(); i++)
|
|
|
|
mActionListeners[i]->DidInsertText(aTextNode, aOffset, aStringToInsert, result);
|
|
|
|
|
|
|
|
// Added some cruft here for bug 43366. Layout was crashing because we left an
|
|
|
|
// empty text node lying around in the document. So I delete empty text nodes
|
|
|
|
// caused by IME. I have to mark the IME transaction as "fixed", which means
|
|
|
|
// that furure ime txns won't merge with it. This is because we don't want
|
|
|
|
// future ime txns trying to put their text into a node that is no longer in
|
|
|
|
// the document. This does not break undo/redo, because all these txns are
|
|
|
|
// wrapped in a parent PlaceHolder txn, and placeholder txns are already
|
|
|
|
// savvy to having multiple ime txns inside them.
|
|
|
|
|
|
|
|
// delete empty ime text node if there is one
|
2011-01-09 17:14:58 -08:00
|
|
|
if (isIMETransaction && mIMETextNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t len;
|
2007-03-22 10:30:00 -07:00
|
|
|
mIMETextNode->GetLength(&len);
|
|
|
|
if (!len)
|
|
|
|
{
|
|
|
|
DeleteNode(mIMETextNode);
|
2012-07-30 07:20:58 -07:00
|
|
|
mIMETextNode = nullptr;
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<IMETextTxn*>(txn.get())->MarkFixed(); // mark the ime txn "fixed"
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsEditor::SelectEntireDocument(nsISelection *aSelection)
|
|
|
|
{
|
|
|
|
if (!aSelection) { return NS_ERROR_NULL_POINTER; }
|
|
|
|
|
2011-12-03 13:50:15 -08:00
|
|
|
nsCOMPtr<nsIDOMElement> rootElement = do_QueryInterface(GetRoot());
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!rootElement) { return NS_ERROR_NOT_INITIALIZED; }
|
|
|
|
|
|
|
|
return aSelection->SelectAllChildren(rootElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-01 02:54:22 -08:00
|
|
|
nsINode*
|
|
|
|
nsEditor::GetFirstEditableNode(nsINode* aRoot)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-02-01 02:54:22 -08:00
|
|
|
MOZ_ASSERT(aRoot);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-01 02:54:22 -08:00
|
|
|
nsIContent* node = GetLeftmostChild(aRoot);
|
|
|
|
if (node && !IsEditable(node)) {
|
|
|
|
node = GetNextNode(node, /* aEditableNode = */ true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return (node != aRoot) ? node : nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::NotifyDocumentListeners(TDocumentListenerNotification aNotificationType)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t numListeners = mDocStateListeners.Count();
|
2010-06-21 08:02:14 -07:00
|
|
|
if (!numListeners) // maybe there just aren't any.
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-07-08 11:38:13 -07:00
|
|
|
nsCOMArray<nsIDocumentStateListener> listeners(mDocStateListeners);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = NS_OK;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t i;
|
2009-07-08 11:38:13 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
switch (aNotificationType)
|
|
|
|
{
|
|
|
|
case eDocumentCreated:
|
|
|
|
for (i = 0; i < numListeners;i++)
|
|
|
|
{
|
2009-07-08 11:38:13 -07:00
|
|
|
rv = listeners[i]->NotifyDocumentCreated();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case eDocumentToBeDestroyed:
|
|
|
|
for (i = 0; i < numListeners;i++)
|
|
|
|
{
|
2009-07-08 11:38:13 -07:00
|
|
|
rv = listeners[i]->NotifyDocumentWillBeDestroyed();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2013-01-06 11:41:46 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
case eDocumentStateChanged:
|
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool docIsDirty;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = GetDocumentModified(&docIsDirty);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-01-06 11:41:46 -08:00
|
|
|
|
|
|
|
if (static_cast<int8_t>(docIsDirty) == mDocDirtyState)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
2013-01-06 11:41:46 -08:00
|
|
|
|
|
|
|
mDocDirtyState = docIsDirty;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = 0; i < numListeners;i++)
|
|
|
|
{
|
2009-07-08 11:38:13 -07:00
|
|
|
rv = listeners[i]->NotifyDocumentStateChanged(mDocDirtyState);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Unknown notification");
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsEditor::CreateTxnForInsertText(const nsAString & aStringToInsert,
|
|
|
|
nsIDOMCharacterData *aTextNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aOffset,
|
2007-03-22 10:30:00 -07:00
|
|
|
InsertTextTxn ** aTxn)
|
|
|
|
{
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(aTextNode && aTxn, NS_ERROR_NULL_POINTER);
|
2011-02-19 14:18:37 -08:00
|
|
|
nsresult rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-02-19 14:18:37 -08:00
|
|
|
nsRefPtr<InsertTextTxn> txn = new InsertTextTxn();
|
|
|
|
rv = txn->Init(aTextNode, aOffset, aStringToInsert, this);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
2011-02-23 14:22:07 -08:00
|
|
|
txn.forget(aTxn);
|
2011-02-19 14:18:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsEditor::DeleteText(nsIDOMCharacterData *aElement,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aOffset,
|
|
|
|
uint32_t aLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-07-02 07:01:27 -07:00
|
|
|
nsRefPtr<DeleteTextTxn> txn;
|
|
|
|
nsresult result = CreateTxnForDeleteText(aElement, aOffset, aLength,
|
|
|
|
getter_AddRefs(txn));
|
2012-08-12 11:28:26 -07:00
|
|
|
nsAutoRules beginRulesSniffing(this, EditAction::deleteText, nsIEditor::ePrevious);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
|
|
|
// let listeners know what's up
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t i;
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = 0; i < mActionListeners.Count(); i++)
|
|
|
|
mActionListeners[i]->WillDeleteText(aElement, aOffset, aLength);
|
|
|
|
|
|
|
|
result = DoTransaction(txn);
|
|
|
|
|
|
|
|
// let listeners know what happened
|
|
|
|
for (i = 0; i < mActionListeners.Count(); i++)
|
|
|
|
mActionListeners[i]->DidDeleteText(aElement, aOffset, aLength, result);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-12 08:44:44 -07:00
|
|
|
nsresult
|
|
|
|
nsEditor::CreateTxnForDeleteText(nsIDOMCharacterData* aElement,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aOffset,
|
|
|
|
uint32_t aLength,
|
2012-06-12 08:44:44 -07:00
|
|
|
DeleteTextTxn** aTxn)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aElement, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-02-19 14:18:37 -08:00
|
|
|
nsRefPtr<DeleteTextTxn> txn = new DeleteTextTxn();
|
|
|
|
|
2012-06-12 08:44:44 -07:00
|
|
|
nsresult res = txn->Init(this, aElement, aOffset, aLength, &mRangeUpdater);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2011-02-19 14:18:37 -08:00
|
|
|
|
2012-06-12 08:44:44 -07:00
|
|
|
txn.forget(aTxn);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-01-09 15:05:08 -08:00
|
|
|
NS_IMETHODIMP nsEditor::CreateTxnForSplitNode(nsINode *aNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aOffset,
|
2007-03-22 10:30:00 -07:00
|
|
|
SplitElementTxn **aTxn)
|
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aNode, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-02-19 14:18:37 -08:00
|
|
|
nsRefPtr<SplitElementTxn> txn = new SplitElementTxn();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-02-19 14:18:37 -08:00
|
|
|
nsresult rv = txn->Init(this, aNode, aOffset);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
2011-02-23 14:22:07 -08:00
|
|
|
txn.forget(aTxn);
|
2011-02-19 14:18:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-01-09 15:05:08 -08:00
|
|
|
NS_IMETHODIMP nsEditor::CreateTxnForJoinNode(nsINode *aLeftNode,
|
|
|
|
nsINode *aRightNode,
|
2007-03-22 10:30:00 -07:00
|
|
|
JoinElementTxn **aTxn)
|
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aLeftNode && aRightNode, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-02-19 14:18:37 -08:00
|
|
|
nsRefPtr<JoinElementTxn> txn = new JoinElementTxn();
|
|
|
|
|
|
|
|
nsresult rv = txn->Init(this, aLeftNode, aRightNode);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
2011-02-23 14:22:07 -08:00
|
|
|
txn.forget(aTxn);
|
2011-02-19 14:18:37 -08:00
|
|
|
}
|
2009-04-24 15:45:34 -07:00
|
|
|
|
2011-02-19 14:18:37 -08:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// END nsEditor core implementation
|
|
|
|
|
|
|
|
|
|
|
|
// BEGIN nsEditor public helper methods
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsEditor::SplitNodeImpl(nsIDOMNode * aExistingRightNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aOffset,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIDOMNode* aNewLeftNode,
|
|
|
|
nsIDOMNode* aParent)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ASSERTION(((nullptr!=aExistingRightNode) &&
|
|
|
|
(nullptr!=aNewLeftNode) &&
|
|
|
|
(nullptr!=aParent)),
|
2007-03-22 10:30:00 -07:00
|
|
|
"null arg");
|
|
|
|
nsresult result;
|
2012-07-30 07:20:58 -07:00
|
|
|
if ((nullptr!=aExistingRightNode) &&
|
|
|
|
(nullptr!=aNewLeftNode) &&
|
|
|
|
(nullptr!=aParent))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// get selection
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
result = GetSelection(getter_AddRefs(selection));
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// remember some selection points
|
|
|
|
nsCOMPtr<nsIDOMNode> selStartNode, selEndNode;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t selStartOffset, selEndOffset;
|
2010-06-10 19:46:51 -07:00
|
|
|
result = GetStartNodeAndOffset(selection, getter_AddRefs(selStartNode), &selStartOffset);
|
2012-07-30 07:20:58 -07:00
|
|
|
if (NS_FAILED(result)) selStartNode = nullptr; // if selection is cleared, remember that
|
2010-06-10 19:46:51 -07:00
|
|
|
result = GetEndNodeAndOffset(selection, getter_AddRefs(selEndNode), &selEndOffset);
|
2012-07-30 07:20:58 -07:00
|
|
|
if (NS_FAILED(result)) selStartNode = nullptr; // if selection is cleared, remember that
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> resultNode;
|
|
|
|
result = aParent->InsertBefore(aNewLeftNode, aExistingRightNode, getter_AddRefs(resultNode));
|
|
|
|
//printf(" after insert\n"); content->List(); // DEBUG
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
|
|
|
// split the children between the 2 nodes
|
|
|
|
// at this point, aExistingRightNode has all the children
|
|
|
|
// move all the children whose index is < aOffset to aNewLeftNode
|
|
|
|
if (0<=aOffset) // don't bother unless we're going to move at least one child
|
|
|
|
{
|
|
|
|
// if it's a text node, just shuffle around some text
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> rightNodeAsText( do_QueryInterface(aExistingRightNode) );
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> leftNodeAsText( do_QueryInterface(aNewLeftNode) );
|
|
|
|
if (leftNodeAsText && rightNodeAsText)
|
|
|
|
{
|
|
|
|
// fix right node
|
|
|
|
nsAutoString leftText;
|
|
|
|
rightNodeAsText->SubstringData(0, aOffset, leftText);
|
|
|
|
rightNodeAsText->DeleteData(0, aOffset);
|
|
|
|
// fix left node
|
|
|
|
leftNodeAsText->SetData(leftText);
|
|
|
|
// moose
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // otherwise it's an interior node, so shuffle around the children
|
|
|
|
// go through list backwards so deletes don't interfere with the iteration
|
|
|
|
nsCOMPtr<nsIDOMNodeList> childNodes;
|
|
|
|
result = aExistingRightNode->GetChildNodes(getter_AddRefs(childNodes));
|
|
|
|
if ((NS_SUCCEEDED(result)) && (childNodes))
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t i=aOffset-1;
|
2007-03-22 10:30:00 -07:00
|
|
|
for ( ; ((NS_SUCCEEDED(result)) && (0<=i)); i--)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> childNode;
|
|
|
|
result = childNodes->Item(i, getter_AddRefs(childNode));
|
|
|
|
if ((NS_SUCCEEDED(result)) && (childNode))
|
|
|
|
{
|
|
|
|
result = aExistingRightNode->RemoveChild(childNode, getter_AddRefs(resultNode));
|
|
|
|
//printf(" after remove\n"); content->List(); // DEBUG
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> firstChild;
|
|
|
|
aNewLeftNode->GetFirstChild(getter_AddRefs(firstChild));
|
|
|
|
result = aNewLeftNode->InsertBefore(childNode, firstChild, getter_AddRefs(resultNode));
|
|
|
|
//printf(" after append\n"); content->List(); // DEBUG
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// handle selection
|
2011-05-22 05:43:13 -07:00
|
|
|
nsCOMPtr<nsIPresShell> ps = GetPresShell();
|
2010-05-18 01:58:38 -07:00
|
|
|
if (ps)
|
|
|
|
ps->FlushPendingNotifications(Flush_Frames);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (GetShouldTxnSetSelection())
|
|
|
|
{
|
|
|
|
// editor wants us to set selection at split point
|
|
|
|
selection->Collapse(aNewLeftNode, aOffset);
|
|
|
|
}
|
|
|
|
else if (selStartNode)
|
|
|
|
{
|
|
|
|
// else adjust the selection if needed. if selStartNode is null, then there was no selection.
|
|
|
|
// HACK: this is overly simplified - multi-range selections need more work than this
|
|
|
|
if (selStartNode.get() == aExistingRightNode)
|
|
|
|
{
|
|
|
|
if (selStartOffset < aOffset)
|
|
|
|
{
|
|
|
|
selStartNode = aNewLeftNode;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
selStartOffset -= aOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (selEndNode.get() == aExistingRightNode)
|
|
|
|
{
|
|
|
|
if (selEndOffset < aOffset)
|
|
|
|
{
|
|
|
|
selEndNode = aNewLeftNode;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
selEndOffset -= aOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
selection->Collapse(selStartNode,selStartOffset);
|
|
|
|
selection->Extend(selEndNode,selEndOffset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result = NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-07-24 00:39:08 -07:00
|
|
|
nsEditor::JoinNodesImpl(nsINode* aNodeToKeep,
|
|
|
|
nsINode* aNodeToJoin,
|
|
|
|
nsINode* aParent)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-07-24 00:39:04 -07:00
|
|
|
MOZ_ASSERT(aNodeToKeep);
|
|
|
|
MOZ_ASSERT(aNodeToJoin);
|
|
|
|
MOZ_ASSERT(aParent);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-24 00:39:08 -07:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
2013-07-24 00:39:04 -07:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-24 00:39:04 -07:00
|
|
|
// remember some selection points
|
2013-07-24 00:39:08 -07:00
|
|
|
nsCOMPtr<nsINode> selStartNode;
|
|
|
|
int32_t selStartOffset;
|
2013-07-24 00:39:04 -07:00
|
|
|
nsresult result = GetStartNodeAndOffset(selection, getter_AddRefs(selStartNode), &selStartOffset);
|
2013-07-24 00:39:08 -07:00
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
selStartNode = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsINode> selEndNode;
|
|
|
|
int32_t selEndOffset;
|
2013-07-24 00:39:04 -07:00
|
|
|
result = GetEndNodeAndOffset(selection, getter_AddRefs(selEndNode), &selEndOffset);
|
|
|
|
// Joe or Kin should comment here on why the following line is not a copy/paste error
|
2013-07-24 00:39:08 -07:00
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
selStartNode = nullptr;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-24 00:39:08 -07:00
|
|
|
uint32_t firstNodeLength = aNodeToJoin->Length();
|
2013-07-24 00:39:04 -07:00
|
|
|
|
2013-07-24 00:39:08 -07:00
|
|
|
int32_t joinOffset;
|
|
|
|
GetNodeLocation(aNodeToJoin, &joinOffset);
|
|
|
|
int32_t keepOffset;
|
|
|
|
nsINode* parent = GetNodeLocation(aNodeToKeep, &keepOffset);
|
2013-07-24 00:39:04 -07:00
|
|
|
|
|
|
|
// if selection endpoint is between the nodes, remember it as being
|
|
|
|
// in the one that is going away instead. This simplifies later selection
|
|
|
|
// adjustment logic at end of this method.
|
2013-07-24 00:39:08 -07:00
|
|
|
if (selStartNode) {
|
|
|
|
if (selStartNode == parent &&
|
|
|
|
joinOffset < selStartOffset && selStartOffset <= keepOffset) {
|
|
|
|
selStartNode = aNodeToJoin;
|
|
|
|
selStartOffset = firstNodeLength;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-07-24 00:39:08 -07:00
|
|
|
if (selEndNode == parent &&
|
|
|
|
joinOffset < selEndOffset && selEndOffset <= keepOffset) {
|
|
|
|
selEndNode = aNodeToJoin;
|
|
|
|
selEndOffset = firstNodeLength;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-07-24 00:39:04 -07:00
|
|
|
}
|
2013-07-24 00:39:08 -07:00
|
|
|
|
2013-07-24 00:39:04 -07:00
|
|
|
// ok, ready to do join now.
|
|
|
|
// if it's a text node, just shuffle around some text
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> keepNodeAsText( do_QueryInterface(aNodeToKeep) );
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> joinNodeAsText( do_QueryInterface(aNodeToJoin) );
|
2013-07-24 00:39:08 -07:00
|
|
|
if (keepNodeAsText && joinNodeAsText) {
|
2013-07-24 00:39:04 -07:00
|
|
|
nsAutoString rightText;
|
|
|
|
nsAutoString leftText;
|
|
|
|
keepNodeAsText->GetData(rightText);
|
|
|
|
joinNodeAsText->GetData(leftText);
|
|
|
|
leftText += rightText;
|
|
|
|
keepNodeAsText->SetData(leftText);
|
2013-07-24 00:39:08 -07:00
|
|
|
} else {
|
|
|
|
// otherwise it's an interior node, so shuffle around the children
|
|
|
|
nsCOMPtr<nsINodeList> childNodes = aNodeToJoin->ChildNodes();
|
|
|
|
MOZ_ASSERT(childNodes);
|
2013-07-24 00:39:04 -07:00
|
|
|
|
|
|
|
// remember the first child in aNodeToKeep, we'll insert all the children of aNodeToJoin in front of it
|
|
|
|
// GetFirstChild returns nullptr firstNode if aNodeToKeep has no children, that's ok.
|
2013-07-24 00:39:08 -07:00
|
|
|
nsCOMPtr<nsIContent> firstNode = aNodeToKeep->GetFirstChild();
|
2013-07-24 00:39:04 -07:00
|
|
|
|
2013-07-24 00:39:08 -07:00
|
|
|
// have to go through the list backwards to keep deletes from interfering with iteration
|
|
|
|
for (uint32_t i = childNodes->Length(); i > 0; --i) {
|
|
|
|
nsCOMPtr<nsIContent> childNode = childNodes->Item(i - 1);
|
2013-07-24 00:39:04 -07:00
|
|
|
if (childNode) {
|
|
|
|
// prepend children of aNodeToJoin
|
2013-07-24 00:39:08 -07:00
|
|
|
ErrorResult err;
|
|
|
|
aNodeToKeep->InsertBefore(*childNode, firstNode, err);
|
|
|
|
NS_ENSURE_SUCCESS(err.ErrorCode(), err.ErrorCode());
|
|
|
|
firstNode = childNode.forget();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-24 00:39:04 -07:00
|
|
|
|
|
|
|
// delete the extra node
|
2013-07-24 00:39:08 -07:00
|
|
|
ErrorResult err;
|
|
|
|
aParent->RemoveChild(*aNodeToJoin, err);
|
2013-07-24 00:39:04 -07:00
|
|
|
|
2013-07-24 00:39:08 -07:00
|
|
|
if (GetShouldTxnSetSelection()) {
|
2013-07-24 00:39:04 -07:00
|
|
|
// editor wants us to set selection at join point
|
2013-07-24 00:39:08 -07:00
|
|
|
selection->Collapse(aNodeToKeep, SafeCast<int32_t>(firstNodeLength));
|
|
|
|
} else if (selStartNode) {
|
2013-07-24 00:39:04 -07:00
|
|
|
// and adjust the selection if needed
|
|
|
|
// HACK: this is overly simplified - multi-range selections need more work than this
|
|
|
|
bool bNeedToAdjust = false;
|
|
|
|
|
|
|
|
// check to see if we joined nodes where selection starts
|
2013-07-24 00:39:08 -07:00
|
|
|
if (selStartNode == aNodeToJoin) {
|
2013-07-24 00:39:04 -07:00
|
|
|
bNeedToAdjust = true;
|
|
|
|
selStartNode = aNodeToKeep;
|
2013-07-24 00:39:08 -07:00
|
|
|
} else if (selStartNode == aNodeToKeep) {
|
2013-07-24 00:39:04 -07:00
|
|
|
bNeedToAdjust = true;
|
|
|
|
selStartOffset += firstNodeLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check to see if we joined nodes where selection ends
|
2013-07-24 00:39:08 -07:00
|
|
|
if (selEndNode == aNodeToJoin) {
|
2013-07-24 00:39:04 -07:00
|
|
|
bNeedToAdjust = true;
|
|
|
|
selEndNode = aNodeToKeep;
|
2013-07-24 00:39:08 -07:00
|
|
|
} else if (selEndNode == aNodeToKeep) {
|
2013-07-24 00:39:04 -07:00
|
|
|
bNeedToAdjust = true;
|
|
|
|
selEndOffset += firstNodeLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
// adjust selection if needed
|
2013-07-24 00:39:08 -07:00
|
|
|
if (bNeedToAdjust) {
|
|
|
|
selection->Collapse(selStartNode, selStartOffset);
|
|
|
|
selection->Extend(selEndNode, selEndOffset);
|
2013-07-24 00:39:04 -07:00
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-24 00:39:08 -07:00
|
|
|
return err.ErrorCode();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t
|
2012-07-02 04:42:19 -07:00
|
|
|
nsEditor::GetChildOffset(nsIDOMNode* aChild, nsIDOMNode* aParent)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-02 04:42:19 -07:00
|
|
|
MOZ_ASSERT(aChild && aParent);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-02 04:42:19 -07:00
|
|
|
nsCOMPtr<nsINode> parent = do_QueryInterface(aParent);
|
|
|
|
nsCOMPtr<nsINode> child = do_QueryInterface(aChild);
|
|
|
|
MOZ_ASSERT(parent && child);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t idx = parent->IndexOf(child);
|
2012-07-02 04:42:19 -07:00
|
|
|
MOZ_ASSERT(idx != -1);
|
|
|
|
return idx;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-07-02 05:07:57 -07:00
|
|
|
// static
|
2012-07-08 17:45:12 -07:00
|
|
|
already_AddRefed<nsIDOMNode>
|
2012-08-22 08:56:38 -07:00
|
|
|
nsEditor::GetNodeLocation(nsIDOMNode* aChild, int32_t* outOffset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-08 17:45:12 -07:00
|
|
|
MOZ_ASSERT(aChild && outOffset);
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(aChild && outOffset, nullptr);
|
2012-07-02 05:07:57 -07:00
|
|
|
*outOffset = -1;
|
|
|
|
|
2012-07-08 17:45:12 -07:00
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
|
|
|
|
2012-07-02 05:07:57 -07:00
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(
|
2012-07-08 17:45:12 -07:00
|
|
|
aChild->GetParentNode(getter_AddRefs(parent))));
|
|
|
|
if (parent) {
|
|
|
|
*outOffset = GetChildOffset(aChild, parent);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-07-08 17:45:12 -07:00
|
|
|
|
|
|
|
return parent.forget();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-07-10 03:03:38 -07:00
|
|
|
nsINode*
|
|
|
|
nsEditor::GetNodeLocation(nsINode* aChild, int32_t* aOffset)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aChild);
|
|
|
|
MOZ_ASSERT(aOffset);
|
|
|
|
|
|
|
|
nsINode* parent = aChild->GetParentNode();
|
|
|
|
if (parent) {
|
|
|
|
*aOffset = parent->IndexOf(aChild);
|
|
|
|
MOZ_ASSERT(*aOffset != -1);
|
|
|
|
} else {
|
|
|
|
*aOffset = -1;
|
|
|
|
}
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// returns the number of things inside aNode.
|
|
|
|
// If aNode is text, returns number of characters. If not, returns number of children nodes.
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsEditor::GetLengthOfDOMNode(nsIDOMNode *aNode, uint32_t &aCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
aCount = 0;
|
2012-03-29 14:09:04 -07:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
NS_ENSURE_TRUE(node, NS_ERROR_NULL_POINTER);
|
|
|
|
aCount = node->Length();
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsEditor::GetPriorNode(nsIDOMNode *aParentNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aOffset,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aEditableNode,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIDOMNode> *aResultNode,
|
2012-05-06 00:53:11 -07:00
|
|
|
bool bNoBlockCrossing)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-05-05 02:00:05 -07:00
|
|
|
NS_ENSURE_TRUE(aResultNode, NS_ERROR_NULL_POINTER);
|
2012-07-30 07:20:58 -07:00
|
|
|
*aResultNode = nullptr;
|
2012-05-05 02:00:05 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsINode> parentNode = do_QueryInterface(aParentNode);
|
|
|
|
NS_ENSURE_TRUE(parentNode, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
*aResultNode = do_QueryInterface(GetPriorNode(parentNode, aOffset,
|
2012-05-06 00:53:11 -07:00
|
|
|
aEditableNode,
|
|
|
|
bNoBlockCrossing));
|
2012-05-05 02:00:05 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIContent*
|
|
|
|
nsEditor::GetPriorNode(nsINode* aParentNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aOffset,
|
2012-05-05 02:00:05 -07:00
|
|
|
bool aEditableNode,
|
2012-05-06 00:53:11 -07:00
|
|
|
bool aNoBlockCrossing)
|
2012-05-05 02:00:05 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aParentNode);
|
|
|
|
|
|
|
|
// If we are at the beginning of the node, or it is a text node, then just
|
|
|
|
// look before it.
|
|
|
|
if (!aOffset || aParentNode->NodeType() == nsIDOMNode::TEXT_NODE) {
|
|
|
|
if (aNoBlockCrossing && IsBlockNode(aParentNode)) {
|
|
|
|
// If we aren't allowed to cross blocks, don't look before this block.
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-05-06 00:53:11 -07:00
|
|
|
return GetPriorNode(aParentNode, aEditableNode, aNoBlockCrossing);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// else look before the child at 'aOffset'
|
2012-05-05 02:00:05 -07:00
|
|
|
if (nsIContent* child = aParentNode->GetChildAt(aOffset)) {
|
2012-05-06 00:53:11 -07:00
|
|
|
return GetPriorNode(child, aEditableNode, aNoBlockCrossing);
|
2012-05-05 02:00:05 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// unless there isn't one, in which case we are at the end of the node
|
|
|
|
// and want the deep-right child.
|
2012-05-05 02:00:05 -07:00
|
|
|
nsIContent* resultNode = GetRightmostChild(aParentNode, aNoBlockCrossing);
|
|
|
|
if (!resultNode || !aEditableNode || IsEditable(resultNode)) {
|
|
|
|
return resultNode;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// restart the search from the non-editable node we just found
|
2012-05-06 00:53:11 -07:00
|
|
|
return GetPriorNode(resultNode, aEditableNode, aNoBlockCrossing);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsEditor::GetNextNode(nsIDOMNode *aParentNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aOffset,
|
2011-11-28 04:54:32 -08:00
|
|
|
bool aEditableNode,
|
|
|
|
nsCOMPtr<nsIDOMNode> *aResultNode,
|
2012-05-06 00:53:11 -07:00
|
|
|
bool bNoBlockCrossing)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-05-05 02:00:05 -07:00
|
|
|
NS_ENSURE_TRUE(aResultNode, NS_ERROR_NULL_POINTER);
|
2012-07-30 07:20:58 -07:00
|
|
|
*aResultNode = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-05-05 02:00:05 -07:00
|
|
|
nsCOMPtr<nsINode> parentNode = do_QueryInterface(aParentNode);
|
|
|
|
NS_ENSURE_TRUE(parentNode, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
*aResultNode = do_QueryInterface(GetNextNode(parentNode, aOffset,
|
2012-05-06 00:53:11 -07:00
|
|
|
aEditableNode,
|
|
|
|
bNoBlockCrossing));
|
2012-05-05 02:00:05 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIContent*
|
|
|
|
nsEditor::GetNextNode(nsINode* aParentNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aOffset,
|
2012-05-05 02:00:05 -07:00
|
|
|
bool aEditableNode,
|
2012-05-06 00:53:11 -07:00
|
|
|
bool aNoBlockCrossing)
|
2012-05-05 02:00:05 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aParentNode);
|
|
|
|
|
2012-02-15 01:18:24 -08:00
|
|
|
// if aParentNode is a text node, use its location instead
|
2012-05-05 02:00:05 -07:00
|
|
|
if (aParentNode->NodeType() == nsIDOMNode::TEXT_NODE) {
|
2012-10-09 05:31:24 -07:00
|
|
|
nsINode* parent = aParentNode->GetParentNode();
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(parent, nullptr);
|
2012-05-05 02:00:05 -07:00
|
|
|
aOffset = parent->IndexOf(aParentNode) + 1; // _after_ the text node
|
2007-03-22 10:30:00 -07:00
|
|
|
aParentNode = parent;
|
|
|
|
}
|
2012-05-05 02:00:05 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// look at the child at 'aOffset'
|
2012-05-05 02:00:05 -07:00
|
|
|
nsIContent* child = aParentNode->GetChildAt(aOffset);
|
|
|
|
if (child) {
|
|
|
|
if (aNoBlockCrossing && IsBlockNode(child)) {
|
|
|
|
return child;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-05-05 02:00:05 -07:00
|
|
|
|
|
|
|
nsIContent* resultNode = GetLeftmostChild(child, aNoBlockCrossing);
|
|
|
|
if (!resultNode) {
|
|
|
|
return child;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-05-05 02:00:05 -07:00
|
|
|
|
2012-05-06 00:53:11 -07:00
|
|
|
if (!IsDescendantOfEditorRoot(resultNode)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-05-05 02:00:05 -07:00
|
|
|
if (!aEditableNode || IsEditable(resultNode)) {
|
|
|
|
return resultNode;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// restart the search from the non-editable node we just found
|
2012-05-06 00:53:11 -07:00
|
|
|
return GetNextNode(resultNode, aEditableNode, aNoBlockCrossing);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// unless there isn't one, in which case we are at the end of the node
|
|
|
|
// and want the next one.
|
2012-05-05 02:00:05 -07:00
|
|
|
if (aNoBlockCrossing && IsBlockNode(aParentNode)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// don't cross out of parent block
|
2012-07-27 06:59:29 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-05-05 02:00:05 -07:00
|
|
|
|
2012-05-06 00:53:11 -07:00
|
|
|
return GetNextNode(aParentNode, aEditableNode, aNoBlockCrossing);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsEditor::GetPriorNode(nsIDOMNode *aCurrentNode,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aEditableNode,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIDOMNode> *aResultNode,
|
2012-05-06 00:53:11 -07:00
|
|
|
bool bNoBlockCrossing)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-05-05 02:00:05 -07:00
|
|
|
NS_ENSURE_TRUE(aResultNode, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-11-28 04:54:03 -08:00
|
|
|
nsCOMPtr<nsINode> currentNode = do_QueryInterface(aCurrentNode);
|
2012-05-05 02:00:05 -07:00
|
|
|
NS_ENSURE_TRUE(currentNode, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
*aResultNode = do_QueryInterface(GetPriorNode(currentNode, aEditableNode,
|
2012-05-06 00:53:11 -07:00
|
|
|
bNoBlockCrossing));
|
2012-05-05 02:00:05 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-11-28 04:54:32 -08:00
|
|
|
|
2012-05-05 02:00:05 -07:00
|
|
|
nsIContent*
|
|
|
|
nsEditor::GetPriorNode(nsINode* aCurrentNode, bool aEditableNode,
|
2012-05-06 00:53:11 -07:00
|
|
|
bool aNoBlockCrossing /* = false */)
|
2012-05-05 02:00:05 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCurrentNode);
|
|
|
|
|
2012-05-06 00:53:11 -07:00
|
|
|
if (!IsDescendantOfEditorRoot(aCurrentNode)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2011-11-28 04:54:32 -08:00
|
|
|
}
|
|
|
|
|
2012-05-06 00:53:11 -07:00
|
|
|
return FindNode(aCurrentNode, false, aEditableNode, aNoBlockCrossing);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-11-28 04:54:03 -08:00
|
|
|
nsIContent*
|
|
|
|
nsEditor::FindNextLeafNode(nsINode *aCurrentNode,
|
|
|
|
bool aGoForward,
|
2012-05-06 00:53:11 -07:00
|
|
|
bool bNoBlockCrossing)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// called only by GetPriorNode so we don't need to check params.
|
2012-05-06 00:53:11 -07:00
|
|
|
NS_PRECONDITION(IsDescendantOfEditorRoot(aCurrentNode) &&
|
|
|
|
!IsEditorRoot(aCurrentNode),
|
2011-11-28 04:54:03 -08:00
|
|
|
"Bogus arguments");
|
|
|
|
|
|
|
|
nsINode* cur = aCurrentNode;
|
|
|
|
for (;;) {
|
|
|
|
// if aCurrentNode has a sibling in the right direction, return
|
|
|
|
// that sibling's closest child (or itself if it has no children)
|
|
|
|
nsIContent* sibling =
|
|
|
|
aGoForward ? cur->GetNextSibling() : cur->GetPreviousSibling();
|
|
|
|
if (sibling) {
|
|
|
|
if (bNoBlockCrossing && IsBlockNode(sibling)) {
|
|
|
|
// don't look inside prevsib, since it is a block
|
|
|
|
return sibling;
|
|
|
|
}
|
|
|
|
nsIContent *leaf =
|
|
|
|
aGoForward ? GetLeftmostChild(sibling, bNoBlockCrossing) :
|
|
|
|
GetRightmostChild(sibling, bNoBlockCrossing);
|
|
|
|
if (!leaf) {
|
|
|
|
return sibling;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-11-28 04:54:03 -08:00
|
|
|
return leaf;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-11-28 04:54:03 -08:00
|
|
|
|
2012-10-09 05:31:24 -07:00
|
|
|
nsINode *parent = cur->GetParentNode();
|
2011-11-28 04:54:03 -08:00
|
|
|
if (!parent) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-11-28 04:54:03 -08:00
|
|
|
|
2012-05-06 00:53:11 -07:00
|
|
|
NS_ASSERTION(IsDescendantOfEditorRoot(parent),
|
2011-11-28 04:54:03 -08:00
|
|
|
"We started with a proper descendant of root, and should stop "
|
|
|
|
"if we ever hit the root, so we better have a descendant of "
|
|
|
|
"root now!");
|
2012-05-06 00:53:11 -07:00
|
|
|
if (IsEditorRoot(parent) ||
|
|
|
|
(bNoBlockCrossing && IsBlockNode(parent))) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-11-28 04:54:03 -08:00
|
|
|
|
|
|
|
cur = parent;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-11-28 04:54:03 -08:00
|
|
|
|
|
|
|
NS_NOTREACHED("What part of for(;;) do you not understand?");
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-02-01 02:54:22 -08:00
|
|
|
nsresult
|
|
|
|
nsEditor::GetNextNode(nsIDOMNode* aCurrentNode,
|
|
|
|
bool aEditableNode,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIDOMNode> *aResultNode,
|
2012-05-06 00:53:11 -07:00
|
|
|
bool bNoBlockCrossing)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-11-28 04:54:14 -08:00
|
|
|
nsCOMPtr<nsINode> currentNode = do_QueryInterface(aCurrentNode);
|
2012-02-01 02:54:22 -08:00
|
|
|
if (!currentNode || !aResultNode) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResultNode = do_QueryInterface(GetNextNode(currentNode, aEditableNode,
|
2012-05-06 00:53:11 -07:00
|
|
|
bNoBlockCrossing));
|
2012-02-01 02:54:22 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIContent*
|
|
|
|
nsEditor::GetNextNode(nsINode* aCurrentNode,
|
|
|
|
bool aEditableNode,
|
2012-05-06 00:53:11 -07:00
|
|
|
bool bNoBlockCrossing)
|
2012-02-01 02:54:22 -08:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCurrentNode);
|
|
|
|
|
2012-05-06 00:53:11 -07:00
|
|
|
if (!IsDescendantOfEditorRoot(aCurrentNode)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2011-11-28 04:54:32 -08:00
|
|
|
}
|
|
|
|
|
2012-05-06 00:53:11 -07:00
|
|
|
return FindNode(aCurrentNode, true, aEditableNode, bNoBlockCrossing);
|
2011-11-28 04:54:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIContent*
|
|
|
|
nsEditor::FindNode(nsINode *aCurrentNode,
|
|
|
|
bool aGoForward,
|
|
|
|
bool aEditableNode,
|
2012-05-06 00:53:11 -07:00
|
|
|
bool bNoBlockCrossing)
|
2011-11-28 04:54:14 -08:00
|
|
|
{
|
2012-05-06 00:53:11 -07:00
|
|
|
if (IsEditorRoot(aCurrentNode)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// Don't allow traversal above the root node! This helps
|
|
|
|
// prevent us from accidentally editing browser content
|
|
|
|
// when the editor is in a text widget.
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-10-11 03:26:15 -07:00
|
|
|
nsCOMPtr<nsIContent> candidate =
|
2012-05-06 00:53:11 -07:00
|
|
|
FindNextLeafNode(aCurrentNode, aGoForward, bNoBlockCrossing);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-11-28 04:54:14 -08:00
|
|
|
if (!candidate) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-11-28 04:54:14 -08:00
|
|
|
|
|
|
|
if (!aEditableNode || IsEditable(candidate)) {
|
|
|
|
return candidate;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-11-28 04:54:14 -08:00
|
|
|
|
2012-05-06 00:53:11 -07:00
|
|
|
return FindNode(candidate, aGoForward, aEditableNode, bNoBlockCrossing);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-12-02 00:59:45 -08:00
|
|
|
nsIDOMNode*
|
|
|
|
nsEditor::GetRightmostChild(nsIDOMNode* aCurrentNode,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool bNoBlockCrossing)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-12-02 00:59:45 -08:00
|
|
|
nsCOMPtr<nsINode> currentNode = do_QueryInterface(aCurrentNode);
|
|
|
|
nsIContent* result = GetRightmostChild(currentNode, bNoBlockCrossing);
|
|
|
|
return result ? result->AsDOMNode() : nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-11-28 04:53:29 -08:00
|
|
|
nsIContent*
|
|
|
|
nsEditor::GetRightmostChild(nsINode *aCurrentNode,
|
|
|
|
bool bNoBlockCrossing)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(aCurrentNode, nullptr);
|
2011-11-28 04:53:29 -08:00
|
|
|
nsIContent *cur = aCurrentNode->GetLastChild();
|
|
|
|
if (!cur) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2011-11-28 04:53:29 -08:00
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
if (bNoBlockCrossing && IsBlockNode(cur)) {
|
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
nsIContent* next = cur->GetLastChild();
|
|
|
|
if (!next) {
|
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
cur = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_NOTREACHED("What part of for(;;) do you not understand?");
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2011-11-28 04:53:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIContent*
|
|
|
|
nsEditor::GetLeftmostChild(nsINode *aCurrentNode,
|
|
|
|
bool bNoBlockCrossing)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(aCurrentNode, nullptr);
|
2011-11-28 04:53:29 -08:00
|
|
|
nsIContent *cur = aCurrentNode->GetFirstChild();
|
|
|
|
if (!cur) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2011-11-28 04:53:29 -08:00
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
if (bNoBlockCrossing && IsBlockNode(cur)) {
|
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
nsIContent *next = cur->GetFirstChild();
|
|
|
|
if (!next) {
|
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
cur = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_NOTREACHED("What part of for(;;) do you not understand?");
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2011-11-28 04:53:29 -08:00
|
|
|
}
|
|
|
|
|
2012-12-02 00:59:45 -08:00
|
|
|
nsIDOMNode*
|
|
|
|
nsEditor::GetLeftmostChild(nsIDOMNode* aCurrentNode,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool bNoBlockCrossing)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-12-02 00:59:45 -08:00
|
|
|
nsCOMPtr<nsINode> currentNode = do_QueryInterface(aCurrentNode);
|
|
|
|
nsIContent* result = GetLeftmostChild(currentNode, bNoBlockCrossing);
|
|
|
|
return result ? result->AsDOMNode() : nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-07-01 12:53:16 -07:00
|
|
|
bool
|
|
|
|
nsEditor::IsBlockNode(nsIDOMNode* aNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-01 12:53:16 -07:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
return IsBlockNode(node);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-07-01 12:53:16 -07:00
|
|
|
bool
|
|
|
|
nsEditor::IsBlockNode(nsINode* aNode)
|
2011-11-28 04:53:29 -08:00
|
|
|
{
|
|
|
|
// stub to be overridden in nsHTMLEditor.
|
|
|
|
// screwing around with the class hierarchy here in order
|
|
|
|
// to not duplicate the code in GetNextNode/GetPrevNode
|
|
|
|
// across both nsEditor/nsHTMLEditor.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-30 23:34:52 -07:00
|
|
|
bool
|
|
|
|
nsEditor::CanContain(nsIDOMNode* aParent, nsIDOMNode* aChild)
|
|
|
|
{
|
2012-05-18 01:29:38 -07:00
|
|
|
nsCOMPtr<nsIContent> parent = do_QueryInterface(aParent);
|
|
|
|
NS_ENSURE_TRUE(parent, false);
|
2012-04-30 23:34:52 -07:00
|
|
|
|
2012-05-18 01:29:38 -07:00
|
|
|
switch (parent->NodeType()) {
|
|
|
|
case nsIDOMNode::ELEMENT_NODE:
|
|
|
|
case nsIDOMNode::DOCUMENT_FRAGMENT_NODE:
|
|
|
|
return TagCanContain(parent->Tag(), aChild);
|
|
|
|
}
|
|
|
|
return false;
|
2012-04-30 23:34:52 -07:00
|
|
|
}
|
|
|
|
|
2011-11-28 04:53:29 -08:00
|
|
|
bool
|
2012-04-30 23:34:52 -07:00
|
|
|
nsEditor::CanContainTag(nsIDOMNode* aParent, nsIAtom* aChildTag)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-05-18 01:29:38 -07:00
|
|
|
nsCOMPtr<nsIContent> parent = do_QueryInterface(aParent);
|
|
|
|
NS_ENSURE_TRUE(parent, false);
|
2012-04-30 23:34:52 -07:00
|
|
|
|
2012-05-18 01:29:38 -07:00
|
|
|
switch (parent->NodeType()) {
|
|
|
|
case nsIDOMNode::ELEMENT_NODE:
|
|
|
|
case nsIDOMNode::DOCUMENT_FRAGMENT_NODE:
|
|
|
|
return TagCanContainTag(parent->Tag(), aChildTag);
|
|
|
|
}
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2012-04-30 23:34:52 -07:00
|
|
|
nsEditor::TagCanContain(nsIAtom* aParentTag, nsIDOMNode* aChild)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-05-18 01:29:38 -07:00
|
|
|
nsCOMPtr<nsIContent> child = do_QueryInterface(aChild);
|
|
|
|
NS_ENSURE_TRUE(child, false);
|
2012-04-30 23:34:52 -07:00
|
|
|
|
2012-05-18 01:29:38 -07:00
|
|
|
switch (child->NodeType()) {
|
|
|
|
case nsIDOMNode::TEXT_NODE:
|
|
|
|
case nsIDOMNode::ELEMENT_NODE:
|
|
|
|
case nsIDOMNode::DOCUMENT_FRAGMENT_NODE:
|
|
|
|
return TagCanContainTag(aParentTag, child->Tag());
|
|
|
|
}
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2012-04-30 23:34:52 -07:00
|
|
|
nsEditor::TagCanContainTag(nsIAtom* aParentTag, nsIAtom* aChildTag)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-11-28 04:53:29 -08:00
|
|
|
bool
|
2012-05-01 03:01:28 -07:00
|
|
|
nsEditor::IsRoot(nsIDOMNode* inNode)
|
2011-11-28 04:53:29 -08:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(inNode, false);
|
|
|
|
|
2011-12-03 13:50:15 -08:00
|
|
|
nsCOMPtr<nsIDOMNode> rootNode = do_QueryInterface(GetRoot());
|
|
|
|
|
|
|
|
return inNode == rootNode;
|
2011-11-28 04:53:29 -08:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2012-05-01 03:01:28 -07:00
|
|
|
nsEditor::IsRoot(nsINode* inNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
NS_ENSURE_TRUE(inNode, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-12-03 13:50:15 -08:00
|
|
|
nsCOMPtr<nsINode> rootNode = GetRoot();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-12-03 13:50:15 -08:00
|
|
|
return inNode == rootNode;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-05-06 00:53:11 -07:00
|
|
|
bool
|
|
|
|
nsEditor::IsEditorRoot(nsINode* aNode)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(aNode, false);
|
|
|
|
nsCOMPtr<nsINode> rootNode = GetEditorRoot();
|
|
|
|
return aNode == rootNode;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2012-05-01 03:01:28 -07:00
|
|
|
nsEditor::IsDescendantOfRoot(nsIDOMNode* inNode)
|
2011-11-28 04:53:29 -08:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(inNode);
|
2012-05-01 03:01:28 -07:00
|
|
|
return IsDescendantOfRoot(node);
|
2011-11-28 04:53:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-05-01 03:01:28 -07:00
|
|
|
nsEditor::IsDescendantOfRoot(nsINode* inNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
NS_ENSURE_TRUE(inNode, false);
|
2011-12-03 13:50:15 -08:00
|
|
|
nsCOMPtr<nsIContent> root = GetRoot();
|
2011-11-28 04:53:29 -08:00
|
|
|
NS_ENSURE_TRUE(root, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-11-28 04:53:29 -08:00
|
|
|
return nsContentUtils::ContentIsDescendantOf(inNode, root);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-05-06 00:53:11 -07:00
|
|
|
bool
|
|
|
|
nsEditor::IsDescendantOfEditorRoot(nsIDOMNode* aNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
return IsDescendantOfEditorRoot(node);
|
|
|
|
}
|
|
|
|
|
2012-05-06 00:53:11 -07:00
|
|
|
bool
|
|
|
|
nsEditor::IsDescendantOfEditorRoot(nsINode* aNode)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(aNode, false);
|
|
|
|
nsCOMPtr<nsIContent> root = GetEditorRoot();
|
|
|
|
NS_ENSURE_TRUE(root, false);
|
|
|
|
|
|
|
|
return nsContentUtils::ContentIsDescendantOf(aNode, root);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsEditor::IsContainer(nsIDOMNode *aNode)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return aNode ? true : false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-11-28 04:53:15 -08:00
|
|
|
static inline bool
|
|
|
|
IsElementVisible(dom::Element* aElement)
|
|
|
|
{
|
|
|
|
if (aElement->GetPrimaryFrame()) {
|
|
|
|
// It's visible, for our purposes
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIContent *cur = aElement;
|
|
|
|
for (; ;) {
|
2012-01-18 13:10:06 -08:00
|
|
|
// Walk up the tree looking for the nearest ancestor with a frame.
|
|
|
|
// The state of the child right below it will determine whether
|
|
|
|
// we might possibly have a frame or not.
|
|
|
|
bool haveLazyBitOnChild = cur->HasFlag(NODE_NEEDS_FRAME);
|
2011-11-28 04:53:15 -08:00
|
|
|
cur = cur->GetFlattenedTreeParent();
|
|
|
|
if (!cur) {
|
2012-01-18 13:10:06 -08:00
|
|
|
if (!haveLazyBitOnChild) {
|
|
|
|
// None of our ancestors have lazy bits set, so we shouldn't
|
|
|
|
// have a frame
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The root has a lazy frame construction bit. We need to check
|
|
|
|
// our style.
|
|
|
|
break;
|
2011-11-28 04:53:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cur->GetPrimaryFrame()) {
|
2012-01-18 13:10:06 -08:00
|
|
|
if (!haveLazyBitOnChild) {
|
|
|
|
// Our ancestor directly under |cur| doesn't have lazy bits;
|
|
|
|
// that means we won't get a frame
|
|
|
|
return false;
|
|
|
|
}
|
2011-11-28 04:53:15 -08:00
|
|
|
|
2012-01-18 13:10:06 -08:00
|
|
|
if (cur->GetPrimaryFrame()->IsLeaf()) {
|
|
|
|
// Nothing under here will ever get frames
|
|
|
|
return false;
|
2011-11-28 04:53:15 -08:00
|
|
|
}
|
|
|
|
|
2012-01-18 13:10:06 -08:00
|
|
|
// Otherwise, we might end up with a frame when that lazy bit is
|
|
|
|
// processed. Figure out our actual style.
|
2011-11-28 04:53:15 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now it might be that we have no frame because we're in a
|
|
|
|
// display:none subtree, or it might be that we're just dealing with
|
|
|
|
// lazy frame construction and it hasn't happened yet. Check which
|
|
|
|
// one it is.
|
2011-10-16 21:11:29 -07:00
|
|
|
nsRefPtr<nsStyleContext> styleContext =
|
2011-11-28 04:53:15 -08:00
|
|
|
nsComputedDOMStyle::GetStyleContextForElementNoFlush(aElement,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, nullptr);
|
2011-10-16 21:11:29 -07:00
|
|
|
if (styleContext) {
|
2013-02-16 13:51:02 -08:00
|
|
|
return styleContext->StyleDisplay()->mDisplay != NS_STYLE_DISPLAY_NONE;
|
2011-10-16 21:11:29 -07:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsEditor::IsEditable(nsIDOMNode *aNode)
|
2011-11-28 04:53:29 -08:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
|
|
|
|
return IsEditable(content);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsEditor::IsEditable(nsIContent *aNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
NS_ENSURE_TRUE(aNode, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
if (IsMozEditorBogusNode(aNode) || !IsModifiableNode(aNode)) return false;
|
2007-06-27 19:48:16 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// see if it has a frame. If so, we'll edit it.
|
|
|
|
// special case for textnodes: frame must have width.
|
2011-11-28 04:53:29 -08:00
|
|
|
if (aNode->IsElement() && !IsElementVisible(aNode->AsElement())) {
|
|
|
|
// If the element has no frame, it's not editable. Note that we
|
|
|
|
// need to check IsElement() here, because some of our tests
|
|
|
|
// rely on frameless textnodes being visible.
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-02-08 15:09:51 -08:00
|
|
|
switch (aNode->NodeType()) {
|
|
|
|
case nsIDOMNode::ELEMENT_NODE:
|
|
|
|
case nsIDOMNode::TEXT_NODE:
|
2012-10-03 18:25:00 -07:00
|
|
|
return true; // element or text node; not invisible
|
2012-02-08 15:09:51 -08:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-11-28 04:53:29 -08:00
|
|
|
bool
|
|
|
|
nsEditor::IsMozEditorBogusNode(nsIContent *element)
|
|
|
|
{
|
2010-05-03 06:23:36 -07:00
|
|
|
return element &&
|
|
|
|
element->AttrValueIs(kNameSpaceID_None, kMOZEditorBogusNodeAttrAtom,
|
|
|
|
kMOZEditorBogusNodeValue, eCaseMatters);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t
|
2012-06-06 00:36:07 -07:00
|
|
|
nsEditor::CountEditableChildren(nsINode* aNode)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aNode);
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = 0;
|
2012-06-06 00:36:07 -07:00
|
|
|
for (nsIContent* child = aNode->GetFirstChild();
|
|
|
|
child;
|
|
|
|
child = child->GetNextSibling()) {
|
|
|
|
if (IsEditable(child)) {
|
|
|
|
++count;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2012-06-06 00:36:07 -07:00
|
|
|
return count;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//END nsEditor static utility methods
|
|
|
|
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
NS_IMETHODIMP nsEditor::IncrementModificationCount(int32_t inNumMods)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t oldModCount = mModCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
mModCount += inNumMods;
|
|
|
|
|
|
|
|
if ((oldModCount == 0 && mModCount != 0)
|
|
|
|
|| (oldModCount != 0 && mModCount == 0))
|
|
|
|
NotifyDocumentListeners(eDocumentStateChanged);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
NS_IMETHODIMP nsEditor::GetModificationCount(int32_t *outModCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(outModCount);
|
|
|
|
*outModCount = mModCount;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsEditor::ResetModificationCount()
|
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool doNotify = (mModCount != 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
mModCount = 0;
|
|
|
|
|
|
|
|
if (doNotify)
|
|
|
|
NotifyDocumentListeners(eDocumentStateChanged);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//END nsEditor Private methods
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetTag: digs out the atom for the tag of this node
|
2011-11-28 04:53:29 -08:00
|
|
|
//
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIAtom *
|
|
|
|
nsEditor::GetTag(nsIDOMNode *aNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
|
|
|
|
|
|
|
|
if (!content)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aNode, "null node passed to nsEditor::Tag()");
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return content->Tag();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetTagString: digs out string for the tag of this node
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsEditor::GetTagString(nsIDOMNode *aNode, nsAString& outString)
|
|
|
|
{
|
|
|
|
if (!aNode)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("null node passed to nsEditor::GetTag()");
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIAtom *atom = GetTag(aNode);
|
|
|
|
if (!atom)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
atom->ToString(outString);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// NodesSameType: do these nodes have the same tag?
|
|
|
|
//
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsEditor::NodesSameType(nsIDOMNode *aNode1, nsIDOMNode *aNode2)
|
|
|
|
{
|
2012-05-18 01:29:38 -07:00
|
|
|
if (!aNode1 || !aNode2) {
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_NOTREACHED("null node passed to nsEditor::NodesSameType()");
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-05-18 01:29:38 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> content1 = do_QueryInterface(aNode1);
|
|
|
|
NS_ENSURE_TRUE(content1, false);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> content2 = do_QueryInterface(aNode2);
|
|
|
|
NS_ENSURE_TRUE(content2, false);
|
|
|
|
|
|
|
|
return AreNodesSameType(content1, content2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */
|
|
|
|
bool
|
|
|
|
nsEditor::AreNodesSameType(nsIContent* aNode1, nsIContent* aNode2)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aNode1);
|
|
|
|
MOZ_ASSERT(aNode2);
|
|
|
|
return aNode1->Tag() == aNode2->Tag();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// IsTextNode: true if node of dom type text
|
|
|
|
//
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsEditor::IsTextNode(nsIDOMNode *aNode)
|
|
|
|
{
|
|
|
|
if (!aNode)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("null node passed to IsTextNode()");
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t nodeType;
|
2007-03-22 10:30:00 -07:00
|
|
|
aNode->GetNodeType(&nodeType);
|
|
|
|
return (nodeType == nsIDOMNode::TEXT_NODE);
|
|
|
|
}
|
|
|
|
|
2011-11-28 04:53:29 -08:00
|
|
|
bool
|
|
|
|
nsEditor::IsTextNode(nsINode *aNode)
|
|
|
|
{
|
|
|
|
return aNode->NodeType() == nsIDOMNode::TEXT_NODE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetChildAt: returns the node at this position index in the parent
|
|
|
|
//
|
|
|
|
nsCOMPtr<nsIDOMNode>
|
2012-08-22 08:56:38 -07:00
|
|
|
nsEditor::GetChildAt(nsIDOMNode *aParent, int32_t aOffset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> resultNode;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> parent = do_QueryInterface(aParent);
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(parent, resultNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
resultNode = do_QueryInterface(parent->GetChildAt(aOffset));
|
|
|
|
|
|
|
|
return resultNode;
|
|
|
|
}
|
2011-07-14 08:25:07 -07:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetNodeAtRangeOffsetPoint: returns the node at this position in a range,
|
|
|
|
// assuming that aParentOrNode is the node itself if it's a text node, or
|
|
|
|
// the node's parent otherwise.
|
|
|
|
//
|
|
|
|
nsCOMPtr<nsIDOMNode>
|
2012-08-22 08:56:38 -07:00
|
|
|
nsEditor::GetNodeAtRangeOffsetPoint(nsIDOMNode* aParentOrNode, int32_t aOffset)
|
2011-07-14 08:25:07 -07:00
|
|
|
{
|
|
|
|
if (IsTextNode(aParentOrNode)) {
|
|
|
|
return aParentOrNode;
|
|
|
|
}
|
|
|
|
return GetChildAt(aParentOrNode, aOffset);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetStartNodeAndOffset: returns whatever the start parent & offset is of
|
|
|
|
// the first range in the selection.
|
|
|
|
nsresult
|
|
|
|
nsEditor::GetStartNodeAndOffset(nsISelection *aSelection,
|
2010-06-10 19:46:51 -07:00
|
|
|
nsIDOMNode **outStartNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t *outStartOffset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(outStartNode && outStartOffset && aSelection, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-24 00:39:08 -07:00
|
|
|
nsCOMPtr<nsINode> startNode;
|
|
|
|
nsresult rv = GetStartNodeAndOffset(static_cast<Selection*>(aSelection),
|
|
|
|
getter_AddRefs(startNode),
|
|
|
|
outStartOffset);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-24 00:39:08 -07:00
|
|
|
if (startNode) {
|
|
|
|
NS_ADDREF(*outStartNode = startNode->AsDOMNode());
|
|
|
|
} else {
|
|
|
|
*outStartNode = nullptr;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-24 00:39:08 -07:00
|
|
|
nsresult
|
|
|
|
nsEditor::GetStartNodeAndOffset(Selection* aSelection, nsINode** aStartNode,
|
|
|
|
int32_t* aStartOffset)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aSelection);
|
|
|
|
MOZ_ASSERT(aStartNode);
|
|
|
|
MOZ_ASSERT(aStartOffset);
|
2010-06-18 14:01:08 -07:00
|
|
|
|
2013-07-24 00:39:08 -07:00
|
|
|
*aStartNode = nullptr;
|
|
|
|
*aStartOffset = 0;
|
2010-06-18 14:01:08 -07:00
|
|
|
|
2013-07-24 00:39:08 -07:00
|
|
|
NS_ENSURE_TRUE(aSelection->GetRangeCount(), NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
const nsRange* range = aSelection->GetRangeAt(0);
|
|
|
|
NS_ENSURE_TRUE(range, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(range->IsPositioned(), NS_ERROR_FAILURE);
|
2010-06-18 14:01:08 -07:00
|
|
|
|
2013-07-24 00:39:08 -07:00
|
|
|
NS_IF_ADDREF(*aStartNode = range->GetStartParent());
|
|
|
|
*aStartOffset = range->StartOffset();
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetEndNodeAndOffset: returns whatever the end parent & offset is of
|
|
|
|
// the first range in the selection.
|
|
|
|
nsresult
|
|
|
|
nsEditor::GetEndNodeAndOffset(nsISelection *aSelection,
|
2010-06-10 19:46:51 -07:00
|
|
|
nsIDOMNode **outEndNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t *outEndOffset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-07-24 00:39:08 -07:00
|
|
|
NS_ENSURE_TRUE(outEndNode && outEndOffset && aSelection, NS_ERROR_NULL_POINTER);
|
2010-06-10 19:46:51 -07:00
|
|
|
|
2013-07-24 00:39:08 -07:00
|
|
|
nsCOMPtr<nsINode> endNode;
|
|
|
|
nsresult rv = GetEndNodeAndOffset(static_cast<Selection*>(aSelection),
|
|
|
|
getter_AddRefs(endNode),
|
|
|
|
outEndOffset);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-24 00:39:08 -07:00
|
|
|
if (endNode) {
|
|
|
|
NS_ADDREF(*outEndNode = endNode->AsDOMNode());
|
|
|
|
} else {
|
|
|
|
*outEndNode = nullptr;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsEditor::GetEndNodeAndOffset(Selection* aSelection, nsINode** aEndNode,
|
|
|
|
int32_t* aEndOffset)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aSelection);
|
|
|
|
MOZ_ASSERT(aEndNode);
|
|
|
|
MOZ_ASSERT(aEndOffset);
|
|
|
|
|
|
|
|
*aEndNode = nullptr;
|
|
|
|
*aEndOffset = 0;
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(aSelection->GetRangeCount(), NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
const nsRange* range = aSelection->GetRangeAt(0);
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(range, NS_ERROR_FAILURE);
|
2013-07-24 00:39:08 -07:00
|
|
|
|
|
|
|
NS_ENSURE_TRUE(range->IsPositioned(), NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
NS_IF_ADDREF(*aEndNode = range->GetEndParent());
|
|
|
|
*aEndOffset = range->EndOffset();
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// IsPreformatted: checks the style info for the node for the preformatted
|
|
|
|
// text style.
|
|
|
|
nsresult
|
2011-09-28 23:19:26 -07:00
|
|
|
nsEditor::IsPreformatted(nsIDOMNode *aNode, bool *aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
|
|
|
|
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(aResult && content, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-05-22 05:43:13 -07:00
|
|
|
nsCOMPtr<nsIPresShell> ps = GetPresShell();
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(ps, NS_ERROR_NOT_INITIALIZED);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-09-02 17:36:42 -07:00
|
|
|
// Look at the node (and its parent if it's not an element), and grab its style context
|
2010-05-21 13:36:42 -07:00
|
|
|
nsRefPtr<nsStyleContext> elementStyle;
|
2010-09-02 17:36:42 -07:00
|
|
|
if (!content->IsElement()) {
|
|
|
|
content = content->GetParent();
|
|
|
|
}
|
|
|
|
if (content && content->IsElement()) {
|
2013-03-06 22:12:41 -08:00
|
|
|
elementStyle = nsComputedDOMStyle::GetStyleContextForElementNoFlush(content->AsElement(),
|
|
|
|
nullptr,
|
|
|
|
ps);
|
2010-05-21 13:36:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!elementStyle)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Consider nodes without a style context to be NOT preformatted:
|
|
|
|
// For instance, this is true of JS tags inside the body (which show
|
|
|
|
// up as #text nodes but have no style context).
|
2011-10-17 07:59:28 -07:00
|
|
|
*aResult = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-02-16 13:51:02 -08:00
|
|
|
const nsStyleText* styleText = elementStyle->StyleText();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-02-19 18:07:48 -08:00
|
|
|
*aResult = styleText->WhiteSpaceIsSignificant();
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// SplitNodeDeep: this splits a node "deeply", splitting children as
|
|
|
|
// appropriate. The place to split is represented by
|
|
|
|
// a dom point at {splitPointParent, splitPointOffset}.
|
|
|
|
// That dom point must be inside aNode, which is the node to
|
|
|
|
// split. outOffset is set to the offset in the parent of aNode where
|
|
|
|
// the split terminates - where you would want to insert
|
|
|
|
// a new element, for instance, if that's why you were splitting
|
|
|
|
// the node.
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsEditor::SplitNodeDeep(nsIDOMNode *aNode,
|
|
|
|
nsIDOMNode *aSplitPointParent,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aSplitPointOffset,
|
|
|
|
int32_t *outOffset,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aNoEmptyContainers,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIDOMNode> *outLeftNode,
|
|
|
|
nsCOMPtr<nsIDOMNode> *outRightNode)
|
|
|
|
{
|
2012-05-18 01:29:39 -07:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
NS_ENSURE_TRUE(node && aSplitPointParent && outOffset, NS_ERROR_NULL_POINTER);
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t offset = aSplitPointOffset;
|
2012-05-18 01:29:39 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
if (outLeftNode) *outLeftNode = nullptr;
|
|
|
|
if (outRightNode) *outRightNode = nullptr;
|
2012-05-18 01:29:39 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsINode> nodeToSplit = do_QueryInterface(aSplitPointParent);
|
|
|
|
while (nodeToSplit) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// need to insert rules code call here to do things like
|
|
|
|
// not split a list if you are after the last <li> or before the first, etc.
|
|
|
|
// for now we just have some smarts about unneccessarily splitting
|
|
|
|
// textnodes, which should be universal enough to put straight in
|
|
|
|
// this nsEditor routine.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> nodeAsText = do_QueryInterface(nodeToSplit);
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t len = nodeToSplit->Length();
|
2011-09-28 23:19:26 -07:00
|
|
|
bool bDoSplit = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
if (!(aNoEmptyContainers || nodeAsText) || (offset && (offset != (int32_t)len)))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
bDoSplit = true;
|
2012-05-18 01:29:39 -07:00
|
|
|
nsCOMPtr<nsIDOMNode> tempNode;
|
|
|
|
nsresult rv = SplitNode(nodeToSplit->AsDOMNode(), offset,
|
|
|
|
getter_AddRefs(tempNode));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (outRightNode) {
|
|
|
|
*outRightNode = nodeToSplit->AsDOMNode();
|
|
|
|
}
|
|
|
|
if (outLeftNode) {
|
|
|
|
*outLeftNode = tempNode;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-10-09 05:31:24 -07:00
|
|
|
nsINode* parentNode = nodeToSplit->GetParentNode();
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(parentNode, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-05-18 01:29:39 -07:00
|
|
|
if (!bDoSplit && offset) {
|
|
|
|
// must be "end of text node" case, we didn't split it, just move past it
|
|
|
|
offset = parentNode->IndexOf(nodeToSplit) + 1;
|
|
|
|
if (outLeftNode) {
|
|
|
|
*outLeftNode = nodeToSplit->AsDOMNode();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
offset = parentNode->IndexOf(nodeToSplit);
|
|
|
|
if (outRightNode) {
|
|
|
|
*outRightNode = nodeToSplit->AsDOMNode();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-05-18 01:29:39 -07:00
|
|
|
|
|
|
|
if (nodeToSplit == node) {
|
|
|
|
// we split all the way up to (and including) aNode; we're done
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
2012-05-18 01:29:39 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nodeToSplit = parentNode;
|
|
|
|
}
|
2012-05-18 01:29:39 -07:00
|
|
|
|
|
|
|
if (!nodeToSplit) {
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_NOTREACHED("null node obtained in nsEditor::SplitNodeDeep()");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-05-18 01:29:39 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
*outOffset = offset;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// JoinNodeDeep: this joins two like nodes "deeply", joining children as
|
|
|
|
// appropriate.
|
|
|
|
nsresult
|
|
|
|
nsEditor::JoinNodeDeep(nsIDOMNode *aLeftNode,
|
|
|
|
nsIDOMNode *aRightNode,
|
|
|
|
nsCOMPtr<nsIDOMNode> *aOutJoinNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t *outOffset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(aLeftNode && aRightNode && aOutJoinNode && outOffset, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// while the rightmost children and their descendants of the left node
|
|
|
|
// match the leftmost children and their descendants of the right node
|
|
|
|
// join them up. Can you say that three times fast?
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> leftNodeToJoin = do_QueryInterface(aLeftNode);
|
|
|
|
nsCOMPtr<nsIDOMNode> rightNodeToJoin = do_QueryInterface(aRightNode);
|
|
|
|
nsCOMPtr<nsIDOMNode> parentNode,tmp;
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
|
|
|
|
rightNodeToJoin->GetParentNode(getter_AddRefs(parentNode));
|
|
|
|
|
|
|
|
while (leftNodeToJoin && rightNodeToJoin && parentNode &&
|
|
|
|
NodesSameType(leftNodeToJoin, rightNodeToJoin))
|
|
|
|
{
|
|
|
|
// adjust out params
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t length;
|
2012-03-29 14:09:04 -07:00
|
|
|
res = GetLengthOfDOMNode(leftNodeToJoin, length);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
*aOutJoinNode = rightNodeToJoin;
|
|
|
|
*outOffset = length;
|
|
|
|
|
|
|
|
// do the join
|
|
|
|
res = JoinNodes(leftNodeToJoin, rightNodeToJoin, parentNode);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (IsTextNode(parentNode)) // we've joined all the way down to text nodes, we're done!
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// get new left and right nodes, and begin anew
|
|
|
|
parentNode = rightNodeToJoin;
|
|
|
|
leftNodeToJoin = GetChildAt(parentNode, length-1);
|
|
|
|
rightNodeToJoin = GetChildAt(parentNode, length);
|
|
|
|
|
|
|
|
// skip over non-editable nodes
|
|
|
|
while (leftNodeToJoin && !IsEditable(leftNodeToJoin))
|
|
|
|
{
|
|
|
|
leftNodeToJoin->GetPreviousSibling(getter_AddRefs(tmp));
|
|
|
|
leftNodeToJoin = tmp;
|
|
|
|
}
|
|
|
|
if (!leftNodeToJoin) break;
|
|
|
|
|
|
|
|
while (rightNodeToJoin && !IsEditable(rightNodeToJoin))
|
|
|
|
{
|
|
|
|
rightNodeToJoin->GetNextSibling(getter_AddRefs(tmp));
|
|
|
|
rightNodeToJoin = tmp;
|
|
|
|
}
|
|
|
|
if (!rightNodeToJoin) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-06-01 04:03:17 -07:00
|
|
|
void
|
|
|
|
nsEditor::BeginUpdateViewBatch()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(mUpdateCount >= 0, "bad state");
|
|
|
|
|
|
|
|
if (0 == mUpdateCount)
|
|
|
|
{
|
|
|
|
// Turn off selection updates and notifications.
|
|
|
|
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
GetSelection(getter_AddRefs(selection));
|
|
|
|
|
|
|
|
if (selection)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISelectionPrivate> selPrivate(do_QueryInterface(selection));
|
|
|
|
selPrivate->StartBatchChanges();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mUpdateCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult nsEditor::EndUpdateViewBatch()
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(mUpdateCount > 0, "bad state");
|
|
|
|
|
|
|
|
if (mUpdateCount <= 0)
|
|
|
|
{
|
|
|
|
mUpdateCount = 0;
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mUpdateCount--;
|
|
|
|
|
|
|
|
if (0 == mUpdateCount)
|
|
|
|
{
|
|
|
|
// Turn selection updating and notifications back on.
|
|
|
|
|
|
|
|
nsCOMPtr<nsISelection>selection;
|
|
|
|
GetSelection(getter_AddRefs(selection));
|
|
|
|
|
|
|
|
if (selection) {
|
|
|
|
nsCOMPtr<nsISelectionPrivate>selPrivate(do_QueryInterface(selection));
|
|
|
|
selPrivate->EndBatchChanges();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsEditor::GetShouldTxnSetSelection()
|
|
|
|
{
|
|
|
|
return mShouldTxnSetSelection;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-05-10 07:54:33 -07:00
|
|
|
nsEditor::DeleteSelectionImpl(EDirection aAction,
|
|
|
|
EStripWrappers aStripWrappers)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-05-10 07:54:33 -07:00
|
|
|
MOZ_ASSERT(aStripWrappers == eStrip || aStripWrappers == eNoStrip);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsISelection>selection;
|
|
|
|
nsresult res = GetSelection(getter_AddRefs(selection));
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-07-02 07:01:27 -07:00
|
|
|
nsRefPtr<EditAggregateTxn> txn;
|
2012-06-12 08:44:44 -07:00
|
|
|
nsCOMPtr<nsINode> deleteNode;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t deleteCharOffset = 0, deleteCharLength = 0;
|
2007-07-02 07:01:27 -07:00
|
|
|
res = CreateTxnForDeleteSelection(aAction, getter_AddRefs(txn),
|
|
|
|
getter_AddRefs(deleteNode),
|
|
|
|
&deleteCharOffset, &deleteCharLength);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIDOMCharacterData> deleteCharData(do_QueryInterface(deleteNode));
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
{
|
2012-08-12 11:28:26 -07:00
|
|
|
nsAutoRules beginRulesSniffing(this, EditAction::deleteSelection, aAction);
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t i;
|
2007-03-22 10:30:00 -07:00
|
|
|
// Notify nsIEditActionListener::WillDelete[Selection|Text|Node]
|
|
|
|
if (!deleteNode)
|
|
|
|
for (i = 0; i < mActionListeners.Count(); i++)
|
|
|
|
mActionListeners[i]->WillDeleteSelection(selection);
|
|
|
|
else if (deleteCharData)
|
|
|
|
for (i = 0; i < mActionListeners.Count(); i++)
|
|
|
|
mActionListeners[i]->WillDeleteText(deleteCharData, deleteCharOffset, 1);
|
|
|
|
else
|
|
|
|
for (i = 0; i < mActionListeners.Count(); i++)
|
2012-06-12 08:44:44 -07:00
|
|
|
mActionListeners[i]->WillDeleteNode(deleteNode->AsDOMNode());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Delete the specified amount
|
|
|
|
res = DoTransaction(txn);
|
|
|
|
|
|
|
|
// Notify nsIEditActionListener::DidDelete[Selection|Text|Node]
|
|
|
|
if (!deleteNode)
|
|
|
|
for (i = 0; i < mActionListeners.Count(); i++)
|
|
|
|
mActionListeners[i]->DidDeleteSelection(selection);
|
|
|
|
else if (deleteCharData)
|
|
|
|
for (i = 0; i < mActionListeners.Count(); i++)
|
|
|
|
mActionListeners[i]->DidDeleteText(deleteCharData, deleteCharOffset, 1, res);
|
|
|
|
else
|
|
|
|
for (i = 0; i < mActionListeners.Count(); i++)
|
2012-06-12 08:44:44 -07:00
|
|
|
mActionListeners[i]->DidDeleteNode(deleteNode->AsDOMNode(), res);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: error handling in this routine needs to be cleaned up!
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::DeleteSelectionAndCreateNode(const nsAString& aTag,
|
|
|
|
nsIDOMNode ** aNewNode)
|
|
|
|
{
|
2012-06-21 05:54:26 -07:00
|
|
|
nsresult result = DeleteSelectionAndPrepareToCreateNode();
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-21 05:54:26 -07:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
nsCOMPtr<nsINode> node = selection->GetAnchorNode();
|
2013-12-17 06:12:33 -08:00
|
|
|
uint32_t offset = selection->AnchorOffset();
|
2012-06-21 05:54:26 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIDOMNode> newNode;
|
2012-06-21 05:54:26 -07:00
|
|
|
result = CreateNode(aTag, node->AsDOMNode(), offset,
|
2007-03-22 10:30:00 -07:00
|
|
|
getter_AddRefs(newNode));
|
2012-06-21 05:54:26 -07:00
|
|
|
// XXX: ERROR_HANDLING check result, and make sure aNewNode is set correctly
|
|
|
|
// in success/failure cases
|
2007-03-22 10:30:00 -07:00
|
|
|
*aNewNode = newNode;
|
|
|
|
NS_IF_ADDREF(*aNewNode);
|
|
|
|
|
|
|
|
// we want the selection to be just after the new node
|
2012-06-21 05:54:26 -07:00
|
|
|
return selection->Collapse(node, offset + 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Non-interface, protected methods */
|
|
|
|
|
2014-02-12 05:02:56 -08:00
|
|
|
TextComposition*
|
|
|
|
nsEditor::GetComposition() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-02-12 05:02:56 -08:00
|
|
|
return mComposition;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-02-12 05:02:56 -08:00
|
|
|
nsEditor::IsIMEComposing() const
|
|
|
|
{
|
|
|
|
return mComposition && mComposition->IsComposing();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-06-21 05:54:26 -07:00
|
|
|
nsresult
|
|
|
|
nsEditor::DeleteSelectionAndPrepareToCreateNode()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-06-21 05:54:26 -07:00
|
|
|
nsresult res;
|
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2012-07-02 06:31:49 -07:00
|
|
|
MOZ_ASSERT(selection->GetAnchorFocusRange());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-02 06:31:49 -07:00
|
|
|
if (!selection->GetAnchorFocusRange()->Collapsed()) {
|
2012-06-21 05:54:26 -07:00
|
|
|
res = DeleteSelection(nsIEditor::eNone, nsIEditor::eStrip);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-07-11 21:23:25 -07:00
|
|
|
|
2012-07-02 06:31:49 -07:00
|
|
|
MOZ_ASSERT(selection->GetAnchorFocusRange() &&
|
|
|
|
selection->GetAnchorFocusRange()->Collapsed(),
|
2012-06-21 05:54:26 -07:00
|
|
|
"Selection not collapsed after delete");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-06-21 05:54:26 -07:00
|
|
|
// If the selection is a chardata node, split it if necessary and compute
|
|
|
|
// where to put the new node
|
|
|
|
nsCOMPtr<nsINode> node = selection->GetAnchorNode();
|
|
|
|
MOZ_ASSERT(node, "Selection has no ranges in it");
|
|
|
|
|
|
|
|
if (node && node->IsNodeOfType(nsINode::eDATA_NODE)) {
|
2012-10-09 05:31:24 -07:00
|
|
|
NS_ASSERTION(node->GetParentNode(),
|
2012-06-21 05:54:26 -07:00
|
|
|
"It's impossible to insert into chardata with no parent -- "
|
|
|
|
"fix the caller");
|
2012-10-09 05:31:24 -07:00
|
|
|
NS_ENSURE_STATE(node->GetParentNode());
|
2012-06-21 05:54:26 -07:00
|
|
|
|
2013-12-17 06:12:33 -08:00
|
|
|
uint32_t offset = selection->AnchorOffset();
|
2012-06-21 05:54:26 -07:00
|
|
|
|
|
|
|
if (offset == 0) {
|
2012-10-09 05:31:24 -07:00
|
|
|
res = selection->Collapse(node->GetParentNode(),
|
|
|
|
node->GetParentNode()->IndexOf(node));
|
2012-06-21 05:54:26 -07:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(res));
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2013-12-17 06:12:33 -08:00
|
|
|
} else if (offset == node->Length()) {
|
2012-10-09 05:31:24 -07:00
|
|
|
res = selection->Collapse(node->GetParentNode(),
|
|
|
|
node->GetParentNode()->IndexOf(node) + 1);
|
2012-06-21 05:54:26 -07:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(res));
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
|
|
|
res = SplitNode(node->AsDOMNode(), offset, getter_AddRefs(tmp));
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2012-10-09 05:31:24 -07:00
|
|
|
res = selection->Collapse(node->GetParentNode(),
|
|
|
|
node->GetParentNode()->IndexOf(node));
|
2012-06-21 05:54:26 -07:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(res));
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2012-06-21 05:54:26 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-07-05 00:45:09 -07:00
|
|
|
void
|
2007-03-22 10:30:00 -07:00
|
|
|
nsEditor::DoAfterDoTransaction(nsITransaction *aTxn)
|
|
|
|
{
|
2012-07-05 00:45:09 -07:00
|
|
|
bool isTransientTransaction;
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(
|
|
|
|
aTxn->GetIsTransient(&isTransientTransaction)));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!isTransientTransaction)
|
|
|
|
{
|
|
|
|
// we need to deal here with the case where the user saved after some
|
|
|
|
// edits, then undid one or more times. Then, the undo count is -ve,
|
|
|
|
// but we can't let a do take it back to zero. So we flip it up to
|
|
|
|
// a +ve number.
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t modCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
GetModificationCount(&modCount);
|
|
|
|
if (modCount < 0)
|
|
|
|
modCount = -modCount;
|
|
|
|
|
2012-07-05 00:45:09 -07:00
|
|
|
// don't count transient transactions
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(
|
|
|
|
IncrementModificationCount(1)));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-05 00:45:09 -07:00
|
|
|
void
|
2007-03-22 10:30:00 -07:00
|
|
|
nsEditor::DoAfterUndoTransaction()
|
|
|
|
{
|
2012-07-05 00:45:09 -07:00
|
|
|
// all undoable transactions are non-transient
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(
|
|
|
|
IncrementModificationCount(-1)));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-07-05 00:45:09 -07:00
|
|
|
void
|
2007-03-22 10:30:00 -07:00
|
|
|
nsEditor::DoAfterRedoTransaction()
|
|
|
|
{
|
2012-07-05 00:45:09 -07:00
|
|
|
// all redoable transactions are non-transient
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(
|
|
|
|
IncrementModificationCount(1)));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-01-09 15:05:08 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::CreateTxnForSetAttribute(Element *aElement,
|
|
|
|
const nsAString& aAttribute,
|
2007-03-22 10:30:00 -07:00
|
|
|
const nsAString& aValue,
|
|
|
|
ChangeAttributeTxn ** aTxn)
|
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aElement, NS_ERROR_NULL_POINTER);
|
2009-04-24 15:45:34 -07:00
|
|
|
|
2011-02-19 14:18:37 -08:00
|
|
|
nsRefPtr<ChangeAttributeTxn> txn = new ChangeAttributeTxn();
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
nsresult rv = txn->Init(this, aElement, aAttribute, aValue, false);
|
2011-02-19 14:18:37 -08:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
2011-02-23 14:22:07 -08:00
|
|
|
txn.forget(aTxn);
|
2011-02-19 14:18:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-09 15:05:08 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::CreateTxnForRemoveAttribute(Element *aElement,
|
2007-03-22 10:30:00 -07:00
|
|
|
const nsAString& aAttribute,
|
|
|
|
ChangeAttributeTxn ** aTxn)
|
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aElement, NS_ERROR_NULL_POINTER);
|
2009-04-24 15:45:34 -07:00
|
|
|
|
2011-02-19 14:18:37 -08:00
|
|
|
nsRefPtr<ChangeAttributeTxn> txn = new ChangeAttributeTxn();
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
nsresult rv = txn->Init(this, aElement, aAttribute, EmptyString(), true);
|
2011-02-19 14:18:37 -08:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
2011-02-23 14:22:07 -08:00
|
|
|
txn.forget(aTxn);
|
2011-02-19 14:18:37 -08:00
|
|
|
}
|
2009-04-24 15:45:34 -07:00
|
|
|
|
2011-02-19 14:18:37 -08:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsEditor::CreateTxnForCreateElement(const nsAString& aTag,
|
2013-01-09 15:05:08 -08:00
|
|
|
nsINode *aParent,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aPosition,
|
2007-03-22 10:30:00 -07:00
|
|
|
CreateElementTxn ** aTxn)
|
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aParent, NS_ERROR_NULL_POINTER);
|
2009-04-24 15:45:34 -07:00
|
|
|
|
2011-02-19 14:18:37 -08:00
|
|
|
nsRefPtr<CreateElementTxn> txn = new CreateElementTxn();
|
2009-04-24 15:45:34 -07:00
|
|
|
|
2011-02-19 14:18:37 -08:00
|
|
|
nsresult rv = txn->Init(this, aTag, aParent, aPosition);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
2011-02-23 14:22:07 -08:00
|
|
|
txn.forget(aTxn);
|
2011-02-19 14:18:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-09 15:05:08 -08:00
|
|
|
NS_IMETHODIMP nsEditor::CreateTxnForInsertElement(nsINode * aNode,
|
|
|
|
nsINode * aParent,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aPosition,
|
2007-03-22 10:30:00 -07:00
|
|
|
InsertElementTxn ** aTxn)
|
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aNode && aParent, NS_ERROR_NULL_POINTER);
|
2009-04-24 15:45:34 -07:00
|
|
|
|
2011-02-19 14:18:37 -08:00
|
|
|
nsRefPtr<InsertElementTxn> txn = new InsertElementTxn();
|
2009-04-24 15:45:34 -07:00
|
|
|
|
2011-02-19 14:18:37 -08:00
|
|
|
nsresult rv = txn->Init(aNode, aParent, aPosition, this);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
2011-02-23 14:22:07 -08:00
|
|
|
txn.forget(aTxn);
|
2011-02-19 14:18:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
nsresult
|
2012-06-19 06:23:36 -07:00
|
|
|
nsEditor::CreateTxnForDeleteNode(nsINode* aNode, DeleteNodeTxn** aTxn)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-06-19 06:23:36 -07:00
|
|
|
NS_ENSURE_TRUE(aNode, NS_ERROR_NULL_POINTER);
|
2009-04-24 15:45:34 -07:00
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
nsRefPtr<DeleteNodeTxn> txn = new DeleteNodeTxn();
|
2011-02-19 14:18:37 -08:00
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
nsresult res = txn->Init(this, aNode, &mRangeUpdater);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2009-04-24 15:45:34 -07:00
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
txn.forget(aTxn);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::CreateTxnForIMEText(const nsAString& aStringToInsert,
|
|
|
|
IMETextTxn ** aTxn)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aTxn, "illegal value- null ptr- aTxn");
|
|
|
|
|
2011-02-19 14:18:37 -08:00
|
|
|
nsRefPtr<IMETextTxn> txn = new IMETextTxn();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-02-12 05:02:56 -08:00
|
|
|
// During handling IME composition, mComposition must have been initialized.
|
2014-03-04 05:48:26 -08:00
|
|
|
// TODO: We can simplify IMETextTxn::Init() with TextComposition class.
|
2014-02-12 05:02:56 -08:00
|
|
|
nsresult rv = txn->Init(mIMETextNode, mIMETextOffset,
|
|
|
|
mComposition->String().Length(),
|
2014-03-04 05:48:26 -08:00
|
|
|
mComposition->GetRanges(), aStringToInsert, this);
|
2011-02-19 14:18:37 -08:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
2011-02-23 14:22:07 -08:00
|
|
|
txn.forget(aTxn);
|
2011-02-19 14:18:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-05-11 13:41:47 -07:00
|
|
|
nsEditor::CreateTxnForAddStyleSheet(nsCSSStyleSheet* aSheet, AddStyleSheetTxn* *aTxn)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-02-19 14:18:37 -08:00
|
|
|
nsRefPtr<AddStyleSheetTxn> txn = new AddStyleSheetTxn();
|
|
|
|
|
|
|
|
nsresult rv = txn->Init(this, aSheet);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
2011-02-23 14:22:07 -08:00
|
|
|
txn.forget(aTxn);
|
2011-02-19 14:18:37 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-02-19 14:18:37 -08:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-05-11 13:41:47 -07:00
|
|
|
nsEditor::CreateTxnForRemoveStyleSheet(nsCSSStyleSheet* aSheet, RemoveStyleSheetTxn* *aTxn)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-02-19 14:18:37 -08:00
|
|
|
nsRefPtr<RemoveStyleSheetTxn> txn = new RemoveStyleSheetTxn();
|
|
|
|
|
|
|
|
nsresult rv = txn->Init(this, aSheet);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
2011-02-23 14:22:07 -08:00
|
|
|
txn.forget(aTxn);
|
2011-02-19 14:18:37 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-02-19 14:18:37 -08:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-12 08:44:44 -07:00
|
|
|
nsresult
|
|
|
|
nsEditor::CreateTxnForDeleteSelection(EDirection aAction,
|
|
|
|
EditAggregateTxn** aTxn,
|
|
|
|
nsINode** aNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t* aOffset,
|
|
|
|
int32_t* aLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-06-12 08:44:44 -07:00
|
|
|
MOZ_ASSERT(aTxn);
|
2012-07-30 07:20:58 -07:00
|
|
|
*aTxn = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-12 08:44:44 -07:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
|
|
|
NS_ENSURE_STATE(selection);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-12 08:44:44 -07:00
|
|
|
// Check whether the selection is collapsed and we should do nothing:
|
|
|
|
if (selection->Collapsed() && aAction == eNone) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-12 08:44:44 -07:00
|
|
|
// allocate the out-param transaction
|
|
|
|
nsRefPtr<EditAggregateTxn> aggTxn = new EditAggregateTxn();
|
|
|
|
|
2013-02-18 08:32:32 -08:00
|
|
|
for (int32_t rangeIdx = 0; rangeIdx < selection->GetRangeCount(); ++rangeIdx) {
|
2013-02-15 02:52:57 -08:00
|
|
|
nsRefPtr<nsRange> range = selection->GetRangeAt(rangeIdx);
|
2012-06-12 08:44:44 -07:00
|
|
|
NS_ENSURE_STATE(range);
|
|
|
|
|
|
|
|
// Same with range as with selection; if it is collapsed and action
|
|
|
|
// is eNone, do nothing.
|
|
|
|
if (!range->Collapsed()) {
|
|
|
|
nsRefPtr<DeleteRangeTxn> txn = new DeleteRangeTxn();
|
|
|
|
txn->Init(this, range, &mRangeUpdater);
|
|
|
|
aggTxn->AppendChild(txn);
|
|
|
|
} else if (aAction != eNone) {
|
|
|
|
// we have an insertion point. delete the thing in front of it or
|
|
|
|
// behind it, depending on aAction
|
|
|
|
nsresult res = CreateTxnForDeleteInsertionPoint(range, aAction, aggTxn,
|
|
|
|
aNode, aOffset, aLength);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-12 08:44:44 -07:00
|
|
|
aggTxn.forget(aTxn);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-12 08:44:44 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-06-12 08:44:44 -07:00
|
|
|
nsEditor::CreateTxnForDeleteCharacter(nsIDOMCharacterData* aData,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aOffset,
|
2012-06-12 08:44:44 -07:00
|
|
|
EDirection aDirection,
|
|
|
|
DeleteTextTxn** aTxn)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aDirection == eNext || aDirection == ePrevious,
|
|
|
|
"invalid direction");
|
|
|
|
nsAutoString data;
|
|
|
|
aData->GetData(data);
|
2012-06-12 09:05:27 -07:00
|
|
|
NS_ASSERTION(data.Length(), "Trying to delete from a zero-length node");
|
|
|
|
NS_ENSURE_STATE(data.Length());
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t segOffset = aOffset, segLength = 1;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aDirection == eNext) {
|
2010-06-16 09:33:13 -07:00
|
|
|
if (segOffset + 1 < data.Length() &&
|
|
|
|
NS_IS_HIGH_SURROGATE(data[segOffset]) &&
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IS_LOW_SURROGATE(data[segOffset+1])) {
|
|
|
|
// delete both halves of the surrogate pair
|
|
|
|
++segLength;
|
|
|
|
}
|
2011-04-06 11:28:08 -07:00
|
|
|
} else if (aOffset > 0) {
|
|
|
|
--segOffset;
|
2011-03-16 12:34:29 -07:00
|
|
|
if (segOffset > 0 &&
|
2011-04-06 11:28:08 -07:00
|
|
|
NS_IS_LOW_SURROGATE(data[segOffset]) &&
|
|
|
|
NS_IS_HIGH_SURROGATE(data[segOffset-1])) {
|
2007-03-22 10:30:00 -07:00
|
|
|
++segLength;
|
|
|
|
--segOffset;
|
|
|
|
}
|
2011-04-06 11:28:08 -07:00
|
|
|
} else {
|
|
|
|
return NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return CreateTxnForDeleteText(aData, segOffset, segLength, aTxn);
|
|
|
|
}
|
|
|
|
|
2012-06-12 08:44:44 -07:00
|
|
|
//XXX: currently, this doesn't handle edge conditions because GetNext/GetPrior
|
|
|
|
//are not implemented
|
|
|
|
nsresult
|
|
|
|
nsEditor::CreateTxnForDeleteInsertionPoint(nsRange* aRange,
|
|
|
|
EDirection aAction,
|
|
|
|
EditAggregateTxn* aTxn,
|
|
|
|
nsINode** aNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t* aOffset,
|
|
|
|
int32_t* aLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-06-12 08:44:44 -07:00
|
|
|
MOZ_ASSERT(aAction != eNone);
|
|
|
|
|
|
|
|
nsresult res;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// get the node and offset of the insertion point
|
2012-06-12 08:44:44 -07:00
|
|
|
nsCOMPtr<nsINode> node = aRange->GetStartParent();
|
|
|
|
NS_ENSURE_STATE(node);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t offset = aRange->StartOffset();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-12 08:44:44 -07:00
|
|
|
// determine if the insertion point is at the beginning, middle, or end of
|
|
|
|
// the node
|
2012-06-13 08:38:05 -07:00
|
|
|
nsCOMPtr<nsIDOMCharacterData> nodeAsCharData = do_QueryInterface(node);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = node->Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isFirst = (0 == offset);
|
2012-08-22 08:56:38 -07:00
|
|
|
bool isLast = (count == (uint32_t)offset);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-12 08:44:44 -07:00
|
|
|
// XXX: if isFirst && isLast, then we'll need to delete the node
|
2007-03-22 10:30:00 -07:00
|
|
|
// as well as the 1 child
|
|
|
|
|
|
|
|
// build a transaction for deleting the appropriate data
|
|
|
|
// XXX: this has to come from rule section
|
2012-06-12 08:44:44 -07:00
|
|
|
if (aAction == ePrevious && isFirst) {
|
|
|
|
// we're backspacing from the beginning of the node. Delete the first
|
|
|
|
// thing to our left
|
|
|
|
nsCOMPtr<nsIContent> priorNode = GetPriorNode(node, true);
|
|
|
|
NS_ENSURE_STATE(priorNode);
|
|
|
|
|
2012-06-13 08:38:05 -07:00
|
|
|
// there is a priorNode, so delete its last child (if chardata, delete the
|
|
|
|
// last char). if it has no children, delete it
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> priorNodeAsCharData =
|
|
|
|
do_QueryInterface(priorNode);
|
|
|
|
if (priorNodeAsCharData) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t length = priorNode->Length();
|
2012-06-13 08:38:05 -07:00
|
|
|
// Bail out for empty chardata XXX: Do we want to do something else?
|
2012-06-12 08:44:44 -07:00
|
|
|
NS_ENSURE_STATE(length);
|
|
|
|
nsRefPtr<DeleteTextTxn> txn;
|
2012-06-13 08:38:05 -07:00
|
|
|
res = CreateTxnForDeleteCharacter(priorNodeAsCharData, length,
|
2012-06-12 08:44:44 -07:00
|
|
|
ePrevious, getter_AddRefs(txn));
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
|
|
|
|
*aOffset = txn->GetOffset();
|
|
|
|
*aLength = txn->GetNumCharsToDelete();
|
|
|
|
aTxn->AppendChild(txn);
|
|
|
|
} else {
|
2012-06-13 08:38:05 -07:00
|
|
|
// priorNode is not chardata, so tell its parent to delete it
|
2012-06-19 06:23:36 -07:00
|
|
|
nsRefPtr<DeleteNodeTxn> txn;
|
|
|
|
res = CreateTxnForDeleteNode(priorNode, getter_AddRefs(txn));
|
2012-06-12 08:44:44 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
|
|
|
|
aTxn->AppendChild(txn);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-06-12 08:44:44 -07:00
|
|
|
|
|
|
|
NS_ADDREF(*aNode = priorNode);
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-06-12 08:44:44 -07:00
|
|
|
|
|
|
|
if (aAction == eNext && isLast) {
|
|
|
|
// we're deleting from the end of the node. Delete the first thing to our
|
|
|
|
// right
|
|
|
|
nsCOMPtr<nsIContent> nextNode = GetNextNode(node, true);
|
|
|
|
NS_ENSURE_STATE(nextNode);
|
|
|
|
|
2012-06-13 08:38:05 -07:00
|
|
|
// there is a nextNode, so delete its first child (if chardata, delete the
|
|
|
|
// first char). if it has no children, delete it
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> nextNodeAsCharData =
|
|
|
|
do_QueryInterface(nextNode);
|
|
|
|
if (nextNodeAsCharData) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t length = nextNode->Length();
|
2012-06-13 08:38:05 -07:00
|
|
|
// Bail out for empty chardata XXX: Do we want to do something else?
|
2012-06-12 08:44:44 -07:00
|
|
|
NS_ENSURE_STATE(length);
|
|
|
|
nsRefPtr<DeleteTextTxn> txn;
|
2012-06-13 08:38:05 -07:00
|
|
|
res = CreateTxnForDeleteCharacter(nextNodeAsCharData, 0, eNext,
|
2012-06-12 08:44:44 -07:00
|
|
|
getter_AddRefs(txn));
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
|
|
|
|
*aOffset = txn->GetOffset();
|
|
|
|
*aLength = txn->GetNumCharsToDelete();
|
|
|
|
aTxn->AppendChild(txn);
|
|
|
|
} else {
|
2012-06-13 08:38:05 -07:00
|
|
|
// nextNode is not chardata, so tell its parent to delete it
|
2012-06-19 06:23:36 -07:00
|
|
|
nsRefPtr<DeleteNodeTxn> txn;
|
|
|
|
res = CreateTxnForDeleteNode(nextNode, getter_AddRefs(txn));
|
2012-06-12 08:44:44 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
aTxn->AppendChild(txn);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-06-12 08:44:44 -07:00
|
|
|
|
|
|
|
NS_ADDREF(*aNode = nextNode);
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-06-12 08:44:44 -07:00
|
|
|
|
2012-06-13 08:38:05 -07:00
|
|
|
if (nodeAsCharData) {
|
|
|
|
// we have chardata, so delete a char at the proper offset
|
2012-06-12 08:44:44 -07:00
|
|
|
nsRefPtr<DeleteTextTxn> txn;
|
2012-06-13 08:38:05 -07:00
|
|
|
res = CreateTxnForDeleteCharacter(nodeAsCharData, offset, aAction,
|
2012-06-12 08:44:44 -07:00
|
|
|
getter_AddRefs(txn));
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
|
|
|
|
aTxn->AppendChild(txn);
|
|
|
|
NS_ADDREF(*aNode = node);
|
|
|
|
*aOffset = txn->GetOffset();
|
|
|
|
*aLength = txn->GetNumCharsToDelete();
|
|
|
|
} else {
|
2012-06-13 08:38:05 -07:00
|
|
|
// we're either deleting a node or chardata, need to dig into the next/prev
|
|
|
|
// node to find out
|
2012-06-12 08:44:44 -07:00
|
|
|
nsCOMPtr<nsINode> selectedNode;
|
|
|
|
if (aAction == ePrevious) {
|
|
|
|
selectedNode = GetPriorNode(node, offset, true);
|
|
|
|
} else if (aAction == eNext) {
|
|
|
|
selectedNode = GetNextNode(node, offset, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-06-12 08:44:44 -07:00
|
|
|
|
2012-06-20 07:48:02 -07:00
|
|
|
while (selectedNode &&
|
|
|
|
selectedNode->IsNodeOfType(nsINode::eDATA_NODE) &&
|
2012-06-12 09:05:27 -07:00
|
|
|
!selectedNode->Length()) {
|
|
|
|
// Can't delete an empty chardata node (bug 762183)
|
|
|
|
if (aAction == ePrevious) {
|
|
|
|
selectedNode = GetPriorNode(selectedNode, true);
|
|
|
|
} else if (aAction == eNext) {
|
|
|
|
selectedNode = GetNextNode(selectedNode, true);
|
|
|
|
}
|
|
|
|
}
|
2012-06-20 07:48:02 -07:00
|
|
|
NS_ENSURE_STATE(selectedNode);
|
2012-06-12 09:05:27 -07:00
|
|
|
|
2012-06-13 08:38:05 -07:00
|
|
|
nsCOMPtr<nsIDOMCharacterData> selectedNodeAsCharData =
|
2012-06-12 08:44:44 -07:00
|
|
|
do_QueryInterface(selectedNode);
|
2012-06-13 08:38:05 -07:00
|
|
|
if (selectedNodeAsCharData) {
|
|
|
|
// we are deleting from a chardata node, so do a character deletion
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t position = 0;
|
2012-06-12 08:44:44 -07:00
|
|
|
if (aAction == ePrevious) {
|
|
|
|
position = selectedNode->Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-06-12 08:44:44 -07:00
|
|
|
nsRefPtr<DeleteTextTxn> delTextTxn;
|
2012-06-13 08:38:05 -07:00
|
|
|
res = CreateTxnForDeleteCharacter(selectedNodeAsCharData, position,
|
2012-06-12 08:44:44 -07:00
|
|
|
aAction, getter_AddRefs(delTextTxn));
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
NS_ENSURE_TRUE(delTextTxn, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
aTxn->AppendChild(delTextTxn);
|
|
|
|
*aOffset = delTextTxn->GetOffset();
|
|
|
|
*aLength = delTextTxn->GetNumCharsToDelete();
|
|
|
|
} else {
|
2012-06-19 06:23:36 -07:00
|
|
|
nsRefPtr<DeleteNodeTxn> delElementTxn;
|
|
|
|
res = CreateTxnForDeleteNode(selectedNode, getter_AddRefs(delElementTxn));
|
2012-06-12 08:44:44 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
NS_ENSURE_TRUE(delElementTxn, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
aTxn->AppendChild(delElementTxn);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-06-12 08:44:44 -07:00
|
|
|
|
|
|
|
NS_ADDREF(*aNode = selectedNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-06-12 08:44:44 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsEditor::CreateRange(nsIDOMNode *aStartParent, int32_t aStartOffset,
|
|
|
|
nsIDOMNode *aEndParent, int32_t aEndOffset,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIDOMRange **aRange)
|
|
|
|
{
|
2012-01-15 00:13:12 -08:00
|
|
|
return nsRange::CreateRange(aStartParent, aStartOffset, aEndParent,
|
|
|
|
aEndOffset, aRange);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsEditor::AppendNodeToSelectionAsRange(nsIDOMNode *aNode)
|
|
|
|
{
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(aNode, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
nsresult res = GetSelection(getter_AddRefs(selection));
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
if(!selection) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> parentNode;
|
|
|
|
res = aNode->GetParentNode(getter_AddRefs(parentNode));
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(parentNode, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t offset = GetChildOffset(aNode, parentNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMRange> range;
|
|
|
|
res = CreateRange(parentNode, offset, parentNode, offset+1, getter_AddRefs(range));
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(range, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return selection->AddRange(range);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsEditor::ClearSelection()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
nsresult res = nsEditor::GetSelection(getter_AddRefs(selection));
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
return selection->RemoveAllRanges();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-06-06 00:35:47 -07:00
|
|
|
nsEditor::CreateHTMLContent(const nsAString& aTag, dom::Element** aContent)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-03-24 01:19:14 -07:00
|
|
|
nsCOMPtr<nsIDocument> doc = GetDocument();
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// XXX Wallpaper over editor bug (editor tries to create elements with an
|
|
|
|
// empty nodename).
|
|
|
|
if (aTag.IsEmpty()) {
|
|
|
|
NS_ERROR("Don't pass an empty tag to nsEditor::CreateHTMLContent, "
|
|
|
|
"check caller.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return doc->CreateElem(aTag, nullptr, kNameSpaceID_XHTML,
|
2012-06-06 00:35:47 -07:00
|
|
|
reinterpret_cast<nsIContent**>(aContent));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsEditor::SetAttributeOrEquivalent(nsIDOMElement * aElement,
|
|
|
|
const nsAString & aAttribute,
|
|
|
|
const nsAString & aValue,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aSuppressTransaction)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return SetAttribute(aElement, aAttribute, aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsEditor::RemoveAttributeOrEquivalent(nsIDOMElement * aElement,
|
|
|
|
const nsAString & aAttribute,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aSuppressTransaction)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return RemoveAttribute(aElement, aAttribute);
|
|
|
|
}
|
|
|
|
|
2010-06-09 18:16:58 -07:00
|
|
|
nsresult
|
|
|
|
nsEditor::HandleKeyPressEvent(nsIDOMKeyEvent* aKeyEvent)
|
|
|
|
{
|
|
|
|
// NOTE: When you change this method, you should also change:
|
|
|
|
// * editor/libeditor/text/tests/test_texteditor_keyevent_handling.html
|
|
|
|
// * editor/libeditor/html/tests/test_htmleditor_keyevent_handling.html
|
|
|
|
//
|
|
|
|
// And also when you add new key handling, you need to change the subclass's
|
|
|
|
// HandleKeyPressEvent()'s switch statement.
|
|
|
|
|
2013-10-17 23:10:24 -07:00
|
|
|
WidgetKeyboardEvent* nativeKeyEvent =
|
|
|
|
aKeyEvent->GetInternalNSEvent()->AsKeyboardEvent();
|
2010-06-09 18:16:58 -07:00
|
|
|
NS_ENSURE_TRUE(nativeKeyEvent, NS_ERROR_UNEXPECTED);
|
|
|
|
NS_ASSERTION(nativeKeyEvent->message == NS_KEY_PRESS,
|
|
|
|
"HandleKeyPressEvent gets non-keypress event");
|
|
|
|
|
|
|
|
// if we are readonly or disabled, then do nothing.
|
|
|
|
if (IsReadonly() || IsDisabled()) {
|
|
|
|
// consume backspace for disabled and readonly textfields, to prevent
|
|
|
|
// back in history, which could be confusing to users
|
|
|
|
if (nativeKeyEvent->keyCode == nsIDOMKeyEvent::DOM_VK_BACK_SPACE) {
|
|
|
|
aKeyEvent->PreventDefault();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (nativeKeyEvent->keyCode) {
|
|
|
|
case nsIDOMKeyEvent::DOM_VK_META:
|
2012-07-18 18:28:17 -07:00
|
|
|
case nsIDOMKeyEvent::DOM_VK_WIN:
|
2010-06-09 18:16:58 -07:00
|
|
|
case nsIDOMKeyEvent::DOM_VK_SHIFT:
|
|
|
|
case nsIDOMKeyEvent::DOM_VK_CONTROL:
|
|
|
|
case nsIDOMKeyEvent::DOM_VK_ALT:
|
|
|
|
aKeyEvent->PreventDefault(); // consumed
|
|
|
|
return NS_OK;
|
|
|
|
case nsIDOMKeyEvent::DOM_VK_BACK_SPACE:
|
2012-04-24 20:00:02 -07:00
|
|
|
if (nativeKeyEvent->IsControl() || nativeKeyEvent->IsAlt() ||
|
2012-07-18 18:28:17 -07:00
|
|
|
nativeKeyEvent->IsMeta() || nativeKeyEvent->IsOS()) {
|
2010-06-09 18:16:58 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-05-10 07:54:33 -07:00
|
|
|
DeleteSelection(nsIEditor::ePrevious, nsIEditor::eStrip);
|
2010-06-09 18:16:58 -07:00
|
|
|
aKeyEvent->PreventDefault(); // consumed
|
|
|
|
return NS_OK;
|
|
|
|
case nsIDOMKeyEvent::DOM_VK_DELETE:
|
|
|
|
// on certain platforms (such as windows) the shift key
|
|
|
|
// modifies what delete does (cmd_cut in this case).
|
|
|
|
// bailing here to allow the keybindings to do the cut.
|
2012-04-24 20:00:02 -07:00
|
|
|
if (nativeKeyEvent->IsShift() || nativeKeyEvent->IsControl() ||
|
2012-07-18 18:28:17 -07:00
|
|
|
nativeKeyEvent->IsAlt() || nativeKeyEvent->IsMeta() ||
|
|
|
|
nativeKeyEvent->IsOS()) {
|
2010-06-09 18:16:58 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-05-10 07:54:33 -07:00
|
|
|
DeleteSelection(nsIEditor::eNext, nsIEditor::eStrip);
|
2010-06-09 18:16:58 -07:00
|
|
|
aKeyEvent->PreventDefault(); // consumed
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
2012-08-12 11:28:26 -07:00
|
|
|
nsEditor::HandleInlineSpellCheck(EditAction action,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsISelection *aSelection,
|
|
|
|
nsIDOMNode *previousSelectedNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t previousSelectedOffset,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIDOMNode *aStartNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aStartOffset,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIDOMNode *aEndNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aEndOffset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-01 05:20:59 -07:00
|
|
|
// Have to cast action here because this method is from an IDL
|
|
|
|
return mInlineSpellChecker ? mInlineSpellChecker->SpellCheckAfterEditorChange(
|
2012-08-22 08:56:38 -07:00
|
|
|
(int32_t)action, aSelection,
|
2012-08-01 05:20:59 -07:00
|
|
|
previousSelectedNode, previousSelectedOffset,
|
|
|
|
aStartNode, aStartOffset, aEndNode,
|
|
|
|
aEndOffset)
|
|
|
|
: NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-06-16 22:30:42 -07:00
|
|
|
already_AddRefed<nsIContent>
|
|
|
|
nsEditor::FindSelectionRoot(nsINode *aNode)
|
|
|
|
{
|
2011-12-03 13:50:15 -08:00
|
|
|
nsCOMPtr<nsIContent> rootContent = GetRoot();
|
2010-06-16 22:30:42 -07:00
|
|
|
return rootContent.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsEditor::InitializeSelection(nsIDOMEventTarget* aFocusEventTarget)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsINode> targetNode = do_QueryInterface(aFocusEventTarget);
|
|
|
|
NS_ENSURE_TRUE(targetNode, NS_ERROR_INVALID_ARG);
|
|
|
|
nsCOMPtr<nsIContent> selectionRootContent = FindSelectionRoot(targetNode);
|
|
|
|
if (!selectionRootContent) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isTargetDoc =
|
2012-02-01 02:54:22 -08:00
|
|
|
targetNode->NodeType() == nsIDOMNode::DOCUMENT_NODE &&
|
2010-06-16 22:30:42 -07:00
|
|
|
targetNode->HasFlag(NODE_IS_EDITABLE);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
nsresult rv = GetSelection(getter_AddRefs(selection));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-05-22 05:43:13 -07:00
|
|
|
nsCOMPtr<nsIPresShell> presShell = GetPresShell();
|
|
|
|
NS_ENSURE_TRUE(presShell, NS_ERROR_NOT_INITIALIZED);
|
2010-06-16 22:30:42 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsISelectionController> selCon;
|
|
|
|
rv = GetSelectionController(getter_AddRefs(selCon));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISelectionPrivate> selectionPrivate =
|
|
|
|
do_QueryInterface(selection);
|
|
|
|
NS_ENSURE_TRUE(selectionPrivate, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
// Init the caret
|
|
|
|
nsRefPtr<nsCaret> caret = presShell->GetCaret();
|
|
|
|
NS_ENSURE_TRUE(caret, NS_ERROR_UNEXPECTED);
|
2011-10-17 07:59:28 -07:00
|
|
|
caret->SetIgnoreUserModify(false);
|
2010-06-16 22:30:42 -07:00
|
|
|
caret->SetCaretDOMSelection(selection);
|
|
|
|
selCon->SetCaretReadOnly(IsReadonly());
|
2011-10-17 07:59:28 -07:00
|
|
|
selCon->SetCaretEnabled(true);
|
2010-06-16 22:30:42 -07:00
|
|
|
|
|
|
|
// Init selection
|
|
|
|
selCon->SetDisplaySelection(nsISelectionController::SELECTION_ON);
|
2011-11-18 13:47:40 -08:00
|
|
|
selCon->SetSelectionFlags(nsISelectionDisplay::DISPLAY_ALL);
|
2010-06-16 22:30:42 -07:00
|
|
|
selCon->RepaintSelection(nsISelectionController::SELECTION_NORMAL);
|
|
|
|
// If the computed selection root isn't root content, we should set it
|
|
|
|
// as selection ancestor limit. However, if that is root element, it means
|
2013-04-02 17:13:10 -07:00
|
|
|
// there is not limitation of the selection, then, we must set nullptr.
|
2010-06-16 22:30:42 -07:00
|
|
|
// NOTE: If we set a root element to the ancestor limit, some selection
|
|
|
|
// methods don't work fine.
|
|
|
|
if (selectionRootContent->GetParent()) {
|
|
|
|
selectionPrivate->SetAncestorLimiter(selectionRootContent);
|
|
|
|
} else {
|
2012-07-30 07:20:58 -07:00
|
|
|
selectionPrivate->SetAncestorLimiter(nullptr);
|
2010-06-16 22:30:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// XXX What case needs this?
|
|
|
|
if (isTargetDoc) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t rangeCount;
|
2010-06-16 22:30:42 -07:00
|
|
|
selection->GetRangeCount(&rangeCount);
|
|
|
|
if (rangeCount == 0) {
|
|
|
|
BeginningOfDocument();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-05-17 17:07:46 -07:00
|
|
|
void
|
|
|
|
nsEditor::FinalizeSelection()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISelectionController> selCon;
|
|
|
|
nsresult rv = GetSelectionController(getter_AddRefs(selCon));
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
rv = selCon->GetSelection(nsISelectionController::SELECTION_NORMAL,
|
|
|
|
getter_AddRefs(selection));
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISelectionPrivate> selectionPrivate = do_QueryInterface(selection);
|
|
|
|
NS_ENSURE_TRUE_VOID(selectionPrivate);
|
|
|
|
|
|
|
|
selectionPrivate->SetAncestorLimiter(nullptr);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPresShell> presShell = GetPresShell();
|
|
|
|
NS_ENSURE_TRUE_VOID(presShell);
|
|
|
|
|
|
|
|
selCon->SetCaretEnabled(false);
|
|
|
|
|
2013-05-21 20:28:43 -07:00
|
|
|
nsFocusManager* fm = nsFocusManager::GetFocusManager();
|
|
|
|
NS_ENSURE_TRUE_VOID(fm);
|
|
|
|
fm->UpdateCaretForCaretBrowsingMode();
|
|
|
|
|
2013-05-17 17:07:46 -07:00
|
|
|
if (!HasIndependentSelection()) {
|
|
|
|
// If this editor doesn't have an independent selection, i.e., it must
|
|
|
|
// mean that it is an HTML editor, the selection controller is shared with
|
|
|
|
// presShell. So, even this editor loses focus, other part of the document
|
|
|
|
// may still have focus.
|
|
|
|
nsCOMPtr<nsIDocument> doc = GetDocument();
|
|
|
|
ErrorResult ret;
|
|
|
|
if (!doc || !doc->HasFocus(ret)) {
|
|
|
|
// If the document already lost focus, mark the selection as disabled.
|
|
|
|
selCon->SetDisplaySelection(nsISelectionController::SELECTION_DISABLED);
|
|
|
|
} else {
|
|
|
|
// Otherwise, mark selection as normal because outside of a
|
|
|
|
// contenteditable element should be selected with normal selection
|
|
|
|
// color after here.
|
|
|
|
selCon->SetDisplaySelection(nsISelectionController::SELECTION_ON);
|
|
|
|
}
|
|
|
|
} else if (IsFormWidget() || IsPasswordEditor() ||
|
|
|
|
IsReadonly() || IsDisabled() || IsInputFiltered()) {
|
|
|
|
// In <input> or <textarea>, the independent selection should be hidden
|
|
|
|
// while this editor doesn't have focus.
|
|
|
|
selCon->SetDisplaySelection(nsISelectionController::SELECTION_HIDDEN);
|
|
|
|
} else {
|
|
|
|
// Otherwise, although we're not sure how this case happens, the
|
|
|
|
// independent selection should be marked as disabled.
|
|
|
|
selCon->SetDisplaySelection(nsISelectionController::SELECTION_DISABLED);
|
|
|
|
}
|
|
|
|
|
|
|
|
selCon->RepaintSelection(nsISelectionController::SELECTION_NORMAL);
|
|
|
|
}
|
|
|
|
|
2011-12-03 13:50:15 -08:00
|
|
|
dom::Element *
|
2007-03-22 10:30:00 -07:00
|
|
|
nsEditor::GetRoot()
|
|
|
|
{
|
|
|
|
if (!mRootElement)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMElement> root;
|
|
|
|
|
|
|
|
// Let GetRootElement() do the work
|
|
|
|
GetRootElement(getter_AddRefs(root));
|
|
|
|
}
|
|
|
|
|
|
|
|
return mRootElement;
|
|
|
|
}
|
|
|
|
|
2012-05-06 00:53:11 -07:00
|
|
|
dom::Element*
|
|
|
|
nsEditor::GetEditorRoot()
|
|
|
|
{
|
|
|
|
return GetRoot();
|
|
|
|
}
|
|
|
|
|
2014-01-17 07:31:03 -08:00
|
|
|
Element*
|
|
|
|
nsEditor::GetExposedRoot()
|
|
|
|
{
|
|
|
|
Element* rootElement = GetRoot();
|
|
|
|
|
|
|
|
// For plaintext editors, we need to ask the input/textarea element directly.
|
|
|
|
if (rootElement && rootElement->IsRootOfNativeAnonymousSubtree()) {
|
|
|
|
rootElement = rootElement->GetParent()->AsElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
return rootElement;
|
|
|
|
}
|
|
|
|
|
2011-09-15 07:54:50 -07:00
|
|
|
nsresult
|
|
|
|
nsEditor::DetermineCurrentDirection()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Get the current root direction from its frame
|
2014-01-17 07:31:03 -08:00
|
|
|
nsIContent* rootElement = GetExposedRoot();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-01-26 21:26:33 -08:00
|
|
|
// If we don't have an explicit direction, determine our direction
|
|
|
|
// from the content's direction
|
|
|
|
if (!(mFlags & (nsIPlaintextEditor::eEditorLeftToRight |
|
|
|
|
nsIPlaintextEditor::eEditorRightToLeft))) {
|
|
|
|
|
2011-12-03 13:50:15 -08:00
|
|
|
nsIFrame* frame = rootElement->GetPrimaryFrame();
|
2011-01-26 21:26:33 -08:00
|
|
|
NS_ENSURE_TRUE(frame, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
// Set the flag here, to enable us to use the same code path below.
|
|
|
|
// It will be flipped before returning from the function.
|
2013-02-16 13:51:02 -08:00
|
|
|
if (frame->StyleVisibility()->mDirection == NS_STYLE_DIRECTION_RTL) {
|
2011-01-26 21:26:33 -08:00
|
|
|
mFlags |= nsIPlaintextEditor::eEditorRightToLeft;
|
|
|
|
} else {
|
|
|
|
mFlags |= nsIPlaintextEditor::eEditorLeftToRight;
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-15 07:54:50 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::SwitchTextDirection()
|
|
|
|
{
|
|
|
|
// Get the current root direction from its frame
|
2014-01-17 07:31:03 -08:00
|
|
|
nsIContent* rootElement = GetExposedRoot();
|
|
|
|
|
2011-09-15 07:54:50 -07:00
|
|
|
nsresult rv = DetermineCurrentDirection();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Apply the opposite direction
|
2011-01-26 21:26:33 -08:00
|
|
|
if (mFlags & nsIPlaintextEditor::eEditorRightToLeft) {
|
|
|
|
NS_ASSERTION(!(mFlags & nsIPlaintextEditor::eEditorLeftToRight),
|
|
|
|
"Unexpected mutually exclusive flag");
|
|
|
|
mFlags &= ~nsIPlaintextEditor::eEditorRightToLeft;
|
|
|
|
mFlags |= nsIPlaintextEditor::eEditorLeftToRight;
|
2011-12-03 13:50:15 -08:00
|
|
|
rv = rootElement->SetAttr(kNameSpaceID_None, nsGkAtoms::dir, NS_LITERAL_STRING("ltr"), true);
|
2011-01-26 21:26:33 -08:00
|
|
|
} else if (mFlags & nsIPlaintextEditor::eEditorLeftToRight) {
|
|
|
|
NS_ASSERTION(!(mFlags & nsIPlaintextEditor::eEditorRightToLeft),
|
|
|
|
"Unexpected mutually exclusive flag");
|
|
|
|
mFlags |= nsIPlaintextEditor::eEditorRightToLeft;
|
|
|
|
mFlags &= ~nsIPlaintextEditor::eEditorLeftToRight;
|
2011-12-03 13:50:15 -08:00
|
|
|
rv = rootElement->SetAttr(kNameSpaceID_None, nsGkAtoms::dir, NS_LITERAL_STRING("rtl"), true);
|
2011-01-26 21:26:33 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-17 07:31:18 -08:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
FireInputEvent();
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-09-15 07:54:50 -07:00
|
|
|
void
|
2012-08-22 08:56:38 -07:00
|
|
|
nsEditor::SwitchTextDirectionTo(uint32_t aDirection)
|
2011-09-15 07:54:50 -07:00
|
|
|
{
|
|
|
|
// Get the current root direction from its frame
|
2014-01-17 07:31:03 -08:00
|
|
|
nsIContent* rootElement = GetExposedRoot();
|
|
|
|
|
2011-09-15 07:54:50 -07:00
|
|
|
nsresult rv = DetermineCurrentDirection();
|
2012-09-14 03:00:31 -07:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
2011-09-15 07:54:50 -07:00
|
|
|
|
|
|
|
// Apply the requested direction
|
|
|
|
if (aDirection == nsIPlaintextEditor::eEditorLeftToRight &&
|
|
|
|
(mFlags & nsIPlaintextEditor::eEditorRightToLeft)) {
|
|
|
|
NS_ASSERTION(!(mFlags & nsIPlaintextEditor::eEditorLeftToRight),
|
|
|
|
"Unexpected mutually exclusive flag");
|
|
|
|
mFlags &= ~nsIPlaintextEditor::eEditorRightToLeft;
|
|
|
|
mFlags |= nsIPlaintextEditor::eEditorLeftToRight;
|
2014-01-17 07:31:18 -08:00
|
|
|
rv = rootElement->SetAttr(kNameSpaceID_None, nsGkAtoms::dir, NS_LITERAL_STRING("ltr"), true);
|
2011-09-15 07:54:50 -07:00
|
|
|
} else if (aDirection == nsIPlaintextEditor::eEditorRightToLeft &&
|
|
|
|
(mFlags & nsIPlaintextEditor::eEditorLeftToRight)) {
|
|
|
|
NS_ASSERTION(!(mFlags & nsIPlaintextEditor::eEditorRightToLeft),
|
|
|
|
"Unexpected mutually exclusive flag");
|
|
|
|
mFlags |= nsIPlaintextEditor::eEditorRightToLeft;
|
|
|
|
mFlags &= ~nsIPlaintextEditor::eEditorLeftToRight;
|
2014-01-17 07:31:18 -08:00
|
|
|
rv = rootElement->SetAttr(kNameSpaceID_None, nsGkAtoms::dir, NS_LITERAL_STRING("rtl"), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
FireInputEvent();
|
2011-09-15 07:54:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#if DEBUG_JOE
|
|
|
|
void
|
2012-08-22 08:56:38 -07:00
|
|
|
nsEditor::DumpNode(nsIDOMNode *aNode, int32_t indent)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t i;
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i=0; i<indent; i++)
|
|
|
|
printf(" ");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMElement> element = do_QueryInterface(aNode);
|
|
|
|
nsCOMPtr<nsIDOMDocumentFragment> docfrag = do_QueryInterface(aNode);
|
|
|
|
|
|
|
|
if (element || docfrag)
|
|
|
|
{
|
|
|
|
if (element)
|
|
|
|
{
|
|
|
|
nsAutoString tag;
|
|
|
|
element->GetTagName(tag);
|
|
|
|
printf("<%s>\n", NS_LossyConvertUTF16toASCII(tag).get());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("<document fragment>\n");
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIDOMNodeList> childList;
|
|
|
|
aNode->GetChildNodes(getter_AddRefs(childList));
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(childList, NS_ERROR_NULL_POINTER);
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t numChildren;
|
2007-03-22 10:30:00 -07:00
|
|
|
childList->GetLength(&numChildren);
|
|
|
|
nsCOMPtr<nsIDOMNode> child, tmp;
|
|
|
|
aNode->GetFirstChild(getter_AddRefs(child));
|
|
|
|
for (i=0; i<numChildren; i++)
|
|
|
|
{
|
|
|
|
DumpNode(child, indent+1);
|
|
|
|
child->GetNextSibling(getter_AddRefs(tmp));
|
|
|
|
child = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (IsTextNode(aNode))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> textNode = do_QueryInterface(aNode);
|
|
|
|
nsAutoString str;
|
|
|
|
textNode->GetData(str);
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString cstr;
|
2007-03-22 10:30:00 -07:00
|
|
|
LossyCopyUTF16toASCII(str, cstr);
|
|
|
|
cstr.ReplaceChar('\n', ' ');
|
|
|
|
printf("<textnode> %s\n", cstr.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2007-06-27 19:48:16 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-06-27 19:48:16 -07:00
|
|
|
nsEditor::IsModifiableNode(nsIDOMNode *aNode)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-06-27 19:48:16 -07:00
|
|
|
}
|
2010-05-04 10:40:39 -07:00
|
|
|
|
2011-11-28 04:53:29 -08:00
|
|
|
bool
|
|
|
|
nsEditor::IsModifiableNode(nsINode *aNode)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-10-29 09:30:52 -07:00
|
|
|
already_AddRefed<nsIContent>
|
|
|
|
nsEditor::GetFocusedContent()
|
2010-05-04 10:40:39 -07:00
|
|
|
{
|
2011-06-23 19:18:01 -07:00
|
|
|
nsCOMPtr<nsIDOMEventTarget> piTarget = GetDOMEventTarget();
|
2010-05-04 10:40:39 -07:00
|
|
|
if (!piTarget) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-05-04 10:40:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsFocusManager* fm = nsFocusManager::GetFocusManager();
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(fm, nullptr);
|
2010-05-04 10:40:39 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> content = fm->GetFocusedContent();
|
2012-07-30 07:20:58 -07:00
|
|
|
return SameCOMIdentity(content, piTarget) ? content.forget() : nullptr;
|
2010-05-04 10:40:39 -07:00
|
|
|
}
|
2010-07-22 09:22:44 -07:00
|
|
|
|
2012-11-09 00:40:39 -08:00
|
|
|
already_AddRefed<nsIContent>
|
|
|
|
nsEditor::GetFocusedContentForIME()
|
|
|
|
{
|
|
|
|
return GetFocusedContent();
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-07-22 09:22:44 -07:00
|
|
|
nsEditor::IsActiveInDOMWindow()
|
|
|
|
{
|
2011-06-23 19:18:01 -07:00
|
|
|
nsCOMPtr<nsIDOMEventTarget> piTarget = GetDOMEventTarget();
|
2010-07-22 09:22:44 -07:00
|
|
|
if (!piTarget) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-07-22 09:22:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsFocusManager* fm = nsFocusManager::GetFocusManager();
|
2011-10-17 07:59:28 -07:00
|
|
|
NS_ENSURE_TRUE(fm, false);
|
2010-07-22 09:22:44 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocWeak);
|
|
|
|
nsPIDOMWindow* ourWindow = doc->GetWindow();
|
|
|
|
nsCOMPtr<nsPIDOMWindow> win;
|
|
|
|
nsIContent* content =
|
2011-10-17 07:59:28 -07:00
|
|
|
nsFocusManager::GetFocusedDescendant(ourWindow, false,
|
2010-07-22 09:22:44 -07:00
|
|
|
getter_AddRefs(win));
|
|
|
|
return SameCOMIdentity(content, piTarget);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-07-22 09:22:44 -07:00
|
|
|
nsEditor::IsAcceptableInputEvent(nsIDOMEvent* aEvent)
|
|
|
|
{
|
|
|
|
// If the event is trusted, the event should always cause input.
|
2012-08-04 00:44:00 -07:00
|
|
|
NS_ENSURE_TRUE(aEvent, false);
|
2010-07-22 09:22:44 -07:00
|
|
|
|
2011-11-25 20:51:48 -08:00
|
|
|
// If this is mouse event but this editor doesn't have focus, we shouldn't
|
|
|
|
// handle it.
|
|
|
|
nsCOMPtr<nsIDOMMouseEvent> mouseEvent = do_QueryInterface(aEvent);
|
|
|
|
if (mouseEvent) {
|
|
|
|
nsCOMPtr<nsIContent> focusedContent = GetFocusedContent();
|
|
|
|
if (!focusedContent) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-12 05:02:56 -08:00
|
|
|
} else {
|
|
|
|
nsAutoString eventType;
|
|
|
|
aEvent->GetType(eventType);
|
|
|
|
// If composition event or text event isn't dispatched via widget,
|
|
|
|
// we need to ignore them since they cannot be managed by TextComposition.
|
|
|
|
// E.g., the event was created by chrome JS.
|
|
|
|
// Note that if we allow to handle such events, editor may be confused by
|
|
|
|
// strange event order.
|
|
|
|
if (eventType.EqualsLiteral("text") ||
|
|
|
|
eventType.EqualsLiteral("compositionstart") ||
|
|
|
|
eventType.EqualsLiteral("compositionend")) {
|
|
|
|
WidgetGUIEvent* widgetGUIEvent =
|
|
|
|
aEvent->GetInternalNSEvent()->AsGUIEvent();
|
|
|
|
if (!widgetGUIEvent || !widgetGUIEvent->widget) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2011-11-25 20:51:48 -08:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isTrusted;
|
2012-08-04 00:44:00 -07:00
|
|
|
nsresult rv = aEvent->GetIsTrusted(&isTrusted);
|
2011-10-17 07:59:28 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
2010-07-22 09:22:44 -07:00
|
|
|
if (isTrusted) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-07-22 09:22:44 -07:00
|
|
|
}
|
2011-11-25 20:51:48 -08:00
|
|
|
|
|
|
|
// Ignore untrusted mouse event.
|
|
|
|
// XXX Why are we handling other untrusted input events?
|
|
|
|
if (mouseEvent) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-22 09:22:44 -07:00
|
|
|
// Otherwise, we shouldn't handle any input events when we're not an active
|
|
|
|
// element of the DOM window.
|
|
|
|
return IsActiveInDOMWindow();
|
|
|
|
}
|
2010-12-06 22:30:05 -08:00
|
|
|
|
2012-03-26 18:36:44 -07:00
|
|
|
void
|
|
|
|
nsEditor::OnFocus(nsIDOMEventTarget* aFocusEventTarget)
|
2010-12-06 22:30:05 -08:00
|
|
|
{
|
2012-03-26 18:36:44 -07:00
|
|
|
InitializeSelection(aFocusEventTarget);
|
|
|
|
if (mInlineSpellChecker) {
|
|
|
|
mInlineSpellChecker->UpdateCurrentDictionary();
|
2010-12-06 22:30:05 -08:00
|
|
|
}
|
2012-03-26 18:36:44 -07:00
|
|
|
}
|
2010-12-06 22:30:05 -08:00
|
|
|
|
2012-03-26 18:36:44 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::GetSuppressDispatchingInputEvent(bool *aSuppressed)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aSuppressed);
|
|
|
|
*aSuppressed = !mDispatchInputEvent;
|
2010-12-06 22:30:05 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-26 18:36:44 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEditor::SetSuppressDispatchingInputEvent(bool aSuppress)
|
2010-12-06 22:30:05 -08:00
|
|
|
{
|
2012-03-26 18:36:44 -07:00
|
|
|
mDispatchInputEvent = !aSuppress;
|
|
|
|
return NS_OK;
|
2010-12-06 22:30:05 -08:00
|
|
|
}
|