2001-09-25 22:53:13 +00:00
|
|
|
/* -*- 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/. */
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2016-07-09 02:54:50 +00:00
|
|
|
#include "mozilla/TextEditor.h"
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2018-04-11 08:37:49 +00:00
|
|
|
#include "EditAggregateTransaction.h"
|
|
|
|
#include "HTMLEditRules.h"
|
2016-07-07 09:00:19 +00:00
|
|
|
#include "InternetCiter.h"
|
2016-07-07 04:44:32 +00:00
|
|
|
#include "TextEditUtils.h"
|
2015-10-26 10:47:16 +00:00
|
|
|
#include "gfxFontUtils.h"
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "mozilla/Assertions.h"
|
2019-01-11 01:50:19 +00:00
|
|
|
#include "mozilla/ContentIterator.h"
|
2018-01-12 10:01:04 +00:00
|
|
|
#include "mozilla/EditAction.h"
|
2017-11-07 10:50:25 +00:00
|
|
|
#include "mozilla/EditorDOMPoint.h"
|
2017-03-09 09:38:41 +00:00
|
|
|
#include "mozilla/HTMLEditor.h"
|
2018-04-18 15:31:51 +00:00
|
|
|
#include "mozilla/IMEStateManager.h"
|
2019-07-22 03:55:13 +00:00
|
|
|
#include "mozilla/LookAndFeel.h"
|
2016-07-09 02:34:41 +00:00
|
|
|
#include "mozilla/mozalloc.h"
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
2019-03-15 05:01:10 +00:00
|
|
|
#include "mozilla/PresShell.h"
|
2019-07-26 01:10:23 +00:00
|
|
|
#include "mozilla/StaticPrefs_editor.h"
|
2016-07-09 02:34:41 +00:00
|
|
|
#include "mozilla/TextEditRules.h"
|
2014-04-03 04:18:37 +00:00
|
|
|
#include "mozilla/TextComposition.h"
|
2013-09-25 11:21:19 +00:00
|
|
|
#include "mozilla/TextEvents.h"
|
2018-04-11 08:37:49 +00:00
|
|
|
#include "mozilla/TextServicesDocument.h"
|
2019-07-22 03:53:36 +00:00
|
|
|
#include "mozilla/dom/DocumentInlines.h"
|
2016-07-09 02:34:41 +00:00
|
|
|
#include "mozilla/dom/Event.h"
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "mozilla/dom/Element.h"
|
2019-07-22 03:53:36 +00:00
|
|
|
#include "mozilla/dom/Selection.h"
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "nsAString.h"
|
|
|
|
#include "nsCRT.h"
|
2008-07-16 10:52:01 +00:00
|
|
|
#include "nsCaret.h"
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "nsCharTraits.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsContentCID.h"
|
2017-12-20 05:46:01 +00:00
|
|
|
#include "nsContentList.h"
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "nsCopySupport.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsDependentSubstring.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsGkAtoms.h"
|
2018-04-18 15:31:51 +00:00
|
|
|
#include "nsIAbsorbingTransaction.h"
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "nsIClipboard.h"
|
2001-01-28 20:13:07 +00:00
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDocumentEncoder.h"
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "nsINode.h"
|
2019-06-10 10:27:07 +00:00
|
|
|
#include "nsIPrincipal.h"
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "nsISelectionController.h"
|
2001-01-28 20:13:07 +00:00
|
|
|
#include "nsISupportsPrimitives.h"
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "nsITransferable.h"
|
|
|
|
#include "nsIWeakReferenceUtils.h"
|
2016-07-07 10:33:32 +00:00
|
|
|
#include "nsNameSpaceManager.h"
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "nsLiteralString.h"
|
2003-07-28 13:13:50 +00:00
|
|
|
#include "nsReadableUtils.h"
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsStringFwd.h"
|
2019-07-22 03:56:33 +00:00
|
|
|
#include "nsTextFragment.h"
|
2017-08-24 10:53:34 +00:00
|
|
|
#include "nsTextNode.h"
|
2002-03-19 06:46:56 +00:00
|
|
|
#include "nsUnicharUtils.h"
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "nsXPCOM.h"
|
2001-04-05 23:48:01 +00:00
|
|
|
|
2012-07-13 06:33:42 +00:00
|
|
|
class nsIOutputStream;
|
|
|
|
class nsISupports;
|
2010-05-19 23:22:19 +00:00
|
|
|
|
2016-07-09 02:54:50 +00:00
|
|
|
namespace mozilla {
|
2011-05-25 06:32:00 +00:00
|
|
|
|
2016-07-09 02:54:50 +00:00
|
|
|
using namespace dom;
|
|
|
|
|
|
|
|
TextEditor::TextEditor()
|
|
|
|
: mWrapColumn(0),
|
|
|
|
mMaxTextLength(-1),
|
|
|
|
mInitTriggerCounter(0),
|
2019-07-22 03:53:36 +00:00
|
|
|
mNewlineHandling(nsIPlaintextEditor::eNewlinesPasteToFirst),
|
2006-10-10 16:50:50 +00:00
|
|
|
#ifdef XP_WIN
|
2019-07-22 03:53:36 +00:00
|
|
|
mCaretStyle(1),
|
2006-10-10 16:50:50 +00:00
|
|
|
#else
|
2019-07-22 03:53:36 +00:00
|
|
|
mCaretStyle(0),
|
2006-10-10 16:50:50 +00:00
|
|
|
#endif
|
2019-07-22 03:53:36 +00:00
|
|
|
mUnmaskedStart(UINT32_MAX),
|
|
|
|
mUnmaskedLength(0),
|
|
|
|
mIsMaskingPassword(true) {
|
2018-11-26 06:33:28 +00:00
|
|
|
// printf("Size of TextEditor: %zu\n", sizeof(TextEditor));
|
|
|
|
static_assert(
|
|
|
|
sizeof(TextEditor) <= 512,
|
|
|
|
"TextEditor instance should be allocatable in the quantum class bins");
|
|
|
|
|
2014-05-22 22:06:44 +00:00
|
|
|
// check the "single line editor newline handling"
|
|
|
|
// and "caret behaviour in selection" prefs
|
|
|
|
GetDefaultEditorPrefs(mNewlineHandling, mCaretStyle);
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2016-07-09 02:54:50 +00:00
|
|
|
TextEditor::~TextEditor() {
|
2002-11-12 23:30:29 +00:00
|
|
|
// Remove event listeners. Note that if we had an HTML editor,
|
|
|
|
// it installed its own instead of these
|
2005-12-11 09:26:29 +00:00
|
|
|
RemoveEventListeners();
|
2009-05-09 04:59:24 +00:00
|
|
|
|
|
|
|
if (mRules) mRules->DetachEditor();
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2016-07-09 02:54:50 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(TextEditor)
|
2013-08-02 01:29:05 +00:00
|
|
|
|
2016-07-08 04:10:13 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(TextEditor, EditorBase)
|
2019-07-22 03:53:36 +00:00
|
|
|
if (tmp->mRules) {
|
|
|
|
tmp->mRules->DetachEditor();
|
|
|
|
}
|
|
|
|
if (tmp->mMaskTimer) {
|
|
|
|
tmp->mMaskTimer->Cancel();
|
|
|
|
}
|
2012-11-15 07:32:40 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mRules)
|
2017-04-17 08:29:46 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mCachedDocumentEncoder)
|
2019-07-22 03:53:36 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mMaskTimer)
|
2009-05-09 04:59:25 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2016-07-08 04:10:13 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(TextEditor, EditorBase)
|
2012-11-15 07:32:40 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRules)
|
2017-04-17 08:29:46 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCachedDocumentEncoder)
|
2019-07-22 03:53:36 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMaskTimer)
|
2009-05-09 04:59:25 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2016-07-08 04:10:13 +00:00
|
|
|
NS_IMPL_ADDREF_INHERITED(TextEditor, EditorBase)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(TextEditor, EditorBase)
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2017-08-29 23:02:48 +00:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TextEditor)
|
2003-06-25 19:21:59 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIPlaintextEditor)
|
2019-07-22 03:53:36 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsITimerCallback)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsINamed)
|
2016-07-08 04:10:13 +00:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(EditorBase)
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2019-01-02 13:05:23 +00:00
|
|
|
nsresult TextEditor::Init(Document& aDoc, Element* aRoot,
|
2016-07-09 02:54:50 +00:00
|
|
|
nsISelectionController* aSelCon, uint32_t aFlags,
|
|
|
|
const nsAString& aInitialValue) {
|
2012-12-06 23:31:48 +00:00
|
|
|
if (mRules) {
|
|
|
|
mRules->DetachEditor();
|
|
|
|
}
|
2015-05-28 15:58:42 +00:00
|
|
|
|
2016-10-19 09:09:33 +00:00
|
|
|
nsresult rulesRv = NS_OK;
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
2016-06-23 10:18:13 +00:00
|
|
|
// block to scope AutoEditInitRulesTrigger
|
2016-10-19 09:09:33 +00:00
|
|
|
AutoEditInitRulesTrigger rulesTrigger(this, rulesRv);
|
2015-05-28 15:58:42 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Init the base editor
|
2016-10-19 09:09:33 +00:00
|
|
|
nsresult rv = EditorBase::Init(aDoc, aRoot, aSelCon, aFlags, aInitialValue);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
2019-07-22 03:53:58 +00:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rulesRv), "Failed to initialize us");
|
|
|
|
return EditorBase::ToGenericNSResult(rulesRv);
|
2010-02-01 18:12:31 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
static int32_t sNewlineHandlingPref = -1, sCaretStylePref = -1;
|
2010-04-13 17:44:17 +00:00
|
|
|
|
2016-07-09 02:54:50 +00:00
|
|
|
static void EditorPrefsChangedCallback(const char* aPrefName, void*) {
|
2016-10-24 02:27:45 +00:00
|
|
|
if (!nsCRT::strcmp(aPrefName, "editor.singleLine.pasteNewlines")) {
|
2011-05-25 06:32:00 +00:00
|
|
|
sNewlineHandlingPref =
|
|
|
|
Preferences::GetInt("editor.singleLine.pasteNewlines",
|
|
|
|
nsIPlaintextEditor::eNewlinesPasteToFirst);
|
2016-10-24 02:27:45 +00:00
|
|
|
} else if (!nsCRT::strcmp(aPrefName, "layout.selection.caret_style")) {
|
2011-05-25 06:32:00 +00:00
|
|
|
sCaretStylePref = Preferences::GetInt("layout.selection.caret_style",
|
2010-02-01 18:12:31 +00:00
|
|
|
#ifdef XP_WIN
|
2010-04-13 17:44:17 +00:00
|
|
|
1);
|
2016-10-24 02:27:45 +00:00
|
|
|
if (!sCaretStylePref) {
|
2010-04-13 17:44:17 +00:00
|
|
|
sCaretStylePref = 1;
|
2016-10-24 02:27:45 +00:00
|
|
|
}
|
2010-02-01 18:12:31 +00:00
|
|
|
#else
|
2010-04-13 17:44:17 +00:00
|
|
|
0);
|
2010-02-01 18:12:31 +00:00
|
|
|
#endif
|
2010-04-13 17:44:17 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-01 18:12:31 +00:00
|
|
|
|
2010-04-13 17:44:17 +00:00
|
|
|
// static
|
2016-07-09 02:54:50 +00:00
|
|
|
void TextEditor::GetDefaultEditorPrefs(int32_t& aNewlineHandling,
|
|
|
|
int32_t& aCaretStyle) {
|
2010-04-13 17:44:17 +00:00
|
|
|
if (sNewlineHandlingPref == -1) {
|
2017-03-21 18:59:02 +00:00
|
|
|
Preferences::RegisterCallbackAndCall(EditorPrefsChangedCallback,
|
|
|
|
"editor.singleLine.pasteNewlines");
|
|
|
|
Preferences::RegisterCallbackAndCall(EditorPrefsChangedCallback,
|
|
|
|
"layout.selection.caret_style");
|
2006-10-10 16:50:50 +00:00
|
|
|
}
|
2010-04-13 17:44:17 +00:00
|
|
|
|
|
|
|
aNewlineHandling = sNewlineHandlingPref;
|
|
|
|
aCaretStyle = sCaretStylePref;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2016-07-09 02:54:50 +00:00
|
|
|
void TextEditor::BeginEditorInit() { mInitTriggerCounter++; }
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2016-07-09 02:54:50 +00:00
|
|
|
nsresult TextEditor::EndEditorInit() {
|
2018-04-28 19:50:58 +00:00
|
|
|
MOZ_ASSERT(mInitTriggerCounter > 0, "ended editor init before we began?");
|
2001-01-28 20:13:07 +00:00
|
|
|
mInitTriggerCounter--;
|
2016-10-19 09:09:33 +00:00
|
|
|
if (mInitTriggerCounter) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-10-30 09:59:33 +00:00
|
|
|
AutoEditActionDataSetter editActionData(*this, EditAction::eNotEditing);
|
|
|
|
if (NS_WARN_IF(!editActionData.CanHandle())) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2016-10-19 09:09:33 +00:00
|
|
|
nsresult rv = InitRules();
|
2019-02-25 09:07:54 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2019-07-22 03:53:58 +00:00
|
|
|
return rv;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
2016-10-19 09:09:33 +00:00
|
|
|
// Throw away the old transaction manager if this is not the first time that
|
|
|
|
// we're initializing the editor.
|
2018-03-23 06:25:13 +00:00
|
|
|
ClearUndoRedo();
|
|
|
|
EnableUndoRedo();
|
2016-10-19 09:09:33 +00:00
|
|
|
return NS_OK;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2012-02-01 10:54:22 +00:00
|
|
|
NS_IMETHODIMP
|
2016-07-09 02:54:50 +00:00
|
|
|
TextEditor::SetDocumentCharacterSet(const nsACString& characterSet) {
|
2018-10-30 09:59:33 +00:00
|
|
|
AutoEditActionDataSetter editActionData(*this, EditAction::eSetCharacterSet);
|
|
|
|
if (NS_WARN_IF(!editActionData.CanHandle())) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2016-07-08 04:10:13 +00:00
|
|
|
nsresult rv = EditorBase::SetDocumentCharacterSet(characterSet);
|
2019-02-25 09:07:54 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return EditorBase::ToGenericNSResult(rv);
|
|
|
|
}
|
2012-02-01 10:54:22 +00:00
|
|
|
|
|
|
|
// Update META charset element.
|
2019-01-02 13:05:23 +00:00
|
|
|
RefPtr<Document> doc = GetDocument();
|
2017-03-17 07:32:06 +00:00
|
|
|
if (NS_WARN_IF(!doc)) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2012-02-01 10:54:22 +00:00
|
|
|
|
2017-03-17 07:32:06 +00:00
|
|
|
if (UpdateMetaCharset(*doc, characterSet)) {
|
2012-02-01 10:54:22 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-17 07:32:06 +00:00
|
|
|
RefPtr<nsContentList> headList =
|
|
|
|
doc->GetElementsByTagName(NS_LITERAL_STRING("head"));
|
|
|
|
if (NS_WARN_IF(!headList)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2017-03-17 07:32:06 +00:00
|
|
|
nsCOMPtr<nsIContent> headNode = headList->Item(0);
|
|
|
|
if (NS_WARN_IF(!headNode)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-02-01 10:54:22 +00:00
|
|
|
|
|
|
|
// Create a new meta charset tag
|
2018-04-09 16:17:26 +00:00
|
|
|
RefPtr<Element> metaElement =
|
2019-05-08 09:40:17 +00:00
|
|
|
CreateNodeWithTransaction(*nsGkAtoms::meta, EditorDOMPoint(headNode, 0));
|
2017-03-17 07:32:06 +00:00
|
|
|
if (NS_WARN_IF(!metaElement)) {
|
2012-02-01 10:54:22 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-17 07:32:06 +00:00
|
|
|
// Set attributes to the created element
|
|
|
|
if (characterSet.IsEmpty()) {
|
2012-02-01 10:54:22 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-04-09 16:17:26 +00:00
|
|
|
// not undoable, undo should undo CreateNodeWithTransaction().
|
2012-02-01 10:54:22 +00:00
|
|
|
metaElement->SetAttr(kNameSpaceID_None, nsGkAtoms::httpEquiv,
|
|
|
|
NS_LITERAL_STRING("Content-Type"), true);
|
|
|
|
metaElement->SetAttr(kNameSpaceID_None, nsGkAtoms::content,
|
|
|
|
NS_LITERAL_STRING("text/html;charset=") +
|
|
|
|
NS_ConvertASCIItoUTF16(characterSet),
|
|
|
|
true);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-01-02 13:05:23 +00:00
|
|
|
bool TextEditor::UpdateMetaCharset(Document& aDocument,
|
2016-07-09 02:54:50 +00:00
|
|
|
const nsACString& aCharacterSet) {
|
2012-02-01 10:54:22 +00:00
|
|
|
// get a list of META tags
|
2017-03-17 07:32:06 +00:00
|
|
|
RefPtr<nsContentList> metaList =
|
|
|
|
aDocument.GetElementsByTagName(NS_LITERAL_STRING("meta"));
|
|
|
|
if (NS_WARN_IF(!metaList)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-02-01 10:54:22 +00:00
|
|
|
|
2017-03-17 07:32:06 +00:00
|
|
|
for (uint32_t i = 0; i < metaList->Length(true); ++i) {
|
2012-10-13 12:50:24 +00:00
|
|
|
nsCOMPtr<nsIContent> metaNode = metaList->Item(i);
|
2012-02-01 10:54:22 +00:00
|
|
|
MOZ_ASSERT(metaNode);
|
|
|
|
|
|
|
|
if (!metaNode->IsElement()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString currentValue;
|
2017-12-07 18:13:50 +00:00
|
|
|
metaNode->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::httpEquiv,
|
|
|
|
currentValue);
|
2012-02-01 10:54:22 +00:00
|
|
|
|
|
|
|
if (!FindInReadable(NS_LITERAL_STRING("content-type"), currentValue,
|
|
|
|
nsCaseInsensitiveStringComparator())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-12-07 18:13:50 +00:00
|
|
|
metaNode->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::content,
|
|
|
|
currentValue);
|
2012-02-01 10:54:22 +00:00
|
|
|
|
|
|
|
NS_NAMED_LITERAL_STRING(charsetEquals, "charset=");
|
|
|
|
nsAString::const_iterator originalStart, start, end;
|
|
|
|
originalStart = currentValue.BeginReading(start);
|
|
|
|
currentValue.EndReading(end);
|
|
|
|
if (!FindInReadable(charsetEquals, start, end,
|
|
|
|
nsCaseInsensitiveStringComparator())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set attribute to <original prefix> charset=text/html
|
2016-12-20 11:53:00 +00:00
|
|
|
RefPtr<Element> metaElement = metaNode->AsElement();
|
2012-02-01 10:54:22 +00:00
|
|
|
MOZ_ASSERT(metaElement);
|
2018-04-12 07:58:33 +00:00
|
|
|
nsresult rv = SetAttributeWithTransaction(
|
|
|
|
*metaElement, *nsGkAtoms::content,
|
|
|
|
Substring(originalStart, start) + charsetEquals +
|
|
|
|
NS_ConvertASCIItoUTF16(aCharacterSet));
|
2012-02-01 10:54:22 +00:00
|
|
|
return NS_SUCCEEDED(rv);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2016-07-09 02:54:50 +00:00
|
|
|
nsresult TextEditor::InitRules() {
|
2018-10-30 10:02:58 +00:00
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
|
|
|
|
2014-05-09 21:02:29 +00:00
|
|
|
if (!mRules) {
|
|
|
|
// instantiate the rules for this text editor
|
2016-07-09 02:34:41 +00:00
|
|
|
mRules = new TextEditRules();
|
2014-05-09 21:02:29 +00:00
|
|
|
}
|
2019-03-30 11:55:29 +00:00
|
|
|
RefPtr<TextEditRules> textEditRules(mRules);
|
|
|
|
return textEditRules->Init(this);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2017-02-08 11:18:17 +00:00
|
|
|
nsresult TextEditor::HandleKeyPressEvent(WidgetKeyboardEvent* aKeyboardEvent) {
|
2010-06-10 01:16:58 +00:00
|
|
|
// NOTE: When you change this method, you should also change:
|
2014-08-19 00:59:37 +00:00
|
|
|
// * editor/libeditor/tests/test_texteditor_keyevent_handling.html
|
2014-08-18 21:28:17 +00:00
|
|
|
// * editor/libeditor/tests/test_htmleditor_keyevent_handling.html
|
2010-06-10 01:16:58 +00:00
|
|
|
//
|
|
|
|
// And also when you add new key handling, you need to change the subclass's
|
|
|
|
// HandleKeyPressEvent()'s switch statement.
|
|
|
|
|
|
|
|
if (IsReadonly() || IsDisabled()) {
|
2016-07-08 04:10:13 +00:00
|
|
|
// When we're not editable, the events handled on EditorBase.
|
2017-02-08 11:18:17 +00:00
|
|
|
return EditorBase::HandleKeyPressEvent(aKeyboardEvent);
|
2010-06-10 01:16:58 +00:00
|
|
|
}
|
2010-06-02 07:24:56 +00:00
|
|
|
|
2017-02-08 11:18:17 +00:00
|
|
|
if (NS_WARN_IF(!aKeyboardEvent)) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(aKeyboardEvent->mMessage == eKeyPress,
|
|
|
|
"HandleKeyPressEvent gets non-keypress event");
|
2010-06-10 01:16:58 +00:00
|
|
|
|
2017-02-08 11:18:17 +00:00
|
|
|
switch (aKeyboardEvent->mKeyCode) {
|
2016-05-12 08:13:49 +00:00
|
|
|
case NS_VK_META:
|
|
|
|
case NS_VK_WIN:
|
|
|
|
case NS_VK_SHIFT:
|
|
|
|
case NS_VK_CONTROL:
|
|
|
|
case NS_VK_ALT:
|
2016-07-08 04:10:13 +00:00
|
|
|
// These keys are handled on EditorBase
|
2017-02-08 11:18:17 +00:00
|
|
|
return EditorBase::HandleKeyPressEvent(aKeyboardEvent);
|
2019-06-10 10:27:07 +00:00
|
|
|
case NS_VK_BACK: {
|
2018-04-11 08:37:49 +00:00
|
|
|
if (aKeyboardEvent->IsControl() || aKeyboardEvent->IsAlt() ||
|
|
|
|
aKeyboardEvent->IsMeta() || aKeyboardEvent->IsOS()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
DeleteSelectionAsAction(nsIEditor::ePrevious, nsIEditor::eStrip);
|
|
|
|
aKeyboardEvent->PreventDefault(); // consumed
|
|
|
|
return NS_OK;
|
2019-06-10 10:27:07 +00:00
|
|
|
}
|
|
|
|
case NS_VK_DELETE: {
|
2018-04-11 08:37:49 +00:00
|
|
|
// 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.
|
|
|
|
if (aKeyboardEvent->IsShift() || aKeyboardEvent->IsControl() ||
|
|
|
|
aKeyboardEvent->IsAlt() || aKeyboardEvent->IsMeta() ||
|
|
|
|
aKeyboardEvent->IsOS()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
DeleteSelectionAsAction(nsIEditor::eNext, nsIEditor::eStrip);
|
|
|
|
aKeyboardEvent->PreventDefault(); // consumed
|
|
|
|
return NS_OK;
|
2019-06-10 10:27:07 +00:00
|
|
|
}
|
2016-05-12 08:13:49 +00:00
|
|
|
case NS_VK_TAB: {
|
2010-06-10 01:16:58 +00:00
|
|
|
if (IsTabbable()) {
|
|
|
|
return NS_OK; // let it be used for focus switching
|
|
|
|
}
|
2010-06-04 01:28:03 +00:00
|
|
|
|
2017-02-08 11:18:17 +00:00
|
|
|
if (aKeyboardEvent->IsShift() || aKeyboardEvent->IsControl() ||
|
|
|
|
aKeyboardEvent->IsAlt() || aKeyboardEvent->IsMeta() ||
|
|
|
|
aKeyboardEvent->IsOS()) {
|
2010-06-10 01:16:58 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// else we insert the tab straight through
|
2017-02-08 11:18:17 +00:00
|
|
|
aKeyboardEvent->PreventDefault();
|
2018-04-16 14:43:36 +00:00
|
|
|
return OnInputText(NS_LITERAL_STRING("\t"));
|
2010-06-04 01:28:03 +00:00
|
|
|
}
|
2016-05-12 08:13:49 +00:00
|
|
|
case NS_VK_RETURN:
|
2018-01-25 14:59:20 +00:00
|
|
|
if (IsSingleLineEditor() || !aKeyboardEvent->IsInputtingLineBreak()) {
|
2010-06-10 01:16:58 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-02-08 11:18:17 +00:00
|
|
|
aKeyboardEvent->PreventDefault();
|
2018-11-03 11:22:13 +00:00
|
|
|
return InsertLineBreakAsAction();
|
2010-06-10 01:16:58 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 14:27:07 +00:00
|
|
|
if (!aKeyboardEvent->IsInputtingText()) {
|
2010-06-10 01:16:58 +00:00
|
|
|
// we don't PreventDefault() here or keybindings like control-x won't work
|
|
|
|
return NS_OK;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
2017-02-08 11:18:17 +00:00
|
|
|
aKeyboardEvent->PreventDefault();
|
|
|
|
nsAutoString str(aKeyboardEvent->mCharCode);
|
2018-04-16 14:43:36 +00:00
|
|
|
return OnInputText(str);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2018-04-16 14:43:36 +00:00
|
|
|
nsresult TextEditor::OnInputText(const nsAString& aStringToInsert) {
|
2018-10-30 09:59:33 +00:00
|
|
|
AutoEditActionDataSetter editActionData(*this, EditAction::eInsertText);
|
|
|
|
if (NS_WARN_IF(!editActionData.CanHandle())) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2019-02-19 06:28:19 +00:00
|
|
|
MOZ_ASSERT(!aStringToInsert.IsVoid());
|
|
|
|
editActionData.SetData(aStringToInsert);
|
2018-10-30 09:59:33 +00:00
|
|
|
|
2018-10-24 09:42:06 +00:00
|
|
|
AutoPlaceholderBatch treatAsOneTransaction(*this, *nsGkAtoms::TypingTxnName);
|
2018-07-02 11:12:22 +00:00
|
|
|
nsresult rv = InsertTextAsSubAction(aStringToInsert);
|
2018-04-16 14:43:36 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2019-02-25 09:07:54 +00:00
|
|
|
return EditorBase::ToGenericNSResult(rv);
|
2018-04-16 14:43:36 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2019-06-10 10:27:07 +00:00
|
|
|
nsresult TextEditor::InsertLineBreakAsAction(nsIPrincipal* aPrincipal) {
|
|
|
|
AutoEditActionDataSetter editActionData(*this, EditAction::eInsertLineBreak,
|
|
|
|
aPrincipal);
|
2018-11-03 11:22:13 +00:00
|
|
|
if (NS_WARN_IF(!editActionData.CanHandle())) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX This may be called by execCommand() with "insertParagraph".
|
|
|
|
// In such case, naming the transaction "TypingTxnName" is odd.
|
|
|
|
AutoPlaceholderBatch treatAsOneTransaction(*this, *nsGkAtoms::TypingTxnName);
|
|
|
|
nsresult rv = InsertLineBreakAsSubAction();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2019-02-25 09:07:54 +00:00
|
|
|
return EditorBase::ToGenericNSResult(rv);
|
2018-11-03 11:22:13 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-10-30 10:01:38 +00:00
|
|
|
nsresult TextEditor::ExtendSelectionForDelete(nsIEditor::EDirection* aAction) {
|
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
|
|
|
|
|
|
|
bool bCollapsed = SelectionRefPtr()->IsCollapsed();
|
2008-10-16 07:44:32 +00:00
|
|
|
|
2016-10-24 02:27:45 +00:00
|
|
|
if (*aAction == eNextWord || *aAction == ePreviousWord ||
|
|
|
|
(*aAction == eNext && bCollapsed) ||
|
|
|
|
(*aAction == ePrevious && bCollapsed) || *aAction == eToBeginningOfLine ||
|
|
|
|
*aAction == eToEndOfLine) {
|
2011-03-11 05:40:30 +00:00
|
|
|
nsCOMPtr<nsISelectionController> selCont;
|
|
|
|
GetSelectionController(getter_AddRefs(selCont));
|
2010-06-17 20:40:48 +00:00
|
|
|
NS_ENSURE_TRUE(selCont, NS_ERROR_NO_INTERFACE);
|
2008-10-16 07:44:32 +00:00
|
|
|
|
2016-10-24 02:27:45 +00:00
|
|
|
switch (*aAction) {
|
2017-11-08 12:55:10 +00:00
|
|
|
case eNextWord: {
|
|
|
|
nsresult rv = selCont->WordExtendForDelete(true);
|
2018-04-11 10:11:15 +00:00
|
|
|
// DeleteSelectionWithTransaction() doesn't handle these actions
|
2008-10-16 07:44:32 +00:00
|
|
|
// because it's inside batching, so don't confuse it:
|
|
|
|
*aAction = eNone;
|
2017-11-08 12:55:10 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
case ePreviousWord: {
|
|
|
|
nsresult rv = selCont->WordExtendForDelete(false);
|
2008-10-16 07:44:32 +00:00
|
|
|
*aAction = eNone;
|
2017-11-08 12:55:10 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
case eNext: {
|
|
|
|
nsresult rv = selCont->CharacterExtendForDelete();
|
2009-08-03 21:46:48 +00:00
|
|
|
// Don't set aAction to eNone (see Bug 502259)
|
2017-11-08 12:55:10 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-06-04 17:28:19 +00:00
|
|
|
case ePrevious: {
|
|
|
|
// Only extend the selection where the selection is after a UTF-16
|
2015-10-26 10:47:16 +00:00
|
|
|
// surrogate pair or a variation selector.
|
|
|
|
// For other cases we don't want to do that, in order
|
2010-06-04 17:28:19 +00:00
|
|
|
// to make sure that pressing backspace will only delete the last
|
|
|
|
// typed character.
|
2017-11-08 12:55:10 +00:00
|
|
|
EditorRawDOMPoint atStartOfSelection =
|
2018-10-30 10:01:38 +00:00
|
|
|
EditorBase::GetStartPoint(*SelectionRefPtr());
|
2017-11-08 12:55:10 +00:00
|
|
|
if (NS_WARN_IF(!atStartOfSelection.IsSet())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2010-06-04 17:28:19 +00:00
|
|
|
|
2016-10-17 05:22:46 +00:00
|
|
|
// node might be anonymous DIV, so we find better text node
|
2017-11-08 12:55:10 +00:00
|
|
|
EditorRawDOMPoint insertionPoint =
|
|
|
|
FindBetterInsertionPoint(atStartOfSelection);
|
2016-10-17 05:22:46 +00:00
|
|
|
|
2017-12-07 09:45:52 +00:00
|
|
|
if (insertionPoint.IsInTextNode()) {
|
2017-11-08 12:55:10 +00:00
|
|
|
const nsTextFragment* data =
|
2019-05-22 05:18:48 +00:00
|
|
|
&insertionPoint.GetContainerAsText()->TextFragment();
|
2017-11-08 12:55:10 +00:00
|
|
|
uint32_t offset = insertionPoint.Offset();
|
2019-08-06 05:43:21 +00:00
|
|
|
if ((offset > 1 &&
|
|
|
|
data->IsLowSurrogateFollowingHighSurrogateAt(offset - 1)) ||
|
2017-01-23 05:14:30 +00:00
|
|
|
(offset > 0 &&
|
|
|
|
gfxFontUtils::IsVarSelector(data->CharAt(offset - 1)))) {
|
2017-11-08 12:55:10 +00:00
|
|
|
nsresult rv = selCont->CharacterExtendForBackspace();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2010-06-04 17:28:19 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-08 12:55:10 +00:00
|
|
|
return NS_OK;
|
2010-06-04 17:28:19 +00:00
|
|
|
}
|
2017-11-08 12:55:10 +00:00
|
|
|
case eToBeginningOfLine: {
|
|
|
|
// Select to beginning
|
|
|
|
nsresult rv = selCont->IntraLineMove(false, true);
|
2008-10-16 07:44:32 +00:00
|
|
|
*aAction = eNone;
|
2017-11-08 12:55:10 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
case eToEndOfLine: {
|
|
|
|
nsresult rv = selCont->IntraLineMove(true, true);
|
2008-10-16 07:44:32 +00:00
|
|
|
*aAction = eNext;
|
2017-11-08 12:55:10 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
// For avoiding several compiler warnings
|
|
|
|
default:
|
|
|
|
return NS_OK;
|
2008-10-16 07:44:32 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-24 15:43:49 +00:00
|
|
|
return NS_OK;
|
2008-10-16 07:44:32 +00:00
|
|
|
}
|
|
|
|
|
2018-04-11 08:37:49 +00:00
|
|
|
NS_IMETHODIMP
|
2016-07-09 02:54:50 +00:00
|
|
|
TextEditor::DeleteSelection(EDirection aAction, EStripWrappers aStripWrappers) {
|
2018-04-11 08:37:49 +00:00
|
|
|
nsresult rv = DeleteSelectionAsAction(aAction, aStripWrappers);
|
2019-06-10 10:27:07 +00:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to do delete selection");
|
|
|
|
return rv;
|
2018-04-11 08:37:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult TextEditor::DeleteSelectionAsAction(EDirection aDirection,
|
2019-06-10 10:27:07 +00:00
|
|
|
EStripWrappers aStripWrappers,
|
|
|
|
nsIPrincipal* aPrincipal) {
|
2012-05-10 14:54:33 +00:00
|
|
|
MOZ_ASSERT(aStripWrappers == eStrip || aStripWrappers == eNoStrip);
|
Bug 1467794 - Split TextEditor::DeleteSelectionAsAction() to itself and TextEditor::DeleteSelectionAsSubAction() r=m_kato
TextEditor::DeleteSelectionAsAction() is called even if it's a part of edit
action. For example, it's called to prepare for inserting text.
For bug 1465702, editor itself and edit rules classes should not call
public DeleteSelectionAsAction() directly. Therefore, this patch creates
DeleteSelectionAsSubAction() for internal use.
Note that this patch adds NS_ASSERTION() to detect wrong caller. However,
it cannot distinguish if the call is valid, for example, it's allowed to
call DeleteSelectionAsSelection() even if it's handling an edit action but
the method is called via mutation event listener. So, we need to allow
some assertions with some tests. But unfortunately, 1405747.html uses
mutation event listener too many times (about 1,000 times) and the number
of assertion isn't stable. Therefore, this patch makes the test stop using
the mutation event listener 2nd time since I can reproduce the crash with
ESR 52 at the 2nd time.
MozReview-Commit-ID: 1TWaypmnoCC
--HG--
extra : rebase_source : a6a4fb1cbcaf2ab6f10c5f3e7168a6bc0fcb02ed
2018-06-29 11:16:50 +00:00
|
|
|
// Showing this assertion is fine if this method is called by outside via
|
|
|
|
// mutation event listener or something. Otherwise, this is called by
|
|
|
|
// wrong method.
|
|
|
|
NS_ASSERTION(!mPlaceholderBatch,
|
|
|
|
"Should be called only when this is the only edit action of the "
|
|
|
|
"operation "
|
|
|
|
"unless mutation event listener nests some operations");
|
|
|
|
|
2018-10-30 09:59:33 +00:00
|
|
|
EditAction editAction = EditAction::eDeleteSelection;
|
|
|
|
switch (aDirection) {
|
|
|
|
case nsIEditor::ePrevious:
|
|
|
|
editAction = EditAction::eDeleteBackward;
|
|
|
|
break;
|
|
|
|
case nsIEditor::eNext:
|
|
|
|
editAction = EditAction::eDeleteForward;
|
|
|
|
break;
|
|
|
|
case nsIEditor::ePreviousWord:
|
|
|
|
editAction = EditAction::eDeleteWordBackward;
|
|
|
|
break;
|
|
|
|
case nsIEditor::eNextWord:
|
|
|
|
editAction = EditAction::eDeleteWordForward;
|
|
|
|
break;
|
|
|
|
case nsIEditor::eToBeginningOfLine:
|
|
|
|
editAction = EditAction::eDeleteToBeginningOfSoftLine;
|
|
|
|
break;
|
|
|
|
case nsIEditor::eToEndOfLine:
|
|
|
|
editAction = EditAction::eDeleteToEndOfSoftLine;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-06-10 10:27:07 +00:00
|
|
|
AutoEditActionDataSetter editActionData(*this, editAction, aPrincipal);
|
2018-10-30 09:59:33 +00:00
|
|
|
if (NS_WARN_IF(!editActionData.CanHandle())) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
Bug 1503231 - Make TextEditor::DeleteSelectionAsAction() removes removing range information from EditAction when Selection is NOT collapsed r=m_kato
When Selection is NOT collapsed, we remove selected content. Therefore,
web apps don't need to know range information of user operation. However, web
apps may want to know direction of the operation (backward or forward). E.g.,
web apps may just mark selected range as "deleted" and move caret before or
after the range.
Therefore, when computed EditAction is eDeleteWordBackward or
eDeleteToBeginningOfSoftLine, we should use eDeleteBackward instead. When it
is eDeleteWordForward or eDeleteToEndOfSoftLine, we should use eDeleteForward
instead.
Note that only on Windows, we follow behavior of richtext control (and Word).
That is, Ctrl + Backspace/Delete collapse from start of selected range to
start/end of current word. I.e., collapsing Selection to start first and
removing to start or end of current word is Windows's standard behavior.
Currently, we do this in DeleteSelectionAsSubAction() but every caller
specifies eNone to aDirection except DeleteSelectionAsAction(). So, we can
move this before re-computing EditAction in DeleteSelectionAsAction().
Differential Revision: https://phabricator.services.mozilla.com/D10992
--HG--
extra : moz-landing-system : lando
2018-11-07 08:38:15 +00:00
|
|
|
// If there is an existing selection when an extended delete is requested,
|
|
|
|
// platforms that use "caret-style" caret positioning collapse the
|
|
|
|
// selection to the start and then create a new selection.
|
|
|
|
// Platforms that use "selection-style" caret positioning just delete the
|
|
|
|
// existing selection without extending it.
|
|
|
|
if (!SelectionRefPtr()->IsCollapsed()) {
|
|
|
|
switch (aDirection) {
|
|
|
|
case eNextWord:
|
|
|
|
case ePreviousWord:
|
|
|
|
case eToBeginningOfLine:
|
|
|
|
case eToEndOfLine: {
|
|
|
|
if (mCaretStyle != 1) {
|
|
|
|
aDirection = eNone;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ErrorResult error;
|
|
|
|
SelectionRefPtr()->CollapseToStart(error);
|
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
2019-02-25 09:07:54 +00:00
|
|
|
return EditorBase::ToGenericNSResult(error.StealNSResult());
|
Bug 1503231 - Make TextEditor::DeleteSelectionAsAction() removes removing range information from EditAction when Selection is NOT collapsed r=m_kato
When Selection is NOT collapsed, we remove selected content. Therefore,
web apps don't need to know range information of user operation. However, web
apps may want to know direction of the operation (backward or forward). E.g.,
web apps may just mark selected range as "deleted" and move caret before or
after the range.
Therefore, when computed EditAction is eDeleteWordBackward or
eDeleteToBeginningOfSoftLine, we should use eDeleteBackward instead. When it
is eDeleteWordForward or eDeleteToEndOfSoftLine, we should use eDeleteForward
instead.
Note that only on Windows, we follow behavior of richtext control (and Word).
That is, Ctrl + Backspace/Delete collapse from start of selected range to
start/end of current word. I.e., collapsing Selection to start first and
removing to start or end of current word is Windows's standard behavior.
Currently, we do this in DeleteSelectionAsSubAction() but every caller
specifies eNone to aDirection except DeleteSelectionAsAction(). So, we can
move this before re-computing EditAction in DeleteSelectionAsAction().
Differential Revision: https://phabricator.services.mozilla.com/D10992
--HG--
extra : moz-landing-system : lando
2018-11-07 08:38:15 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If Selection is still NOT collapsed, it does not important removing
|
|
|
|
// range of the operation since we'll remove the selected content. However,
|
|
|
|
// information of direction (backward or forward) may be important for
|
|
|
|
// web apps. E.g., web apps may want to mark selected range as "deleted"
|
|
|
|
// and move caret before or after the range. Therefore, we should forget
|
|
|
|
// only the range information but keep range information. See discussion
|
|
|
|
// of the spec issue for the detail:
|
|
|
|
// https://github.com/w3c/input-events/issues/82
|
|
|
|
if (!SelectionRefPtr()->IsCollapsed()) {
|
|
|
|
switch (editAction) {
|
|
|
|
case EditAction::eDeleteWordBackward:
|
|
|
|
case EditAction::eDeleteToBeginningOfSoftLine:
|
|
|
|
editActionData.UpdateEditAction(EditAction::eDeleteBackward);
|
|
|
|
break;
|
|
|
|
case EditAction::eDeleteWordForward:
|
|
|
|
case EditAction::eDeleteToEndOfSoftLine:
|
|
|
|
editActionData.UpdateEditAction(EditAction::eDeleteForward);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Bug 1467794 - Split TextEditor::DeleteSelectionAsAction() to itself and TextEditor::DeleteSelectionAsSubAction() r=m_kato
TextEditor::DeleteSelectionAsAction() is called even if it's a part of edit
action. For example, it's called to prepare for inserting text.
For bug 1465702, editor itself and edit rules classes should not call
public DeleteSelectionAsAction() directly. Therefore, this patch creates
DeleteSelectionAsSubAction() for internal use.
Note that this patch adds NS_ASSERTION() to detect wrong caller. However,
it cannot distinguish if the call is valid, for example, it's allowed to
call DeleteSelectionAsSelection() even if it's handling an edit action but
the method is called via mutation event listener. So, we need to allow
some assertions with some tests. But unfortunately, 1405747.html uses
mutation event listener too many times (about 1,000 times) and the number
of assertion isn't stable. Therefore, this patch makes the test stop using
the mutation event listener 2nd time since I can reproduce the crash with
ESR 52 at the 2nd time.
MozReview-Commit-ID: 1TWaypmnoCC
--HG--
extra : rebase_source : a6a4fb1cbcaf2ab6f10c5f3e7168a6bc0fcb02ed
2018-06-29 11:16:50 +00:00
|
|
|
// delete placeholder txns merge.
|
2018-10-24 09:42:06 +00:00
|
|
|
AutoPlaceholderBatch treatAsOneTransaction(*this, *nsGkAtoms::DeleteTxnName);
|
Bug 1467794 - Split TextEditor::DeleteSelectionAsAction() to itself and TextEditor::DeleteSelectionAsSubAction() r=m_kato
TextEditor::DeleteSelectionAsAction() is called even if it's a part of edit
action. For example, it's called to prepare for inserting text.
For bug 1465702, editor itself and edit rules classes should not call
public DeleteSelectionAsAction() directly. Therefore, this patch creates
DeleteSelectionAsSubAction() for internal use.
Note that this patch adds NS_ASSERTION() to detect wrong caller. However,
it cannot distinguish if the call is valid, for example, it's allowed to
call DeleteSelectionAsSelection() even if it's handling an edit action but
the method is called via mutation event listener. So, we need to allow
some assertions with some tests. But unfortunately, 1405747.html uses
mutation event listener too many times (about 1,000 times) and the number
of assertion isn't stable. Therefore, this patch makes the test stop using
the mutation event listener 2nd time since I can reproduce the crash with
ESR 52 at the 2nd time.
MozReview-Commit-ID: 1TWaypmnoCC
--HG--
extra : rebase_source : a6a4fb1cbcaf2ab6f10c5f3e7168a6bc0fcb02ed
2018-06-29 11:16:50 +00:00
|
|
|
nsresult rv = DeleteSelectionAsSubAction(aDirection, aStripWrappers);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2019-02-25 09:07:54 +00:00
|
|
|
return EditorBase::ToGenericNSResult(rv);
|
Bug 1467794 - Split TextEditor::DeleteSelectionAsAction() to itself and TextEditor::DeleteSelectionAsSubAction() r=m_kato
TextEditor::DeleteSelectionAsAction() is called even if it's a part of edit
action. For example, it's called to prepare for inserting text.
For bug 1465702, editor itself and edit rules classes should not call
public DeleteSelectionAsAction() directly. Therefore, this patch creates
DeleteSelectionAsSubAction() for internal use.
Note that this patch adds NS_ASSERTION() to detect wrong caller. However,
it cannot distinguish if the call is valid, for example, it's allowed to
call DeleteSelectionAsSelection() even if it's handling an edit action but
the method is called via mutation event listener. So, we need to allow
some assertions with some tests. But unfortunately, 1405747.html uses
mutation event listener too many times (about 1,000 times) and the number
of assertion isn't stable. Therefore, this patch makes the test stop using
the mutation event listener 2nd time since I can reproduce the crash with
ESR 52 at the 2nd time.
MozReview-Commit-ID: 1TWaypmnoCC
--HG--
extra : rebase_source : a6a4fb1cbcaf2ab6f10c5f3e7168a6bc0fcb02ed
2018-06-29 11:16:50 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult TextEditor::DeleteSelectionAsSubAction(EDirection aDirection,
|
|
|
|
EStripWrappers aStripWrappers) {
|
2018-10-30 10:02:58 +00:00
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
Bug 1467794 - Split TextEditor::DeleteSelectionAsAction() to itself and TextEditor::DeleteSelectionAsSubAction() r=m_kato
TextEditor::DeleteSelectionAsAction() is called even if it's a part of edit
action. For example, it's called to prepare for inserting text.
For bug 1465702, editor itself and edit rules classes should not call
public DeleteSelectionAsAction() directly. Therefore, this patch creates
DeleteSelectionAsSubAction() for internal use.
Note that this patch adds NS_ASSERTION() to detect wrong caller. However,
it cannot distinguish if the call is valid, for example, it's allowed to
call DeleteSelectionAsSelection() even if it's handling an edit action but
the method is called via mutation event listener. So, we need to allow
some assertions with some tests. But unfortunately, 1405747.html uses
mutation event listener too many times (about 1,000 times) and the number
of assertion isn't stable. Therefore, this patch makes the test stop using
the mutation event listener 2nd time since I can reproduce the crash with
ESR 52 at the 2nd time.
MozReview-Commit-ID: 1TWaypmnoCC
--HG--
extra : rebase_source : a6a4fb1cbcaf2ab6f10c5f3e7168a6bc0fcb02ed
2018-06-29 11:16:50 +00:00
|
|
|
MOZ_ASSERT(mPlaceholderBatch);
|
2012-05-10 14:54:33 +00:00
|
|
|
|
2018-10-30 10:02:58 +00:00
|
|
|
MOZ_ASSERT(aStripWrappers == eStrip || aStripWrappers == eNoStrip);
|
|
|
|
|
2016-10-24 02:27:45 +00:00
|
|
|
if (!mRules) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2010-05-28 01:18:48 +00:00
|
|
|
// Protect the edit rules object from dying
|
2017-12-21 05:52:32 +00:00
|
|
|
RefPtr<TextEditRules> rules(mRules);
|
2010-05-28 01:18:48 +00:00
|
|
|
|
Bug 1467794 - Split TextEditor::DeleteSelectionAsAction() to itself and TextEditor::DeleteSelectionAsSubAction() r=m_kato
TextEditor::DeleteSelectionAsAction() is called even if it's a part of edit
action. For example, it's called to prepare for inserting text.
For bug 1465702, editor itself and edit rules classes should not call
public DeleteSelectionAsAction() directly. Therefore, this patch creates
DeleteSelectionAsSubAction() for internal use.
Note that this patch adds NS_ASSERTION() to detect wrong caller. However,
it cannot distinguish if the call is valid, for example, it's allowed to
call DeleteSelectionAsSelection() even if it's handling an edit action but
the method is called via mutation event listener. So, we need to allow
some assertions with some tests. But unfortunately, 1405747.html uses
mutation event listener too many times (about 1,000 times) and the number
of assertion isn't stable. Therefore, this patch makes the test stop using
the mutation event listener 2nd time since I can reproduce the crash with
ESR 52 at the 2nd time.
MozReview-Commit-ID: 1TWaypmnoCC
--HG--
extra : rebase_source : a6a4fb1cbcaf2ab6f10c5f3e7168a6bc0fcb02ed
2018-06-29 11:16:50 +00:00
|
|
|
AutoTopLevelEditSubActionNotifier maybeTopLevelEditSubAction(
|
|
|
|
*this, EditSubAction::eDeleteSelectedContent, aDirection);
|
2018-05-28 12:49:56 +00:00
|
|
|
EditSubActionInfo subActionInfo(EditSubAction::eDeleteSelectedContent);
|
Bug 1463985 - part 1: Rename EditAction to EditSubAction and related stuff r=m_kato
When we implement InputEvent.inputType, we need to set a stack class to record
which edit action is currently handled. However, currently, we call smaller
jobs as edit action. For example, when user types a character at selecting
some characters, then, EditAction::deleteSelection is performed first, then,
EditAction::insertText is performed. However, for the InputEvent.inputType,
we need inserText information. So, for making new enum EditAction, we need
to rename current EditAction to EditSubAction.
And also this renames related stuff:
EditorBase::mIsInEditAction -> EditorBase::mIsInEditSubAction
EditorBase::IsInEditAction() -> EditorBase::IsInEditSubAction()
EditorBase::mAction -> EditorBase::mTopLevelEditSubAction
TextEditRules::mTheAction -> TextEditRules::mTopLevelEditSubAction
EditorBase::StartOperation() ->
EditorBase::OnStartToHandleTopLevelEditSubAction()
EditorBase::EndOperation() ->
EditorBase::OnEndHandlingTopLevelEditSubAction()
AutoRules -> AutoTopLevelEditSubActionNotifier
RulesInfo -> EditSubActionInfo
MozReview-Commit-ID: cvSkPUjFm1
--HG--
extra : rebase_source : baf527a3e353b7a8ebe9a46be2243b059c500234
2018-05-28 11:12:34 +00:00
|
|
|
subActionInfo.collapsedAction = aDirection;
|
|
|
|
subActionInfo.stripWrappers = aStripWrappers;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool cancel, handled;
|
2018-10-30 10:02:58 +00:00
|
|
|
nsresult rv = rules->WillDoAction(subActionInfo, &cancel, &handled);
|
Bug 1463985 - part 1: Rename EditAction to EditSubAction and related stuff r=m_kato
When we implement InputEvent.inputType, we need to set a stack class to record
which edit action is currently handled. However, currently, we call smaller
jobs as edit action. For example, when user types a character at selecting
some characters, then, EditAction::deleteSelection is performed first, then,
EditAction::insertText is performed. However, for the InputEvent.inputType,
we need inserText information. So, for making new enum EditAction, we need
to rename current EditAction to EditSubAction.
And also this renames related stuff:
EditorBase::mIsInEditAction -> EditorBase::mIsInEditSubAction
EditorBase::IsInEditAction() -> EditorBase::IsInEditSubAction()
EditorBase::mAction -> EditorBase::mTopLevelEditSubAction
TextEditRules::mTheAction -> TextEditRules::mTopLevelEditSubAction
EditorBase::StartOperation() ->
EditorBase::OnStartToHandleTopLevelEditSubAction()
EditorBase::EndOperation() ->
EditorBase::OnEndHandlingTopLevelEditSubAction()
AutoRules -> AutoTopLevelEditSubActionNotifier
RulesInfo -> EditSubActionInfo
MozReview-Commit-ID: cvSkPUjFm1
--HG--
extra : rebase_source : baf527a3e353b7a8ebe9a46be2243b059c500234
2018-05-28 11:12:34 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2016-10-24 02:27:45 +00:00
|
|
|
if (!cancel && !handled) {
|
2018-04-11 10:11:15 +00:00
|
|
|
rv = DeleteSelectionWithTransaction(aDirection, aStripWrappers);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
2016-10-24 02:27:45 +00:00
|
|
|
if (!cancel) {
|
2015-05-28 15:58:42 +00:00
|
|
|
// post-process
|
2018-10-30 10:02:58 +00:00
|
|
|
rv = rules->DidDoAction(subActionInfo, rv);
|
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"TextEditRules::DidDoAction() failed");
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
2016-10-24 15:43:49 +00:00
|
|
|
return rv;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2018-04-11 10:11:15 +00:00
|
|
|
nsresult TextEditor::DeleteSelectionWithTransaction(
|
|
|
|
EDirection aDirection, EStripWrappers aStripWrappers) {
|
2018-10-30 10:02:58 +00:00
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
2018-04-11 08:37:49 +00:00
|
|
|
|
2018-10-30 10:02:58 +00:00
|
|
|
MOZ_ASSERT(aStripWrappers == eStrip || aStripWrappers == eNoStrip);
|
2018-04-11 08:37:49 +00:00
|
|
|
|
|
|
|
RefPtr<EditAggregateTransaction> deleteSelectionTransaction;
|
|
|
|
nsCOMPtr<nsINode> deleteNode;
|
|
|
|
int32_t deleteCharOffset = 0, deleteCharLength = 0;
|
2018-10-30 10:02:58 +00:00
|
|
|
if (!SelectionRefPtr()->IsCollapsed() || aDirection != eNone) {
|
2018-04-11 08:37:49 +00:00
|
|
|
deleteSelectionTransaction =
|
|
|
|
CreateTxnForDeleteSelection(aDirection, getter_AddRefs(deleteNode),
|
|
|
|
&deleteCharOffset, &deleteCharLength);
|
|
|
|
if (NS_WARN_IF(!deleteSelectionTransaction)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<CharacterData> deleteCharData =
|
|
|
|
CharacterData::FromNodeOrNull(deleteNode);
|
Bug 1463985 - part 1: Rename EditAction to EditSubAction and related stuff r=m_kato
When we implement InputEvent.inputType, we need to set a stack class to record
which edit action is currently handled. However, currently, we call smaller
jobs as edit action. For example, when user types a character at selecting
some characters, then, EditAction::deleteSelection is performed first, then,
EditAction::insertText is performed. However, for the InputEvent.inputType,
we need inserText information. So, for making new enum EditAction, we need
to rename current EditAction to EditSubAction.
And also this renames related stuff:
EditorBase::mIsInEditAction -> EditorBase::mIsInEditSubAction
EditorBase::IsInEditAction() -> EditorBase::IsInEditSubAction()
EditorBase::mAction -> EditorBase::mTopLevelEditSubAction
TextEditRules::mTheAction -> TextEditRules::mTopLevelEditSubAction
EditorBase::StartOperation() ->
EditorBase::OnStartToHandleTopLevelEditSubAction()
EditorBase::EndOperation() ->
EditorBase::OnEndHandlingTopLevelEditSubAction()
AutoRules -> AutoTopLevelEditSubActionNotifier
RulesInfo -> EditSubActionInfo
MozReview-Commit-ID: cvSkPUjFm1
--HG--
extra : rebase_source : baf527a3e353b7a8ebe9a46be2243b059c500234
2018-05-28 11:12:34 +00:00
|
|
|
AutoTopLevelEditSubActionNotifier maybeTopLevelEditSubAction(
|
2018-05-28 12:49:56 +00:00
|
|
|
*this, EditSubAction::eDeleteSelectedContent, aDirection);
|
2018-04-11 08:37:49 +00:00
|
|
|
|
|
|
|
if (mRules && mRules->AsHTMLEditRules()) {
|
|
|
|
if (!deleteNode) {
|
|
|
|
RefPtr<HTMLEditRules> htmlEditRules = mRules->AsHTMLEditRules();
|
2018-10-30 10:02:58 +00:00
|
|
|
htmlEditRules->WillDeleteSelection();
|
2018-04-11 08:37:49 +00:00
|
|
|
} else if (!deleteCharData) {
|
|
|
|
RefPtr<HTMLEditRules> htmlEditRules = mRules->AsHTMLEditRules();
|
2018-10-30 10:02:58 +00:00
|
|
|
htmlEditRules->WillDeleteNode(*deleteNode);
|
2018-04-11 08:37:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify nsIEditActionListener::WillDelete[Selection|Text]
|
|
|
|
if (!mActionListeners.IsEmpty()) {
|
|
|
|
if (!deleteNode) {
|
|
|
|
AutoActionListenerArray listeners(mActionListeners);
|
|
|
|
for (auto& listener : listeners) {
|
2018-10-30 10:02:58 +00:00
|
|
|
listener->WillDeleteSelection(SelectionRefPtr());
|
2018-04-11 08:37:49 +00:00
|
|
|
}
|
|
|
|
} else if (deleteCharData) {
|
|
|
|
AutoActionListenerArray listeners(mActionListeners);
|
|
|
|
for (auto& listener : listeners) {
|
|
|
|
listener->WillDeleteText(deleteCharData, deleteCharOffset, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the specified amount
|
2018-10-30 10:01:38 +00:00
|
|
|
nsresult rv = DoTransactionInternal(deleteSelectionTransaction);
|
2018-04-11 08:37:49 +00:00
|
|
|
|
|
|
|
if (mRules && mRules->AsHTMLEditRules() && deleteCharData) {
|
2018-04-26 14:27:50 +00:00
|
|
|
MOZ_ASSERT(deleteNode);
|
2018-04-11 08:37:49 +00:00
|
|
|
RefPtr<HTMLEditRules> htmlEditRules = mRules->AsHTMLEditRules();
|
2018-10-30 10:02:58 +00:00
|
|
|
htmlEditRules->DidDeleteText(*deleteNode, deleteCharOffset, 1);
|
2018-04-11 08:37:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mTextServicesDocument && NS_SUCCEEDED(rv) && deleteNode &&
|
|
|
|
!deleteCharData) {
|
|
|
|
RefPtr<TextServicesDocument> textServicesDocument = mTextServicesDocument;
|
|
|
|
textServicesDocument->DidDeleteNode(deleteNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify nsIEditActionListener::DidDelete[Selection|Text|Node]
|
|
|
|
{
|
|
|
|
AutoActionListenerArray listeners(mActionListeners);
|
|
|
|
if (!deleteNode) {
|
|
|
|
for (auto& listener : mActionListeners) {
|
2018-10-30 10:02:58 +00:00
|
|
|
listener->DidDeleteSelection(SelectionRefPtr());
|
2018-04-11 08:37:49 +00:00
|
|
|
}
|
|
|
|
} else if (deleteCharData) {
|
|
|
|
for (auto& listener : mActionListeners) {
|
|
|
|
listener->DidDeleteText(deleteCharData, deleteCharOffset, 1, rv);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (auto& listener : mActionListeners) {
|
2018-05-24 17:18:34 +00:00
|
|
|
listener->DidDeleteNode(deleteNode, rv);
|
2018-04-11 08:37:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Element> TextEditor::DeleteSelectionAndCreateElement(
|
|
|
|
nsAtom& aTag) {
|
2018-10-30 10:04:08 +00:00
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
|
|
|
|
2018-04-11 08:37:49 +00:00
|
|
|
nsresult rv = DeleteSelectionAndPrepareToCreateNode();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-05-08 09:40:17 +00:00
|
|
|
EditorDOMPoint pointToInsert(SelectionRefPtr()->AnchorRef());
|
2018-04-11 08:37:49 +00:00
|
|
|
if (!pointToInsert.IsSet()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
RefPtr<Element> newElement = CreateNodeWithTransaction(aTag, pointToInsert);
|
|
|
|
|
|
|
|
// We want the selection to be just after the new node
|
|
|
|
EditorRawDOMPoint afterNewElement(newElement);
|
|
|
|
MOZ_ASSERT(afterNewElement.IsSetAndValid());
|
|
|
|
DebugOnly<bool> advanced = afterNewElement.AdvanceOffset();
|
|
|
|
NS_WARNING_ASSERTION(advanced,
|
|
|
|
"Failed to move offset next to the new element");
|
|
|
|
ErrorResult error;
|
2018-10-30 10:04:08 +00:00
|
|
|
SelectionRefPtr()->Collapse(afterNewElement, error);
|
2018-04-11 08:37:49 +00:00
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
|
|
// XXX Even if it succeeded to create new element, this returns error
|
|
|
|
// when Selection.Collapse() fails something. This could occur with
|
|
|
|
// mutation observer or mutation event listener.
|
|
|
|
error.SuppressException();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return newElement.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult TextEditor::DeleteSelectionAndPrepareToCreateNode() {
|
2018-10-30 10:04:08 +00:00
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
2018-04-11 08:37:49 +00:00
|
|
|
|
2018-10-30 10:04:08 +00:00
|
|
|
if (NS_WARN_IF(!SelectionRefPtr()->GetAnchorFocusRange())) {
|
2018-04-11 08:37:49 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-10-30 10:04:08 +00:00
|
|
|
if (!SelectionRefPtr()->GetAnchorFocusRange()->Collapsed()) {
|
Bug 1467794 - Split TextEditor::DeleteSelectionAsAction() to itself and TextEditor::DeleteSelectionAsSubAction() r=m_kato
TextEditor::DeleteSelectionAsAction() is called even if it's a part of edit
action. For example, it's called to prepare for inserting text.
For bug 1465702, editor itself and edit rules classes should not call
public DeleteSelectionAsAction() directly. Therefore, this patch creates
DeleteSelectionAsSubAction() for internal use.
Note that this patch adds NS_ASSERTION() to detect wrong caller. However,
it cannot distinguish if the call is valid, for example, it's allowed to
call DeleteSelectionAsSelection() even if it's handling an edit action but
the method is called via mutation event listener. So, we need to allow
some assertions with some tests. But unfortunately, 1405747.html uses
mutation event listener too many times (about 1,000 times) and the number
of assertion isn't stable. Therefore, this patch makes the test stop using
the mutation event listener 2nd time since I can reproduce the crash with
ESR 52 at the 2nd time.
MozReview-Commit-ID: 1TWaypmnoCC
--HG--
extra : rebase_source : a6a4fb1cbcaf2ab6f10c5f3e7168a6bc0fcb02ed
2018-06-29 11:16:50 +00:00
|
|
|
nsresult rv = DeleteSelectionAsSubAction(eNone, eStrip);
|
2018-04-11 08:37:49 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2018-10-30 10:04:08 +00:00
|
|
|
MOZ_ASSERT(SelectionRefPtr()->GetAnchorFocusRange() &&
|
|
|
|
SelectionRefPtr()->GetAnchorFocusRange()->Collapsed(),
|
2018-04-11 08:37:49 +00:00
|
|
|
"Selection not collapsed after delete");
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the selection is a chardata node, split it if necessary and compute
|
|
|
|
// where to put the new node
|
2018-10-30 10:04:08 +00:00
|
|
|
EditorDOMPoint atAnchor(SelectionRefPtr()->AnchorRef());
|
2018-04-11 08:37:49 +00:00
|
|
|
if (NS_WARN_IF(!atAnchor.IsSet()) || !atAnchor.IsInDataNode()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!atAnchor.GetContainer()->GetParentNode())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (atAnchor.IsStartOfContainer()) {
|
|
|
|
EditorRawDOMPoint atAnchorContainer(atAnchor.GetContainer());
|
|
|
|
if (NS_WARN_IF(!atAnchorContainer.IsSetAndValid())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
ErrorResult error;
|
2018-10-30 10:04:08 +00:00
|
|
|
SelectionRefPtr()->Collapse(atAnchorContainer, error);
|
2018-04-11 08:37:49 +00:00
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
|
|
return error.StealNSResult();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (atAnchor.IsEndOfContainer()) {
|
|
|
|
EditorRawDOMPoint afterAnchorContainer(atAnchor.GetContainer());
|
|
|
|
if (NS_WARN_IF(!afterAnchorContainer.AdvanceOffset())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
ErrorResult error;
|
2018-10-30 10:04:08 +00:00
|
|
|
SelectionRefPtr()->Collapse(afterAnchorContainer, error);
|
2018-04-11 08:37:49 +00:00
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
|
|
return error.StealNSResult();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorResult error;
|
|
|
|
nsCOMPtr<nsIContent> newLeftNode = SplitNodeWithTransaction(atAnchor, error);
|
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
|
|
return error.StealNSResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorRawDOMPoint atRightNode(atAnchor.GetContainer());
|
|
|
|
if (NS_WARN_IF(!atRightNode.IsSet())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(atRightNode.IsSetAndValid());
|
2018-10-30 10:04:08 +00:00
|
|
|
SelectionRefPtr()->Collapse(atRightNode, error);
|
2018-04-11 08:37:49 +00:00
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
|
|
return error.StealNSResult();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-07-09 02:54:50 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
TextEditor::InsertText(const nsAString& aStringToInsert) {
|
2018-04-16 14:43:36 +00:00
|
|
|
nsresult rv = InsertTextAsAction(aStringToInsert);
|
2019-06-10 10:27:07 +00:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to insert text");
|
|
|
|
return rv;
|
2018-04-16 14:43:36 +00:00
|
|
|
}
|
|
|
|
|
2019-06-10 10:27:07 +00:00
|
|
|
nsresult TextEditor::InsertTextAsAction(const nsAString& aStringToInsert,
|
|
|
|
nsIPrincipal* aPrincipal) {
|
2018-07-02 11:12:22 +00:00
|
|
|
// Showing this assertion is fine if this method is called by outside via
|
|
|
|
// mutation event listener or something. Otherwise, this is called by
|
|
|
|
// wrong method.
|
|
|
|
NS_ASSERTION(!mPlaceholderBatch,
|
|
|
|
"Should be called only when this is the only edit action of the "
|
|
|
|
"operation "
|
|
|
|
"unless mutation event listener nests some operations");
|
|
|
|
|
2019-06-10 10:27:07 +00:00
|
|
|
AutoEditActionDataSetter editActionData(*this, EditAction::eInsertText,
|
|
|
|
aPrincipal);
|
2018-10-30 09:59:33 +00:00
|
|
|
if (NS_WARN_IF(!editActionData.CanHandle())) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2019-02-19 06:28:19 +00:00
|
|
|
// Note that we don't need to replace native line breaks with XP line breaks
|
|
|
|
// here because Chrome does not do it.
|
|
|
|
MOZ_ASSERT(!aStringToInsert.IsVoid());
|
|
|
|
editActionData.SetData(aStringToInsert);
|
2018-10-30 09:59:33 +00:00
|
|
|
|
2018-10-24 09:42:06 +00:00
|
|
|
AutoPlaceholderBatch treatAsOneTransaction(*this);
|
2018-07-02 11:12:22 +00:00
|
|
|
nsresult rv = InsertTextAsSubAction(aStringToInsert);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2019-02-25 09:07:54 +00:00
|
|
|
return EditorBase::ToGenericNSResult(rv);
|
2018-07-02 11:12:22 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult TextEditor::InsertTextAsSubAction(const nsAString& aStringToInsert) {
|
2018-10-30 10:02:58 +00:00
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
2018-07-02 11:12:22 +00:00
|
|
|
MOZ_ASSERT(mPlaceholderBatch);
|
|
|
|
|
2016-10-24 02:27:45 +00:00
|
|
|
if (!mRules) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2018-07-02 11:12:22 +00:00
|
|
|
// Protect the edit rules object from dying.
|
2017-12-21 05:52:32 +00:00
|
|
|
RefPtr<TextEditRules> rules(mRules);
|
2010-05-28 01:18:48 +00:00
|
|
|
|
2018-07-02 11:12:22 +00:00
|
|
|
EditSubAction editSubAction = ShouldHandleIMEComposition()
|
|
|
|
? EditSubAction::eInsertTextComingFromIME
|
|
|
|
: EditSubAction::eInsertText;
|
2018-04-16 14:43:36 +00:00
|
|
|
|
Bug 1463985 - part 1: Rename EditAction to EditSubAction and related stuff r=m_kato
When we implement InputEvent.inputType, we need to set a stack class to record
which edit action is currently handled. However, currently, we call smaller
jobs as edit action. For example, when user types a character at selecting
some characters, then, EditAction::deleteSelection is performed first, then,
EditAction::insertText is performed. However, for the InputEvent.inputType,
we need inserText information. So, for making new enum EditAction, we need
to rename current EditAction to EditSubAction.
And also this renames related stuff:
EditorBase::mIsInEditAction -> EditorBase::mIsInEditSubAction
EditorBase::IsInEditAction() -> EditorBase::IsInEditSubAction()
EditorBase::mAction -> EditorBase::mTopLevelEditSubAction
TextEditRules::mTheAction -> TextEditRules::mTopLevelEditSubAction
EditorBase::StartOperation() ->
EditorBase::OnStartToHandleTopLevelEditSubAction()
EditorBase::EndOperation() ->
EditorBase::OnEndHandlingTopLevelEditSubAction()
AutoRules -> AutoTopLevelEditSubActionNotifier
RulesInfo -> EditSubActionInfo
MozReview-Commit-ID: cvSkPUjFm1
--HG--
extra : rebase_source : baf527a3e353b7a8ebe9a46be2243b059c500234
2018-05-28 11:12:34 +00:00
|
|
|
AutoTopLevelEditSubActionNotifier maybeTopLevelEditSubAction(
|
|
|
|
*this, editSubAction, nsIEditor::eNext);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
nsAutoString resultString;
|
Bug 1463985 - part 1: Rename EditAction to EditSubAction and related stuff r=m_kato
When we implement InputEvent.inputType, we need to set a stack class to record
which edit action is currently handled. However, currently, we call smaller
jobs as edit action. For example, when user types a character at selecting
some characters, then, EditAction::deleteSelection is performed first, then,
EditAction::insertText is performed. However, for the InputEvent.inputType,
we need inserText information. So, for making new enum EditAction, we need
to rename current EditAction to EditSubAction.
And also this renames related stuff:
EditorBase::mIsInEditAction -> EditorBase::mIsInEditSubAction
EditorBase::IsInEditAction() -> EditorBase::IsInEditSubAction()
EditorBase::mAction -> EditorBase::mTopLevelEditSubAction
TextEditRules::mTheAction -> TextEditRules::mTopLevelEditSubAction
EditorBase::StartOperation() ->
EditorBase::OnStartToHandleTopLevelEditSubAction()
EditorBase::EndOperation() ->
EditorBase::OnEndHandlingTopLevelEditSubAction()
AutoRules -> AutoTopLevelEditSubActionNotifier
RulesInfo -> EditSubActionInfo
MozReview-Commit-ID: cvSkPUjFm1
--HG--
extra : rebase_source : baf527a3e353b7a8ebe9a46be2243b059c500234
2018-05-28 11:12:34 +00:00
|
|
|
// XXX can we trust instring to outlive subActionInfo,
|
|
|
|
// XXX and subActionInfo not to refer to instring in its dtor?
|
2001-04-07 00:45:26 +00:00
|
|
|
// nsAutoString instring(aStringToInsert);
|
Bug 1463985 - part 1: Rename EditAction to EditSubAction and related stuff r=m_kato
When we implement InputEvent.inputType, we need to set a stack class to record
which edit action is currently handled. However, currently, we call smaller
jobs as edit action. For example, when user types a character at selecting
some characters, then, EditAction::deleteSelection is performed first, then,
EditAction::insertText is performed. However, for the InputEvent.inputType,
we need inserText information. So, for making new enum EditAction, we need
to rename current EditAction to EditSubAction.
And also this renames related stuff:
EditorBase::mIsInEditAction -> EditorBase::mIsInEditSubAction
EditorBase::IsInEditAction() -> EditorBase::IsInEditSubAction()
EditorBase::mAction -> EditorBase::mTopLevelEditSubAction
TextEditRules::mTheAction -> TextEditRules::mTopLevelEditSubAction
EditorBase::StartOperation() ->
EditorBase::OnStartToHandleTopLevelEditSubAction()
EditorBase::EndOperation() ->
EditorBase::OnEndHandlingTopLevelEditSubAction()
AutoRules -> AutoTopLevelEditSubActionNotifier
RulesInfo -> EditSubActionInfo
MozReview-Commit-ID: cvSkPUjFm1
--HG--
extra : rebase_source : baf527a3e353b7a8ebe9a46be2243b059c500234
2018-05-28 11:12:34 +00:00
|
|
|
EditSubActionInfo subActionInfo(editSubAction);
|
|
|
|
subActionInfo.inString = &aStringToInsert;
|
|
|
|
subActionInfo.outString = &resultString;
|
|
|
|
subActionInfo.maxLength = mMaxTextLength;
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool cancel, handled;
|
2018-10-30 10:02:58 +00:00
|
|
|
nsresult rv = rules->WillDoAction(subActionInfo, &cancel, &handled);
|
2018-04-16 14:43:36 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2016-10-24 02:27:45 +00:00
|
|
|
if (!cancel && !handled) {
|
2001-01-28 20:13:07 +00:00
|
|
|
// we rely on rules code for now - no default implementation
|
|
|
|
}
|
2016-10-19 09:09:33 +00:00
|
|
|
if (cancel) {
|
|
|
|
return NS_OK;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
2016-10-19 09:09:33 +00:00
|
|
|
// post-process
|
2018-10-30 10:02:58 +00:00
|
|
|
rv = rules->DidDoAction(subActionInfo, NS_OK);
|
2018-07-02 11:12:22 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2016-07-09 02:54:50 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
TextEditor::InsertLineBreak() {
|
2018-11-03 04:19:22 +00:00
|
|
|
if (NS_WARN_IF(IsSingleLineEditor())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoEditActionDataSetter editActionData(*this, EditAction::eInsertLineBreak);
|
2018-10-30 09:59:33 +00:00
|
|
|
if (NS_WARN_IF(!editActionData.CanHandle())) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2018-11-03 11:19:07 +00:00
|
|
|
AutoPlaceholderBatch treatAsOneTransaction(*this);
|
2018-11-03 04:19:22 +00:00
|
|
|
nsresult rv = InsertLineBreakAsSubAction();
|
2018-11-03 11:22:13 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2019-02-25 09:07:54 +00:00
|
|
|
return EditorBase::ToGenericNSResult(rv);
|
2018-11-03 11:22:13 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult TextEditor::InsertLineBreakAsSubAction() {
|
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
|
|
|
|
|
|
|
if (!mRules) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Protect the edit rules object from dying
|
|
|
|
RefPtr<TextEditRules> rules(mRules);
|
|
|
|
|
|
|
|
AutoTopLevelEditSubActionNotifier maybeTopLevelEditSubAction(
|
|
|
|
*this, EditSubAction::eInsertLineBreak, nsIEditor::eNext);
|
|
|
|
|
|
|
|
EditSubActionInfo subActionInfo(EditSubAction::eInsertLineBreak);
|
|
|
|
subActionInfo.maxLength = mMaxTextLength;
|
|
|
|
bool cancel, handled;
|
|
|
|
nsresult rv = rules->WillDoAction(subActionInfo, &cancel, &handled);
|
|
|
|
if (cancel) {
|
|
|
|
return rv; // We don't need to call DidDoAction() if canceled.
|
|
|
|
}
|
|
|
|
// XXX DidDoAction() does nothing for eInsertParagraphSeparator. However,
|
|
|
|
// we should call it until we keep using this style. Perhaps, each
|
|
|
|
// editor method should call necessary method of
|
2018-11-03 11:21:15 +00:00
|
|
|
// TextEditRules/HTMLEditRules directly.
|
|
|
|
rv = rules->DidDoAction(subActionInfo, rv);
|
2018-04-16 14:43:36 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2018-11-03 11:21:15 +00:00
|
|
|
return NS_OK;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2019-06-10 10:27:07 +00:00
|
|
|
nsresult TextEditor::SetTextAsAction(const nsAString& aString,
|
|
|
|
nsIPrincipal* aPrincipal) {
|
2018-07-24 08:46:12 +00:00
|
|
|
MOZ_ASSERT(aString.FindChar(static_cast<char16_t>('\r')) == kNotFound);
|
|
|
|
|
2019-06-10 10:27:07 +00:00
|
|
|
AutoEditActionDataSetter editActionData(*this, EditAction::eSetText,
|
|
|
|
aPrincipal);
|
2018-10-30 09:59:33 +00:00
|
|
|
if (NS_WARN_IF(!editActionData.CanHandle())) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2018-10-24 09:42:06 +00:00
|
|
|
AutoPlaceholderBatch treatAsOneTransaction(*this);
|
2018-07-03 13:25:52 +00:00
|
|
|
nsresult rv = SetTextAsSubAction(aString);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2019-02-25 09:07:54 +00:00
|
|
|
return EditorBase::ToGenericNSResult(rv);
|
2018-07-03 13:25:52 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-06-10 10:27:07 +00:00
|
|
|
nsresult TextEditor::ReplaceTextAsAction(const nsAString& aString,
|
|
|
|
nsRange* aReplaceRange,
|
|
|
|
nsIPrincipal* aPrincipal) {
|
|
|
|
AutoEditActionDataSetter editActionData(*this, EditAction::eReplaceText,
|
|
|
|
aPrincipal);
|
2018-10-30 09:59:33 +00:00
|
|
|
if (NS_WARN_IF(!editActionData.CanHandle())) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
Bug 998941 - part 1-3: Make TextEditor (only when not HTMLEditor instance) set InputEvent.data to inserting string when InputEvent.inputType is "insertFromPaste", "insertFromDrop" or "insertReplacementText" r=smaug,m_kato
https://rawgit.com/w3c/input-events/v1/index.html#dfn-data
https://w3c.github.io/input-events/#dfn-data
Both Input Events Level 1 and Level 2 declare that InputEvent.data should be
set to inserting string only on TextEditor when InputEvent.inputType is
"insertFromPaste", "insertFromPasteAsQuotation", "insertFromDrop",
"insertTranspose", "insertReplacementText" or "insertFromYank".
Currently, we support only "insertFromPaste", "insertFromDrop",
"insertReplacementText". Therefore, this patch makes TextEditor set
EditorBase::mEditActionData::mData only for them (and the instance is not
HTMLEditor's).
Differential Revision: https://phabricator.services.mozilla.com/D19287
--HG--
extra : moz-landing-system : lando
2019-02-19 06:28:57 +00:00
|
|
|
if (!AsHTMLEditor()) {
|
|
|
|
editActionData.SetData(aString);
|
2019-02-19 06:33:42 +00:00
|
|
|
} else {
|
|
|
|
editActionData.InitializeDataTransfer(aString);
|
Bug 998941 - part 1-3: Make TextEditor (only when not HTMLEditor instance) set InputEvent.data to inserting string when InputEvent.inputType is "insertFromPaste", "insertFromDrop" or "insertReplacementText" r=smaug,m_kato
https://rawgit.com/w3c/input-events/v1/index.html#dfn-data
https://w3c.github.io/input-events/#dfn-data
Both Input Events Level 1 and Level 2 declare that InputEvent.data should be
set to inserting string only on TextEditor when InputEvent.inputType is
"insertFromPaste", "insertFromPasteAsQuotation", "insertFromDrop",
"insertTranspose", "insertReplacementText" or "insertFromYank".
Currently, we support only "insertFromPaste", "insertFromDrop",
"insertReplacementText". Therefore, this patch makes TextEditor set
EditorBase::mEditActionData::mData only for them (and the instance is not
HTMLEditor's).
Differential Revision: https://phabricator.services.mozilla.com/D19287
--HG--
extra : moz-landing-system : lando
2019-02-19 06:28:57 +00:00
|
|
|
}
|
2018-10-30 09:59:33 +00:00
|
|
|
|
2018-10-24 09:42:06 +00:00
|
|
|
AutoPlaceholderBatch treatAsOneTransaction(*this);
|
2018-07-03 13:25:52 +00:00
|
|
|
|
|
|
|
// This should emulates inserting text for better undo/redo behavior.
|
|
|
|
AutoTopLevelEditSubActionNotifier maybeTopLevelEditSubAction(
|
|
|
|
*this, EditSubAction::eInsertText, nsIEditor::eNext);
|
|
|
|
|
2018-07-04 13:51:55 +00:00
|
|
|
if (!aReplaceRange) {
|
|
|
|
nsresult rv = SetTextAsSubAction(aString);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2019-02-25 09:07:54 +00:00
|
|
|
return EditorBase::ToGenericNSResult(rv);
|
2018-07-04 13:51:55 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(aString.IsEmpty() && aReplaceRange->Collapsed())) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that do not notify selectionchange caused by selecting all text
|
|
|
|
// because it's preparation of our delete implementation so web apps
|
|
|
|
// shouldn't receive such selectionchange before the first mutation.
|
2018-10-24 09:42:06 +00:00
|
|
|
AutoUpdateViewBatch preventSelectionChangeEvent(*this);
|
2018-07-04 13:51:55 +00:00
|
|
|
|
|
|
|
// Select the range but as far as possible, we should not create new range
|
|
|
|
// even if it's part of special Selection.
|
2018-10-30 09:59:33 +00:00
|
|
|
nsresult rv = SelectionRefPtr()->RemoveAllRangesTemporarily();
|
2018-07-04 13:51:55 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
ErrorResult error;
|
2019-07-02 13:16:12 +00:00
|
|
|
SelectionRefPtr()->AddRangeAndSelectFramesAndNotifyListeners(*aReplaceRange,
|
|
|
|
error);
|
2018-07-04 13:51:55 +00:00
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
|
|
return error.StealNSResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = ReplaceSelectionAsSubAction(aString);
|
2018-07-03 13:25:52 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2019-02-25 09:07:54 +00:00
|
|
|
return EditorBase::ToGenericNSResult(rv);
|
2018-07-03 13:25:52 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult TextEditor::SetTextAsSubAction(const nsAString& aString) {
|
2018-10-30 10:02:58 +00:00
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
2018-07-03 13:25:52 +00:00
|
|
|
MOZ_ASSERT(mPlaceholderBatch);
|
|
|
|
|
2017-05-25 05:30:50 +00:00
|
|
|
if (NS_WARN_IF(!mRules)) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Protect the edit rules object from dying
|
2017-12-21 05:52:32 +00:00
|
|
|
RefPtr<TextEditRules> rules(mRules);
|
2017-05-25 05:30:50 +00:00
|
|
|
|
Bug 1463985 - part 1: Rename EditAction to EditSubAction and related stuff r=m_kato
When we implement InputEvent.inputType, we need to set a stack class to record
which edit action is currently handled. However, currently, we call smaller
jobs as edit action. For example, when user types a character at selecting
some characters, then, EditAction::deleteSelection is performed first, then,
EditAction::insertText is performed. However, for the InputEvent.inputType,
we need inserText information. So, for making new enum EditAction, we need
to rename current EditAction to EditSubAction.
And also this renames related stuff:
EditorBase::mIsInEditAction -> EditorBase::mIsInEditSubAction
EditorBase::IsInEditAction() -> EditorBase::IsInEditSubAction()
EditorBase::mAction -> EditorBase::mTopLevelEditSubAction
TextEditRules::mTheAction -> TextEditRules::mTopLevelEditSubAction
EditorBase::StartOperation() ->
EditorBase::OnStartToHandleTopLevelEditSubAction()
EditorBase::EndOperation() ->
EditorBase::OnEndHandlingTopLevelEditSubAction()
AutoRules -> AutoTopLevelEditSubActionNotifier
RulesInfo -> EditSubActionInfo
MozReview-Commit-ID: cvSkPUjFm1
--HG--
extra : rebase_source : baf527a3e353b7a8ebe9a46be2243b059c500234
2018-05-28 11:12:34 +00:00
|
|
|
AutoTopLevelEditSubActionNotifier maybeTopLevelEditSubAction(
|
2018-05-28 13:20:43 +00:00
|
|
|
*this, EditSubAction::eSetText, nsIEditor::eNext);
|
2017-05-25 05:30:50 +00:00
|
|
|
|
2018-05-28 13:20:43 +00:00
|
|
|
EditSubActionInfo subActionInfo(EditSubAction::eSetText);
|
Bug 1463985 - part 1: Rename EditAction to EditSubAction and related stuff r=m_kato
When we implement InputEvent.inputType, we need to set a stack class to record
which edit action is currently handled. However, currently, we call smaller
jobs as edit action. For example, when user types a character at selecting
some characters, then, EditAction::deleteSelection is performed first, then,
EditAction::insertText is performed. However, for the InputEvent.inputType,
we need inserText information. So, for making new enum EditAction, we need
to rename current EditAction to EditSubAction.
And also this renames related stuff:
EditorBase::mIsInEditAction -> EditorBase::mIsInEditSubAction
EditorBase::IsInEditAction() -> EditorBase::IsInEditSubAction()
EditorBase::mAction -> EditorBase::mTopLevelEditSubAction
TextEditRules::mTheAction -> TextEditRules::mTopLevelEditSubAction
EditorBase::StartOperation() ->
EditorBase::OnStartToHandleTopLevelEditSubAction()
EditorBase::EndOperation() ->
EditorBase::OnEndHandlingTopLevelEditSubAction()
AutoRules -> AutoTopLevelEditSubActionNotifier
RulesInfo -> EditSubActionInfo
MozReview-Commit-ID: cvSkPUjFm1
--HG--
extra : rebase_source : baf527a3e353b7a8ebe9a46be2243b059c500234
2018-05-28 11:12:34 +00:00
|
|
|
subActionInfo.inString = &aString;
|
|
|
|
subActionInfo.maxLength = mMaxTextLength;
|
2017-05-25 05:30:50 +00:00
|
|
|
|
|
|
|
bool cancel;
|
|
|
|
bool handled;
|
2018-10-30 10:02:58 +00:00
|
|
|
nsresult rv = rules->WillDoAction(subActionInfo, &cancel, &handled);
|
2017-05-25 05:30:50 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
if (cancel) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (!handled) {
|
2018-07-03 13:25:52 +00:00
|
|
|
// Note that do not notify selectionchange caused by selecting all text
|
|
|
|
// because it's preparation of our delete implementation so web apps
|
|
|
|
// shouldn't receive such selectionchange before the first mutation.
|
2018-10-24 09:42:06 +00:00
|
|
|
AutoUpdateViewBatch preventSelectionChangeEvent(*this);
|
2018-07-03 13:25:52 +00:00
|
|
|
|
2019-03-26 10:06:43 +00:00
|
|
|
Element* rootElement = GetRoot();
|
|
|
|
if (NS_WARN_IF(!rootElement)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2017-05-25 05:30:50 +00:00
|
|
|
// We want to select trailing BR node to remove all nodes to replace all,
|
|
|
|
// but TextEditor::SelectEntireDocument doesn't select that BR node.
|
|
|
|
if (rules->DocumentIsEmpty()) {
|
2018-10-30 10:02:58 +00:00
|
|
|
rv = SelectionRefPtr()->Collapse(rootElement, 0);
|
2019-03-26 10:06:43 +00:00
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
NS_SUCCEEDED(rv),
|
|
|
|
"Failed to move caret to start of the editor root element");
|
2017-05-25 05:30:50 +00:00
|
|
|
} else {
|
2019-03-26 10:06:43 +00:00
|
|
|
ErrorResult error;
|
|
|
|
SelectionRefPtr()->SelectAllChildren(*rootElement, error);
|
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
!error.Failed(),
|
|
|
|
"Failed to select all children of the editor root element");
|
|
|
|
rv = error.StealNSResult();
|
2017-05-25 05:30:50 +00:00
|
|
|
}
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2018-07-04 13:51:55 +00:00
|
|
|
rv = ReplaceSelectionAsSubAction(aString);
|
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"Failed to replace selection with new string");
|
2017-05-25 05:30:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// post-process
|
2018-10-30 10:02:58 +00:00
|
|
|
rv = rules->DidDoAction(subActionInfo, rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2017-05-25 05:30:50 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 13:51:55 +00:00
|
|
|
nsresult TextEditor::ReplaceSelectionAsSubAction(const nsAString& aString) {
|
|
|
|
if (aString.IsEmpty()) {
|
|
|
|
nsresult rv = DeleteSelectionAsSubAction(eNone, eStrip);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = InsertTextAsSubAction(aString);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-04-18 15:31:51 +00:00
|
|
|
bool TextEditor::EnsureComposition(WidgetCompositionEvent& aCompositionEvent) {
|
|
|
|
if (mComposition) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// The compositionstart event must cause creating new TextComposition
|
|
|
|
// instance at being dispatched by IMEStateManager.
|
|
|
|
mComposition = IMEStateManager::GetTextCompositionFor(&aCompositionEvent);
|
|
|
|
if (!mComposition) {
|
|
|
|
// However, TextComposition may be committed before the composition
|
|
|
|
// event comes here.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mComposition->StartHandlingComposition(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult TextEditor::OnCompositionStart(
|
|
|
|
WidgetCompositionEvent& aCompositionStartEvent) {
|
|
|
|
if (NS_WARN_IF(mComposition)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2005-03-07 12:25:26 +00:00
|
|
|
|
2018-10-30 09:59:33 +00:00
|
|
|
AutoEditActionDataSetter editActionData(*this, EditAction::eStartComposition);
|
|
|
|
if (NS_WARN_IF(!editActionData.CanHandle())) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2018-04-18 15:31:51 +00:00
|
|
|
EnsureComposition(aCompositionStartEvent);
|
|
|
|
NS_WARNING_ASSERTION(mComposition, "Failed to get TextComposition instance?");
|
|
|
|
return NS_OK;
|
2010-06-30 04:05:12 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 08:07:04 +00:00
|
|
|
nsresult TextEditor::OnCompositionChange(
|
|
|
|
WidgetCompositionEvent& aCompositionChangeEvent) {
|
|
|
|
MOZ_ASSERT(aCompositionChangeEvent.mMessage == eCompositionChange,
|
2017-01-17 08:01:17 +00:00
|
|
|
"The event should be eCompositionChange");
|
2014-02-12 13:02:55 +00:00
|
|
|
|
2018-11-01 08:07:04 +00:00
|
|
|
if (NS_WARN_IF(!mComposition)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoEditActionDataSetter editActionData(*this,
|
|
|
|
EditAction::eUpdateComposition);
|
2018-10-30 09:59:33 +00:00
|
|
|
if (NS_WARN_IF(!editActionData.CanHandle())) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2018-11-01 08:07:04 +00:00
|
|
|
// If:
|
|
|
|
// - new composition string is not empty,
|
|
|
|
// - there is no composition string in the DOM tree,
|
|
|
|
// - and there is non-collapsed Selection,
|
|
|
|
// the selected content will be removed by this composition.
|
|
|
|
if (aCompositionChangeEvent.mData.IsEmpty() &&
|
|
|
|
mComposition->String().IsEmpty() && !SelectionRefPtr()->IsCollapsed()) {
|
|
|
|
editActionData.UpdateEditAction(EditAction::eDeleteByComposition);
|
|
|
|
}
|
|
|
|
|
2019-02-19 06:28:19 +00:00
|
|
|
// If Input Events Level 2 is enabled, EditAction::eDeleteByComposition is
|
|
|
|
// mapped to EditorInputType::eDeleteByComposition and it requires null
|
|
|
|
// for InputEvent.data. Therefore, only otherwise, we should set data.
|
|
|
|
if (ToInputType(editActionData.GetEditAction()) !=
|
|
|
|
EditorInputType::eDeleteByComposition) {
|
|
|
|
MOZ_ASSERT(ToInputType(editActionData.GetEditAction()) ==
|
|
|
|
EditorInputType::eInsertCompositionText);
|
|
|
|
MOZ_ASSERT(!aCompositionChangeEvent.mData.IsVoid());
|
|
|
|
editActionData.SetData(aCompositionChangeEvent.mData);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!EnsureComposition(aCompositionChangeEvent)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-03-15 05:01:10 +00:00
|
|
|
if (NS_WARN_IF(!GetPresShell())) {
|
2018-04-18 15:31:51 +00:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2010-06-30 04:05:12 +00:00
|
|
|
|
2014-09-26 00:05:13 +00:00
|
|
|
// NOTE: TextComposition should receive selection change notification before
|
2014-10-07 10:01:49 +00:00
|
|
|
// CompositionChangeEventHandlingMarker notifies TextComposition of the
|
|
|
|
// end of handling compositionchange event because TextComposition may
|
|
|
|
// need to ignore selection changes caused by composition. Therefore,
|
|
|
|
// CompositionChangeEventHandlingMarker must be destroyed after a call
|
|
|
|
// of NotifiyEditorObservers(eNotifyEditorObserversOfEnd) or
|
2014-09-26 00:05:13 +00:00
|
|
|
// NotifiyEditorObservers(eNotifyEditorObserversOfCancel) which notifies
|
|
|
|
// TextComposition of a selection change.
|
2017-06-14 10:05:48 +00:00
|
|
|
MOZ_ASSERT(
|
|
|
|
!mPlaceholderBatch,
|
2014-09-26 00:05:13 +00:00
|
|
|
"UpdateIMEComposition() must be called without place holder batch");
|
2014-10-07 10:01:49 +00:00
|
|
|
TextComposition::CompositionChangeEventHandlingMarker
|
2018-11-01 08:07:04 +00:00
|
|
|
compositionChangeEventHandlingMarker(mComposition,
|
|
|
|
&aCompositionChangeEvent);
|
2014-09-26 00:05:13 +00:00
|
|
|
|
2019-03-15 05:01:10 +00:00
|
|
|
RefPtr<nsCaret> caret = GetCaret();
|
2010-06-30 04:05:12 +00:00
|
|
|
|
2014-11-02 12:04:13 +00:00
|
|
|
nsresult rv;
|
2014-02-12 13:02:57 +00:00
|
|
|
{
|
2018-10-24 09:42:06 +00:00
|
|
|
AutoPlaceholderBatch treatAsOneTransaction(*this, *nsGkAtoms::IMETxnName);
|
2010-11-24 22:57:32 +00:00
|
|
|
|
Bug 1463985 - part 1: Rename EditAction to EditSubAction and related stuff r=m_kato
When we implement InputEvent.inputType, we need to set a stack class to record
which edit action is currently handled. However, currently, we call smaller
jobs as edit action. For example, when user types a character at selecting
some characters, then, EditAction::deleteSelection is performed first, then,
EditAction::insertText is performed. However, for the InputEvent.inputType,
we need inserText information. So, for making new enum EditAction, we need
to rename current EditAction to EditSubAction.
And also this renames related stuff:
EditorBase::mIsInEditAction -> EditorBase::mIsInEditSubAction
EditorBase::IsInEditAction() -> EditorBase::IsInEditSubAction()
EditorBase::mAction -> EditorBase::mTopLevelEditSubAction
TextEditRules::mTheAction -> TextEditRules::mTopLevelEditSubAction
EditorBase::StartOperation() ->
EditorBase::OnStartToHandleTopLevelEditSubAction()
EditorBase::EndOperation() ->
EditorBase::OnEndHandlingTopLevelEditSubAction()
AutoRules -> AutoTopLevelEditSubActionNotifier
RulesInfo -> EditSubActionInfo
MozReview-Commit-ID: cvSkPUjFm1
--HG--
extra : rebase_source : baf527a3e353b7a8ebe9a46be2243b059c500234
2018-05-28 11:12:34 +00:00
|
|
|
MOZ_ASSERT(
|
|
|
|
mIsInEditSubAction,
|
2017-08-14 05:56:39 +00:00
|
|
|
"AutoPlaceholderBatch should've notified the observes of before-edit");
|
2018-11-01 08:07:04 +00:00
|
|
|
rv = InsertTextAsSubAction(aCompositionChangeEvent.mData);
|
2018-04-16 14:43:36 +00:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"Failed to insert new composition string");
|
2010-05-28 01:18:48 +00:00
|
|
|
|
2019-03-15 05:01:10 +00:00
|
|
|
if (caret) {
|
|
|
|
caret->SetSelection(SelectionRefPtr());
|
2001-06-06 01:21:05 +00:00
|
|
|
}
|
2012-01-13 06:19:22 +00:00
|
|
|
}
|
|
|
|
|
2012-02-15 01:38:07 +00:00
|
|
|
// If still composing, we should fire input event via observer.
|
2015-09-08 03:54:14 +00:00
|
|
|
// Note that if the composition will be committed by the following
|
|
|
|
// compositionend event, we don't need to notify editor observes of this
|
|
|
|
// change.
|
2012-02-15 01:38:07 +00:00
|
|
|
// NOTE: We must notify after the auto batch will be gone.
|
2018-11-01 08:07:04 +00:00
|
|
|
if (!aCompositionChangeEvent.IsFollowedByCompositionEnd()) {
|
2014-07-31 04:37:59 +00:00
|
|
|
NotifyEditorObservers(eNotifyEditorObserversOfEnd);
|
2001-06-06 01:21:05 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 09:07:54 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return EditorBase::ToGenericNSResult(rv);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2001-06-06 01:21:05 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2018-04-18 15:31:51 +00:00
|
|
|
void TextEditor::OnCompositionEnd(
|
|
|
|
WidgetCompositionEvent& aCompositionEndEvent) {
|
|
|
|
if (NS_WARN_IF(!mComposition)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-01 08:07:04 +00:00
|
|
|
EditAction editAction = aCompositionEndEvent.mData.IsEmpty()
|
|
|
|
? EditAction::eCancelComposition
|
|
|
|
: EditAction::eCommitComposition;
|
|
|
|
AutoEditActionDataSetter editActionData(*this, editAction);
|
2018-10-30 09:59:33 +00:00
|
|
|
if (NS_WARN_IF(!editActionData.CanHandle())) {
|
|
|
|
return;
|
|
|
|
}
|
2019-02-19 06:28:19 +00:00
|
|
|
// If Input Events Level 2 is enabled, EditAction::eCancelComposition is
|
|
|
|
// mapped to EditorInputType::eDeleteCompositionText and it requires null
|
|
|
|
// for InputEvent.data. Therefore, only otherwise, we should set data.
|
|
|
|
if (ToInputType(editAction) != EditorInputType::eDeleteCompositionText) {
|
|
|
|
MOZ_ASSERT(
|
|
|
|
ToInputType(editAction) == EditorInputType::eInsertCompositionText ||
|
|
|
|
ToInputType(editAction) == EditorInputType::eInsertFromComposition);
|
|
|
|
MOZ_ASSERT(!aCompositionEndEvent.mData.IsVoid());
|
|
|
|
editActionData.SetData(aCompositionEndEvent.mData);
|
|
|
|
}
|
2018-10-30 09:59:33 +00:00
|
|
|
|
2018-04-18 15:31:51 +00: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!
|
|
|
|
if (mTransactionManager) {
|
|
|
|
nsCOMPtr<nsITransaction> txn = mTransactionManager->PeekUndoStack();
|
|
|
|
nsCOMPtr<nsIAbsorbingTransaction> plcTxn = do_QueryInterface(txn);
|
|
|
|
if (plcTxn) {
|
|
|
|
DebugOnly<nsresult> rv = plcTxn->Commit();
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"nsIAbsorbingTransaction::Commit() failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Composition string may have hidden the caret. Therefore, we need to
|
|
|
|
// cancel it here.
|
|
|
|
HideCaret(false);
|
|
|
|
|
|
|
|
// FYI: mComposition still keeps storing container text node of committed
|
|
|
|
// string, its offset and length. However, they will be invalidated
|
|
|
|
// soon since its Destroy() will be called by IMEStateManager.
|
|
|
|
mComposition->EndHandlingComposition(this);
|
|
|
|
mComposition = nullptr;
|
|
|
|
|
|
|
|
// notify editor observers of action
|
|
|
|
NotifyEditorObservers(eNotifyEditorObserversOfEnd);
|
|
|
|
}
|
|
|
|
|
2018-11-21 03:59:02 +00:00
|
|
|
already_AddRefed<Element> TextEditor::GetInputEventTargetElement() {
|
|
|
|
nsCOMPtr<Element> target = do_QueryInterface(mEventTarget);
|
2012-03-27 01:36:44 +00:00
|
|
|
return target.forget();
|
|
|
|
}
|
|
|
|
|
2018-07-18 08:44:14 +00:00
|
|
|
nsresult TextEditor::IsEmpty(bool* aIsEmpty) const {
|
|
|
|
if (NS_WARN_IF(!mRules)) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aIsEmpty = true;
|
2010-05-28 01:18:48 +00:00
|
|
|
|
2019-08-09 07:03:29 +00:00
|
|
|
if (mPaddingBRElementForEmptyEditor) {
|
2017-08-24 10:53:34 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-08-02 05:44:40 +00:00
|
|
|
// Even if there is no padding <br> element for empty editor, we should be
|
|
|
|
// detected as empty editor if all the children are text nodes and these
|
|
|
|
// have no content.
|
2017-08-24 10:53:34 +00:00
|
|
|
Element* rootElement = GetRoot();
|
|
|
|
if (!rootElement) {
|
2018-07-18 08:44:14 +00:00
|
|
|
// XXX Why don't we return an error in such case??
|
2017-08-24 10:53:34 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (nsIContent* child = rootElement->GetFirstChild(); child;
|
|
|
|
child = child->GetNextSibling()) {
|
|
|
|
if (!EditorBase::IsTextNode(child) ||
|
|
|
|
static_cast<nsTextNode*>(child)->TextDataLength()) {
|
|
|
|
*aIsEmpty = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-11 05:03:26 +00:00
|
|
|
return NS_OK;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 10:53:34 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
TextEditor::GetDocumentIsEmpty(bool* aDocumentIsEmpty) {
|
2018-07-18 08:44:14 +00:00
|
|
|
nsresult rv = IsEmpty(aDocumentIsEmpty);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2017-08-24 10:53:34 +00:00
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
2016-07-09 02:54:50 +00:00
|
|
|
TextEditor::GetTextLength(int32_t* aCount) {
|
2018-07-18 08:51:49 +00:00
|
|
|
MOZ_ASSERT(aCount);
|
2005-10-17 00:54:02 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// initialize out params
|
|
|
|
*aCount = 0;
|
2015-05-28 15:58:42 +00:00
|
|
|
|
2019-08-02 05:44:40 +00:00
|
|
|
// special-case for empty document, to account for the padding <br> element
|
|
|
|
// for empty editor.
|
2018-07-18 08:51:49 +00:00
|
|
|
bool isEmpty = false;
|
|
|
|
nsresult rv = IsEmpty(&isEmpty);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
if (isEmpty) {
|
2001-01-28 20:13:07 +00:00
|
|
|
return NS_OK;
|
2016-10-24 02:27:45 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2018-07-18 08:51:49 +00:00
|
|
|
Element* rootElement = GetRoot();
|
|
|
|
if (NS_WARN_IF(!rootElement)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2005-10-17 00:54:02 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t totalLength = 0;
|
2019-01-11 01:52:26 +00:00
|
|
|
PostContentIterator postOrderIter;
|
|
|
|
postOrderIter.Init(rootElement);
|
|
|
|
for (; !postOrderIter.IsDone(); postOrderIter.Next()) {
|
|
|
|
nsCOMPtr<nsINode> currentNode = postOrderIter.GetCurrentNode();
|
2017-01-23 05:14:30 +00:00
|
|
|
if (IsTextNode(currentNode) && IsEditable(currentNode)) {
|
|
|
|
totalLength += currentNode->Length();
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
2005-10-17 00:54:02 +00:00
|
|
|
|
|
|
|
*aCount = totalLength;
|
|
|
|
return NS_OK;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2015-05-28 15:58:42 +00:00
|
|
|
NS_IMETHODIMP
|
2016-07-09 02:54:50 +00:00
|
|
|
TextEditor::GetWrapWidth(int32_t* aWrapColumn) {
|
2018-07-18 11:31:17 +00:00
|
|
|
if (NS_WARN_IF(!aWrapColumn)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
*aWrapColumn = WrapWidth();
|
2001-01-28 20:13:07 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// See if the style value includes this attribute, and if it does,
|
|
|
|
// cut out everything from the attribute to the next semicolon.
|
|
|
|
//
|
|
|
|
static void CutStyle(const char* stylename, nsString& styleValue) {
|
|
|
|
// Find the current wrapping type:
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t styleStart = styleValue.Find(stylename, true);
|
2016-10-24 02:27:45 +00:00
|
|
|
if (styleStart >= 0) {
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t styleEnd = styleValue.Find(";", false, styleStart);
|
2016-10-24 02:27:45 +00:00
|
|
|
if (styleEnd > styleStart) {
|
2001-01-28 20:13:07 +00:00
|
|
|
styleValue.Cut(styleStart, styleEnd - styleStart + 1);
|
2016-10-24 02:27:45 +00:00
|
|
|
} else {
|
2001-01-28 20:13:07 +00:00
|
|
|
styleValue.Cut(styleStart, styleValue.Length() - styleStart);
|
2016-10-24 02:27:45 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-28 15:58:42 +00:00
|
|
|
NS_IMETHODIMP
|
2016-07-09 02:54:50 +00:00
|
|
|
TextEditor::SetWrapWidth(int32_t aWrapColumn) {
|
2018-10-30 09:59:33 +00:00
|
|
|
AutoEditActionDataSetter editActionData(*this, EditAction::eSetWrapWidth);
|
|
|
|
if (NS_WARN_IF(!editActionData.CanHandle())) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2007-07-25 00:11:22 +00:00
|
|
|
SetWrapColumn(aWrapColumn);
|
2002-04-16 23:11:33 +00:00
|
|
|
|
|
|
|
// Make sure we're a plaintext editor, otherwise we shouldn't
|
|
|
|
// do the rest of this.
|
2016-10-24 02:27:45 +00:00
|
|
|
if (!IsPlaintextEditor()) {
|
2002-04-16 23:11:33 +00:00
|
|
|
return NS_OK;
|
2016-10-24 02:27:45 +00:00
|
|
|
}
|
2002-04-16 23:11:33 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Ought to set a style sheet here ...
|
|
|
|
// Probably should keep around an mPlaintextStyleSheet for this purpose.
|
2011-12-03 21:50:15 +00:00
|
|
|
dom::Element* rootElement = GetRoot();
|
2010-06-17 20:40:48 +00:00
|
|
|
NS_ENSURE_TRUE(rootElement, NS_ERROR_NULL_POINTER);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2005-03-24 19:00:01 +00:00
|
|
|
// Get the current style for this root element:
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoString styleValue;
|
2012-07-27 13:46:48 +00:00
|
|
|
rootElement->GetAttr(kNameSpaceID_None, nsGkAtoms::style, styleValue);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
// We'll replace styles for these values:
|
|
|
|
CutStyle("white-space", styleValue);
|
|
|
|
CutStyle("width", styleValue);
|
|
|
|
CutStyle("font-family", styleValue);
|
|
|
|
|
|
|
|
// If we have other style left, trim off any existing semicolons
|
|
|
|
// or whitespace, then add a known semicolon-space:
|
2016-10-24 02:27:45 +00:00
|
|
|
if (!styleValue.IsEmpty()) {
|
2011-10-17 14:59:28 +00:00
|
|
|
styleValue.Trim("; \t", false, true);
|
2004-06-17 00:13:25 +00:00
|
|
|
styleValue.AppendLiteral("; ");
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we have fixed-width font. This should be done for us,
|
|
|
|
// but it isn't, see bug 22502, so we have to add "font: -moz-fixed;".
|
|
|
|
// Only do this if we're wrapping.
|
2016-10-24 02:27:45 +00:00
|
|
|
if (IsWrapHackEnabled() && aWrapColumn >= 0) {
|
2004-06-17 00:13:25 +00:00
|
|
|
styleValue.AppendLiteral("font-family: -moz-fixed; ");
|
2016-10-24 02:27:45 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
// and now we're ready to set the new whitespace/wrapping style.
|
2016-04-04 21:54:00 +00:00
|
|
|
if (aWrapColumn > 0) {
|
|
|
|
// Wrap to a fixed column.
|
2008-02-20 02:07:48 +00:00
|
|
|
styleValue.AppendLiteral("white-space: pre-wrap; width: ");
|
2001-01-28 20:13:07 +00:00
|
|
|
styleValue.AppendInt(aWrapColumn);
|
2004-06-17 00:13:25 +00:00
|
|
|
styleValue.AppendLiteral("ch;");
|
2016-10-24 02:27:45 +00:00
|
|
|
} else if (!aWrapColumn) {
|
2008-02-20 02:07:48 +00:00
|
|
|
styleValue.AppendLiteral("white-space: pre-wrap;");
|
2016-04-04 21:54:00 +00:00
|
|
|
} else {
|
2004-06-17 00:13:25 +00:00
|
|
|
styleValue.AppendLiteral("white-space: pre;");
|
2016-04-04 21:54:00 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2011-12-03 21:50:15 +00:00
|
|
|
return rootElement->SetAttr(kNameSpaceID_None, nsGkAtoms::style, styleValue,
|
|
|
|
true);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2015-05-28 15:58:42 +00:00
|
|
|
NS_IMETHODIMP
|
2016-07-09 02:54:50 +00:00
|
|
|
TextEditor::GetNewlineHandling(int32_t* aNewlineHandling) {
|
2005-12-20 20:12:54 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aNewlineHandling);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2005-12-20 20:12:54 +00:00
|
|
|
*aNewlineHandling = mNewlineHandling;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-05-28 15:58:42 +00:00
|
|
|
NS_IMETHODIMP
|
2016-07-09 02:54:50 +00:00
|
|
|
TextEditor::SetNewlineHandling(int32_t aNewlineHandling) {
|
2005-12-20 20:12:54 +00:00
|
|
|
mNewlineHandling = aNewlineHandling;
|
2015-05-28 15:58:42 +00:00
|
|
|
|
2005-12-20 20:12:54 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2019-06-10 10:27:07 +00:00
|
|
|
nsresult TextEditor::UndoAsAction(uint32_t aCount, nsIPrincipal* aPrincipal) {
|
2019-08-09 08:57:00 +00:00
|
|
|
if (aCount == 0 || IsReadonly() || IsDisabled()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-03-22 16:21:17 +00:00
|
|
|
// If we don't have transaction in the undo stack, we shouldn't notify
|
|
|
|
// anybody of trying to undo since it's not useful notification but we
|
|
|
|
// need to pay some runtime cost.
|
|
|
|
if (!CanUndo()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is composition, we shouldn't allow to undo with committing
|
|
|
|
// composition since Chrome doesn't allow it and it doesn't make sense
|
|
|
|
// because committing composition causes one transaction and Undo(1)
|
|
|
|
// undoes the committing composition.
|
|
|
|
if (GetComposition()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-06-10 10:27:07 +00:00
|
|
|
AutoEditActionDataSetter editActionData(*this, EditAction::eUndo, aPrincipal);
|
2018-10-30 09:59:33 +00:00
|
|
|
if (NS_WARN_IF(!editActionData.CanHandle())) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2018-10-24 09:42:06 +00:00
|
|
|
AutoUpdateViewBatch preventSelectionChangeEvent(*this);
|
2011-01-20 00:25:03 +00:00
|
|
|
|
2014-07-31 04:37:59 +00:00
|
|
|
NotifyEditorObservers(eNotifyEditorObserversOfBefore);
|
2018-03-22 16:21:17 +00:00
|
|
|
if (NS_WARN_IF(!CanUndo()) || NS_WARN_IF(Destroyed())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-07-31 04:37:59 +00:00
|
|
|
|
2019-08-09 08:57:00 +00:00
|
|
|
nsresult rv = NS_OK;
|
2018-03-22 16:21:17 +00:00
|
|
|
{
|
Bug 1463985 - part 1: Rename EditAction to EditSubAction and related stuff r=m_kato
When we implement InputEvent.inputType, we need to set a stack class to record
which edit action is currently handled. However, currently, we call smaller
jobs as edit action. For example, when user types a character at selecting
some characters, then, EditAction::deleteSelection is performed first, then,
EditAction::insertText is performed. However, for the InputEvent.inputType,
we need inserText information. So, for making new enum EditAction, we need
to rename current EditAction to EditSubAction.
And also this renames related stuff:
EditorBase::mIsInEditAction -> EditorBase::mIsInEditSubAction
EditorBase::IsInEditAction() -> EditorBase::IsInEditSubAction()
EditorBase::mAction -> EditorBase::mTopLevelEditSubAction
TextEditRules::mTheAction -> TextEditRules::mTopLevelEditSubAction
EditorBase::StartOperation() ->
EditorBase::OnStartToHandleTopLevelEditSubAction()
EditorBase::EndOperation() ->
EditorBase::OnEndHandlingTopLevelEditSubAction()
AutoRules -> AutoTopLevelEditSubActionNotifier
RulesInfo -> EditSubActionInfo
MozReview-Commit-ID: cvSkPUjFm1
--HG--
extra : rebase_source : baf527a3e353b7a8ebe9a46be2243b059c500234
2018-05-28 11:12:34 +00:00
|
|
|
AutoTopLevelEditSubActionNotifier maybeTopLevelEditSubAction(
|
2018-05-28 11:36:06 +00:00
|
|
|
*this, EditSubAction::eUndo, nsIEditor::eNone);
|
2018-03-22 16:21:17 +00:00
|
|
|
|
2019-08-09 08:57:00 +00:00
|
|
|
RefPtr<TransactionManager> transactionManager(mTransactionManager);
|
|
|
|
for (uint32_t i = 0; i < aCount; ++i) {
|
|
|
|
if (NS_WARN_IF(NS_FAILED(transactionManager->Undo()))) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
DoAfterUndoTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!mRootElement)) {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
} else {
|
|
|
|
// 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.
|
|
|
|
nsIContent* leftMostChild = GetLeftmostChild(mRootElement);
|
|
|
|
if (leftMostChild &&
|
|
|
|
EditorBase::IsPaddingBRElementForEmptyEditor(*leftMostChild)) {
|
|
|
|
mPaddingBRElementForEmptyEditor =
|
|
|
|
static_cast<HTMLBRElement*>(leftMostChild);
|
|
|
|
} else {
|
|
|
|
mPaddingBRElementForEmptyEditor = nullptr;
|
2018-03-22 16:21:17 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-28 15:58:42 +00:00
|
|
|
}
|
2014-07-31 04:37:59 +00:00
|
|
|
|
|
|
|
NotifyEditorObservers(eNotifyEditorObserversOfEnd);
|
2019-02-25 09:07:54 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return EditorBase::ToGenericNSResult(rv);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2019-06-10 10:27:07 +00:00
|
|
|
nsresult TextEditor::RedoAsAction(uint32_t aCount, nsIPrincipal* aPrincipal) {
|
2019-08-09 08:57:00 +00:00
|
|
|
if (aCount == 0 || IsReadonly() || IsDisabled()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-03-22 16:21:17 +00:00
|
|
|
// If we don't have transaction in the redo stack, we shouldn't notify
|
|
|
|
// anybody of trying to redo since it's not useful notification but we
|
|
|
|
// need to pay some runtime cost.
|
|
|
|
if (!CanRedo()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is composition, we shouldn't allow to redo with committing
|
|
|
|
// composition since Chrome doesn't allow it and it doesn't make sense
|
|
|
|
// because committing composition causes removing all transactions from
|
|
|
|
// the redo queue. So, it becomes impossible to redo anything.
|
|
|
|
if (GetComposition()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-06-10 10:27:07 +00:00
|
|
|
AutoEditActionDataSetter editActionData(*this, EditAction::eRedo, aPrincipal);
|
2018-10-30 09:59:33 +00:00
|
|
|
if (NS_WARN_IF(!editActionData.CanHandle())) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2018-10-24 09:42:06 +00:00
|
|
|
AutoUpdateViewBatch preventSelectionChangeEvent(*this);
|
2011-01-20 00:25:03 +00:00
|
|
|
|
2014-07-31 04:37:59 +00:00
|
|
|
NotifyEditorObservers(eNotifyEditorObserversOfBefore);
|
2018-03-22 16:21:17 +00:00
|
|
|
if (NS_WARN_IF(!CanRedo()) || NS_WARN_IF(Destroyed())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-07-31 04:37:59 +00:00
|
|
|
|
2019-08-09 08:57:00 +00:00
|
|
|
nsresult rv = NS_OK;
|
2018-03-22 16:21:17 +00:00
|
|
|
{
|
Bug 1463985 - part 1: Rename EditAction to EditSubAction and related stuff r=m_kato
When we implement InputEvent.inputType, we need to set a stack class to record
which edit action is currently handled. However, currently, we call smaller
jobs as edit action. For example, when user types a character at selecting
some characters, then, EditAction::deleteSelection is performed first, then,
EditAction::insertText is performed. However, for the InputEvent.inputType,
we need inserText information. So, for making new enum EditAction, we need
to rename current EditAction to EditSubAction.
And also this renames related stuff:
EditorBase::mIsInEditAction -> EditorBase::mIsInEditSubAction
EditorBase::IsInEditAction() -> EditorBase::IsInEditSubAction()
EditorBase::mAction -> EditorBase::mTopLevelEditSubAction
TextEditRules::mTheAction -> TextEditRules::mTopLevelEditSubAction
EditorBase::StartOperation() ->
EditorBase::OnStartToHandleTopLevelEditSubAction()
EditorBase::EndOperation() ->
EditorBase::OnEndHandlingTopLevelEditSubAction()
AutoRules -> AutoTopLevelEditSubActionNotifier
RulesInfo -> EditSubActionInfo
MozReview-Commit-ID: cvSkPUjFm1
--HG--
extra : rebase_source : baf527a3e353b7a8ebe9a46be2243b059c500234
2018-05-28 11:12:34 +00:00
|
|
|
AutoTopLevelEditSubActionNotifier maybeTopLevelEditSubAction(
|
2018-05-28 11:36:06 +00:00
|
|
|
*this, EditSubAction::eRedo, nsIEditor::eNone);
|
2018-03-22 16:21:17 +00:00
|
|
|
|
2019-08-09 08:57:00 +00:00
|
|
|
RefPtr<TransactionManager> transactionManager(mTransactionManager);
|
|
|
|
for (uint32_t i = 0; i < aCount; ++i) {
|
|
|
|
if (NS_WARN_IF(NS_FAILED(transactionManager->Redo()))) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
DoAfterRedoTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!mRootElement)) {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
} else {
|
|
|
|
// We may take empty <br> element for empty editor back with this redo.
|
|
|
|
// We need to store it again.
|
|
|
|
// XXX Looks like that this is too slow if there are a lot of nodes.
|
|
|
|
// Shouldn't we just scan children in the root?
|
|
|
|
nsCOMPtr<nsIHTMLCollection> nodeList =
|
|
|
|
mRootElement->GetElementsByTagName(NS_LITERAL_STRING("br"));
|
|
|
|
MOZ_ASSERT(nodeList);
|
|
|
|
Element* brElement =
|
|
|
|
nodeList->Length() == 1 ? nodeList->Item(0) : nullptr;
|
|
|
|
if (brElement &&
|
|
|
|
EditorBase::IsPaddingBRElementForEmptyEditor(*brElement)) {
|
|
|
|
mPaddingBRElementForEmptyEditor =
|
|
|
|
static_cast<HTMLBRElement*>(brElement);
|
|
|
|
} else {
|
|
|
|
mPaddingBRElementForEmptyEditor = nullptr;
|
2018-03-22 16:21:17 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-28 15:58:42 +00:00
|
|
|
}
|
2014-07-31 04:37:59 +00:00
|
|
|
|
|
|
|
NotifyEditorObservers(eNotifyEditorObserversOfEnd);
|
2019-02-25 09:07:54 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return EditorBase::ToGenericNSResult(rv);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 06:21:42 +00:00
|
|
|
bool TextEditor::IsCopyToClipboardAllowedInternal() const {
|
2018-10-30 10:04:08 +00:00
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
2019-07-29 06:21:14 +00:00
|
|
|
if (SelectionRefPtr()->IsCollapsed()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsSingleLineEditor() || !IsPasswordEditor()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're a password editor, we should allow selected text to be copied
|
|
|
|
// to the clipboard only when selection range is in unmasked range.
|
|
|
|
if (IsAllMasked() || IsMaskingPassword() || mUnmaskedLength == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2019-07-29 06:21:14 +00:00
|
|
|
// If there are 2 or more ranges, we don't allow to copy/cut for now since
|
|
|
|
// we need to check whether all ranges are in unmasked range or not.
|
|
|
|
// Anyway, such operation in password field does not make sense.
|
|
|
|
if (SelectionRefPtr()->RangeCount() > 1) {
|
2014-10-08 23:27:31 +00:00
|
|
|
return false;
|
2016-10-24 02:27:45 +00:00
|
|
|
}
|
2014-10-08 23:27:31 +00:00
|
|
|
|
2019-07-29 06:21:14 +00:00
|
|
|
uint32_t selectionStart = 0, selectionEnd = 0;
|
|
|
|
nsContentUtils::GetSelectionInTextControl(SelectionRefPtr(), mRootElement,
|
|
|
|
selectionStart, selectionEnd);
|
|
|
|
return mUnmaskedStart <= selectionStart && UnmaskedEnd() >= selectionEnd;
|
2007-07-26 04:14:33 +00:00
|
|
|
}
|
|
|
|
|
2016-07-09 02:54:50 +00:00
|
|
|
bool TextEditor::FireClipboardEvent(EventMessage aEventMessage,
|
|
|
|
int32_t aSelectionType,
|
|
|
|
bool* aActionTaken) {
|
2018-10-30 10:04:08 +00:00
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
|
|
|
|
2015-09-08 14:33:35 +00:00
|
|
|
if (aEventMessage == ePaste) {
|
2017-08-08 02:25:36 +00:00
|
|
|
CommitComposition();
|
2015-08-26 12:56:59 +00:00
|
|
|
}
|
2007-09-29 17:56:04 +00:00
|
|
|
|
2019-03-15 05:01:10 +00:00
|
|
|
RefPtr<PresShell> presShell = GetPresShell();
|
2018-10-30 10:04:08 +00:00
|
|
|
if (NS_WARN_IF(!presShell)) {
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2014-11-02 12:04:13 +00:00
|
|
|
}
|
2007-09-29 17:56:04 +00:00
|
|
|
|
2019-04-13 12:43:57 +00:00
|
|
|
if (!nsCopySupport::FireClipboardEvent(
|
|
|
|
aEventMessage, aSelectionType, presShell,
|
|
|
|
MOZ_KnownLive(SelectionRefPtr()), aActionTaken)) {
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2015-08-26 12:56:59 +00:00
|
|
|
}
|
2007-09-29 17:56:04 +00:00
|
|
|
|
2010-03-19 18:32:13 +00:00
|
|
|
// If the event handler caused the editor to be destroyed, return false.
|
|
|
|
// Otherwise return true to indicate that the event was not cancelled.
|
|
|
|
return !mDidPreDestroy;
|
2007-09-29 17:56:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-10 10:27:07 +00:00
|
|
|
nsresult TextEditor::CutAsAction(nsIPrincipal* aPrincipal) {
|
|
|
|
AutoEditActionDataSetter editActionData(*this, EditAction::eCut, aPrincipal);
|
2018-10-30 09:59:33 +00:00
|
|
|
if (NS_WARN_IF(!editActionData.CanHandle())) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2015-05-13 06:51:00 +00:00
|
|
|
bool actionTaken = false;
|
2015-09-08 14:33:35 +00:00
|
|
|
if (FireClipboardEvent(eCut, nsIClipboard::kGlobalClipboard, &actionTaken)) {
|
Bug 1467794 - Split TextEditor::DeleteSelectionAsAction() to itself and TextEditor::DeleteSelectionAsSubAction() r=m_kato
TextEditor::DeleteSelectionAsAction() is called even if it's a part of edit
action. For example, it's called to prepare for inserting text.
For bug 1465702, editor itself and edit rules classes should not call
public DeleteSelectionAsAction() directly. Therefore, this patch creates
DeleteSelectionAsSubAction() for internal use.
Note that this patch adds NS_ASSERTION() to detect wrong caller. However,
it cannot distinguish if the call is valid, for example, it's allowed to
call DeleteSelectionAsSelection() even if it's handling an edit action but
the method is called via mutation event listener. So, we need to allow
some assertions with some tests. But unfortunately, 1405747.html uses
mutation event listener too many times (about 1,000 times) and the number
of assertion isn't stable. Therefore, this patch makes the test stop using
the mutation event listener 2nd time since I can reproduce the crash with
ESR 52 at the 2nd time.
MozReview-Commit-ID: 1TWaypmnoCC
--HG--
extra : rebase_source : a6a4fb1cbcaf2ab6f10c5f3e7168a6bc0fcb02ed
2018-06-29 11:16:50 +00:00
|
|
|
// XXX This transaction name is referred by PlaceholderTransaction::Merge()
|
|
|
|
// so that we need to keep using it here.
|
2018-10-24 09:42:06 +00:00
|
|
|
AutoPlaceholderBatch treatAsOneTransaction(*this,
|
|
|
|
*nsGkAtoms::DeleteTxnName);
|
Bug 1467794 - Split TextEditor::DeleteSelectionAsAction() to itself and TextEditor::DeleteSelectionAsSubAction() r=m_kato
TextEditor::DeleteSelectionAsAction() is called even if it's a part of edit
action. For example, it's called to prepare for inserting text.
For bug 1465702, editor itself and edit rules classes should not call
public DeleteSelectionAsAction() directly. Therefore, this patch creates
DeleteSelectionAsSubAction() for internal use.
Note that this patch adds NS_ASSERTION() to detect wrong caller. However,
it cannot distinguish if the call is valid, for example, it's allowed to
call DeleteSelectionAsSelection() even if it's handling an edit action but
the method is called via mutation event listener. So, we need to allow
some assertions with some tests. But unfortunately, 1405747.html uses
mutation event listener too many times (about 1,000 times) and the number
of assertion isn't stable. Therefore, this patch makes the test stop using
the mutation event listener 2nd time since I can reproduce the crash with
ESR 52 at the 2nd time.
MozReview-Commit-ID: 1TWaypmnoCC
--HG--
extra : rebase_source : a6a4fb1cbcaf2ab6f10c5f3e7168a6bc0fcb02ed
2018-06-29 11:16:50 +00:00
|
|
|
DeleteSelectionAsSubAction(eNone, eStrip);
|
2015-05-13 06:51:00 +00:00
|
|
|
}
|
2019-05-02 08:39:53 +00:00
|
|
|
return EditorBase::ToGenericNSResult(
|
|
|
|
actionTaken ? NS_OK : NS_ERROR_EDITOR_ACTION_CANCELED);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 06:21:42 +00:00
|
|
|
bool TextEditor::IsCutCommandEnabled() const {
|
2018-10-30 09:59:33 +00:00
|
|
|
AutoEditActionDataSetter editActionData(*this, EditAction::eNotEditing);
|
|
|
|
if (NS_WARN_IF(!editActionData.CanHandle())) {
|
2019-04-25 07:14:39 +00:00
|
|
|
return false;
|
2018-10-30 09:59:33 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 20:40:19 +00:00
|
|
|
// Cut is always enabled in HTML documents, but if the document is chrome,
|
|
|
|
// let it control it.
|
2019-04-25 07:14:39 +00:00
|
|
|
Document* document = GetDocument();
|
2019-06-18 20:40:19 +00:00
|
|
|
if (document && document->IsHTMLOrXHTML() &&
|
|
|
|
!nsContentUtils::IsChromeDoc(document)) {
|
2019-04-25 07:14:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-29 06:21:42 +00:00
|
|
|
return IsModifiable() && IsCopyToClipboardAllowedInternal();
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2016-07-09 02:54:50 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
TextEditor::Copy() {
|
2018-10-30 09:59:33 +00:00
|
|
|
AutoEditActionDataSetter editActionData(*this, EditAction::eCopy);
|
|
|
|
if (NS_WARN_IF(!editActionData.CanHandle())) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2015-05-13 06:51:00 +00:00
|
|
|
bool actionTaken = false;
|
2015-09-08 14:33:34 +00:00
|
|
|
FireClipboardEvent(eCopy, nsIClipboard::kGlobalClipboard, &actionTaken);
|
2015-05-13 06:51:00 +00:00
|
|
|
|
2019-05-02 08:39:53 +00:00
|
|
|
return EditorBase::ToGenericNSResult(
|
|
|
|
actionTaken ? NS_OK : NS_ERROR_EDITOR_ACTION_CANCELED);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 06:21:42 +00:00
|
|
|
bool TextEditor::IsCopyCommandEnabled() const {
|
2018-10-30 09:59:33 +00:00
|
|
|
AutoEditActionDataSetter editActionData(*this, EditAction::eNotEditing);
|
|
|
|
if (NS_WARN_IF(!editActionData.CanHandle())) {
|
2019-04-25 07:14:39 +00:00
|
|
|
return false;
|
2018-10-30 09:59:33 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 20:40:19 +00:00
|
|
|
// Copy is always enabled in HTML documents, but if the document is chrome,
|
|
|
|
// let it control it.
|
2019-04-25 07:14:39 +00:00
|
|
|
Document* document = GetDocument();
|
2019-06-18 20:40:19 +00:00
|
|
|
if (document && document->IsHTMLOrXHTML() &&
|
|
|
|
!nsContentUtils::IsChromeDoc(document)) {
|
2019-04-25 07:14:39 +00:00
|
|
|
return true;
|
2018-10-30 09:59:33 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 06:21:42 +00:00
|
|
|
return IsCopyToClipboardAllowedInternal();
|
2019-04-25 07:14:39 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 06:21:14 +00:00
|
|
|
bool TextEditor::CanDeleteSelection() const {
|
2018-10-30 09:59:33 +00:00
|
|
|
AutoEditActionDataSetter editActionData(*this, EditAction::eNotEditing);
|
|
|
|
if (NS_WARN_IF(!editActionData.CanHandle())) {
|
2019-04-25 07:14:39 +00:00
|
|
|
return false;
|
2018-10-30 09:59:33 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 06:21:14 +00:00
|
|
|
return IsModifiable() && !SelectionRefPtr()->IsCollapsed();
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 08:29:46 +00:00
|
|
|
already_AddRefed<nsIDocumentEncoder> TextEditor::GetAndInitDocEncoder(
|
2016-07-09 02:54:50 +00:00
|
|
|
const nsAString& aFormatType, uint32_t aDocumentEncoderFlags,
|
2018-07-18 11:51:55 +00:00
|
|
|
const nsACString& aCharset) const {
|
2018-10-30 10:04:08 +00:00
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
|
|
|
|
2017-04-17 08:29:46 +00:00
|
|
|
nsCOMPtr<nsIDocumentEncoder> docEncoder;
|
|
|
|
if (!mCachedDocumentEncoder ||
|
|
|
|
!mCachedDocumentEncoderType.Equals(aFormatType)) {
|
2018-11-05 00:41:05 +00:00
|
|
|
nsAutoCString formatType;
|
2017-04-17 08:29:46 +00:00
|
|
|
LossyAppendUTF16toASCII(aFormatType, formatType);
|
2018-11-05 00:41:05 +00:00
|
|
|
docEncoder = do_createDocumentEncoder(PromiseFlatCString(formatType).get());
|
2017-04-17 08:29:46 +00:00
|
|
|
if (NS_WARN_IF(!docEncoder)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
mCachedDocumentEncoder = docEncoder;
|
|
|
|
mCachedDocumentEncoderType = aFormatType;
|
|
|
|
} else {
|
|
|
|
docEncoder = mCachedDocumentEncoder;
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2019-01-02 13:05:23 +00:00
|
|
|
RefPtr<Document> doc = GetDocument();
|
2017-04-27 08:41:26 +00:00
|
|
|
NS_ASSERTION(doc, "Need a document");
|
2006-05-01 05:25:52 +00:00
|
|
|
|
2017-04-27 08:41:26 +00:00
|
|
|
nsresult rv = docEncoder->NativeInit(
|
|
|
|
doc, aFormatType,
|
2018-07-18 11:51:55 +00:00
|
|
|
aDocumentEncoderFlags | nsIDocumentEncoder::RequiresReinitAfterOutput);
|
2017-04-17 08:29:46 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2012-02-01 10:54:22 +00:00
|
|
|
if (!aCharset.IsEmpty() && !aCharset.EqualsLiteral("null")) {
|
2001-04-07 00:45:26 +00:00
|
|
|
docEncoder->SetCharset(aCharset);
|
2012-02-01 10:54:22 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2018-07-18 11:31:17 +00:00
|
|
|
int32_t wrapWidth = WrapWidth();
|
|
|
|
if (wrapWidth >= 0) {
|
|
|
|
Unused << docEncoder->SetWrapColumn(wrapWidth);
|
2016-10-24 02:27:45 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
// Set the selection, if appropriate.
|
|
|
|
// We do this either if the OutputSelectionOnly flag is set,
|
|
|
|
// in which case we use our existing selection ...
|
2018-07-18 11:51:55 +00:00
|
|
|
if (aDocumentEncoderFlags & nsIDocumentEncoder::OutputSelectionOnly) {
|
2018-10-30 10:04:08 +00:00
|
|
|
rv = docEncoder->SetSelection(SelectionRefPtr());
|
2017-04-17 08:29:46 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
// ... or if the root element is not a body,
|
|
|
|
// in which case we set the selection to encompass the root.
|
2016-10-24 02:27:45 +00:00
|
|
|
else {
|
2012-02-01 10:54:22 +00:00
|
|
|
dom::Element* rootElement = GetRoot();
|
2017-04-17 08:29:46 +00:00
|
|
|
if (NS_WARN_IF(!rootElement)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-03-03 11:08:59 +00:00
|
|
|
if (!rootElement->IsHTMLElement(nsGkAtoms::body)) {
|
2018-05-30 02:58:49 +00:00
|
|
|
rv = docEncoder->SetContainerNode(rootElement);
|
2017-04-17 08:29:46 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 08:29:46 +00:00
|
|
|
return docEncoder.forget();
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2015-05-28 15:58:42 +00:00
|
|
|
NS_IMETHODIMP
|
2016-07-09 02:54:50 +00:00
|
|
|
TextEditor::OutputToString(const nsAString& aFormatType,
|
2018-07-18 12:27:30 +00:00
|
|
|
uint32_t aDocumentEncoderFlags,
|
2016-07-09 02:54:50 +00:00
|
|
|
nsAString& aOutputString) {
|
2018-10-30 09:59:33 +00:00
|
|
|
AutoEditActionDataSetter editActionData(*this, EditAction::eNotEditing);
|
|
|
|
if (NS_WARN_IF(!editActionData.CanHandle())) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2019-02-25 09:07:54 +00:00
|
|
|
nsresult rv =
|
|
|
|
ComputeValueInternal(aFormatType, aDocumentEncoderFlags, aOutputString);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
// This is low level API for XUL applcation. So, we should return raw
|
|
|
|
// error code here.
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2018-07-18 12:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult TextEditor::ComputeValueInternal(const nsAString& aFormatType,
|
|
|
|
uint32_t aDocumentEncoderFlags,
|
|
|
|
nsAString& aOutputString) const {
|
2018-10-30 10:02:58 +00:00
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
|
|
|
|
2010-05-28 01:18:48 +00:00
|
|
|
// Protect the edit rules object from dying
|
2017-12-21 05:52:32 +00:00
|
|
|
RefPtr<TextEditRules> rules(mRules);
|
2010-05-28 01:18:48 +00:00
|
|
|
|
2018-05-28 13:09:55 +00:00
|
|
|
EditSubActionInfo subActionInfo(EditSubAction::eComputeTextToOutput);
|
Bug 1463985 - part 1: Rename EditAction to EditSubAction and related stuff r=m_kato
When we implement InputEvent.inputType, we need to set a stack class to record
which edit action is currently handled. However, currently, we call smaller
jobs as edit action. For example, when user types a character at selecting
some characters, then, EditAction::deleteSelection is performed first, then,
EditAction::insertText is performed. However, for the InputEvent.inputType,
we need inserText information. So, for making new enum EditAction, we need
to rename current EditAction to EditSubAction.
And also this renames related stuff:
EditorBase::mIsInEditAction -> EditorBase::mIsInEditSubAction
EditorBase::IsInEditAction() -> EditorBase::IsInEditSubAction()
EditorBase::mAction -> EditorBase::mTopLevelEditSubAction
TextEditRules::mTheAction -> TextEditRules::mTopLevelEditSubAction
EditorBase::StartOperation() ->
EditorBase::OnStartToHandleTopLevelEditSubAction()
EditorBase::EndOperation() ->
EditorBase::OnEndHandlingTopLevelEditSubAction()
AutoRules -> AutoTopLevelEditSubActionNotifier
RulesInfo -> EditSubActionInfo
MozReview-Commit-ID: cvSkPUjFm1
--HG--
extra : rebase_source : baf527a3e353b7a8ebe9a46be2243b059c500234
2018-05-28 11:12:34 +00:00
|
|
|
subActionInfo.outString = &aOutputString;
|
2018-07-18 12:27:30 +00:00
|
|
|
subActionInfo.flags = aDocumentEncoderFlags;
|
Bug 1463985 - part 1: Rename EditAction to EditSubAction and related stuff r=m_kato
When we implement InputEvent.inputType, we need to set a stack class to record
which edit action is currently handled. However, currently, we call smaller
jobs as edit action. For example, when user types a character at selecting
some characters, then, EditAction::deleteSelection is performed first, then,
EditAction::insertText is performed. However, for the InputEvent.inputType,
we need inserText information. So, for making new enum EditAction, we need
to rename current EditAction to EditSubAction.
And also this renames related stuff:
EditorBase::mIsInEditAction -> EditorBase::mIsInEditSubAction
EditorBase::IsInEditAction() -> EditorBase::IsInEditSubAction()
EditorBase::mAction -> EditorBase::mTopLevelEditSubAction
TextEditRules::mTheAction -> TextEditRules::mTopLevelEditSubAction
EditorBase::StartOperation() ->
EditorBase::OnStartToHandleTopLevelEditSubAction()
EditorBase::EndOperation() ->
EditorBase::OnEndHandlingTopLevelEditSubAction()
AutoRules -> AutoTopLevelEditSubActionNotifier
RulesInfo -> EditSubActionInfo
MozReview-Commit-ID: cvSkPUjFm1
--HG--
extra : rebase_source : baf527a3e353b7a8ebe9a46be2243b059c500234
2018-05-28 11:12:34 +00:00
|
|
|
subActionInfo.outputFormat = &aFormatType;
|
2018-10-30 10:02:58 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool cancel, handled;
|
2018-10-30 10:02:58 +00:00
|
|
|
nsresult rv = rules->WillDoAction(subActionInfo, &cancel, &handled);
|
2016-10-24 02:27:45 +00:00
|
|
|
if (cancel || NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
if (handled) {
|
2018-05-22 12:07:09 +00:00
|
|
|
// This case will get triggered by password fields or single text node only.
|
2001-01-28 20:13:07 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2018-07-18 12:11:32 +00:00
|
|
|
nsAutoCString charset;
|
|
|
|
rv = GetDocumentCharsetInternal(charset);
|
|
|
|
if (NS_FAILED(rv) || charset.IsEmpty()) {
|
|
|
|
charset.AssignLiteral("windows-1252");
|
2016-10-24 02:27:45 +00:00
|
|
|
}
|
2001-07-14 00:50:21 +00:00
|
|
|
|
2017-04-17 08:29:46 +00:00
|
|
|
nsCOMPtr<nsIDocumentEncoder> encoder =
|
2018-07-18 12:27:30 +00:00
|
|
|
GetAndInitDocEncoder(aFormatType, aDocumentEncoderFlags, charset);
|
2017-04-17 08:29:46 +00:00
|
|
|
if (NS_WARN_IF(!encoder)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
Bug 1463985 - part 1: Rename EditAction to EditSubAction and related stuff r=m_kato
When we implement InputEvent.inputType, we need to set a stack class to record
which edit action is currently handled. However, currently, we call smaller
jobs as edit action. For example, when user types a character at selecting
some characters, then, EditAction::deleteSelection is performed first, then,
EditAction::insertText is performed. However, for the InputEvent.inputType,
we need inserText information. So, for making new enum EditAction, we need
to rename current EditAction to EditSubAction.
And also this renames related stuff:
EditorBase::mIsInEditAction -> EditorBase::mIsInEditSubAction
EditorBase::IsInEditAction() -> EditorBase::IsInEditSubAction()
EditorBase::mAction -> EditorBase::mTopLevelEditSubAction
TextEditRules::mTheAction -> TextEditRules::mTopLevelEditSubAction
EditorBase::StartOperation() ->
EditorBase::OnStartToHandleTopLevelEditSubAction()
EditorBase::EndOperation() ->
EditorBase::OnEndHandlingTopLevelEditSubAction()
AutoRules -> AutoTopLevelEditSubActionNotifier
RulesInfo -> EditSubActionInfo
MozReview-Commit-ID: cvSkPUjFm1
--HG--
extra : rebase_source : baf527a3e353b7a8ebe9a46be2243b059c500234
2018-05-28 11:12:34 +00:00
|
|
|
// XXX Why don't we call TextEditRules::DidDoAction() here?
|
2018-10-30 10:02:58 +00:00
|
|
|
rv = encoder->EncodeToString(aOutputString);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 12:05:39 +00:00
|
|
|
nsresult TextEditor::PasteAsQuotationAsAction(int32_t aClipboardType,
|
2019-06-10 10:27:07 +00:00
|
|
|
bool aDispatchPasteEvent,
|
|
|
|
nsIPrincipal* aPrincipal) {
|
2018-07-23 07:34:03 +00:00
|
|
|
MOZ_ASSERT(aClipboardType == nsIClipboard::kGlobalClipboard ||
|
|
|
|
aClipboardType == nsIClipboard::kSelectionClipboard);
|
|
|
|
|
2019-06-10 10:27:07 +00:00
|
|
|
AutoEditActionDataSetter editActionData(*this, EditAction::ePasteAsQuotation,
|
|
|
|
aPrincipal);
|
2018-10-30 09:59:33 +00:00
|
|
|
if (NS_WARN_IF(!editActionData.CanHandle())) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2001-04-05 23:48:01 +00:00
|
|
|
// Get Clipboard Service
|
|
|
|
nsresult rv;
|
2018-07-23 07:34:03 +00:00
|
|
|
nsCOMPtr<nsIClipboard> clipboard =
|
|
|
|
do_GetService("@mozilla.org/widget/clipboard;1", &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2001-04-05 23:48:01 +00:00
|
|
|
|
2018-10-10 12:05:39 +00:00
|
|
|
// XXX Why don't we dispatch ePaste event here?
|
|
|
|
|
2013-01-04 20:33:09 +00:00
|
|
|
// Get the nsITransferable interface for getting the data from the clipboard
|
|
|
|
nsCOMPtr<nsITransferable> trans;
|
|
|
|
rv = PrepareTransferable(getter_AddRefs(trans));
|
2018-07-23 07:34:03 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2019-02-25 09:07:54 +00:00
|
|
|
return EditorBase::ToGenericNSResult(rv);
|
2018-07-23 07:34:03 +00:00
|
|
|
}
|
|
|
|
if (!trans) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-08-08 04:52:05 +00:00
|
|
|
|
2018-07-23 07:34:03 +00:00
|
|
|
// Get the Data from the clipboard
|
|
|
|
clipboard->GetData(trans, aClipboardType);
|
|
|
|
|
|
|
|
// Now we ask the transferable for the data
|
|
|
|
// it still owns the data, we just have a pointer to it.
|
|
|
|
// If it can't support a "text" output of the data the call will fail
|
|
|
|
nsCOMPtr<nsISupports> genericDataObj;
|
|
|
|
nsAutoCString flav;
|
2018-11-02 11:02:25 +00:00
|
|
|
rv = trans->GetAnyTransferData(flav, getter_AddRefs(genericDataObj));
|
2019-02-25 09:07:54 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return EditorBase::ToGenericNSResult(rv);
|
2001-04-05 23:48:01 +00:00
|
|
|
}
|
|
|
|
|
2018-07-23 07:34:03 +00:00
|
|
|
if (!flav.EqualsLiteral(kUnicodeMime) &&
|
|
|
|
!flav.EqualsLiteral(kMozTextInternal)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-11-02 11:02:25 +00:00
|
|
|
if (nsCOMPtr<nsISupportsString> text = do_QueryInterface(genericDataObj)) {
|
2018-07-23 07:34:03 +00:00
|
|
|
nsAutoString stuffToPaste;
|
2018-11-02 11:02:25 +00:00
|
|
|
text->GetData(stuffToPaste);
|
Bug 998941 - part 1-3: Make TextEditor (only when not HTMLEditor instance) set InputEvent.data to inserting string when InputEvent.inputType is "insertFromPaste", "insertFromDrop" or "insertReplacementText" r=smaug,m_kato
https://rawgit.com/w3c/input-events/v1/index.html#dfn-data
https://w3c.github.io/input-events/#dfn-data
Both Input Events Level 1 and Level 2 declare that InputEvent.data should be
set to inserting string only on TextEditor when InputEvent.inputType is
"insertFromPaste", "insertFromPasteAsQuotation", "insertFromDrop",
"insertTranspose", "insertReplacementText" or "insertFromYank".
Currently, we support only "insertFromPaste", "insertFromDrop",
"insertReplacementText". Therefore, this patch makes TextEditor set
EditorBase::mEditActionData::mData only for them (and the instance is not
HTMLEditor's).
Differential Revision: https://phabricator.services.mozilla.com/D19287
--HG--
extra : moz-landing-system : lando
2019-02-19 06:28:57 +00:00
|
|
|
editActionData.SetData(stuffToPaste);
|
2018-11-02 11:02:25 +00:00
|
|
|
if (!stuffToPaste.IsEmpty()) {
|
|
|
|
AutoPlaceholderBatch treatAsOneTransaction(*this);
|
|
|
|
rv = InsertWithQuotationsAsSubAction(stuffToPaste);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2019-02-25 09:07:54 +00:00
|
|
|
return EditorBase::ToGenericNSResult(rv);
|
2018-11-02 11:02:25 +00:00
|
|
|
}
|
2018-07-23 07:34:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2001-04-05 23:48:01 +00:00
|
|
|
}
|
|
|
|
|
2018-07-23 07:05:30 +00:00
|
|
|
nsresult TextEditor::InsertWithQuotationsAsSubAction(
|
|
|
|
const nsAString& aQuotedText) {
|
2018-10-30 10:02:58 +00:00
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
|
|
|
|
2010-05-28 01:18:48 +00:00
|
|
|
// Protect the edit rules object from dying
|
2017-12-21 05:52:32 +00:00
|
|
|
RefPtr<TextEditRules> rules(mRules);
|
2010-05-28 01:18:48 +00:00
|
|
|
|
2001-04-05 23:48:01 +00:00
|
|
|
// Let the citer quote it for us:
|
|
|
|
nsString quotedStuff;
|
2016-07-07 09:00:19 +00:00
|
|
|
nsresult rv = InternetCiter::GetCiteString(aQuotedText, quotedStuff);
|
2018-07-23 07:05:30 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2001-04-05 23:48:01 +00:00
|
|
|
|
|
|
|
// It's best to put a blank line after the quoted text so that mails
|
|
|
|
// written without thinking won't be so ugly.
|
2016-10-24 02:27:45 +00:00
|
|
|
if (!aQuotedText.IsEmpty() && (aQuotedText.Last() != char16_t('\n'))) {
|
2014-01-04 15:02:17 +00:00
|
|
|
quotedStuff.Append(char16_t('\n'));
|
2016-10-24 02:27:45 +00:00
|
|
|
}
|
2001-04-05 23:48:01 +00:00
|
|
|
|
Bug 1463985 - part 1: Rename EditAction to EditSubAction and related stuff r=m_kato
When we implement InputEvent.inputType, we need to set a stack class to record
which edit action is currently handled. However, currently, we call smaller
jobs as edit action. For example, when user types a character at selecting
some characters, then, EditAction::deleteSelection is performed first, then,
EditAction::insertText is performed. However, for the InputEvent.inputType,
we need inserText information. So, for making new enum EditAction, we need
to rename current EditAction to EditSubAction.
And also this renames related stuff:
EditorBase::mIsInEditAction -> EditorBase::mIsInEditSubAction
EditorBase::IsInEditAction() -> EditorBase::IsInEditSubAction()
EditorBase::mAction -> EditorBase::mTopLevelEditSubAction
TextEditRules::mTheAction -> TextEditRules::mTopLevelEditSubAction
EditorBase::StartOperation() ->
EditorBase::OnStartToHandleTopLevelEditSubAction()
EditorBase::EndOperation() ->
EditorBase::OnEndHandlingTopLevelEditSubAction()
AutoRules -> AutoTopLevelEditSubActionNotifier
RulesInfo -> EditSubActionInfo
MozReview-Commit-ID: cvSkPUjFm1
--HG--
extra : rebase_source : baf527a3e353b7a8ebe9a46be2243b059c500234
2018-05-28 11:12:34 +00:00
|
|
|
AutoTopLevelEditSubActionNotifier maybeTopLevelEditSubAction(
|
2018-05-28 12:36:47 +00:00
|
|
|
*this, EditSubAction::eInsertText, nsIEditor::eNext);
|
2001-04-05 23:48:01 +00:00
|
|
|
|
2019-08-09 08:25:37 +00:00
|
|
|
EditSubActionInfo subActionInfo(EditSubAction::eInsertQuotedText);
|
2011-09-29 06:19:26 +00:00
|
|
|
bool cancel, handled;
|
2018-10-30 10:02:58 +00:00
|
|
|
rv = rules->WillDoAction(subActionInfo, &cancel, &handled);
|
Bug 1463985 - part 1: Rename EditAction to EditSubAction and related stuff r=m_kato
When we implement InputEvent.inputType, we need to set a stack class to record
which edit action is currently handled. However, currently, we call smaller
jobs as edit action. For example, when user types a character at selecting
some characters, then, EditAction::deleteSelection is performed first, then,
EditAction::insertText is performed. However, for the InputEvent.inputType,
we need inserText information. So, for making new enum EditAction, we need
to rename current EditAction to EditSubAction.
And also this renames related stuff:
EditorBase::mIsInEditAction -> EditorBase::mIsInEditSubAction
EditorBase::IsInEditAction() -> EditorBase::IsInEditSubAction()
EditorBase::mAction -> EditorBase::mTopLevelEditSubAction
TextEditRules::mTheAction -> TextEditRules::mTopLevelEditSubAction
EditorBase::StartOperation() ->
EditorBase::OnStartToHandleTopLevelEditSubAction()
EditorBase::EndOperation() ->
EditorBase::OnEndHandlingTopLevelEditSubAction()
AutoRules -> AutoTopLevelEditSubActionNotifier
RulesInfo -> EditSubActionInfo
MozReview-Commit-ID: cvSkPUjFm1
--HG--
extra : rebase_source : baf527a3e353b7a8ebe9a46be2243b059c500234
2018-05-28 11:12:34 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2016-10-24 02:27:45 +00:00
|
|
|
if (cancel) {
|
|
|
|
return NS_OK; // Rules canceled the operation.
|
|
|
|
}
|
2018-07-26 08:01:15 +00:00
|
|
|
MOZ_ASSERT(!handled, "WillDoAction() shouldn't handle in this case");
|
2019-08-09 08:25:37 +00:00
|
|
|
rv = InsertTextAsSubAction(quotedStuff);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2001-04-05 23:48:01 +00:00
|
|
|
}
|
Bug 1463985 - part 1: Rename EditAction to EditSubAction and related stuff r=m_kato
When we implement InputEvent.inputType, we need to set a stack class to record
which edit action is currently handled. However, currently, we call smaller
jobs as edit action. For example, when user types a character at selecting
some characters, then, EditAction::deleteSelection is performed first, then,
EditAction::insertText is performed. However, for the InputEvent.inputType,
we need inserText information. So, for making new enum EditAction, we need
to rename current EditAction to EditSubAction.
And also this renames related stuff:
EditorBase::mIsInEditAction -> EditorBase::mIsInEditSubAction
EditorBase::IsInEditAction() -> EditorBase::IsInEditSubAction()
EditorBase::mAction -> EditorBase::mTopLevelEditSubAction
TextEditRules::mTheAction -> TextEditRules::mTopLevelEditSubAction
EditorBase::StartOperation() ->
EditorBase::OnStartToHandleTopLevelEditSubAction()
EditorBase::EndOperation() ->
EditorBase::OnEndHandlingTopLevelEditSubAction()
AutoRules -> AutoTopLevelEditSubActionNotifier
RulesInfo -> EditSubActionInfo
MozReview-Commit-ID: cvSkPUjFm1
--HG--
extra : rebase_source : baf527a3e353b7a8ebe9a46be2243b059c500234
2018-05-28 11:12:34 +00:00
|
|
|
// XXX Why don't we call TextEditRules::DidDoAction()?
|
2018-07-23 07:05:30 +00:00
|
|
|
return NS_OK;
|
2001-04-05 23:48:01 +00:00
|
|
|
}
|
|
|
|
|
2016-07-09 02:54:50 +00:00
|
|
|
nsresult TextEditor::SharedOutputString(uint32_t aFlags, bool* aIsCollapsed,
|
|
|
|
nsAString& aResult) {
|
2018-10-30 10:04:08 +00:00
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
2003-07-28 13:13:50 +00:00
|
|
|
|
2018-10-30 10:04:08 +00:00
|
|
|
*aIsCollapsed = SelectionRefPtr()->IsCollapsed();
|
2003-07-28 13:13:50 +00:00
|
|
|
|
2016-10-24 02:27:45 +00:00
|
|
|
if (!*aIsCollapsed) {
|
2003-07-28 13:13:50 +00:00
|
|
|
aFlags |= nsIDocumentEncoder::OutputSelectionOnly;
|
2016-10-24 02:27:45 +00:00
|
|
|
}
|
2003-09-03 04:17:45 +00:00
|
|
|
// If the selection isn't collapsed, we'll use the whole document.
|
2018-07-18 12:27:30 +00:00
|
|
|
return ComputeValueInternal(NS_LITERAL_STRING("text/plain"), aFlags, aResult);
|
2003-07-28 13:13:50 +00:00
|
|
|
}
|
|
|
|
|
Bug 1463985 - part 1: Rename EditAction to EditSubAction and related stuff r=m_kato
When we implement InputEvent.inputType, we need to set a stack class to record
which edit action is currently handled. However, currently, we call smaller
jobs as edit action. For example, when user types a character at selecting
some characters, then, EditAction::deleteSelection is performed first, then,
EditAction::insertText is performed. However, for the InputEvent.inputType,
we need inserText information. So, for making new enum EditAction, we need
to rename current EditAction to EditSubAction.
And also this renames related stuff:
EditorBase::mIsInEditAction -> EditorBase::mIsInEditSubAction
EditorBase::IsInEditAction() -> EditorBase::IsInEditSubAction()
EditorBase::mAction -> EditorBase::mTopLevelEditSubAction
TextEditRules::mTheAction -> TextEditRules::mTopLevelEditSubAction
EditorBase::StartOperation() ->
EditorBase::OnStartToHandleTopLevelEditSubAction()
EditorBase::EndOperation() ->
EditorBase::OnEndHandlingTopLevelEditSubAction()
AutoRules -> AutoTopLevelEditSubActionNotifier
RulesInfo -> EditSubActionInfo
MozReview-Commit-ID: cvSkPUjFm1
--HG--
extra : rebase_source : baf527a3e353b7a8ebe9a46be2243b059c500234
2018-05-28 11:12:34 +00:00
|
|
|
void TextEditor::OnStartToHandleTopLevelEditSubAction(
|
|
|
|
EditSubAction aEditSubAction, nsIEditor::EDirection aDirection) {
|
2010-05-28 01:18:48 +00:00
|
|
|
// Protect the edit rules object from dying
|
2017-12-21 05:52:32 +00:00
|
|
|
RefPtr<TextEditRules> rules(mRules);
|
2010-05-28 01:18:48 +00:00
|
|
|
|
Bug 1463985 - part 1: Rename EditAction to EditSubAction and related stuff r=m_kato
When we implement InputEvent.inputType, we need to set a stack class to record
which edit action is currently handled. However, currently, we call smaller
jobs as edit action. For example, when user types a character at selecting
some characters, then, EditAction::deleteSelection is performed first, then,
EditAction::insertText is performed. However, for the InputEvent.inputType,
we need inserText information. So, for making new enum EditAction, we need
to rename current EditAction to EditSubAction.
And also this renames related stuff:
EditorBase::mIsInEditAction -> EditorBase::mIsInEditSubAction
EditorBase::IsInEditAction() -> EditorBase::IsInEditSubAction()
EditorBase::mAction -> EditorBase::mTopLevelEditSubAction
TextEditRules::mTheAction -> TextEditRules::mTopLevelEditSubAction
EditorBase::StartOperation() ->
EditorBase::OnStartToHandleTopLevelEditSubAction()
EditorBase::EndOperation() ->
EditorBase::OnEndHandlingTopLevelEditSubAction()
AutoRules -> AutoTopLevelEditSubActionNotifier
RulesInfo -> EditSubActionInfo
MozReview-Commit-ID: cvSkPUjFm1
--HG--
extra : rebase_source : baf527a3e353b7a8ebe9a46be2243b059c500234
2018-05-28 11:12:34 +00:00
|
|
|
EditorBase::OnStartToHandleTopLevelEditSubAction(aEditSubAction, aDirection);
|
|
|
|
if (!rules) {
|
|
|
|
return;
|
2016-07-18 16:36:19 +00:00
|
|
|
}
|
Bug 1463985 - part 1: Rename EditAction to EditSubAction and related stuff r=m_kato
When we implement InputEvent.inputType, we need to set a stack class to record
which edit action is currently handled. However, currently, we call smaller
jobs as edit action. For example, when user types a character at selecting
some characters, then, EditAction::deleteSelection is performed first, then,
EditAction::insertText is performed. However, for the InputEvent.inputType,
we need inserText information. So, for making new enum EditAction, we need
to rename current EditAction to EditSubAction.
And also this renames related stuff:
EditorBase::mIsInEditAction -> EditorBase::mIsInEditSubAction
EditorBase::IsInEditAction() -> EditorBase::IsInEditSubAction()
EditorBase::mAction -> EditorBase::mTopLevelEditSubAction
TextEditRules::mTheAction -> TextEditRules::mTopLevelEditSubAction
EditorBase::StartOperation() ->
EditorBase::OnStartToHandleTopLevelEditSubAction()
EditorBase::EndOperation() ->
EditorBase::OnEndHandlingTopLevelEditSubAction()
AutoRules -> AutoTopLevelEditSubActionNotifier
RulesInfo -> EditSubActionInfo
MozReview-Commit-ID: cvSkPUjFm1
--HG--
extra : rebase_source : baf527a3e353b7a8ebe9a46be2243b059c500234
2018-05-28 11:12:34 +00:00
|
|
|
|
2018-11-26 03:53:29 +00:00
|
|
|
MOZ_ASSERT(GetTopLevelEditSubAction() == aEditSubAction);
|
2018-11-26 06:30:29 +00:00
|
|
|
MOZ_ASSERT(GetDirectionOfTopLevelEditSubAction() == aDirection);
|
|
|
|
DebugOnly<nsresult> rv = rules->BeforeEdit(aEditSubAction, aDirection);
|
Bug 1463985 - part 1: Rename EditAction to EditSubAction and related stuff r=m_kato
When we implement InputEvent.inputType, we need to set a stack class to record
which edit action is currently handled. However, currently, we call smaller
jobs as edit action. For example, when user types a character at selecting
some characters, then, EditAction::deleteSelection is performed first, then,
EditAction::insertText is performed. However, for the InputEvent.inputType,
we need inserText information. So, for making new enum EditAction, we need
to rename current EditAction to EditSubAction.
And also this renames related stuff:
EditorBase::mIsInEditAction -> EditorBase::mIsInEditSubAction
EditorBase::IsInEditAction() -> EditorBase::IsInEditSubAction()
EditorBase::mAction -> EditorBase::mTopLevelEditSubAction
TextEditRules::mTheAction -> TextEditRules::mTopLevelEditSubAction
EditorBase::StartOperation() ->
EditorBase::OnStartToHandleTopLevelEditSubAction()
EditorBase::EndOperation() ->
EditorBase::OnEndHandlingTopLevelEditSubAction()
AutoRules -> AutoTopLevelEditSubActionNotifier
RulesInfo -> EditSubActionInfo
MozReview-Commit-ID: cvSkPUjFm1
--HG--
extra : rebase_source : baf527a3e353b7a8ebe9a46be2243b059c500234
2018-05-28 11:12:34 +00:00
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
NS_SUCCEEDED(rv),
|
|
|
|
"TextEditRules::BeforeEdit() failed to handle something");
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
Bug 1463985 - part 1: Rename EditAction to EditSubAction and related stuff r=m_kato
When we implement InputEvent.inputType, we need to set a stack class to record
which edit action is currently handled. However, currently, we call smaller
jobs as edit action. For example, when user types a character at selecting
some characters, then, EditAction::deleteSelection is performed first, then,
EditAction::insertText is performed. However, for the InputEvent.inputType,
we need inserText information. So, for making new enum EditAction, we need
to rename current EditAction to EditSubAction.
And also this renames related stuff:
EditorBase::mIsInEditAction -> EditorBase::mIsInEditSubAction
EditorBase::IsInEditAction() -> EditorBase::IsInEditSubAction()
EditorBase::mAction -> EditorBase::mTopLevelEditSubAction
TextEditRules::mTheAction -> TextEditRules::mTopLevelEditSubAction
EditorBase::StartOperation() ->
EditorBase::OnStartToHandleTopLevelEditSubAction()
EditorBase::EndOperation() ->
EditorBase::OnEndHandlingTopLevelEditSubAction()
AutoRules -> AutoTopLevelEditSubActionNotifier
RulesInfo -> EditSubActionInfo
MozReview-Commit-ID: cvSkPUjFm1
--HG--
extra : rebase_source : baf527a3e353b7a8ebe9a46be2243b059c500234
2018-05-28 11:12:34 +00:00
|
|
|
void TextEditor::OnEndHandlingTopLevelEditSubAction() {
|
2010-05-28 01:18:48 +00:00
|
|
|
// Protect the edit rules object from dying
|
2017-12-21 05:52:32 +00:00
|
|
|
RefPtr<TextEditRules> rules(mRules);
|
2010-05-28 01:18:48 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// post processing
|
Bug 1463985 - part 1: Rename EditAction to EditSubAction and related stuff r=m_kato
When we implement InputEvent.inputType, we need to set a stack class to record
which edit action is currently handled. However, currently, we call smaller
jobs as edit action. For example, when user types a character at selecting
some characters, then, EditAction::deleteSelection is performed first, then,
EditAction::insertText is performed. However, for the InputEvent.inputType,
we need inserText information. So, for making new enum EditAction, we need
to rename current EditAction to EditSubAction.
And also this renames related stuff:
EditorBase::mIsInEditAction -> EditorBase::mIsInEditSubAction
EditorBase::IsInEditAction() -> EditorBase::IsInEditSubAction()
EditorBase::mAction -> EditorBase::mTopLevelEditSubAction
TextEditRules::mTheAction -> TextEditRules::mTopLevelEditSubAction
EditorBase::StartOperation() ->
EditorBase::OnStartToHandleTopLevelEditSubAction()
EditorBase::EndOperation() ->
EditorBase::OnEndHandlingTopLevelEditSubAction()
AutoRules -> AutoTopLevelEditSubActionNotifier
RulesInfo -> EditSubActionInfo
MozReview-Commit-ID: cvSkPUjFm1
--HG--
extra : rebase_source : baf527a3e353b7a8ebe9a46be2243b059c500234
2018-05-28 11:12:34 +00:00
|
|
|
DebugOnly<nsresult> rv =
|
2018-11-26 06:30:29 +00:00
|
|
|
rules ? rules->AfterEdit(GetTopLevelEditSubAction(),
|
|
|
|
GetDirectionOfTopLevelEditSubAction())
|
|
|
|
: NS_OK;
|
Bug 1463985 - part 1: Rename EditAction to EditSubAction and related stuff r=m_kato
When we implement InputEvent.inputType, we need to set a stack class to record
which edit action is currently handled. However, currently, we call smaller
jobs as edit action. For example, when user types a character at selecting
some characters, then, EditAction::deleteSelection is performed first, then,
EditAction::insertText is performed. However, for the InputEvent.inputType,
we need inserText information. So, for making new enum EditAction, we need
to rename current EditAction to EditSubAction.
And also this renames related stuff:
EditorBase::mIsInEditAction -> EditorBase::mIsInEditSubAction
EditorBase::IsInEditAction() -> EditorBase::IsInEditSubAction()
EditorBase::mAction -> EditorBase::mTopLevelEditSubAction
TextEditRules::mTheAction -> TextEditRules::mTopLevelEditSubAction
EditorBase::StartOperation() ->
EditorBase::OnStartToHandleTopLevelEditSubAction()
EditorBase::EndOperation() ->
EditorBase::OnEndHandlingTopLevelEditSubAction()
AutoRules -> AutoTopLevelEditSubActionNotifier
RulesInfo -> EditSubActionInfo
MozReview-Commit-ID: cvSkPUjFm1
--HG--
extra : rebase_source : baf527a3e353b7a8ebe9a46be2243b059c500234
2018-05-28 11:12:34 +00:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"TextEditRules::AfterEdit() failed to handle something");
|
|
|
|
EditorBase::OnEndHandlingTopLevelEditSubAction();
|
2018-11-26 03:53:29 +00:00
|
|
|
MOZ_ASSERT(!GetTopLevelEditSubAction());
|
2018-11-26 06:30:29 +00:00
|
|
|
MOZ_ASSERT(GetDirectionOfTopLevelEditSubAction() == eNone);
|
2015-05-28 15:58:42 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2018-10-30 10:01:38 +00:00
|
|
|
nsresult TextEditor::SelectEntireDocument() {
|
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
|
|
|
|
|
|
|
if (!mRules) {
|
2016-10-24 02:27:45 +00:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
2005-03-24 19:00:01 +00:00
|
|
|
|
2019-03-26 10:06:43 +00:00
|
|
|
Element* rootElement = GetRoot();
|
|
|
|
if (NS_WARN_IF(!rootElement)) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2010-05-28 01:18:48 +00:00
|
|
|
// Protect the edit rules object from dying
|
2017-12-21 05:52:32 +00:00
|
|
|
RefPtr<TextEditRules> rules(mRules);
|
2010-05-28 01:18:48 +00:00
|
|
|
|
2019-03-26 10:06:43 +00:00
|
|
|
// If we're empty, don't select all children because that would select the
|
2019-08-02 05:44:40 +00:00
|
|
|
// padding <br> element for empty editor.
|
2017-05-11 05:03:26 +00:00
|
|
|
if (rules->DocumentIsEmpty()) {
|
2019-03-26 10:06:43 +00:00
|
|
|
nsresult rv = SelectionRefPtr()->Collapse(rootElement, 0);
|
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
NS_SUCCEEDED(rv),
|
|
|
|
"Failed to move caret to start of the editor root element");
|
2018-10-30 10:01:38 +00:00
|
|
|
return rv;
|
|
|
|
}
|
2011-05-25 20:30:14 +00:00
|
|
|
|
|
|
|
// Don't select the trailing BR node if we have one
|
2017-10-04 17:55:29 +00:00
|
|
|
nsCOMPtr<nsIContent> childNode;
|
2019-03-26 10:06:43 +00:00
|
|
|
nsresult rv = EditorBase::GetEndChildNode(*SelectionRefPtr(),
|
|
|
|
getter_AddRefs(childNode));
|
2017-10-04 17:55:29 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
if (childNode) {
|
|
|
|
childNode = childNode->GetPreviousSibling();
|
|
|
|
}
|
2011-05-25 20:30:14 +00:00
|
|
|
|
2019-08-02 05:45:18 +00:00
|
|
|
if (childNode && EditorBase::IsPaddingBRElementForEmptyLastLine(*childNode)) {
|
2019-03-26 10:06:43 +00:00
|
|
|
ErrorResult error;
|
|
|
|
MOZ_KnownLive(SelectionRefPtr())
|
|
|
|
->SetStartAndEndInLimiter(RawRangeBoundary(rootElement, 0),
|
|
|
|
EditorRawDOMPoint(childNode), error);
|
|
|
|
NS_WARNING_ASSERTION(!error.Failed(),
|
|
|
|
"Failed to select all children of the editor root "
|
2019-08-02 05:45:18 +00:00
|
|
|
"element except the padding <br> element");
|
2019-03-26 10:06:43 +00:00
|
|
|
return error.StealNSResult();
|
2011-05-25 20:30:14 +00:00
|
|
|
}
|
|
|
|
|
2019-03-26 10:06:43 +00:00
|
|
|
ErrorResult error;
|
|
|
|
SelectionRefPtr()->SelectAllChildren(*rootElement, error);
|
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
!error.Failed(),
|
|
|
|
"Failed to select all children of the editor root element");
|
|
|
|
return error.StealNSResult();
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2016-07-09 02:54:50 +00:00
|
|
|
EventTarget* TextEditor::GetDOMEventTarget() { return mEventTarget; }
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2016-12-20 10:24:08 +00:00
|
|
|
nsresult TextEditor::SetAttributeOrEquivalent(Element* aElement,
|
2017-10-02 22:05:19 +00:00
|
|
|
nsAtom* aAttribute,
|
2016-07-09 02:54:50 +00:00
|
|
|
const nsAString& aValue,
|
|
|
|
bool aSuppressTransaction) {
|
2018-04-12 07:58:33 +00:00
|
|
|
if (NS_WARN_IF(!aElement) || NS_WARN_IF(!aAttribute)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2018-10-30 09:59:33 +00:00
|
|
|
|
|
|
|
AutoEditActionDataSetter editActionData(*this, EditAction::eSetAttribute);
|
|
|
|
if (NS_WARN_IF(!editActionData.CanHandle())) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2019-02-25 09:07:54 +00:00
|
|
|
nsresult rv = SetAttributeWithTransaction(*aElement, *aAttribute, aValue);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return EditorBase::ToGenericNSResult(rv);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2002-03-15 15:33:29 +00:00
|
|
|
}
|
|
|
|
|
2016-12-20 10:24:08 +00:00
|
|
|
nsresult TextEditor::RemoveAttributeOrEquivalent(Element* aElement,
|
2017-10-02 22:05:19 +00:00
|
|
|
nsAtom* aAttribute,
|
2016-07-09 02:54:50 +00:00
|
|
|
bool aSuppressTransaction) {
|
2018-04-12 07:58:33 +00:00
|
|
|
if (NS_WARN_IF(!aElement) || NS_WARN_IF(!aAttribute)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2018-10-30 09:59:33 +00:00
|
|
|
|
|
|
|
AutoEditActionDataSetter editActionData(*this, EditAction::eRemoveAttribute);
|
|
|
|
if (NS_WARN_IF(!editActionData.CanHandle())) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2019-02-25 09:07:54 +00:00
|
|
|
nsresult rv = RemoveAttributeWithTransaction(*aElement, *aAttribute);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return EditorBase::ToGenericNSResult(rv);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2002-01-09 13:51:37 +00:00
|
|
|
}
|
2016-07-09 02:54:50 +00:00
|
|
|
|
2019-08-09 09:01:56 +00:00
|
|
|
nsresult TextEditor::MaybeChangePaddingBRElementForEmptyEditor() {
|
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
|
|
|
MOZ_ASSERT(!AsHTMLEditor());
|
|
|
|
|
|
|
|
// If there is padding <br> element for empty editor, we have no work to do.
|
|
|
|
if (mPaddingBRElementForEmptyEditor) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Likewise, nothing to be done if we could never have inserted a trailing
|
|
|
|
// <br> element.
|
|
|
|
// XXX Why don't we use same path for <textarea> and <input>?
|
|
|
|
if (IsSingleLineEditor()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!mRootElement)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mRootElement->GetChildCount() > 1) {
|
|
|
|
// The trailing br is redundant if it is the only remaining child node
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<HTMLBRElement> brElement =
|
|
|
|
HTMLBRElement::FromNodeOrNull(mRootElement->GetFirstChild());
|
|
|
|
if (!brElement ||
|
|
|
|
!EditorBase::IsPaddingBRElementForEmptyLastLine(*brElement)) {
|
|
|
|
// XXX Why don't we create new padding <br> element when there is no
|
|
|
|
// children?
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rather than deleting this node from the DOM tree we should instead
|
|
|
|
// morph this <br> element into the padding <br> element for editor.
|
|
|
|
mPaddingBRElementForEmptyEditor = std::move(brElement);
|
|
|
|
mPaddingBRElementForEmptyEditor->UnsetFlags(NS_PADDING_FOR_EMPTY_LAST_LINE);
|
|
|
|
mPaddingBRElementForEmptyEditor->SetFlags(NS_PADDING_FOR_EMPTY_EDITOR);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-07-22 03:53:36 +00:00
|
|
|
nsresult TextEditor::SetUnmaskRangeInternal(uint32_t aStart, uint32_t aLength,
|
|
|
|
uint32_t aTimeout, bool aNotify,
|
|
|
|
bool aForceStartMasking) {
|
|
|
|
mIsMaskingPassword = aForceStartMasking || aTimeout != 0;
|
|
|
|
|
|
|
|
// We cannot manage multiple unmasked ranges so that shrink the previous
|
|
|
|
// range first.
|
|
|
|
if (!IsAllMasked()) {
|
|
|
|
mUnmaskedLength = 0;
|
|
|
|
if (mMaskTimer) {
|
|
|
|
mMaskTimer->Cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're not a password editor, return error since this call does not
|
|
|
|
// make sense.
|
|
|
|
if (!IsPasswordEditor()) {
|
|
|
|
if (mMaskTimer) {
|
|
|
|
mMaskTimer = nullptr;
|
|
|
|
}
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Element* rootElement = GetRoot();
|
|
|
|
if (NS_WARN_IF(!rootElement)) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2019-07-22 03:56:33 +00:00
|
|
|
Text* text = Text::FromNodeOrNull(rootElement->GetFirstChild());
|
|
|
|
if (!text) {
|
2019-07-22 03:53:36 +00:00
|
|
|
// There is no anonymous text node in the editor.
|
2019-07-29 06:30:50 +00:00
|
|
|
return aStart > 0 && aStart != UINT32_MAX ? NS_ERROR_INVALID_ARG : NS_OK;
|
2019-07-22 03:53:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aStart < UINT32_MAX) {
|
2019-07-22 03:56:33 +00:00
|
|
|
uint32_t valueLength = text->Length();
|
2019-07-22 03:53:36 +00:00
|
|
|
if (aStart >= valueLength) {
|
|
|
|
return NS_ERROR_INVALID_ARG; // There is no character can be masked.
|
|
|
|
}
|
2019-07-22 03:56:33 +00:00
|
|
|
// If aStart is middle of a surrogate pair, expand it to include the
|
|
|
|
// preceding high surrogate because the caller may want to show a
|
|
|
|
// character before the character at `aStart + 1`.
|
|
|
|
const nsTextFragment* textFragment = text->GetText();
|
2019-08-06 05:43:21 +00:00
|
|
|
if (textFragment->IsLowSurrogateFollowingHighSurrogateAt(aStart)) {
|
2019-07-22 03:56:33 +00:00
|
|
|
mUnmaskedStart = aStart - 1;
|
|
|
|
// If caller collapses the range, keep it. Otherwise, expand the length.
|
|
|
|
if (aLength > 0) {
|
|
|
|
++aLength;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mUnmaskedStart = aStart;
|
|
|
|
}
|
2019-07-22 03:53:36 +00:00
|
|
|
mUnmaskedLength = std::min(valueLength - mUnmaskedStart, aLength);
|
2019-07-22 03:56:33 +00:00
|
|
|
// If unmasked end is middle of a surrogate pair, expand it to include
|
|
|
|
// the following low surrogate because the caller may want to show a
|
|
|
|
// character after the character at `aStart + aLength`.
|
2019-08-06 05:43:21 +00:00
|
|
|
if (UnmaskedEnd() < valueLength &&
|
|
|
|
textFragment->IsLowSurrogateFollowingHighSurrogateAt(UnmaskedEnd())) {
|
2019-07-22 03:56:33 +00:00
|
|
|
++mUnmaskedLength;
|
|
|
|
}
|
2019-07-22 03:53:36 +00:00
|
|
|
// If it's first time to mask the unmasking characters with timer, create
|
|
|
|
// the timer now. Then, we'll keep using it for saving the creation cost.
|
|
|
|
if (!mMaskTimer && aLength && aTimeout && mUnmaskedLength) {
|
|
|
|
mMaskTimer = NS_NewTimer();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (NS_WARN_IF(aLength != 0)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
mUnmaskedStart = UINT32_MAX;
|
|
|
|
mUnmaskedLength = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify nsTextFrame of this update if the caller wants this to do it.
|
|
|
|
// Only in this case, script may run.
|
|
|
|
if (aNotify) {
|
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
|
|
|
|
|
|
|
RefPtr<Document> document = GetDocument();
|
|
|
|
if (NS_WARN_IF(!document)) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
// Notify nsTextFrame of masking range change.
|
|
|
|
if (PresShell* presShell = document->GetObservingPresShell()) {
|
2019-07-22 03:56:33 +00:00
|
|
|
uint32_t valueLength = text->Length();
|
|
|
|
CharacterDataChangeInfo changeInfo = {false, 0, valueLength, valueLength,
|
|
|
|
nullptr};
|
|
|
|
presShell->CharacterDataChanged(text, changeInfo);
|
2019-07-22 03:53:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scroll caret into the view since masking or unmasking character may
|
|
|
|
// move caret to outside of the view.
|
|
|
|
ScrollSelectionIntoView(false);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(Destroyed())) {
|
|
|
|
return NS_ERROR_EDITOR_DESTROYED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsAllMasked() && aTimeout != 0) {
|
|
|
|
// Initialize the timer to mask the range automatically.
|
|
|
|
MOZ_ASSERT(mMaskTimer);
|
|
|
|
mMaskTimer->InitWithCallback(this, aTimeout, nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-07-22 03:55:13 +00:00
|
|
|
// static
|
|
|
|
char16_t TextEditor::PasswordMask() {
|
|
|
|
char16_t ret = LookAndFeel::GetPasswordCharacter();
|
|
|
|
if (!ret) {
|
|
|
|
ret = '*';
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-07-22 03:53:36 +00:00
|
|
|
MOZ_CAN_RUN_SCRIPT_BOUNDARY
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TextEditor::Notify(nsITimer* aTimer) {
|
|
|
|
// Check whether our text editor's password flag was changed before this
|
|
|
|
// "hide password character" timer actually fires.
|
|
|
|
if (!IsPasswordEditor()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsAllMasked()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoEditActionDataSetter editActionData(*this, EditAction::eHidePassword);
|
|
|
|
if (NS_WARN_IF(!editActionData.CanHandle())) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mask all characters.
|
|
|
|
nsresult rv = MaskAllCharactersAndNotify();
|
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to mask all characters");
|
2019-07-22 03:55:13 +00:00
|
|
|
|
|
|
|
if (StaticPrefs::editor_password_testing_mask_delay()) {
|
|
|
|
if (RefPtr<Element> target = GetInputEventTargetElement()) {
|
|
|
|
RefPtr<Document> document = target->OwnerDoc();
|
|
|
|
nsContentUtils::DispatchTrustedEvent(
|
|
|
|
document, target, NS_LITERAL_STRING("MozLastInputMasked"),
|
|
|
|
CanBubble::eYes, Cancelable::eNo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-22 03:53:36 +00:00
|
|
|
return EditorBase::ToGenericNSResult(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TextEditor::GetName(nsACString& aName) {
|
|
|
|
aName.AssignLiteral("TextEditor");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEditor::WillDeleteText(uint32_t aCurrentLength,
|
|
|
|
uint32_t aRemoveStartOffset,
|
|
|
|
uint32_t aRemoveLength) {
|
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
|
|
|
|
|
|
|
if (!IsPasswordEditor() || IsAllMasked()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust unmasked range before deletion since DOM mutation may cause
|
|
|
|
// layout referring the range in old text.
|
|
|
|
|
|
|
|
// If we need to mask automatically, mask all now.
|
|
|
|
if (mIsMaskingPassword) {
|
|
|
|
DebugOnly<nsresult> rvIgnored = MaskAllCharacters();
|
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored), "MaskAllCharacters() failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aRemoveStartOffset < mUnmaskedStart) {
|
|
|
|
// If removing range is before the unmasked range, move it.
|
|
|
|
if (aRemoveStartOffset + aRemoveLength <= mUnmaskedStart) {
|
|
|
|
DebugOnly<nsresult> rvIgnored =
|
|
|
|
SetUnmaskRange(mUnmaskedStart - aRemoveLength, mUnmaskedLength);
|
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored), "SetUnmaskRange() failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If removing range starts before unmasked range, and ends in unmasked
|
|
|
|
// range, move and shrink the range.
|
|
|
|
if (aRemoveStartOffset + aRemoveLength < UnmaskedEnd()) {
|
|
|
|
uint32_t unmaskedLengthInRemovingRange =
|
|
|
|
aRemoveStartOffset + aRemoveLength - mUnmaskedStart;
|
|
|
|
DebugOnly<nsresult> rvIgnored = SetUnmaskRange(
|
|
|
|
aRemoveStartOffset, mUnmaskedLength - unmaskedLengthInRemovingRange);
|
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored), "SetUnmaskRange() failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If removing range includes all unmasked range, collapse it to the
|
|
|
|
// remove offset.
|
|
|
|
DebugOnly<nsresult> rvIgnored = SetUnmaskRange(aRemoveStartOffset, 0);
|
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored), "SetUnmaskRange() failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aRemoveStartOffset < UnmaskedEnd()) {
|
|
|
|
// If removing range is in unmasked range, shrink the range.
|
|
|
|
if (aRemoveStartOffset + aRemoveLength <= UnmaskedEnd()) {
|
|
|
|
DebugOnly<nsresult> rvIgnored =
|
|
|
|
SetUnmaskRange(mUnmaskedStart, mUnmaskedLength - aRemoveLength);
|
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored), "SetUnmaskRange() failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If removing range starts from unmasked range, and ends after it,
|
|
|
|
// shrink it.
|
|
|
|
DebugOnly<nsresult> rvIgnored =
|
|
|
|
SetUnmaskRange(mUnmaskedStart, aRemoveStartOffset - mUnmaskedStart);
|
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored), "SetUnmaskRange() failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If removing range is after the unmasked range, keep it.
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult TextEditor::DidInsertText(uint32_t aNewLength,
|
|
|
|
uint32_t aInsertedOffset,
|
|
|
|
uint32_t aInsertedLength) {
|
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
|
|
|
|
|
|
|
if (!IsPasswordEditor() || IsAllMasked()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mIsMaskingPassword) {
|
|
|
|
// If we need to mask password, mask all right now.
|
|
|
|
nsresult rv = MaskAllCharactersAndNotify();
|
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "MaskAllCharacters() failed");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aInsertedOffset < mUnmaskedStart) {
|
|
|
|
// If insertion point is before unmasked range, expand the unmasked range
|
|
|
|
// to include the new text.
|
|
|
|
nsresult rv = SetUnmaskRangeAndNotify(
|
|
|
|
aInsertedOffset, UnmaskedEnd() + aInsertedLength - aInsertedOffset);
|
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "SetUnmaskRange() failed");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aInsertedOffset <= UnmaskedEnd()) {
|
|
|
|
// If insertion point is in unmasked range, unmask new text.
|
|
|
|
nsresult rv = SetUnmaskRangeAndNotify(mUnmaskedStart,
|
|
|
|
mUnmaskedLength + aInsertedLength);
|
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "SetUnmaskRange() failed");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If insertion point is after unmasked range, extend the unmask range to
|
|
|
|
// include the new text.
|
|
|
|
nsresult rv = SetUnmaskRangeAndNotify(
|
|
|
|
mUnmaskedStart, aInsertedOffset + aInsertedLength - mUnmaskedStart);
|
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "SetUnmaskRange() failed");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-07-09 02:54:50 +00:00
|
|
|
} // namespace mozilla
|