gecko-dev/editor/libeditor/TextEditRules.cpp

1819 lines
52 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2012-05-21 11:12:37 +00: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/. */
#include "mozilla/TextEditRules.h"
#include "HTMLEditRules.h"
#include "TextEditUtils.h"
#include "mozilla/Assertions.h"
#include "mozilla/EditAction.h"
#include "mozilla/EditorDOMPoint.h"
#include "mozilla/EditorUtils.h"
#include "mozilla/LookAndFeel.h"
#include "mozilla/Preferences.h"
#include "mozilla/TextComposition.h"
#include "mozilla/TextEditor.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/NodeFilterBinding.h"
#include "mozilla/dom/NodeIterator.h"
#include "mozilla/dom/Selection.h"
#include "nsAString.h"
#include "nsCOMPtr.h"
#include "nsCRT.h"
#include "nsCRTGlue.h"
#include "nsComponentManagerUtils.h"
#include "nsContentUtils.h"
#include "nsDebug.h"
#include "nsError.h"
#include "nsGkAtoms.h"
#include "nsIContent.h"
#include "nsIDocumentEncoder.h"
#include "nsNameSpaceManager.h"
#include "nsINode.h"
#include "nsIPlaintextEditor.h"
#include "nsISupportsBase.h"
#include "nsLiteralString.h"
#include "nsTextNode.h"
#include "nsUnicharUtils.h"
#include "nsIHTMLCollection.h"
#include "nsPrintfCString.h"
namespace mozilla {
using namespace dom;
Preparation for ender-based text control * added focus listener. Doesn't do much yet, but when focus notifications start appearing, we'll be ready for them. The code is in place to hide selection when we lose focus and paint selection when we get focus. That's probably not quite right, but it's a start. We will need to be able to determine the distinction between losing focus to another control within our app, and losing focus to another app. * added support for disabled and readonly states in the editor. This is accomplished by having flags set by the client, and letting the rules system deal with those flags. The flags I added are: TEXT_EDITOR_FLAG_PLAINTEXT 0x01 // only plain text editing is allowed TEXT_EDITOR_FLAG_SINGLELINE 0x02 // enter key and CR-LF handled specially TEXT_EDITOR_FLAG_PASSWORD 0x04 // text is not entered into content, only a representative character TEXT_EDITOR_FLAG_READONLY 0x08 // editing events are disabled. Editor may still accept focus. TEXT_EDITOR_FLAG_DISALBED 0x10 // all events are disabled (like scrolling). Editor will not accept focus. * added WillInsertBreak/DidInsertBreak into text rules, so flags could be checked. This gets us readonly, disabled, and single line behavior. * cleaned up the code that allocates, registers, and destroys event listeners. Thanks to Kin and Simon for cleaning up the ownership model on the listeners, it was a big help. * added support for a max text length. You can now tell the text editor, be no bigger than n characters.
1999-05-28 21:24:18 +00:00
#define CANCEL_OPERATION_IF_READONLY_OR_DISABLED \
if (IsReadonly() || IsDisabled()) \
Preparation for ender-based text control * added focus listener. Doesn't do much yet, but when focus notifications start appearing, we'll be ready for them. The code is in place to hide selection when we lose focus and paint selection when we get focus. That's probably not quite right, but it's a start. We will need to be able to determine the distinction between losing focus to another control within our app, and losing focus to another app. * added support for disabled and readonly states in the editor. This is accomplished by having flags set by the client, and letting the rules system deal with those flags. The flags I added are: TEXT_EDITOR_FLAG_PLAINTEXT 0x01 // only plain text editing is allowed TEXT_EDITOR_FLAG_SINGLELINE 0x02 // enter key and CR-LF handled specially TEXT_EDITOR_FLAG_PASSWORD 0x04 // text is not entered into content, only a representative character TEXT_EDITOR_FLAG_READONLY 0x08 // editing events are disabled. Editor may still accept focus. TEXT_EDITOR_FLAG_DISALBED 0x10 // all events are disabled (like scrolling). Editor will not accept focus. * added WillInsertBreak/DidInsertBreak into text rules, so flags could be checked. This gets us readonly, disabled, and single line behavior. * cleaned up the code that allocates, registers, and destroys event listeners. Thanks to Kin and Simon for cleaning up the ownership model on the listeners, it was a big help. * added support for a max text length. You can now tell the text editor, be no bigger than n characters.
1999-05-28 21:24:18 +00:00
{ \
*aCancel = true; \
Preparation for ender-based text control * added focus listener. Doesn't do much yet, but when focus notifications start appearing, we'll be ready for them. The code is in place to hide selection when we lose focus and paint selection when we get focus. That's probably not quite right, but it's a start. We will need to be able to determine the distinction between losing focus to another control within our app, and losing focus to another app. * added support for disabled and readonly states in the editor. This is accomplished by having flags set by the client, and letting the rules system deal with those flags. The flags I added are: TEXT_EDITOR_FLAG_PLAINTEXT 0x01 // only plain text editing is allowed TEXT_EDITOR_FLAG_SINGLELINE 0x02 // enter key and CR-LF handled specially TEXT_EDITOR_FLAG_PASSWORD 0x04 // text is not entered into content, only a representative character TEXT_EDITOR_FLAG_READONLY 0x08 // editing events are disabled. Editor may still accept focus. TEXT_EDITOR_FLAG_DISALBED 0x10 // all events are disabled (like scrolling). Editor will not accept focus. * added WillInsertBreak/DidInsertBreak into text rules, so flags could be checked. This gets us readonly, disabled, and single line behavior. * cleaned up the code that allocates, registers, and destroys event listeners. Thanks to Kin and Simon for cleaning up the ownership model on the listeners, it was a big help. * added support for a max text length. You can now tell the text editor, be no bigger than n characters.
1999-05-28 21:24:18 +00:00
return NS_OK; \
};
1999-04-05 17:21:59 +00:00
/********************************************************
* mozilla::TextEditRules
1999-04-05 17:21:59 +00:00
********************************************************/
TextEditRules::TextEditRules()
: mTextEditor(nullptr)
, mData(nullptr)
, mPasswordIMEIndex(0)
, mCachedSelectionOffset(0)
, mActionNesting(0)
, mLockRulesSniffing(false)
, mDidExplicitlySetInterline(false)
, mDeleteBidiImmediately(false)
, mIsHTMLEditRules(false)
, mTheAction(EditAction::none)
, mLastStart(0)
, mLastLength(0)
{
InitFields();
}
void
TextEditRules::InitFields()
{
mTextEditor = nullptr;
mPasswordText.Truncate();
mPasswordIMEText.Truncate();
mPasswordIMEIndex = 0;
mBogusNode = nullptr;
mCachedSelectionNode = nullptr;
mCachedSelectionOffset = 0;
mActionNesting = 0;
mLockRulesSniffing = false;
mDidExplicitlySetInterline = false;
mDeleteBidiImmediately = false;
mTheAction = EditAction::none;
mTimer = nullptr;
mLastStart = 0;
mLastLength = 0;
}
TextEditRules::~TextEditRules()
{
// do NOT delete mTextEditor here. We do not hold a ref count to
// mTextEditor. mTextEditor owns our lifespan.
if (mTimer) {
mTimer->Cancel();
}
}
HTMLEditRules*
TextEditRules::AsHTMLEditRules()
{
return mIsHTMLEditRules ? static_cast<HTMLEditRules*>(this) : nullptr;
}
const HTMLEditRules*
TextEditRules::AsHTMLEditRules() const
{
return mIsHTMLEditRules ? static_cast<const HTMLEditRules*>(this) : nullptr;
}
NS_IMPL_CYCLE_COLLECTION(TextEditRules, mBogusNode, mCachedSelectionNode)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TextEditRules)
NS_INTERFACE_MAP_ENTRY(nsITimerCallback)
NS_INTERFACE_MAP_ENTRY(nsINamed)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsITimerCallback)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(TextEditRules)
NS_IMPL_CYCLE_COLLECTING_RELEASE(TextEditRules)
nsresult
TextEditRules::Init(TextEditor* aTextEditor)
{
if (NS_WARN_IF(!aTextEditor)) {
return NS_ERROR_INVALID_ARG;
}
Selection* selection = aTextEditor->GetSelection();
if (NS_WARN_IF(!selection)) {
return NS_ERROR_FAILURE;
}
InitFields();
// We hold a non-refcounted reference back to our editor.
mTextEditor = aTextEditor;
AutoSafeEditorData setData(*this, *mTextEditor, *selection);
// Put in a magic <br> if needed. This method handles null selection,
// which should never happen anyway
nsresult rv = CreateBogusNodeIfNeeded(&SelectionRef());
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
// If the selection hasn't been set up yet, set it up collapsed to the end of
// our editable content.
if (!SelectionRef().RangeCount()) {
rv = TextEditorRef().CollapseSelectionToEnd(&SelectionRef());
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
}
if (IsPlaintextEditor()) {
// ensure trailing br node
rv = CreateTrailingBRIfNeeded();
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
}
// XXX We should use AddBoolVarCache and use "current" value at initializing.
mDeleteBidiImmediately =
Preferences::GetBool("bidi.edit.delete_immediately", false);
return NS_OK;
Preparation for ender-based text control * added focus listener. Doesn't do much yet, but when focus notifications start appearing, we'll be ready for them. The code is in place to hide selection when we lose focus and paint selection when we get focus. That's probably not quite right, but it's a start. We will need to be able to determine the distinction between losing focus to another control within our app, and losing focus to another app. * added support for disabled and readonly states in the editor. This is accomplished by having flags set by the client, and letting the rules system deal with those flags. The flags I added are: TEXT_EDITOR_FLAG_PLAINTEXT 0x01 // only plain text editing is allowed TEXT_EDITOR_FLAG_SINGLELINE 0x02 // enter key and CR-LF handled specially TEXT_EDITOR_FLAG_PASSWORD 0x04 // text is not entered into content, only a representative character TEXT_EDITOR_FLAG_READONLY 0x08 // editing events are disabled. Editor may still accept focus. TEXT_EDITOR_FLAG_DISALBED 0x10 // all events are disabled (like scrolling). Editor will not accept focus. * added WillInsertBreak/DidInsertBreak into text rules, so flags could be checked. This gets us readonly, disabled, and single line behavior. * cleaned up the code that allocates, registers, and destroys event listeners. Thanks to Kin and Simon for cleaning up the ownership model on the listeners, it was a big help. * added support for a max text length. You can now tell the text editor, be no bigger than n characters.
1999-05-28 21:24:18 +00:00
}
nsresult
TextEditRules::SetInitialValue(const nsAString& aValue)
{
if (IsPasswordEditor()) {
mPasswordText = aValue;
}
return NS_OK;
}
nsresult
TextEditRules::DetachEditor()
{
if (mTimer) {
mTimer->Cancel();
}
mTextEditor = nullptr;
return NS_OK;
}
nsresult
TextEditRules::BeforeEdit(EditAction aAction,
nsIEditor::EDirection aDirection)
{
if (mLockRulesSniffing) {
return NS_OK;
}
AutoLockRulesSniffing lockIt(this);
mDidExplicitlySetInterline = false;
if (!mActionNesting) {
// let rules remember the top level action
mTheAction = aAction;
}
mActionNesting++;
// get the selection and cache the position before editing
if (NS_WARN_IF(!mTextEditor)) {
return NS_ERROR_NOT_AVAILABLE;
}
if (aAction == EditAction::setText) {
// setText replaces all text, so mCachedSelectionNode might be invalid on
// AfterEdit.
// Since this will be used as start position of spellchecker, we should
// use root instead.
mCachedSelectionNode = mTextEditor->GetRoot();
mCachedSelectionOffset = 0;
} else {
Selection* selection = mTextEditor->GetSelection();
if (NS_WARN_IF(!selection)) {
return NS_ERROR_FAILURE;
}
mCachedSelectionNode = selection->GetAnchorNode();
mCachedSelectionOffset = selection->AnchorOffset();
}
return NS_OK;
}
nsresult
TextEditRules::AfterEdit(EditAction aAction,
nsIEditor::EDirection aDirection)
{
if (mLockRulesSniffing) {
return NS_OK;
}
AutoLockRulesSniffing lockIt(this);
MOZ_ASSERT(mActionNesting>0, "bad action nesting!");
if (!--mActionNesting) {
if (NS_WARN_IF(!mTextEditor)) {
return NS_ERROR_NOT_AVAILABLE;
}
Selection* selection = mTextEditor->GetSelection();
if (NS_WARN_IF(!selection)) {
return NS_ERROR_FAILURE;
}
AutoSafeEditorData setData(*this, *mTextEditor, *selection);
nsresult rv =
TextEditorRef().HandleInlineSpellCheck(aAction, *selection,
mCachedSelectionNode,
mCachedSelectionOffset,
nullptr, 0, nullptr, 0);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
// no longer uses mCachedSelectionNode, so release it.
mCachedSelectionNode = nullptr;
// if only trailing <br> remaining remove it
rv = RemoveRedundantTrailingBR();
if (NS_FAILED(rv)) {
return rv;
}
// detect empty doc
rv = CreateBogusNodeIfNeeded(&SelectionRef());
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
// ensure trailing br node
rv = CreateTrailingBRIfNeeded();
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
// collapse the selection to the trailing BR if it's at the end of our text node
CollapseSelectionToTrailingBRIfNeeded(&SelectionRef());
}
return NS_OK;
}
nsresult
TextEditRules::WillDoAction(Selection* aSelection,
RulesInfo* aInfo,
bool* aCancel,
bool* aHandled)
{
if (NS_WARN_IF(!aSelection) || NS_WARN_IF(!aInfo)) {
return NS_ERROR_INVALID_ARG;
}
if (NS_WARN_IF(!mTextEditor)) {
return NS_ERROR_NOT_AVAILABLE;
}
MOZ_ASSERT(aCancel);
MOZ_ASSERT(aHandled);
*aCancel = false;
*aHandled = false;
1999-04-12 12:01:32 +00:00
AutoSafeEditorData setData(*this, *mTextEditor, *aSelection);
1999-04-12 12:01:32 +00:00
// my kingdom for dynamic cast
switch (aInfo->action) {
case EditAction::insertBreak:
UndefineCaretBidiLevel(&SelectionRef());
return WillInsertBreak(&SelectionRef(), aCancel, aHandled,
aInfo->maxLength);
case EditAction::insertText:
case EditAction::insertIMEText:
UndefineCaretBidiLevel(&SelectionRef());
return WillInsertText(aInfo->action, &SelectionRef(), aCancel, aHandled,
aInfo->inString, aInfo->outString,
aInfo->maxLength);
case EditAction::setText:
UndefineCaretBidiLevel(&SelectionRef());
return WillSetText(SelectionRef(), aCancel, aHandled, aInfo->inString,
aInfo->maxLength);
case EditAction::deleteSelection:
return WillDeleteSelection(&SelectionRef(), aInfo->collapsedAction,
aCancel, aHandled);
case EditAction::undo:
return WillUndo(&SelectionRef(), aCancel, aHandled);
case EditAction::redo:
return WillRedo(&SelectionRef(), aCancel, aHandled);
case EditAction::setTextProperty:
return WillSetTextProperty(&SelectionRef(), aCancel, aHandled);
case EditAction::removeTextProperty:
return WillRemoveTextProperty(&SelectionRef(), aCancel, aHandled);
case EditAction::outputText:
return WillOutputText(&SelectionRef(), aInfo->outputFormat,
aInfo->outString, aInfo->flags, aCancel, aHandled);
case EditAction::insertElement:
// i had thought this would be html rules only. but we put pre elements
// into plaintext mail when doing quoting for reply! doh!
WillInsert(SelectionRef(), aCancel);
return NS_OK;
default:
return NS_ERROR_FAILURE;
1999-04-05 17:21:59 +00:00
}
}
nsresult
TextEditRules::DidDoAction(Selection* aSelection,
RulesInfo* aInfo,
nsresult aResult)
{
if (NS_WARN_IF(!aSelection) || NS_WARN_IF(!aInfo)) {
return NS_ERROR_INVALID_ARG;
}
if (NS_WARN_IF(!mTextEditor)) {
return NS_ERROR_NOT_AVAILABLE;
}
AutoSafeEditorData setData(*this, *mTextEditor, *aSelection);
// don't let any txns in here move the selection around behind our back.
// Note that this won't prevent explicit selection setting from working.
AutoTransactionsConserveSelection dontChangeMySelection(&TextEditorRef());
switch (aInfo->action) {
case EditAction::insertBreak:
return DidInsertBreak(&SelectionRef(), aResult);
case EditAction::insertText:
case EditAction::insertIMEText:
return DidInsertText(&SelectionRef(), aResult);
case EditAction::setText:
return DidSetText(SelectionRef(), aResult);
case EditAction::deleteSelection:
return DidDeleteSelection(&SelectionRef(), aInfo->collapsedAction,
aResult);
case EditAction::undo:
return DidUndo(&SelectionRef(), aResult);
case EditAction::redo:
return DidRedo(&SelectionRef(), aResult);
case EditAction::setTextProperty:
return DidSetTextProperty(&SelectionRef(), aResult);
case EditAction::removeTextProperty:
return DidRemoveTextProperty(&SelectionRef(), aResult);
case EditAction::outputText:
return DidOutputText(&SelectionRef(), aResult);
default:
// Don't fail on transactions we don't handle here!
return NS_OK;
1999-04-05 17:21:59 +00:00
}
}
/**
* Return false if the editor has non-empty text nodes or non-text
* nodes. Otherwise, i.e., there is no meaningful content,
* return true.
*/
bool
TextEditRules::DocumentIsEmpty()
{
bool retVal = false;
if (!mTextEditor || NS_FAILED(mTextEditor->DocumentIsEmpty(&retVal))) {
retVal = true;
}
return retVal;
}
1999-04-05 17:21:59 +00:00
void
TextEditRules::WillInsert(Selection& aSelection, bool* aCancel)
{
MOZ_ASSERT(IsEditorDataAvailable());
MOZ_ASSERT(aCancel);
if (IsReadonly() || IsDisabled()) {
*aCancel = true;
return;
}
Preparation for ender-based text control * added focus listener. Doesn't do much yet, but when focus notifications start appearing, we'll be ready for them. The code is in place to hide selection when we lose focus and paint selection when we get focus. That's probably not quite right, but it's a start. We will need to be able to determine the distinction between losing focus to another control within our app, and losing focus to another app. * added support for disabled and readonly states in the editor. This is accomplished by having flags set by the client, and letting the rules system deal with those flags. The flags I added are: TEXT_EDITOR_FLAG_PLAINTEXT 0x01 // only plain text editing is allowed TEXT_EDITOR_FLAG_SINGLELINE 0x02 // enter key and CR-LF handled specially TEXT_EDITOR_FLAG_PASSWORD 0x04 // text is not entered into content, only a representative character TEXT_EDITOR_FLAG_READONLY 0x08 // editing events are disabled. Editor may still accept focus. TEXT_EDITOR_FLAG_DISALBED 0x10 // all events are disabled (like scrolling). Editor will not accept focus. * added WillInsertBreak/DidInsertBreak into text rules, so flags could be checked. This gets us readonly, disabled, and single line behavior. * cleaned up the code that allocates, registers, and destroys event listeners. Thanks to Kin and Simon for cleaning up the ownership model on the listeners, it was a big help. * added support for a max text length. You can now tell the text editor, be no bigger than n characters.
1999-05-28 21:24:18 +00:00
// initialize out param
*aCancel = false;
// check for the magic content node and delete it if it exists
if (mBogusNode) {
DebugOnly<nsresult> rv =
TextEditorRef().DeleteNodeWithTransaction(*mBogusNode);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"Failed to remove the bogus node");
mBogusNode = nullptr;
}
}
1999-04-12 12:01:32 +00:00
nsresult
TextEditRules::DidInsert(Selection* aSelection,
nsresult aResult)
{
return NS_OK;
}
nsresult
TextEditRules::WillInsertBreak(Selection* aSelection,
bool* aCancel,
bool* aHandled,
int32_t aMaxLength)
Preparation for ender-based text control * added focus listener. Doesn't do much yet, but when focus notifications start appearing, we'll be ready for them. The code is in place to hide selection when we lose focus and paint selection when we get focus. That's probably not quite right, but it's a start. We will need to be able to determine the distinction between losing focus to another control within our app, and losing focus to another app. * added support for disabled and readonly states in the editor. This is accomplished by having flags set by the client, and letting the rules system deal with those flags. The flags I added are: TEXT_EDITOR_FLAG_PLAINTEXT 0x01 // only plain text editing is allowed TEXT_EDITOR_FLAG_SINGLELINE 0x02 // enter key and CR-LF handled specially TEXT_EDITOR_FLAG_PASSWORD 0x04 // text is not entered into content, only a representative character TEXT_EDITOR_FLAG_READONLY 0x08 // editing events are disabled. Editor may still accept focus. TEXT_EDITOR_FLAG_DISALBED 0x10 // all events are disabled (like scrolling). Editor will not accept focus. * added WillInsertBreak/DidInsertBreak into text rules, so flags could be checked. This gets us readonly, disabled, and single line behavior. * cleaned up the code that allocates, registers, and destroys event listeners. Thanks to Kin and Simon for cleaning up the ownership model on the listeners, it was a big help. * added support for a max text length. You can now tell the text editor, be no bigger than n characters.
1999-05-28 21:24:18 +00:00
{
MOZ_ASSERT(IsEditorDataAvailable());
if (!aSelection || !aCancel || !aHandled) {
return NS_ERROR_NULL_POINTER;
}
Preparation for ender-based text control * added focus listener. Doesn't do much yet, but when focus notifications start appearing, we'll be ready for them. The code is in place to hide selection when we lose focus and paint selection when we get focus. That's probably not quite right, but it's a start. We will need to be able to determine the distinction between losing focus to another control within our app, and losing focus to another app. * added support for disabled and readonly states in the editor. This is accomplished by having flags set by the client, and letting the rules system deal with those flags. The flags I added are: TEXT_EDITOR_FLAG_PLAINTEXT 0x01 // only plain text editing is allowed TEXT_EDITOR_FLAG_SINGLELINE 0x02 // enter key and CR-LF handled specially TEXT_EDITOR_FLAG_PASSWORD 0x04 // text is not entered into content, only a representative character TEXT_EDITOR_FLAG_READONLY 0x08 // editing events are disabled. Editor may still accept focus. TEXT_EDITOR_FLAG_DISALBED 0x10 // all events are disabled (like scrolling). Editor will not accept focus. * added WillInsertBreak/DidInsertBreak into text rules, so flags could be checked. This gets us readonly, disabled, and single line behavior. * cleaned up the code that allocates, registers, and destroys event listeners. Thanks to Kin and Simon for cleaning up the ownership model on the listeners, it was a big help. * added support for a max text length. You can now tell the text editor, be no bigger than n characters.
1999-05-28 21:24:18 +00:00
CANCEL_OPERATION_IF_READONLY_OR_DISABLED
*aHandled = false;
if (IsSingleLineEditor()) {
*aCancel = true;
} else {
// handle docs with a max length
// NOTE, this function copies inString into outString for us.
NS_NAMED_LITERAL_STRING(inString, "\n");
nsAutoString outString;
bool didTruncate;
nsresult rv = TruncateInsertionIfNeeded(aSelection, &inString.AsString(),
&outString, aMaxLength,
&didTruncate);
NS_ENSURE_SUCCESS(rv, rv);
if (didTruncate) {
*aCancel = true;
return NS_OK;
}
*aCancel = false;
// if the selection isn't collapsed, delete it.
if (!aSelection->IsCollapsed()) {
rv = TextEditorRef().DeleteSelectionAsAction(nsIEditor::eNone,
nsIEditor::eStrip);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
}
WillInsert(*aSelection, aCancel);
// initialize out param
// we want to ignore result of WillInsert()
*aCancel = false;
Preparation for ender-based text control * added focus listener. Doesn't do much yet, but when focus notifications start appearing, we'll be ready for them. The code is in place to hide selection when we lose focus and paint selection when we get focus. That's probably not quite right, but it's a start. We will need to be able to determine the distinction between losing focus to another control within our app, and losing focus to another app. * added support for disabled and readonly states in the editor. This is accomplished by having flags set by the client, and letting the rules system deal with those flags. The flags I added are: TEXT_EDITOR_FLAG_PLAINTEXT 0x01 // only plain text editing is allowed TEXT_EDITOR_FLAG_SINGLELINE 0x02 // enter key and CR-LF handled specially TEXT_EDITOR_FLAG_PASSWORD 0x04 // text is not entered into content, only a representative character TEXT_EDITOR_FLAG_READONLY 0x08 // editing events are disabled. Editor may still accept focus. TEXT_EDITOR_FLAG_DISALBED 0x10 // all events are disabled (like scrolling). Editor will not accept focus. * added WillInsertBreak/DidInsertBreak into text rules, so flags could be checked. This gets us readonly, disabled, and single line behavior. * cleaned up the code that allocates, registers, and destroys event listeners. Thanks to Kin and Simon for cleaning up the ownership model on the listeners, it was a big help. * added support for a max text length. You can now tell the text editor, be no bigger than n characters.
1999-05-28 21:24:18 +00:00
}
return NS_OK;
}
nsresult
TextEditRules::DidInsertBreak(Selection* aSelection,
nsresult aResult)
{
return NS_OK;
}
nsresult
TextEditRules::CollapseSelectionToTrailingBRIfNeeded(Selection* aSelection)
Preparation for ender-based text control * added focus listener. Doesn't do much yet, but when focus notifications start appearing, we'll be ready for them. The code is in place to hide selection when we lose focus and paint selection when we get focus. That's probably not quite right, but it's a start. We will need to be able to determine the distinction between losing focus to another control within our app, and losing focus to another app. * added support for disabled and readonly states in the editor. This is accomplished by having flags set by the client, and letting the rules system deal with those flags. The flags I added are: TEXT_EDITOR_FLAG_PLAINTEXT 0x01 // only plain text editing is allowed TEXT_EDITOR_FLAG_SINGLELINE 0x02 // enter key and CR-LF handled specially TEXT_EDITOR_FLAG_PASSWORD 0x04 // text is not entered into content, only a representative character TEXT_EDITOR_FLAG_READONLY 0x08 // editing events are disabled. Editor may still accept focus. TEXT_EDITOR_FLAG_DISALBED 0x10 // all events are disabled (like scrolling). Editor will not accept focus. * added WillInsertBreak/DidInsertBreak into text rules, so flags could be checked. This gets us readonly, disabled, and single line behavior. * cleaned up the code that allocates, registers, and destroys event listeners. Thanks to Kin and Simon for cleaning up the ownership model on the listeners, it was a big help. * added support for a max text length. You can now tell the text editor, be no bigger than n characters.
1999-05-28 21:24:18 +00:00
{
MOZ_ASSERT(IsEditorDataAvailable());
// we only need to execute the stuff below if we are a plaintext editor.
// html editors have a different mechanism for putting in mozBR's
// (because there are a bunch more places you have to worry about it in html)
if (!IsPlaintextEditor()) {
return NS_OK;
}
// If there is no selection ranges, we should set to the end of the editor.
// This is usually performed in TextEditRules::Init(), however, if the
// editor is reframed, this may be called by AfterEdit().
if (!aSelection->RangeCount()) {
TextEditorRef().CollapseSelectionToEnd(aSelection);
if (TextEditorRef().Destroyed()) {
// The editor has been destroyed.
return NS_ERROR_FAILURE;
}
}
// If we are at the end of the <textarea> element, we need to set the
// selection to stick to the moz-<br> at the end of the <textarea>.
EditorRawDOMPoint selectionStartPoint(EditorBase::GetStartPoint(aSelection));
if (NS_WARN_IF(!selectionStartPoint.IsSet())) {
return NS_ERROR_FAILURE;
}
// Nothing to do if we're not at the end of the text node.
if (!selectionStartPoint.IsInTextNode() ||
!selectionStartPoint.IsEndOfContainer()) {
return NS_OK;
}
Element* rootElement = TextEditorRef().GetRoot();
if (NS_WARN_IF(!rootElement)) {
return NS_ERROR_NULL_POINTER;
}
nsINode* parentNode = selectionStartPoint.GetContainer()->GetParentNode();
if (parentNode != rootElement) {
return NS_OK;
}
nsINode* nextNode = selectionStartPoint.GetContainer()->GetNextSibling();
if (!nextNode || !TextEditUtils::IsMozBR(nextNode)) {
return NS_OK;
}
EditorRawDOMPoint afterStartContainer(selectionStartPoint.GetContainer());
if (NS_WARN_IF(!afterStartContainer.AdvanceOffset())) {
return NS_ERROR_FAILURE;
}
ErrorResult error;
aSelection->Collapse(afterStartContainer, error);
if (NS_WARN_IF(error.Failed())) {
return error.StealNSResult();
}
return NS_OK;
Preparation for ender-based text control * added focus listener. Doesn't do much yet, but when focus notifications start appearing, we'll be ready for them. The code is in place to hide selection when we lose focus and paint selection when we get focus. That's probably not quite right, but it's a start. We will need to be able to determine the distinction between losing focus to another control within our app, and losing focus to another app. * added support for disabled and readonly states in the editor. This is accomplished by having flags set by the client, and letting the rules system deal with those flags. The flags I added are: TEXT_EDITOR_FLAG_PLAINTEXT 0x01 // only plain text editing is allowed TEXT_EDITOR_FLAG_SINGLELINE 0x02 // enter key and CR-LF handled specially TEXT_EDITOR_FLAG_PASSWORD 0x04 // text is not entered into content, only a representative character TEXT_EDITOR_FLAG_READONLY 0x08 // editing events are disabled. Editor may still accept focus. TEXT_EDITOR_FLAG_DISALBED 0x10 // all events are disabled (like scrolling). Editor will not accept focus. * added WillInsertBreak/DidInsertBreak into text rules, so flags could be checked. This gets us readonly, disabled, and single line behavior. * cleaned up the code that allocates, registers, and destroys event listeners. Thanks to Kin and Simon for cleaning up the ownership model on the listeners, it was a big help. * added support for a max text length. You can now tell the text editor, be no bigger than n characters.
1999-05-28 21:24:18 +00:00
}
static inline already_AddRefed<nsINode>
GetTextNode(Selection* aSelection)
{
EditorRawDOMPoint selectionStartPoint(EditorBase::GetStartPoint(aSelection));
if (NS_WARN_IF(!selectionStartPoint.IsSet())) {
return nullptr;
}
if (selectionStartPoint.IsInTextNode()) {
nsCOMPtr<nsINode> node = selectionStartPoint.GetContainer();
return node.forget();
}
// This should be the root node, walk the tree looking for text nodes.
nsCOMPtr<nsINode> node = selectionStartPoint.GetContainer();
RefPtr<NodeIterator> iter =
new NodeIterator(node, NodeFilterBinding::SHOW_TEXT, nullptr);
while (!EditorBase::IsTextNode(node)) {
node = iter->NextNode(IgnoreErrors());
if (!node) {
return nullptr;
}
}
return node.forget();
}
#ifdef DEBUG
#define ASSERT_PASSWORD_LENGTHS_EQUAL() \
if (IsPasswordEditor() && mTextEditor->GetRoot()) { \
int32_t txtLen; \
mTextEditor->GetTextLength(&txtLen); \
NS_ASSERTION(mPasswordText.Length() == uint32_t(txtLen), \
"password length not equal to number of asterisks"); \
}
#else
#define ASSERT_PASSWORD_LENGTHS_EQUAL()
#endif
// static
void
TextEditRules::HandleNewLines(nsString& aString,
int32_t aNewlineHandling)
{
if (aNewlineHandling < 0) {
int32_t caretStyle;
TextEditor::GetDefaultEditorPrefs(aNewlineHandling, caretStyle);
}
switch(aNewlineHandling) {
case nsIPlaintextEditor::eNewlinesReplaceWithSpaces:
// Strip trailing newlines first so we don't wind up with trailing spaces
aString.Trim(CRLF, false, true);
aString.ReplaceChar(CRLF, ' ');
break;
case nsIPlaintextEditor::eNewlinesStrip:
aString.StripCRLF();
break;
case nsIPlaintextEditor::eNewlinesPasteToFirst:
default: {
int32_t firstCRLF = aString.FindCharInSet(CRLF);
// we get first *non-empty* line.
int32_t offset = 0;
while (firstCRLF == offset) {
offset++;
firstCRLF = aString.FindCharInSet(CRLF, offset);
}
if (firstCRLF > 0) {
aString.Truncate(firstCRLF);
}
if (offset > 0) {
aString.Cut(0, offset);
}
break;
}
case nsIPlaintextEditor::eNewlinesReplaceWithCommas:
aString.Trim(CRLF, true, true);
aString.ReplaceChar(CRLF, ',');
break;
case nsIPlaintextEditor::eNewlinesStripSurroundingWhitespace: {
nsAutoString result;
uint32_t offset = 0;
while (offset < aString.Length()) {
int32_t nextCRLF = aString.FindCharInSet(CRLF, offset);
if (nextCRLF < 0) {
result.Append(nsDependentSubstring(aString, offset));
break;
}
uint32_t wsBegin = nextCRLF;
// look backwards for the first non-whitespace char
while (wsBegin > offset && NS_IS_SPACE(aString[wsBegin - 1])) {
--wsBegin;
}
result.Append(nsDependentSubstring(aString, offset, wsBegin - offset));
offset = nextCRLF + 1;
while (offset < aString.Length() && NS_IS_SPACE(aString[offset])) {
++offset;
}
}
aString = result;
break;
}
case nsIPlaintextEditor::eNewlinesPasteIntact:
// even if we're pasting newlines, don't paste leading/trailing ones
aString.Trim(CRLF, true, true);
break;
}
}
1999-04-12 12:01:32 +00:00
nsresult
TextEditRules::WillInsertText(EditAction aAction,
Selection* aSelection,
bool* aCancel,
bool* aHandled,
const nsAString* inString,
nsAString* outString,
int32_t aMaxLength)
{
MOZ_ASSERT(IsEditorDataAvailable());
if (!aSelection || !aCancel || !aHandled) {
return NS_ERROR_NULL_POINTER;
}
if (inString->IsEmpty() && aAction != EditAction::insertIMEText) {
// HACK: this is a fix for bug 19395
// I can't outlaw all empty insertions
// because IME transaction depend on them
// There is more work to do to make the
// world safe for IME.
*aCancel = true;
*aHandled = false;
return NS_OK;
}
// initialize out param
*aCancel = false;
*aHandled = true;
// handle docs with a max length
2000-03-29 14:04:26 +00:00
// NOTE, this function copies inString into outString for us.
bool truncated = false;
nsresult rv = TruncateInsertionIfNeeded(aSelection, inString, outString,
aMaxLength, &truncated);
NS_ENSURE_SUCCESS(rv, rv);
// If we're exceeding the maxlength when composing IME, we need to clean up
// the composing text, so we shouldn't return early.
if (truncated && outString->IsEmpty() &&
aAction != EditAction::insertIMEText) {
*aCancel = true;
return NS_OK;
}
uint32_t start = 0;
uint32_t end = 0;
// handle password field docs
if (IsPasswordEditor()) {
nsContentUtils::GetSelectionInTextControl(aSelection,
TextEditorRef().GetRoot(),
start, end);
}
// if the selection isn't collapsed, delete it.
if (!aSelection->IsCollapsed()) {
rv = TextEditorRef().DeleteSelectionAsAction(nsIEditor::eNone,
nsIEditor::eStrip);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
}
WillInsert(*aSelection, aCancel);
// initialize out param
// we want to ignore result of WillInsert()
*aCancel = false;
// handle password field data
// this has the side effect of changing all the characters in aOutString
// to the replacement character
if (IsPasswordEditor() &&
aAction == EditAction::insertIMEText) {
RemoveIMETextFromPWBuf(start, outString);
}
// People have lots of different ideas about what text fields
// should do with multiline pastes. See bugs 21032, 23485, 23485, 50935.
// The six possible options are:
// 0. paste newlines intact
// 1. paste up to the first newline (default)
// 2. replace newlines with spaces
// 3. strip newlines
// 4. replace with commas
// 5. strip newlines and surrounding whitespace
// So find out what we're expected to do:
if (IsSingleLineEditor()) {
nsAutoString tString(*outString);
HandleNewLines(tString, TextEditorRef().mNewlineHandling);
outString->Assign(tString);
}
if (IsPasswordEditor()) {
// manage the password buffer
mPasswordText.Insert(*outString, start);
if (LookAndFeel::GetEchoPassword() && !DontEchoPassword()) {
HideLastPWInput();
mLastStart = start;
mLastLength = outString->Length();
if (mTimer) {
mTimer->Cancel();
} else {
mTimer = NS_NewTimer();
NS_ENSURE_TRUE(mTimer, NS_ERROR_OUT_OF_MEMORY);
}
mTimer->InitWithCallback(this, LookAndFeel::GetPasswordMaskDelay(),
nsITimer::TYPE_ONE_SHOT);
} else {
FillBufWithPWChars(outString, outString->Length());
}
}
// get the (collapsed) selection location
NS_ENSURE_STATE(aSelection->GetRangeAt(0));
EditorRawDOMPoint atStartOfSelection(aSelection->GetRangeAt(0)->StartRef());
if (NS_WARN_IF(!atStartOfSelection.IsSetAndValid())) {
return NS_ERROR_FAILURE;
}
// don't put text in places that can't have it
if (!atStartOfSelection.IsInTextNode() &&
!TextEditorRef().CanContainTag(*atStartOfSelection.GetContainer(),
*nsGkAtoms::textTagName)) {
return NS_ERROR_FAILURE;
}
// we need to get the doc
nsCOMPtr<nsIDocument> doc = TextEditorRef().GetDocument();
if (NS_WARN_IF(!doc)) {
return NS_ERROR_NOT_INITIALIZED;
}
if (aAction == EditAction::insertIMEText) {
// Find better insertion point to insert text.
EditorRawDOMPoint betterInsertionPoint =
TextEditorRef().FindBetterInsertionPoint(atStartOfSelection);
// If there is one or more IME selections, its minimum offset should be
// the insertion point.
int32_t IMESelectionOffset =
TextEditorRef().GetIMESelectionStartOffsetIn(
betterInsertionPoint.GetContainer());
if (IMESelectionOffset >= 0) {
betterInsertionPoint.Set(betterInsertionPoint.GetContainer(),
IMESelectionOffset);
}
rv = TextEditorRef().InsertTextWithTransaction(*doc, *outString,
betterInsertionPoint);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
} else {
// aAction == EditAction::insertText
// don't change my selection in subtransactions
AutoTransactionsConserveSelection dontChangeMySelection(&TextEditorRef());
EditorRawDOMPoint pointAfterStringInserted;
rv = TextEditorRef().InsertTextWithTransaction(*doc, *outString,
atStartOfSelection,
&pointAfterStringInserted);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
if (pointAfterStringInserted.IsSet()) {
// Make the caret attach to the inserted text, unless this text ends with a LF,
// in which case make the caret attach to the next line.
bool endsWithLF =
!outString->IsEmpty() && outString->Last() == nsCRT::LF;
aSelection->SetInterlinePosition(endsWithLF, IgnoreErrors());
MOZ_ASSERT(!pointAfterStringInserted.GetChild(),
"After inserting text into a text node, pointAfterStringInserted."
"GetChild() should be nullptr");
IgnoredErrorResult error;
aSelection->Collapse(pointAfterStringInserted, error);
if (error.Failed()) {
NS_WARNING("Failed to collapse selection after inserting string");
}
}
As a reminder, we decided to do this based strictly content. Some support for style-based text properties is written, but not used anywhere any more. * Cleaned up split and join undo/redo. * Added TypeInState, a data struct that remembers things about text properties for collapsed selections, so you can type * Ctrl-B with an insertion point and the next character will be bold. * Added all the logic to handle inline vs. block elements when setting text properties. * Added some support for italic and underline as well. Adding these things is pretty easy now. Ctrl-B, Ctrl-I, Ctrl-U for testing bold, italic, underline. * Added all the logic to make sure we only add style tags where they're needed, so you should never get the same style tag nested within itself, except as needed for block elements. * Added methods for testing a node to see if a particular style is set. This isn't 100% done yet, but with very little work we could have toolbar buttons that respond to selection changed notification that show the state of bold, italic, underline, etc. in real time. Supports tri-state: whole selection is bold, some of selection is bold, none of selection is bold, ... * Fully undoable and redoable. * Added some debug printfs to transactions and editors. all controlled by a gNoisy static in each module. helps me track down undo/redo problems. if the output bugs people enough, I'll shut it off and re-enable it in my local tree. Noticably missing: make un-bold, make un-italic, etc. This is coming soon.
1999-04-01 17:58:07 +00:00
}
ASSERT_PASSWORD_LENGTHS_EQUAL()
return NS_OK;
As a reminder, we decided to do this based strictly content. Some support for style-based text properties is written, but not used anywhere any more. * Cleaned up split and join undo/redo. * Added TypeInState, a data struct that remembers things about text properties for collapsed selections, so you can type * Ctrl-B with an insertion point and the next character will be bold. * Added all the logic to handle inline vs. block elements when setting text properties. * Added some support for italic and underline as well. Adding these things is pretty easy now. Ctrl-B, Ctrl-I, Ctrl-U for testing bold, italic, underline. * Added all the logic to make sure we only add style tags where they're needed, so you should never get the same style tag nested within itself, except as needed for block elements. * Added methods for testing a node to see if a particular style is set. This isn't 100% done yet, but with very little work we could have toolbar buttons that respond to selection changed notification that show the state of bold, italic, underline, etc. in real time. Supports tri-state: whole selection is bold, some of selection is bold, none of selection is bold, ... * Fully undoable and redoable. * Added some debug printfs to transactions and editors. all controlled by a gNoisy static in each module. helps me track down undo/redo problems. if the output bugs people enough, I'll shut it off and re-enable it in my local tree. Noticably missing: make un-bold, make un-italic, etc. This is coming soon.
1999-04-01 17:58:07 +00:00
}
nsresult
TextEditRules::DidInsertText(Selection* aSelection,
nsresult aResult)
{
return DidInsert(aSelection, aResult);
}
nsresult
TextEditRules::WillSetText(Selection& aSelection,
bool* aCancel,
bool* aHandled,
const nsAString* aString,
int32_t aMaxLength)
{
MOZ_ASSERT(IsEditorDataAvailable());
MOZ_ASSERT(aCancel);
MOZ_ASSERT(aHandled);
MOZ_ASSERT(aString);
CANCEL_OPERATION_IF_READONLY_OR_DISABLED
*aHandled = false;
*aCancel = false;
if (!IsPlaintextEditor() || TextEditorRef().IsIMEComposing() ||
aMaxLength != -1) {
// SetTextImpl only supports plain text editor without IME.
return NS_OK;
}
if (IsPasswordEditor() && LookAndFeel::GetEchoPassword() &&
!DontEchoPassword()) {
// Echo password timer will implement on InsertText.
return NS_OK;
}
WillInsert(aSelection, aCancel);
// we want to ignore result of WillInsert()
*aCancel = false;
RefPtr<Element> rootElement = TextEditorRef().GetRoot();
uint32_t count = rootElement->GetChildCount();
// handles only when there is only one node and it's a text node, or empty.
if (count > 1) {
return NS_OK;
}
nsAutoString tString(*aString);
if (IsPasswordEditor()) {
mPasswordText.Assign(tString);
FillBufWithPWChars(&tString, tString.Length());
} else if (IsSingleLineEditor()) {
HandleNewLines(tString, TextEditorRef().mNewlineHandling);
}
if (!count) {
if (tString.IsEmpty()) {
*aHandled = true;
return NS_OK;
}
RefPtr<nsIDocument> doc = TextEditorRef().GetDocument();
if (NS_WARN_IF(!doc)) {
return NS_OK;
}
RefPtr<nsTextNode> newNode = EditorBase::CreateTextNode(*doc, tString);
if (NS_WARN_IF(!newNode)) {
return NS_OK;
}
nsresult rv =
TextEditorRef().InsertNodeWithTransaction(
*newNode, EditorRawDOMPoint(rootElement, 0));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
*aHandled = true;
ASSERT_PASSWORD_LENGTHS_EQUAL();
return NS_OK;
}
nsINode* curNode = rootElement->GetFirstChild();
if (NS_WARN_IF(!EditorBase::IsTextNode(curNode))) {
return NS_OK;
}
// Even if empty text, we don't remove text node and set empty text
// for performance
nsresult rv = TextEditorRef().SetTextImpl(aSelection, tString,
*curNode->GetAsText());
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
*aHandled = true;
ASSERT_PASSWORD_LENGTHS_EQUAL();
return NS_OK;
}
nsresult
TextEditRules::DidSetText(Selection& aSelection,
nsresult aResult)
{
return NS_OK;
}
nsresult
TextEditRules::WillSetTextProperty(Selection* aSelection,
bool* aCancel,
bool* aHandled)
{
if (!aSelection || !aCancel || !aHandled) {
return NS_ERROR_NULL_POINTER;
}
// XXX: should probably return a success value other than NS_OK that means "not allowed"
if (IsPlaintextEditor()) {
*aCancel = true;
}
return NS_OK;
}
nsresult
TextEditRules::DidSetTextProperty(Selection* aSelection,
nsresult aResult)
{
return NS_OK;
}
nsresult
TextEditRules::WillRemoveTextProperty(Selection* aSelection,
bool* aCancel,
bool* aHandled)
{
if (!aSelection || !aCancel || !aHandled) {
return NS_ERROR_NULL_POINTER;
}
// XXX: should probably return a success value other than NS_OK that means "not allowed"
if (IsPlaintextEditor()) {
*aCancel = true;
}
return NS_OK;
}
nsresult
TextEditRules::DidRemoveTextProperty(Selection* aSelection,
nsresult aResult)
{
return NS_OK;
}
1999-04-12 12:01:32 +00:00
nsresult
TextEditRules::WillDeleteSelection(Selection* aSelection,
nsIEditor::EDirection aCollapsedAction,
bool* aCancel,
bool* aHandled)
{
MOZ_ASSERT(IsEditorDataAvailable());
if (!aSelection || !aCancel || !aHandled) {
return NS_ERROR_NULL_POINTER;
}
Preparation for ender-based text control * added focus listener. Doesn't do much yet, but when focus notifications start appearing, we'll be ready for them. The code is in place to hide selection when we lose focus and paint selection when we get focus. That's probably not quite right, but it's a start. We will need to be able to determine the distinction between losing focus to another control within our app, and losing focus to another app. * added support for disabled and readonly states in the editor. This is accomplished by having flags set by the client, and letting the rules system deal with those flags. The flags I added are: TEXT_EDITOR_FLAG_PLAINTEXT 0x01 // only plain text editing is allowed TEXT_EDITOR_FLAG_SINGLELINE 0x02 // enter key and CR-LF handled specially TEXT_EDITOR_FLAG_PASSWORD 0x04 // text is not entered into content, only a representative character TEXT_EDITOR_FLAG_READONLY 0x08 // editing events are disabled. Editor may still accept focus. TEXT_EDITOR_FLAG_DISALBED 0x10 // all events are disabled (like scrolling). Editor will not accept focus. * added WillInsertBreak/DidInsertBreak into text rules, so flags could be checked. This gets us readonly, disabled, and single line behavior. * cleaned up the code that allocates, registers, and destroys event listeners. Thanks to Kin and Simon for cleaning up the ownership model on the listeners, it was a big help. * added support for a max text length. You can now tell the text editor, be no bigger than n characters.
1999-05-28 21:24:18 +00:00
CANCEL_OPERATION_IF_READONLY_OR_DISABLED
// initialize out param
*aCancel = false;
*aHandled = false;
// if there is only bogus content, cancel the operation
if (mBogusNode) {
*aCancel = true;
return NS_OK;
}
// If the current selection is empty (e.g the user presses backspace with
// a collapsed selection), then we want to avoid sending the selectstart
// event to the user, so we hide selection changes. However, we still
// want to send a single selectionchange event to the document, so we
// batch the selectionchange events, such that a single event fires after
// the AutoHideSelectionChanges destructor has been run.
SelectionBatcher selectionBatcher(aSelection);
AutoHideSelectionChanges hideSelection(aSelection);
nsAutoScriptBlocker scriptBlocker;
if (IsPasswordEditor()) {
nsresult rv =
TextEditorRef().ExtendSelectionForDelete(aSelection, &aCollapsedAction);
NS_ENSURE_SUCCESS(rv, rv);
// manage the password buffer
uint32_t start, end;
nsContentUtils::GetSelectionInTextControl(aSelection,
TextEditorRef().GetRoot(),
start, end);
if (LookAndFeel::GetEchoPassword()) {
HideLastPWInput();
mLastStart = start;
mLastLength = 0;
if (mTimer) {
mTimer->Cancel();
}
}
// Collapsed selection.
if (end == start) {
// Deleting back.
if (nsIEditor::ePrevious == aCollapsedAction && start > 0) {
mPasswordText.Cut(start-1, 1);
}
// Deleting forward.
else if (nsIEditor::eNext == aCollapsedAction) {
mPasswordText.Cut(start, 1);
}
// Otherwise nothing to do for this collapsed selection.
}
// Extended selection.
else {
mPasswordText.Cut(start, end-start);
}
} else {
EditorRawDOMPoint selectionStartPoint(
EditorBase::GetStartPoint(aSelection));
if (NS_WARN_IF(!selectionStartPoint.IsSet())) {
return NS_ERROR_FAILURE;
}
if (!aSelection->IsCollapsed()) {
return NS_OK;
}
// Test for distance between caret and text that will be deleted
nsresult rv = CheckBidiLevelForDeletion(aSelection, selectionStartPoint,
aCollapsedAction, aCancel);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
if (*aCancel) {
return NS_OK;
}
rv = TextEditorRef().ExtendSelectionForDelete(aSelection,
&aCollapsedAction);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
}
nsresult rv =
TextEditorRef().DeleteSelectionWithTransaction(aCollapsedAction,
nsIEditor::eStrip);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
*aHandled = true;
ASSERT_PASSWORD_LENGTHS_EQUAL()
return NS_OK;
}
1999-04-12 12:01:32 +00:00
nsresult
TextEditRules::DidDeleteSelection(Selection* aSelection,
nsIEditor::EDirection aCollapsedAction,
nsresult aResult)
{
MOZ_ASSERT(IsEditorDataAvailable());
EditorRawDOMPoint selectionStartPoint(EditorBase::GetStartPoint(aSelection));
if (NS_WARN_IF(!selectionStartPoint.IsSet())) {
return NS_ERROR_FAILURE;
}
// Delete empty text nodes at selection.
if (selectionStartPoint.IsInTextNode() &&
!selectionStartPoint.GetContainer()->Length()) {
nsresult rv =
TextEditorRef().DeleteNodeWithTransaction(
*selectionStartPoint.GetContainer());
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
}
if (mDidExplicitlySetInterline) {
return NS_OK;
}
// We prevent the caret from sticking on the left of prior BR
// (i.e. the end of previous line) after this deletion. Bug 92124
ErrorResult err;
aSelection->SetInterlinePosition(true, err);
return err.StealNSResult();
}
1999-04-12 12:01:32 +00:00
nsresult
TextEditRules::WillUndo(Selection* aSelection,
bool* aCancel,
bool* aHandled)
{
if (!aSelection || !aCancel || !aHandled) {
return NS_ERROR_NULL_POINTER;
}
Preparation for ender-based text control * added focus listener. Doesn't do much yet, but when focus notifications start appearing, we'll be ready for them. The code is in place to hide selection when we lose focus and paint selection when we get focus. That's probably not quite right, but it's a start. We will need to be able to determine the distinction between losing focus to another control within our app, and losing focus to another app. * added support for disabled and readonly states in the editor. This is accomplished by having flags set by the client, and letting the rules system deal with those flags. The flags I added are: TEXT_EDITOR_FLAG_PLAINTEXT 0x01 // only plain text editing is allowed TEXT_EDITOR_FLAG_SINGLELINE 0x02 // enter key and CR-LF handled specially TEXT_EDITOR_FLAG_PASSWORD 0x04 // text is not entered into content, only a representative character TEXT_EDITOR_FLAG_READONLY 0x08 // editing events are disabled. Editor may still accept focus. TEXT_EDITOR_FLAG_DISALBED 0x10 // all events are disabled (like scrolling). Editor will not accept focus. * added WillInsertBreak/DidInsertBreak into text rules, so flags could be checked. This gets us readonly, disabled, and single line behavior. * cleaned up the code that allocates, registers, and destroys event listeners. Thanks to Kin and Simon for cleaning up the ownership model on the listeners, it was a big help. * added support for a max text length. You can now tell the text editor, be no bigger than n characters.
1999-05-28 21:24:18 +00:00
CANCEL_OPERATION_IF_READONLY_OR_DISABLED
// initialize out param
*aCancel = false;
*aHandled = false;
return NS_OK;
}
/**
* The idea here is to see if the magic empty node has suddenly reappeared as
* the result of the undo. If it has, set our state so we remember it.
* There is a tradeoff between doing here and at redo, or doing it everywhere
* else that might care. Since undo and redo are relatively rare, it makes
* sense to take the (small) performance hit here.
*/
1999-04-12 12:01:32 +00:00
nsresult
TextEditRules::DidUndo(Selection* aSelection,
nsresult aResult)
{
MOZ_ASSERT(IsEditorDataAvailable());
NS_ENSURE_TRUE(aSelection, NS_ERROR_NULL_POINTER);
// If aResult is an error, we return it.
NS_ENSURE_SUCCESS(aResult, aResult);
Element* rootElement = TextEditorRef().GetRoot();
if (NS_WARN_IF(!rootElement)) {
return NS_ERROR_FAILURE;
}
nsIContent* node = TextEditorRef().GetLeftmostChild(rootElement);
if (node && TextEditorRef().IsMozEditorBogusNode(node)) {
mBogusNode = node;
} else {
mBogusNode = nullptr;
}
return aResult;
}
1999-04-12 12:01:32 +00:00
nsresult
TextEditRules::WillRedo(Selection* aSelection,
bool* aCancel,
bool* aHandled)
{
if (!aSelection || !aCancel || !aHandled) {
return NS_ERROR_NULL_POINTER;
}
Preparation for ender-based text control * added focus listener. Doesn't do much yet, but when focus notifications start appearing, we'll be ready for them. The code is in place to hide selection when we lose focus and paint selection when we get focus. That's probably not quite right, but it's a start. We will need to be able to determine the distinction between losing focus to another control within our app, and losing focus to another app. * added support for disabled and readonly states in the editor. This is accomplished by having flags set by the client, and letting the rules system deal with those flags. The flags I added are: TEXT_EDITOR_FLAG_PLAINTEXT 0x01 // only plain text editing is allowed TEXT_EDITOR_FLAG_SINGLELINE 0x02 // enter key and CR-LF handled specially TEXT_EDITOR_FLAG_PASSWORD 0x04 // text is not entered into content, only a representative character TEXT_EDITOR_FLAG_READONLY 0x08 // editing events are disabled. Editor may still accept focus. TEXT_EDITOR_FLAG_DISALBED 0x10 // all events are disabled (like scrolling). Editor will not accept focus. * added WillInsertBreak/DidInsertBreak into text rules, so flags could be checked. This gets us readonly, disabled, and single line behavior. * cleaned up the code that allocates, registers, and destroys event listeners. Thanks to Kin and Simon for cleaning up the ownership model on the listeners, it was a big help. * added support for a max text length. You can now tell the text editor, be no bigger than n characters.
1999-05-28 21:24:18 +00:00
CANCEL_OPERATION_IF_READONLY_OR_DISABLED
// initialize out param
*aCancel = false;
*aHandled = false;
return NS_OK;
}
1999-04-12 12:01:32 +00:00
nsresult
TextEditRules::DidRedo(Selection* aSelection,
nsresult aResult)
{
MOZ_ASSERT(IsEditorDataAvailable());
if (!aSelection) {
return NS_ERROR_NULL_POINTER;
}
if (NS_FAILED(aResult)) {
return aResult; // if aResult is an error, we return it.
}
Element* rootElement = TextEditorRef().GetRoot();
if (NS_WARN_IF(!rootElement)) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsIHTMLCollection> nodeList =
rootElement->GetElementsByTagName(NS_LITERAL_STRING("br"));
MOZ_ASSERT(nodeList);
uint32_t len = nodeList->Length();
if (len != 1) {
// only in the case of one br could there be the bogus node
mBogusNode = nullptr;
return NS_OK;
}
Element* brElement = nodeList->Item(0);
if (TextEditorRef().IsMozEditorBogusNode(brElement)) {
mBogusNode = brElement;
} else {
mBogusNode = nullptr;
}
return NS_OK;
}
1999-06-24 23:36:56 +00:00
nsresult
TextEditRules::WillOutputText(Selection* aSelection,
const nsAString* aOutputFormat,
nsAString* aOutString,
uint32_t aFlags,
bool* aCancel,
bool* aHandled)
1999-06-24 23:36:56 +00:00
{
MOZ_ASSERT(IsEditorDataAvailable());
1999-06-24 23:36:56 +00:00
// null selection ok
if (NS_WARN_IF(!aOutString) || NS_WARN_IF(!aOutputFormat) ||
NS_WARN_IF(!aCancel) || NS_WARN_IF(!aHandled)) {
return NS_ERROR_NULL_POINTER;
}
1999-06-24 23:36:56 +00:00
// initialize out param
*aCancel = false;
*aHandled = false;
if (!aOutputFormat->LowerCaseEqualsLiteral("text/plain")) {
return NS_OK;
}
// XXX Looks like that even if it's password field, we need to use the
// expensive path if the caller requests some complicated handling.
// However, changing the behavior for password field might cause
// security issue. So, be careful when you touch here.
if (IsPasswordEditor()) {
*aOutString = mPasswordText;
*aHandled = true;
return NS_OK;
}
// If there is a bogus node, there's no content. So output empty string.
if (mBogusNode) {
aOutString->Truncate();
*aHandled = true;
return NS_OK;
}
// If it's necessary to check selection range or the editor wraps hard,
// we need some complicated handling. In such case, we need to use the
// expensive path.
// XXX Anything else what we cannot return plain text simply?
if (aFlags & nsIDocumentEncoder::OutputSelectionOnly ||
aFlags & nsIDocumentEncoder::OutputWrap) {
return NS_OK;
}
// If it's neither <input type="text"> nor <textarea>, e.g., an HTML editor
// which is in plaintext mode (e.g., plaintext email composer on Thunderbird),
// it should be handled by the expensive path.
if (TextEditorRef().AsHTMLEditor()) {
return NS_OK;
}
Element* root = TextEditorRef().GetRoot();
if (!root) { // Don't warn it, this is possible, e.g., 997805.html
aOutString->Truncate();
*aHandled = true;
return NS_OK;
}
nsIContent* firstChild = root->GetFirstChild();
if (!firstChild) {
aOutString->Truncate();
*aHandled = true;
return NS_OK;
}
// If it's an <input type="text"> element, the DOM tree should be:
// <div class="anonymous-div">
// #text
// </div>
//
// If it's a <textarea> element, the DOM tree should be:
// <div class="anonymous-div">
// #text (if there is)
// <br type="_moz">
// <scrollbar orient="horizontal">
// ...
// </div>
Text* text = firstChild->GetAsText();
nsIContent* firstChildExceptText =
text ? firstChild->GetNextSibling() : firstChild;
// If the DOM tree is unexpected, fall back to the expensive path.
bool isInput = IsSingleLineEditor();
bool isTextarea = !isInput;
if (NS_WARN_IF(isInput && firstChildExceptText) ||
NS_WARN_IF(isTextarea && !firstChildExceptText) ||
NS_WARN_IF(isTextarea &&
!TextEditUtils::IsMozBR(firstChildExceptText) &&
!firstChildExceptText->IsXULElement(nsGkAtoms::scrollbar))) {
return NS_OK;
}
// If there is no text node in the expected DOM tree, we can say that it's
// just empty.
if (!text) {
aOutString->Truncate();
*aHandled = true;
return NS_OK;
}
// Otherwise, the text is the value.
text->GetData(*aOutString);
*aHandled = true;
1999-06-24 23:36:56 +00:00
return NS_OK;
}
nsresult
TextEditRules::DidOutputText(Selection* aSelection,
nsresult aResult)
1999-06-24 23:36:56 +00:00
{
return NS_OK;
}
nsresult
TextEditRules::RemoveRedundantTrailingBR()
{
MOZ_ASSERT(IsEditorDataAvailable());
// If the bogus node exists, we have no work to do
if (mBogusNode) {
return NS_OK;
}
// Likewise, nothing to be done if we could never have inserted a trailing br
if (IsSingleLineEditor()) {
return NS_OK;
}
Element* rootElement = TextEditorRef().GetRoot();
if (NS_WARN_IF(!rootElement)) {
return NS_ERROR_NULL_POINTER;
}
uint32_t childCount = rootElement->GetChildCount();
if (childCount > 1) {
// The trailing br is redundant if it is the only remaining child node
return NS_OK;
}
RefPtr<nsIContent> child = rootElement->GetFirstChild();
if (!child || !child->IsElement()) {
return NS_OK;
}
RefPtr<Element> childElement = child->AsElement();
if (!TextEditUtils::IsMozBR(childElement)) {
return NS_OK;
}
// Rather than deleting this node from the DOM tree we should instead
// morph this br into the bogus node
childElement->UnsetAttr(kNameSpaceID_None, nsGkAtoms::type, true);
// set mBogusNode to be this <br>
mBogusNode = childElement;
// give it the bogus node attribute
childElement->SetAttr(kNameSpaceID_None, kMOZEditorBogusNodeAttrAtom,
kMOZEditorBogusNodeValue, false);
return NS_OK;
}
nsresult
TextEditRules::CreateTrailingBRIfNeeded()
{
MOZ_ASSERT(IsEditorDataAvailable());
// but only if we aren't a single line edit field
if (IsSingleLineEditor()) {
return NS_OK;
}
Element* rootElement = TextEditorRef().GetRoot();
if (NS_WARN_IF(!rootElement)) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsIContent> lastChild = rootElement->GetLastChild();
// assuming CreateBogusNodeIfNeeded() has been called first
if (NS_WARN_IF(!lastChild)) {
return NS_ERROR_FAILURE;
}
if (!lastChild->IsHTMLElement(nsGkAtoms::br)) {
AutoTransactionsConserveSelection dontChangeMySelection(&TextEditorRef());
EditorRawDOMPoint endOfRoot;
endOfRoot.SetToEndOf(rootElement);
RefPtr<Element> brElement = CreateMozBR(endOfRoot);
if (NS_WARN_IF(!brElement)) {
return NS_ERROR_FAILURE;
}
return NS_OK;
}
// Check to see if the trailing BR is a former bogus node - this will have
// stuck around if we previously morphed a trailing node into a bogus node.
if (!TextEditorRef().IsMozEditorBogusNode(lastChild)) {
return NS_OK;
}
// Morph it back to a mozBR
lastChild->AsElement()->UnsetAttr(kNameSpaceID_None,
kMOZEditorBogusNodeAttrAtom,
false);
lastChild->AsElement()->SetAttr(kNameSpaceID_None, nsGkAtoms::type,
NS_LITERAL_STRING("_moz"),
true);
return NS_OK;
}
Preparation for ender-based text control * added focus listener. Doesn't do much yet, but when focus notifications start appearing, we'll be ready for them. The code is in place to hide selection when we lose focus and paint selection when we get focus. That's probably not quite right, but it's a start. We will need to be able to determine the distinction between losing focus to another control within our app, and losing focus to another app. * added support for disabled and readonly states in the editor. This is accomplished by having flags set by the client, and letting the rules system deal with those flags. The flags I added are: TEXT_EDITOR_FLAG_PLAINTEXT 0x01 // only plain text editing is allowed TEXT_EDITOR_FLAG_SINGLELINE 0x02 // enter key and CR-LF handled specially TEXT_EDITOR_FLAG_PASSWORD 0x04 // text is not entered into content, only a representative character TEXT_EDITOR_FLAG_READONLY 0x08 // editing events are disabled. Editor may still accept focus. TEXT_EDITOR_FLAG_DISALBED 0x10 // all events are disabled (like scrolling). Editor will not accept focus. * added WillInsertBreak/DidInsertBreak into text rules, so flags could be checked. This gets us readonly, disabled, and single line behavior. * cleaned up the code that allocates, registers, and destroys event listeners. Thanks to Kin and Simon for cleaning up the ownership model on the listeners, it was a big help. * added support for a max text length. You can now tell the text editor, be no bigger than n characters.
1999-05-28 21:24:18 +00:00
nsresult
TextEditRules::CreateBogusNodeIfNeeded(Selection* aSelection)
Preparation for ender-based text control * added focus listener. Doesn't do much yet, but when focus notifications start appearing, we'll be ready for them. The code is in place to hide selection when we lose focus and paint selection when we get focus. That's probably not quite right, but it's a start. We will need to be able to determine the distinction between losing focus to another control within our app, and losing focus to another app. * added support for disabled and readonly states in the editor. This is accomplished by having flags set by the client, and letting the rules system deal with those flags. The flags I added are: TEXT_EDITOR_FLAG_PLAINTEXT 0x01 // only plain text editing is allowed TEXT_EDITOR_FLAG_SINGLELINE 0x02 // enter key and CR-LF handled specially TEXT_EDITOR_FLAG_PASSWORD 0x04 // text is not entered into content, only a representative character TEXT_EDITOR_FLAG_READONLY 0x08 // editing events are disabled. Editor may still accept focus. TEXT_EDITOR_FLAG_DISALBED 0x10 // all events are disabled (like scrolling). Editor will not accept focus. * added WillInsertBreak/DidInsertBreak into text rules, so flags could be checked. This gets us readonly, disabled, and single line behavior. * cleaned up the code that allocates, registers, and destroys event listeners. Thanks to Kin and Simon for cleaning up the ownership model on the listeners, it was a big help. * added support for a max text length. You can now tell the text editor, be no bigger than n characters.
1999-05-28 21:24:18 +00:00
{
MOZ_ASSERT(IsEditorDataAvailable());
NS_ENSURE_TRUE(aSelection, NS_ERROR_NULL_POINTER);
if (mBogusNode) {
// Let's not create more than one, ok?
return NS_OK;
}
// tell rules system to not do any post-processing
AutoRules beginRulesSniffing(&TextEditorRef(), EditAction::ignore,
nsIEditor::eNone);
RefPtr<Element> rootElement = TextEditorRef().GetRoot();
if (!rootElement) {
// We don't even have a body yet, don't insert any bogus nodes at
// this point.
return NS_OK;
}
// Now we've got the body element. Iterate over the body element's children,
// looking for editable content. If no editable content is found, insert the
// bogus node.
bool isRootEditable = TextEditorRef().IsEditable(rootElement);
for (nsIContent* rootChild = rootElement->GetFirstChild();
rootChild;
rootChild = rootChild->GetNextSibling()) {
if (TextEditorRef().IsMozEditorBogusNode(rootChild) ||
!isRootEditable ||
TextEditorRef().IsEditable(rootChild) ||
TextEditorRef().IsBlockNode(rootChild)) {
return NS_OK;
}
}
// Skip adding the bogus node if body is read-only.
if (!TextEditorRef().IsModifiableNode(rootElement)) {
return NS_OK;
}
// Create a br.
RefPtr<Element> newBrElement =
TextEditorRef().CreateHTMLContent(nsGkAtoms::br);
if (NS_WARN_IF(!newBrElement)) {
return NS_ERROR_FAILURE;
}
// set mBogusNode to be the newly created <br>
mBogusNode = newBrElement;
// Give it a special attribute.
newBrElement->SetAttr(kNameSpaceID_None, kMOZEditorBogusNodeAttrAtom,
kMOZEditorBogusNodeValue, false);
// Put the node in the document.
nsresult rv =
TextEditorRef().InsertNodeWithTransaction(
*mBogusNode, EditorRawDOMPoint(rootElement, 0));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
// Set selection.
ErrorResult error;
aSelection->Collapse(EditorRawDOMPoint(rootElement, 0), error);
if (NS_WARN_IF(error.Failed())) {
error.SuppressException();
}
return NS_OK;
}
nsresult
TextEditRules::TruncateInsertionIfNeeded(Selection* aSelection,
const nsAString* aInString,
nsAString* aOutString,
int32_t aMaxLength,
bool* aTruncated)
{
MOZ_ASSERT(IsEditorDataAvailable());
if (!aSelection || !aInString || !aOutString) {
return NS_ERROR_NULL_POINTER;
}
if (!aOutString->Assign(*aInString, mozilla::fallible)) {
return NS_ERROR_OUT_OF_MEMORY;
}
if (aTruncated) {
*aTruncated = false;
}
if (-1 != aMaxLength && IsPlaintextEditor() &&
!TextEditorRef().IsIMEComposing()) {
// Get the current text length.
// Get the length of inString.
// Get the length of the selection.
// If selection is collapsed, it is length 0.
// Subtract the length of the selection from the len(doc)
// since we'll delete the selection on insert.
// This is resultingDocLength.
// Get old length of IME composing string
// which will be replaced by new one.
// If (resultingDocLength) is at or over max, cancel the insert
// If (resultingDocLength) + (length of input) > max,
// set aOutString to subset of inString so length = max
int32_t docLength;
nsresult rv = TextEditorRef().GetTextLength(&docLength);
if (NS_FAILED(rv)) {
return rv;
}
uint32_t start, end;
nsContentUtils::GetSelectionInTextControl(aSelection,
TextEditorRef().GetRoot(),
start, end);
TextComposition* composition = TextEditorRef().GetComposition();
uint32_t oldCompStrLength = composition ? composition->String().Length() : 0;
const uint32_t selectionLength = end - start;
const int32_t resultingDocLength = docLength - selectionLength - oldCompStrLength;
if (resultingDocLength >= aMaxLength) {
// This call is guaranteed to reduce the capacity of the string, so it
// cannot cause an OOM.
aOutString->Truncate();
if (aTruncated) {
*aTruncated = true;
}
} else {
int32_t oldLength = aOutString->Length();
if (oldLength + resultingDocLength > aMaxLength) {
int32_t newLength = aMaxLength - resultingDocLength;
MOZ_ASSERT(newLength > 0);
char16_t newLastChar = aOutString->CharAt(newLength - 1);
char16_t removingFirstChar = aOutString->CharAt(newLength);
// Don't separate the string between a surrogate pair.
if (NS_IS_HIGH_SURROGATE(newLastChar) &&
NS_IS_LOW_SURROGATE(removingFirstChar)) {
newLength--;
}
// XXX What should we do if we're removing IVS and its preceding
// character won't be removed?
// This call is guaranteed to reduce the capacity of the string, so it
// cannot cause an OOM.
aOutString->Truncate(newLength);
if (aTruncated) {
*aTruncated = true;
}
}
}
}
return NS_OK;
Preparation for ender-based text control * added focus listener. Doesn't do much yet, but when focus notifications start appearing, we'll be ready for them. The code is in place to hide selection when we lose focus and paint selection when we get focus. That's probably not quite right, but it's a start. We will need to be able to determine the distinction between losing focus to another control within our app, and losing focus to another app. * added support for disabled and readonly states in the editor. This is accomplished by having flags set by the client, and letting the rules system deal with those flags. The flags I added are: TEXT_EDITOR_FLAG_PLAINTEXT 0x01 // only plain text editing is allowed TEXT_EDITOR_FLAG_SINGLELINE 0x02 // enter key and CR-LF handled specially TEXT_EDITOR_FLAG_PASSWORD 0x04 // text is not entered into content, only a representative character TEXT_EDITOR_FLAG_READONLY 0x08 // editing events are disabled. Editor may still accept focus. TEXT_EDITOR_FLAG_DISALBED 0x10 // all events are disabled (like scrolling). Editor will not accept focus. * added WillInsertBreak/DidInsertBreak into text rules, so flags could be checked. This gets us readonly, disabled, and single line behavior. * cleaned up the code that allocates, registers, and destroys event listeners. Thanks to Kin and Simon for cleaning up the ownership model on the listeners, it was a big help. * added support for a max text length. You can now tell the text editor, be no bigger than n characters.
1999-05-28 21:24:18 +00:00
}
void
TextEditRules::ResetIMETextPWBuf()
{
mPasswordIMEText.Truncate();
}
void
TextEditRules::RemoveIMETextFromPWBuf(uint32_t& aStart,
nsAString* aIMEString)
{
MOZ_ASSERT(aIMEString);
// initialize PasswordIME
if (mPasswordIMEText.IsEmpty()) {
mPasswordIMEIndex = aStart;
} else {
// manage the password buffer
mPasswordText.Cut(mPasswordIMEIndex, mPasswordIMEText.Length());
aStart = mPasswordIMEIndex;
}
mPasswordIMEText.Assign(*aIMEString);
}
NS_IMETHODIMP
TextEditRules::Notify(nsITimer* aTimer)
{
MOZ_ASSERT(mTimer);
if (NS_WARN_IF(!mTextEditor)) {
return NS_ERROR_NOT_AVAILABLE;
}
Selection* selection = mTextEditor->GetSelection();
if (NS_WARN_IF(!selection)) {
return NS_ERROR_FAILURE;
}
AutoSafeEditorData setData(*this, *mTextEditor, *selection);
// Check whether our text editor's password flag was changed before this
// "hide password character" timer actually fires.
nsresult rv = IsPasswordEditor() ? HideLastPWInput() : NS_OK;
ASSERT_PASSWORD_LENGTHS_EQUAL();
mLastLength = 0;
return rv;
}
NS_IMETHODIMP
TextEditRules::GetName(nsACString& aName)
{
aName.AssignLiteral("TextEditRules");
return NS_OK;
}
nsresult
TextEditRules::HideLastPWInput()
{
MOZ_ASSERT(IsEditorDataAvailable());
if (!mLastLength) {
// Special case, we're trying to replace a range that no longer exists
return NS_OK;
}
nsAutoString hiddenText;
FillBufWithPWChars(&hiddenText, mLastLength);
RefPtr<Selection> selection = TextEditorRef().GetSelection();
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
uint32_t start, end;
nsContentUtils::GetSelectionInTextControl(selection,
TextEditorRef().GetRoot(),
start, end);
nsCOMPtr<nsINode> selNode = GetTextNode(selection);
NS_ENSURE_TRUE(selNode, NS_OK);
selNode->GetAsText()->ReplaceData(mLastStart, mLastLength, hiddenText,
IgnoreErrors());
// XXXbz Selection::Collapse/Extend take int32_t, but there are tons of
// callsites... Converting all that is a battle for another day.
selection->Collapse(selNode, start);
if (start != end) {
selection->Extend(selNode, end);
}
return NS_OK;
}
// static
void
TextEditRules::FillBufWithPWChars(nsAString* aOutString,
int32_t aLength)
{
MOZ_ASSERT(aOutString);
// change the output to the platform password character
char16_t passwordChar = LookAndFeel::GetPasswordCharacter();
aOutString->Truncate();
for (int32_t i = 0; i < aLength; i++) {
aOutString->Append(passwordChar);
}
}
already_AddRefed<Element>
TextEditRules::CreateBRInternal(const EditorRawDOMPoint& aPointToInsert,
bool aCreateMozBR)
{
MOZ_ASSERT(IsEditorDataAvailable());
if (NS_WARN_IF(!aPointToInsert.IsSet())) {
return nullptr;
}
RefPtr<Selection> selection = TextEditorRef().GetSelection();
if (NS_WARN_IF(!selection)) {
return nullptr;
}
RefPtr<Element> brElement =
TextEditorRef().InsertBrElementWithTransaction(*selection, aPointToInsert);
if (NS_WARN_IF(!brElement)) {
return nullptr;
}
// give it special moz attr
if (aCreateMozBR) {
// XXX Why do we need to set this attribute with transaction?
nsresult rv =
TextEditorRef().SetAttributeWithTransaction(*brElement, *nsGkAtoms::type,
NS_LITERAL_STRING("_moz"));
if (NS_WARN_IF(NS_FAILED(rv))) {
// XXX Don't we need to remove the new <br> element from the DOM tree
// in this case?
return nullptr;
}
}
return brElement.forget();
}
nsresult
TextEditRules::DocumentModified()
{
return NS_ERROR_NOT_IMPLEMENTED;
}
bool
TextEditRules::IsPasswordEditor() const
{
return mTextEditor ? mTextEditor->IsPasswordEditor() : false;
}
bool
TextEditRules::IsSingleLineEditor() const
{
return mTextEditor ? mTextEditor->IsSingleLineEditor() : false;
}
bool
TextEditRules::IsPlaintextEditor() const
{
return mTextEditor ? mTextEditor->IsPlaintextEditor() : false;
}
bool
TextEditRules::IsReadonly() const
{
return mTextEditor ? mTextEditor->IsReadonly() : false;
}
bool
TextEditRules::IsDisabled() const
{
return mTextEditor ? mTextEditor->IsDisabled() : false;
}
bool
TextEditRules::IsMailEditor() const
{
return mTextEditor ? mTextEditor->IsMailEditor() : false;
}
bool
TextEditRules::DontEchoPassword() const
{
return mTextEditor ? mTextEditor->DontEchoPassword() : false;
}
} // namespace mozilla