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
|
|
|
|
|
|
|
#ifndef nsTextEditRules_h__
|
|
|
|
#define nsTextEditRules_h__
|
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsEditRules.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsEditor.h"
|
|
|
|
#include "nsIEditor.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
2009-11-02 07:37:25 -08:00
|
|
|
#include "nsITimer.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsPlaintextEditor.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nscore.h"
|
|
|
|
|
|
|
|
class nsIDOMElement;
|
|
|
|
class nsIDOMNode;
|
|
|
|
class nsISelection;
|
|
|
|
namespace mozilla {
|
2014-04-10 09:09:40 -07:00
|
|
|
namespace dom {
|
2012-07-12 23:33:42 -07:00
|
|
|
class Selection;
|
2014-04-10 09:09:40 -07:00
|
|
|
} // namespace dom
|
2012-07-12 23:33:42 -07:00
|
|
|
} // namespace mozilla
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/** Object that encapsulates HTML text-specific editing rules.
|
|
|
|
*
|
|
|
|
* To be a good citizen, edit rules must live by these restrictions:
|
|
|
|
* 1. All data manipulation is through the editor.
|
|
|
|
* Content nodes in the document tree must <B>not</B> be manipulated directly.
|
|
|
|
* Content nodes in document fragments that are not part of the document itself
|
|
|
|
* may be manipulated at will. Operations on document fragments must <B>not</B>
|
|
|
|
* go through the editor.
|
|
|
|
* 2. Selection must not be explicitly set by the rule method.
|
|
|
|
* Any manipulation of Selection must be done by the editor.
|
|
|
|
*/
|
2009-11-02 07:37:25 -08:00
|
|
|
class nsTextEditRules : public nsIEditRules, public nsITimerCallback
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
2009-11-02 07:37:25 -08:00
|
|
|
NS_DECL_NSITIMERCALLBACK
|
2009-05-08 21:59:25 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2009-11-02 07:37:25 -08:00
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsTextEditRules, nsIEditRules)
|
2014-06-23 15:40:02 -07:00
|
|
|
|
|
|
|
nsTextEditRules();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// nsIEditRules methods
|
2010-04-11 19:35:18 -07:00
|
|
|
NS_IMETHOD Init(nsPlaintextEditor *aEditor);
|
2014-04-14 05:33:47 -07:00
|
|
|
NS_IMETHOD SetInitialValue(const nsAString& aValue);
|
2009-05-08 21:59:24 -07:00
|
|
|
NS_IMETHOD DetachEditor();
|
2012-08-12 11:28:26 -07:00
|
|
|
NS_IMETHOD BeforeEdit(EditAction action,
|
2012-05-05 11:52:29 -07:00
|
|
|
nsIEditor::EDirection aDirection);
|
2012-08-12 11:28:26 -07:00
|
|
|
NS_IMETHOD AfterEdit(EditAction action,
|
2012-05-05 11:52:29 -07:00
|
|
|
nsIEditor::EDirection aDirection);
|
2014-04-10 09:09:40 -07:00
|
|
|
NS_IMETHOD WillDoAction(mozilla::dom::Selection* aSelection,
|
|
|
|
nsRulesInfo* aInfo, bool* aCancel, bool* aHandled);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHOD DidDoAction(nsISelection *aSelection, nsRulesInfo *aInfo, nsresult aResult);
|
2011-09-28 23:19:26 -07:00
|
|
|
NS_IMETHOD DocumentIsEmpty(bool *aDocumentIsEmpty);
|
2010-11-11 13:40:52 -08:00
|
|
|
NS_IMETHOD DocumentModified();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-23 15:40:02 -07:00
|
|
|
protected:
|
|
|
|
virtual ~nsTextEditRules();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
public:
|
2012-06-01 04:03:17 -07:00
|
|
|
void ResetIMETextPWBuf();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-02-01 10:12:31 -08:00
|
|
|
/**
|
|
|
|
* Handles the newline characters either according to aNewLineHandling
|
|
|
|
* or to the default system prefs if aNewLineHandling is negative.
|
|
|
|
*
|
|
|
|
* @param aString the string to be modified in place.
|
|
|
|
* @param aNewLineHandling determine the desired type of newline handling:
|
|
|
|
* * negative values:
|
|
|
|
* handle newlines according to platform defaults.
|
|
|
|
* * nsIPlaintextEditor::eNewlinesReplaceWithSpaces:
|
|
|
|
* replace newlines with spaces.
|
|
|
|
* * nsIPlaintextEditor::eNewlinesStrip:
|
|
|
|
* remove newlines from the string.
|
|
|
|
* * nsIPlaintextEditor::eNewlinesReplaceWithCommas:
|
|
|
|
* replace newlines with commas.
|
|
|
|
* * nsIPlaintextEditor::eNewlinesStripSurroundingWhitespace:
|
|
|
|
* collapse newlines and surrounding whitespace characters and
|
|
|
|
* remove them from the string.
|
|
|
|
* * nsIPlaintextEditor::eNewlinesPasteIntact:
|
|
|
|
* only remove the leading and trailing newlines.
|
|
|
|
* * nsIPlaintextEditor::eNewlinesPasteToFirst or any other value:
|
|
|
|
* remove the first newline and all characters following it.
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
static void HandleNewLines(nsString &aString, int32_t aNewLineHandling);
|
2010-02-01 10:12:31 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare a string buffer for being displayed as the contents of a password
|
|
|
|
* field. This function uses the platform-specific character for representing
|
|
|
|
* characters entered into password fields.
|
|
|
|
*
|
|
|
|
* @param aOutString the output string. When this function returns,
|
|
|
|
* aOutString will contain aLength password characters.
|
|
|
|
* @param aLength the number of password characters that aOutString should
|
|
|
|
* contain.
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
static void FillBufWithPWChars(nsAString *aOutString, int32_t aLength);
|
2010-02-01 10:12:31 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
protected:
|
|
|
|
|
2014-05-09 14:02:29 -07:00
|
|
|
void InitFields();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// nsTextEditRules implementation methods
|
2012-08-12 11:28:26 -07:00
|
|
|
nsresult WillInsertText( EditAction aAction,
|
2014-04-10 09:09:40 -07:00
|
|
|
mozilla::dom::Selection* aSelection,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool *aCancel,
|
|
|
|
bool *aHandled,
|
2007-03-22 10:30:00 -07:00
|
|
|
const nsAString *inString,
|
|
|
|
nsAString *outString,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aMaxLength);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult DidInsertText(nsISelection *aSelection, nsresult aResult);
|
|
|
|
nsresult GetTopEnclosingPre(nsIDOMNode *aNode, nsIDOMNode** aOutPreNode);
|
|
|
|
|
2014-04-10 09:09:40 -07:00
|
|
|
nsresult WillInsertBreak(mozilla::dom::Selection* aSelection, bool* aCancel,
|
2012-08-22 08:56:38 -07:00
|
|
|
bool *aHandled, int32_t aMaxLength);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult DidInsertBreak(nsISelection *aSelection, nsresult aResult);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
nsresult WillInsert(nsISelection *aSelection, bool *aCancel);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult DidInsert(nsISelection *aSelection, nsresult aResult);
|
|
|
|
|
2014-04-10 09:09:40 -07:00
|
|
|
nsresult WillDeleteSelection(mozilla::dom::Selection* aSelection,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIEditor::EDirection aCollapsedAction,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool *aCancel,
|
|
|
|
bool *aHandled);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult DidDeleteSelection(nsISelection *aSelection,
|
|
|
|
nsIEditor::EDirection aCollapsedAction,
|
|
|
|
nsresult aResult);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
nsresult WillSetTextProperty(nsISelection *aSelection, bool *aCancel, bool *aHandled);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult DidSetTextProperty(nsISelection *aSelection, nsresult aResult);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
nsresult WillRemoveTextProperty(nsISelection *aSelection, bool *aCancel, bool *aHandled);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult DidRemoveTextProperty(nsISelection *aSelection, nsresult aResult);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
nsresult WillUndo(nsISelection *aSelection, bool *aCancel, bool *aHandled);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult DidUndo(nsISelection *aSelection, nsresult aResult);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
nsresult WillRedo(nsISelection *aSelection, bool *aCancel, bool *aHandled);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult DidRedo(nsISelection *aSelection, nsresult aResult);
|
|
|
|
|
|
|
|
/** called prior to nsIEditor::OutputToString
|
|
|
|
* @param aSelection
|
|
|
|
* @param aInFormat the format requested for the output, a MIME type
|
|
|
|
* @param aOutText the string to use for output, if aCancel is set to true
|
2011-10-17 07:59:28 -07:00
|
|
|
* @param aOutCancel if set to true, the caller should cancel the operation
|
2007-03-22 10:30:00 -07:00
|
|
|
* and use aOutText as the result.
|
|
|
|
*/
|
|
|
|
nsresult WillOutputText(nsISelection *aSelection,
|
|
|
|
const nsAString *aInFormat,
|
|
|
|
nsAString *aOutText,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool *aOutCancel,
|
|
|
|
bool *aHandled);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult DidOutputText(nsISelection *aSelection, nsresult aResult);
|
|
|
|
|
|
|
|
|
|
|
|
// helper functions
|
2011-08-12 12:53:10 -07:00
|
|
|
|
|
|
|
/** check for and replace a redundant trailing break */
|
|
|
|
nsresult RemoveRedundantTrailingBR();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/** creates a trailing break in the text doc if there is not one already */
|
|
|
|
nsresult CreateTrailingBRIfNeeded();
|
|
|
|
|
|
|
|
/** creates a bogus text node if the document has no editable content */
|
|
|
|
nsresult CreateBogusNodeIfNeeded(nsISelection *aSelection);
|
|
|
|
|
|
|
|
/** returns a truncated insertion string if insertion would place us
|
|
|
|
over aMaxLength */
|
2014-04-10 09:09:40 -07:00
|
|
|
nsresult TruncateInsertionIfNeeded(mozilla::dom::Selection* aSelection,
|
2007-03-22 10:30:00 -07:00
|
|
|
const nsAString *aInString,
|
|
|
|
nsAString *aOutString,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aMaxLength,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool *aTruncated);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/** Remove IME composition text from password buffer */
|
2012-08-22 08:56:38 -07:00
|
|
|
void RemoveIMETextFromPWBuf(int32_t &aStart, nsAString *aIMEString);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult CreateMozBR(nsIDOMNode* inParent, int32_t inOffset,
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIDOMNode** outBRNode = nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult CheckBidiLevelForDeletion(nsISelection *aSelection,
|
|
|
|
nsIDOMNode *aSelNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aSelOffset,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIEditor::EDirection aAction,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool *aCancel);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-11-02 07:37:25 -08:00
|
|
|
nsresult HideLastPWInput();
|
|
|
|
|
2010-09-01 15:06:52 -07:00
|
|
|
nsresult CollapseSelectionToTrailingBRIfNeeded(nsISelection *aSelection);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsPasswordEditor() const
|
2010-04-11 19:35:18 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return mEditor ? mEditor->IsPasswordEditor() : false;
|
2010-04-11 19:35:18 -07:00
|
|
|
}
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsSingleLineEditor() const
|
2010-04-11 19:35:18 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return mEditor ? mEditor->IsSingleLineEditor() : false;
|
2010-04-11 19:35:18 -07:00
|
|
|
}
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsPlaintextEditor() const
|
2010-04-11 19:35:18 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return mEditor ? mEditor->IsPlaintextEditor() : false;
|
2010-04-11 19:35:18 -07:00
|
|
|
}
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsReadonly() const
|
2010-04-11 19:35:18 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return mEditor ? mEditor->IsReadonly() : false;
|
2010-04-11 19:35:18 -07:00
|
|
|
}
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsDisabled() const
|
2010-04-11 19:35:18 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return mEditor ? mEditor->IsDisabled() : false;
|
2010-04-11 19:35:18 -07:00
|
|
|
}
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsMailEditor() const
|
2010-04-11 19:35:18 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return mEditor ? mEditor->IsMailEditor() : false;
|
2010-04-11 19:35:18 -07:00
|
|
|
}
|
2011-09-28 23:19:26 -07:00
|
|
|
bool DontEchoPassword() const
|
2010-04-11 19:35:18 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return mEditor ? mEditor->DontEchoPassword() : false;
|
2010-04-11 19:35:18 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// data members
|
|
|
|
nsPlaintextEditor *mEditor; // note that we do not refcount the editor
|
|
|
|
nsString mPasswordText; // a buffer we use to store the real value of password editors
|
|
|
|
nsString mPasswordIMEText; // a buffer we use to track the IME composition string
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mPasswordIMEIndex;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIDOMNode> mBogusNode; // magic node acts as placeholder in empty doc
|
|
|
|
nsCOMPtr<nsIDOMNode> mCachedSelectionNode; // cached selected node
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mCachedSelectionOffset; // cached selected offset
|
|
|
|
uint32_t mActionNesting;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mLockRulesSniffing;
|
|
|
|
bool mDidExplicitlySetInterline;
|
|
|
|
bool mDeleteBidiImmediately; // in bidirectional text, delete
|
2007-03-22 10:30:00 -07:00
|
|
|
// characters not visually
|
|
|
|
// adjacent to the caret without
|
|
|
|
// moving the caret first.
|
2012-08-12 11:28:26 -07:00
|
|
|
EditAction mTheAction; // the top level editor action
|
2009-11-02 07:37:25 -08:00
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mLastStart, mLastLength;
|
2009-11-02 07:37:25 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// friends
|
|
|
|
friend class nsAutoLockRulesSniffing;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class nsTextRulesInfo : public nsRulesInfo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2012-08-12 11:28:26 -07:00
|
|
|
nsTextRulesInfo(EditAction aAction) :
|
2007-03-22 10:30:00 -07:00
|
|
|
nsRulesInfo(aAction),
|
|
|
|
inString(0),
|
|
|
|
outString(0),
|
|
|
|
outputFormat(0),
|
|
|
|
maxLength(-1),
|
|
|
|
collapsedAction(nsIEditor::eNext),
|
2012-05-10 07:54:33 -07:00
|
|
|
stripWrappers(nsIEditor::eStrip),
|
2011-10-17 07:59:28 -07:00
|
|
|
bOrdered(false),
|
|
|
|
entireList(false),
|
2007-03-22 10:30:00 -07:00
|
|
|
bulletType(0),
|
|
|
|
alignType(0),
|
|
|
|
blockType(0),
|
|
|
|
insertElement(0)
|
2007-04-23 07:21:53 -07:00
|
|
|
{}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-04-23 07:21:53 -07:00
|
|
|
virtual ~nsTextRulesInfo() {}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// kInsertText
|
|
|
|
const nsAString *inString;
|
|
|
|
nsAString *outString;
|
|
|
|
const nsAString *outputFormat;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t maxLength;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// kDeleteSelection
|
|
|
|
nsIEditor::EDirection collapsedAction;
|
2012-05-10 07:54:33 -07:00
|
|
|
nsIEditor::EStripWrappers stripWrappers;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// kMakeList
|
2011-09-28 23:19:26 -07:00
|
|
|
bool bOrdered;
|
|
|
|
bool entireList;
|
2007-03-22 10:30:00 -07:00
|
|
|
const nsAString *bulletType;
|
|
|
|
|
|
|
|
// kAlign
|
|
|
|
const nsAString *alignType;
|
|
|
|
|
|
|
|
// kMakeBasicBlock
|
|
|
|
const nsAString *blockType;
|
|
|
|
|
|
|
|
// kInsertElement
|
|
|
|
const nsIDOMElement* insertElement;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
* stack based helper class for StartOperation()/EndOperation() sandwich.
|
|
|
|
* this class sets a bool letting us know to ignore any rules sniffing
|
|
|
|
* that tries to occur reentrantly.
|
|
|
|
*/
|
|
|
|
class nsAutoLockRulesSniffing
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
nsAutoLockRulesSniffing(nsTextEditRules *rules) : mRules(rules)
|
2011-10-17 07:59:28 -07:00
|
|
|
{if (mRules) mRules->mLockRulesSniffing = true;}
|
2007-03-22 10:30:00 -07:00
|
|
|
~nsAutoLockRulesSniffing()
|
2011-10-17 07:59:28 -07:00
|
|
|
{if (mRules) mRules->mLockRulesSniffing = false;}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
protected:
|
|
|
|
nsTextEditRules *mRules;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
* stack based helper class for turning on/off the edit listener.
|
|
|
|
*/
|
|
|
|
class nsAutoLockListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
nsAutoLockListener(bool *enabled) : mEnabled(enabled)
|
2011-10-17 07:59:28 -07:00
|
|
|
{if (mEnabled) { mOldState=*mEnabled; *mEnabled = false;}}
|
2007-03-22 10:30:00 -07:00
|
|
|
~nsAutoLockListener()
|
|
|
|
{if (mEnabled) *mEnabled = mOldState;}
|
|
|
|
|
|
|
|
protected:
|
2011-09-28 23:19:26 -07:00
|
|
|
bool *mEnabled;
|
|
|
|
bool mOldState;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif //nsTextEditRules_h__
|