2001-09-26 02:53:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04: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/. */
|
1999-03-10 22:53:26 +03:00
|
|
|
|
2019-09-18 11:40:08 +03:00
|
|
|
#include "TextEditor.h"
|
2014-12-02 08:07:42 +03:00
|
|
|
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2018-01-12 13:01:04 +03:00
|
|
|
#include "mozilla/EditAction.h"
|
2017-11-07 13:50:25 +03:00
|
|
|
#include "mozilla/EditorDOMPoint.h"
|
2016-07-08 08:03:31 +03:00
|
|
|
#include "mozilla/EditorUtils.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "mozilla/LookAndFeel.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
2014-04-03 08:18:37 +04:00
|
|
|
#include "mozilla/TextComposition.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2019-08-02 08:44:40 +03:00
|
|
|
#include "mozilla/dom/HTMLBRElement.h"
|
2018-02-01 22:26:12 +03:00
|
|
|
#include "mozilla/dom/NodeFilterBinding.h"
|
2016-07-09 05:34:41 +03:00
|
|
|
#include "mozilla/dom/NodeIterator.h"
|
|
|
|
#include "mozilla/dom/Selection.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsAString.h"
|
1999-03-10 22:53:26 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsCRTGlue.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsIContent.h"
|
2014-02-28 03:04:46 +04:00
|
|
|
#include "nsNameSpaceManager.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsINode.h"
|
|
|
|
#include "nsISupportsBase.h"
|
|
|
|
#include "nsLiteralString.h"
|
2017-05-25 08:30:50 +03:00
|
|
|
#include "nsTextNode.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsUnicharUtils.h"
|
2017-02-02 18:32:58 +03:00
|
|
|
#include "nsIHTMLCollection.h"
|
2017-05-25 08:30:50 +03:00
|
|
|
#include "nsPrintfCString.h"
|
2011-06-17 04:59:29 +04:00
|
|
|
|
2016-07-09 05:34:41 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace dom;
|
2011-06-17 04:59:29 +04:00
|
|
|
|
2020-03-16 23:32:41 +03:00
|
|
|
#define CANCEL_OPERATION_AND_RETURN_EDIT_ACTION_RESULT_IF_READONLY_OF_DISABLED \
|
|
|
|
if (IsReadonly() || IsDisabled()) { \
|
|
|
|
return EditActionCanceled(NS_OK); \
|
2018-11-03 14:20:06 +03:00
|
|
|
}
|
1999-03-13 07:53:21 +03:00
|
|
|
|
2019-09-18 11:21:37 +03:00
|
|
|
nsresult TextEditor::InitEditorContentAndSelection() {
|
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
2000-01-13 13:17:35 +03:00
|
|
|
|
2019-09-18 11:21:37 +03:00
|
|
|
nsresult rv = MaybeCreatePaddingBRElementForEmptyEditor();
|
2020-03-18 04:14:42 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING(
|
|
|
|
"EditorBase::MaybeCreatePaddingBRElementForEmptyEditor() failed");
|
2018-04-26 16:41:34 +03:00
|
|
|
return rv;
|
|
|
|
}
|
fixes:
14753, 29843, 39864, 40141,
40139, 36679, 39542, 34729,
34855, 37216, 39292, 26447
r=sfraser,cmanske,fm; a=beppe
2000-05-25 03:00:24 +04:00
|
|
|
|
2010-02-02 07:00:12 +03:00
|
|
|
// If the selection hasn't been set up yet, set it up collapsed to the end of
|
|
|
|
// our editable content.
|
2019-09-18 11:21:37 +03:00
|
|
|
// XXX I think that this shouldn't do it in `HTMLEditor` because it maybe
|
|
|
|
// removed by the web app and if they call `Selection::AddRange()`,
|
|
|
|
// it may cause multiple selection ranges.
|
2018-10-30 13:02:58 +03:00
|
|
|
if (!SelectionRefPtr()->RangeCount()) {
|
2019-09-18 11:21:37 +03:00
|
|
|
nsresult rv = CollapseSelectionToEnd();
|
2020-03-18 04:14:42 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("EditorBase::CollapseSelectionToEnd() failed");
|
2018-04-26 16:41:34 +03:00
|
|
|
return rv;
|
|
|
|
}
|
2010-02-02 07:00:12 +03:00
|
|
|
}
|
|
|
|
|
2019-09-17 05:47:49 +03:00
|
|
|
if (IsPlaintextEditor() && !IsSingleLineEditor()) {
|
2019-09-18 11:21:37 +03:00
|
|
|
nsresult rv = EnsurePaddingBRElementInMultilineEditor();
|
2020-03-18 04:14:42 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING(
|
|
|
|
"TextEditor::EnsurePaddingBRElementInMultilineEditor() failed");
|
2018-04-26 16:41:34 +03:00
|
|
|
return rv;
|
|
|
|
}
|
2002-08-23 21:57:51 +04:00
|
|
|
}
|
2003-07-18 18:12:51 +04:00
|
|
|
|
2016-10-19 12:09:33 +03:00
|
|
|
return NS_OK;
|
Preparation for ender-based text control
* added focus listener. Doesn't do much yet, but when focus notifications start appearing, we'll be ready for them. The code is in
place to hide selection when we lose focus and paint selection when we get focus. That's probably not quite right, but it's a start.
We will need to be able to determine the distinction between losing focus to another control within our app, and losing focus to
another app.
* added support for disabled and readonly states in the editor. This is accomplished by having flags set by the client, and letting the
rules system deal with those flags. The flags I added are:
TEXT_EDITOR_FLAG_PLAINTEXT 0x01 // only plain text editing is allowed
TEXT_EDITOR_FLAG_SINGLELINE 0x02 // enter key and CR-LF handled specially
TEXT_EDITOR_FLAG_PASSWORD 0x04 // text is not entered into content, only a representative character
TEXT_EDITOR_FLAG_READONLY 0x08 // editing events are disabled. Editor may still accept focus.
TEXT_EDITOR_FLAG_DISALBED 0x10 // all events are disabled (like scrolling). Editor will not accept focus.
* added WillInsertBreak/DidInsertBreak into text rules, so flags could be checked. This gets us readonly, disabled, and single line
behavior.
* cleaned up the code that allocates, registers, and destroys event listeners. Thanks to Kin and Simon for cleaning up the
ownership model on the listeners, it was a big help.
* added support for a max text length. You can now tell the text editor, be no bigger than n characters.
1999-05-29 01:24:18 +04:00
|
|
|
}
|
|
|
|
|
2019-09-18 06:01:28 +03:00
|
|
|
void TextEditor::OnStartToHandleTopLevelEditSubAction(
|
|
|
|
EditSubAction aTopLevelEditSubAction,
|
|
|
|
nsIEditor::EDirection aDirectionOfTopLevelEditSubAction, ErrorResult& aRv) {
|
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
|
|
|
MOZ_ASSERT(!AsHTMLEditor());
|
|
|
|
MOZ_ASSERT(!aRv.Failed());
|
2019-08-13 04:24:01 +03:00
|
|
|
|
2019-09-18 06:01:28 +03:00
|
|
|
EditorBase::OnStartToHandleTopLevelEditSubAction(
|
|
|
|
aTopLevelEditSubAction, aDirectionOfTopLevelEditSubAction, aRv);
|
2018-04-24 09:23:01 +03:00
|
|
|
|
2019-09-18 06:01:28 +03:00
|
|
|
MOZ_ASSERT(GetTopLevelEditSubAction() == aTopLevelEditSubAction);
|
|
|
|
MOZ_ASSERT(GetDirectionOfTopLevelEditSubAction() ==
|
|
|
|
aDirectionOfTopLevelEditSubAction);
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2019-09-18 06:01:28 +03:00
|
|
|
if (NS_WARN_IF(Destroyed())) {
|
|
|
|
aRv.Throw(NS_ERROR_EDITOR_DESTROYED);
|
|
|
|
return;
|
|
|
|
}
|
1999-12-07 11:30:19 +03:00
|
|
|
|
2020-03-18 04:14:42 +03:00
|
|
|
if (NS_WARN_IF(!mInitSucceeded)) {
|
2019-09-18 06:01:28 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-09-17 05:47:49 +03:00
|
|
|
|
2019-09-18 06:01:28 +03:00
|
|
|
if (aTopLevelEditSubAction == EditSubAction::eSetText) {
|
|
|
|
// SetText replaces all text, so spell checker handles starting from the
|
|
|
|
// start of new value.
|
|
|
|
SetSpellCheckRestartPoint(EditorDOMPoint(mRootElement, 0));
|
|
|
|
return;
|
2018-04-24 09:23:01 +03:00
|
|
|
}
|
|
|
|
|
2019-09-18 06:01:28 +03:00
|
|
|
if (aTopLevelEditSubAction == EditSubAction::eInsertText ||
|
|
|
|
aTopLevelEditSubAction == EditSubAction::eInsertTextComingFromIME) {
|
|
|
|
// For spell checker, previous selected node should be text node if
|
|
|
|
// possible. If anchor is root of editor, it may become invalid offset
|
|
|
|
// after inserting text.
|
|
|
|
EditorRawDOMPoint point = FindBetterInsertionPoint(
|
|
|
|
EditorRawDOMPoint(SelectionRefPtr()->AnchorRef()));
|
2020-03-18 04:14:42 +03:00
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
point.IsSet(),
|
|
|
|
"EditorBase::FindBetterInsertionPoint() failed, but ignored");
|
2019-09-18 06:01:28 +03:00
|
|
|
if (point.IsSet()) {
|
|
|
|
SetSpellCheckRestartPoint(point);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (SelectionRefPtr()->AnchorRef().IsSet()) {
|
|
|
|
SetSpellCheckRestartPoint(
|
|
|
|
EditorRawDOMPoint(SelectionRefPtr()->AnchorRef()));
|
|
|
|
}
|
|
|
|
}
|
2019-08-13 05:42:25 +03:00
|
|
|
|
2019-09-18 06:01:28 +03:00
|
|
|
nsresult TextEditor::OnEndHandlingTopLevelEditSubAction() {
|
|
|
|
MOZ_ASSERT(IsTopLevelEditSubActionDataAvailable());
|
|
|
|
MOZ_ASSERT(!AsHTMLEditor());
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2019-09-18 06:01:28 +03:00
|
|
|
nsresult rv;
|
|
|
|
while (true) {
|
|
|
|
if (NS_WARN_IF(Destroyed())) {
|
|
|
|
rv = NS_ERROR_EDITOR_DESTROYED;
|
|
|
|
break;
|
|
|
|
}
|
2005-02-02 00:12:53 +03:00
|
|
|
|
2019-09-18 06:01:28 +03:00
|
|
|
// XXX Probably, we should spellcheck again after edit action (not top-level
|
|
|
|
// sub-action) is handled because the ranges can be referred only by
|
|
|
|
// users.
|
2020-03-18 04:14:42 +03:00
|
|
|
if (NS_FAILED(rv = HandleInlineSpellCheckAfterEdit())) {
|
|
|
|
NS_WARNING("TextEditor::HandleInlineSpellCheckAfterEdit() failed");
|
2019-09-18 06:01:28 +03:00
|
|
|
break;
|
|
|
|
}
|
2011-08-12 23:53:10 +04:00
|
|
|
|
2020-03-18 04:14:42 +03:00
|
|
|
if (NS_FAILED(rv = EnsurePaddingBRElementForEmptyEditor())) {
|
|
|
|
NS_WARNING("TextEditor::EnsurePaddingBRElementForEmptyEditor() failed");
|
2019-09-18 06:01:28 +03:00
|
|
|
break;
|
2019-09-17 05:47:49 +03:00
|
|
|
}
|
2010-09-02 02:06:52 +04:00
|
|
|
|
2019-09-18 06:01:28 +03:00
|
|
|
if (!IsSingleLineEditor() &&
|
2020-03-18 04:14:42 +03:00
|
|
|
NS_FAILED(rv = EnsurePaddingBRElementInMultilineEditor())) {
|
|
|
|
NS_WARNING(
|
|
|
|
"TextEditor::EnsurePaddingBRElementInMultilineEditor() failed");
|
2019-09-18 06:01:28 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = EnsureCaretNotAtEndOfTextNode();
|
|
|
|
if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
|
|
|
|
break;
|
|
|
|
}
|
2020-03-18 04:14:42 +03:00
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
NS_SUCCEEDED(rv),
|
|
|
|
"TextEditor::EnsureCaretNotAtEndOfTextNode() failed, but ignored");
|
2019-09-18 06:01:28 +03:00
|
|
|
rv = NS_OK;
|
|
|
|
break;
|
1999-12-07 11:30:19 +03:00
|
|
|
}
|
2020-03-18 04:14:42 +03:00
|
|
|
DebugOnly<nsresult> rvIgnored =
|
|
|
|
EditorBase::OnEndHandlingTopLevelEditSubAction();
|
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
NS_SUCCEEDED(rvIgnored),
|
|
|
|
"EditorBase::OnEndHandlingTopLevelEditSubAction() failed, but ignored");
|
2019-09-18 06:01:28 +03:00
|
|
|
MOZ_ASSERT(!GetTopLevelEditSubAction());
|
|
|
|
MOZ_ASSERT(GetDirectionOfTopLevelEditSubAction() == eNone);
|
|
|
|
return rv;
|
1999-12-07 11:30:19 +03:00
|
|
|
}
|
|
|
|
|
2019-09-12 11:03:57 +03:00
|
|
|
EditActionResult TextEditor::InsertLineFeedCharacterAtSelection() {
|
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
|
|
|
MOZ_ASSERT(!AsHTMLEditor());
|
2018-11-03 14:20:06 +03:00
|
|
|
MOZ_ASSERT(!IsSingleLineEditor());
|
|
|
|
|
2019-09-12 11:03:57 +03:00
|
|
|
UndefineCaretBidiLevel();
|
|
|
|
|
2020-03-16 23:32:41 +03:00
|
|
|
CANCEL_OPERATION_AND_RETURN_EDIT_ACTION_RESULT_IF_READONLY_OF_DISABLED
|
2018-11-03 14:20:06 +03:00
|
|
|
|
2019-09-12 11:03:57 +03:00
|
|
|
if (mMaxTextLength >= 0) {
|
2019-09-12 10:48:29 +03:00
|
|
|
nsAutoString insertionString(NS_LITERAL_STRING("\n"));
|
|
|
|
EditActionResult result =
|
2019-09-13 05:40:09 +03:00
|
|
|
TruncateInsertionStringForMaxLength(insertionString);
|
2020-03-18 04:14:42 +03:00
|
|
|
if (result.Failed()) {
|
|
|
|
NS_WARNING("TextEditor::TruncateInsertionStringForMaxLength() failed");
|
2019-09-12 10:48:29 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (result.Handled()) {
|
|
|
|
// Don't return as handled since we stopped inserting the line break.
|
|
|
|
return EditActionCanceled();
|
|
|
|
}
|
2016-10-24 05:27:45 +03:00
|
|
|
}
|
2018-11-02 11:58:29 +03:00
|
|
|
|
2018-11-03 14:20:06 +03:00
|
|
|
// if the selection isn't collapsed, delete it.
|
|
|
|
if (!SelectionRefPtr()->IsCollapsed()) {
|
2019-09-12 10:48:29 +03:00
|
|
|
nsresult rv =
|
2019-09-12 11:03:57 +03:00
|
|
|
DeleteSelectionAsSubAction(nsIEditor::eNone, nsIEditor::eStrip);
|
2020-03-18 04:14:42 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING(
|
|
|
|
"TextEditor::DeleteSelectionAsSubAction(eNone, eStrip) failed");
|
2018-11-03 14:20:06 +03:00
|
|
|
return EditActionIgnored(rv);
|
2018-11-03 03:08:42 +03:00
|
|
|
}
|
2018-11-02 11:58:29 +03:00
|
|
|
}
|
2018-11-03 14:20:06 +03:00
|
|
|
|
2019-09-12 11:03:57 +03:00
|
|
|
nsresult rv = EnsureNoPaddingBRElementForEmptyEditor();
|
2020-03-18 04:14:42 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("EditorBase::EnsureNoPaddingBRElementForEmptyEditor() failed");
|
2018-11-03 14:20:06 +03:00
|
|
|
return EditActionIgnored(rv);
|
|
|
|
}
|
|
|
|
|
2018-11-03 14:21:15 +03:00
|
|
|
// get the (collapsed) selection location
|
|
|
|
nsRange* firstRange = SelectionRefPtr()->GetRangeAt(0);
|
|
|
|
if (NS_WARN_IF(!firstRange)) {
|
|
|
|
return EditActionIgnored(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorRawDOMPoint pointToInsert(firstRange->StartRef());
|
|
|
|
if (NS_WARN_IF(!pointToInsert.IsSet())) {
|
|
|
|
return EditActionIgnored(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(pointToInsert.IsSetAndValid());
|
|
|
|
|
|
|
|
// Don't put text in places that can't have it.
|
|
|
|
if (!pointToInsert.IsInTextNode() &&
|
2019-09-12 11:03:57 +03:00
|
|
|
!CanContainTag(*pointToInsert.GetContainer(), *nsGkAtoms::textTagName)) {
|
2020-03-18 04:14:42 +03:00
|
|
|
NS_WARNING("Insertion point couldn't have text nodes");
|
2018-11-03 14:21:15 +03:00
|
|
|
return EditActionIgnored(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
2019-09-12 11:03:57 +03:00
|
|
|
RefPtr<Document> document = GetDocument();
|
|
|
|
if (NS_WARN_IF(!document)) {
|
2018-11-03 14:21:15 +03:00
|
|
|
return EditActionIgnored(NS_ERROR_NOT_INITIALIZED);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't change my selection in sub-transactions.
|
2019-09-12 11:03:57 +03:00
|
|
|
AutoTransactionsConserveSelection dontChangeMySelection(*this);
|
2018-11-03 14:21:15 +03:00
|
|
|
|
|
|
|
// Insert a linefeed character.
|
2019-09-12 11:03:57 +03:00
|
|
|
EditorRawDOMPoint pointAfterInsertedLineFeed;
|
|
|
|
rv = InsertTextWithTransaction(*document, NS_LITERAL_STRING("\n"),
|
|
|
|
pointToInsert, &pointAfterInsertedLineFeed);
|
2020-03-18 04:14:42 +03:00
|
|
|
if (!pointAfterInsertedLineFeed.IsSet()) {
|
|
|
|
NS_WARNING(
|
|
|
|
"EditorBase::InsertTextWithTransaction(\\n) didn't return position of "
|
|
|
|
"inserted linefeed");
|
2018-11-03 14:21:15 +03:00
|
|
|
return EditActionIgnored(NS_ERROR_FAILURE);
|
|
|
|
}
|
2020-03-18 04:14:42 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("TextEditor::InsertTextWithTransaction(\\n) failed");
|
2018-11-03 14:21:15 +03:00
|
|
|
return EditActionIgnored(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the selection to the correct location
|
|
|
|
MOZ_ASSERT(
|
2019-09-12 11:03:57 +03:00
|
|
|
!pointAfterInsertedLineFeed.GetChild(),
|
|
|
|
"After inserting text into a text node, pointAfterInsertedLineFeed."
|
2018-11-03 14:21:15 +03:00
|
|
|
"GetChild() should be nullptr");
|
2019-09-12 11:03:57 +03:00
|
|
|
rv = SelectionRefPtr()->Collapse(pointAfterInsertedLineFeed);
|
2020-03-18 04:14:42 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Selection::Collapse() failed");
|
2018-11-03 14:21:15 +03:00
|
|
|
return EditActionIgnored(rv);
|
|
|
|
}
|
|
|
|
|
2019-09-12 11:03:57 +03:00
|
|
|
// XXX I don't think we still need this. This must have been required when
|
|
|
|
// `<textarea>` was implemented with text nodes and `<br>` elements.
|
2018-11-03 14:21:15 +03:00
|
|
|
// see if we're at the end of the editor range
|
|
|
|
EditorRawDOMPoint endPoint(EditorBase::GetEndPoint(*SelectionRefPtr()));
|
2019-09-12 11:03:57 +03:00
|
|
|
if (endPoint == pointAfterInsertedLineFeed) {
|
2018-11-03 14:21:15 +03:00
|
|
|
// SetInterlinePosition(true) means we want the caret to stick to the
|
|
|
|
// content on the "right". We want the caret to stick to whatever is
|
|
|
|
// past the break. This is because the break is on the same line we
|
|
|
|
// were on, but the next content will be on the following line.
|
2020-03-18 04:14:42 +03:00
|
|
|
IgnoredErrorResult ignoredError;
|
|
|
|
SelectionRefPtr()->SetInterlinePosition(true, ignoredError);
|
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
!ignoredError.Failed(),
|
|
|
|
"Selection::SetInterlinePosition(true) failed, but ignored");
|
2018-11-03 14:21:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return EditActionHandled();
|
Preparation for ender-based text control
* added focus listener. Doesn't do much yet, but when focus notifications start appearing, we'll be ready for them. The code is in
place to hide selection when we lose focus and paint selection when we get focus. That's probably not quite right, but it's a start.
We will need to be able to determine the distinction between losing focus to another control within our app, and losing focus to
another app.
* added support for disabled and readonly states in the editor. This is accomplished by having flags set by the client, and letting the
rules system deal with those flags. The flags I added are:
TEXT_EDITOR_FLAG_PLAINTEXT 0x01 // only plain text editing is allowed
TEXT_EDITOR_FLAG_SINGLELINE 0x02 // enter key and CR-LF handled specially
TEXT_EDITOR_FLAG_PASSWORD 0x04 // text is not entered into content, only a representative character
TEXT_EDITOR_FLAG_READONLY 0x08 // editing events are disabled. Editor may still accept focus.
TEXT_EDITOR_FLAG_DISALBED 0x10 // all events are disabled (like scrolling). Editor will not accept focus.
* added WillInsertBreak/DidInsertBreak into text rules, so flags could be checked. This gets us readonly, disabled, and single line
behavior.
* cleaned up the code that allocates, registers, and destroys event listeners. Thanks to Kin and Simon for cleaning up the
ownership model on the listeners, it was a big help.
* added support for a max text length. You can now tell the text editor, be no bigger than n characters.
1999-05-29 01:24:18 +04:00
|
|
|
}
|
|
|
|
|
2019-09-17 05:47:49 +03:00
|
|
|
nsresult TextEditor::EnsureCaretNotAtEndOfTextNode() {
|
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
|
|
|
MOZ_ASSERT(IsPlaintextEditor());
|
1999-12-07 11:30:19 +03:00
|
|
|
|
2015-05-28 07:50:44 +03:00
|
|
|
// If there is no selection ranges, we should set to the end of the editor.
|
2019-09-18 11:40:08 +03:00
|
|
|
// This is usually performed in InitEditorContentAndSelection(), however,
|
|
|
|
// if the editor is reframed, this may be called by
|
|
|
|
// OnEndHandlingTopLevelEditSubAction().
|
2018-10-30 13:02:58 +03:00
|
|
|
if (!SelectionRefPtr()->RangeCount()) {
|
2020-03-18 04:14:42 +03:00
|
|
|
DebugOnly<nsresult> rvIgnored = CollapseSelectionToEnd();
|
2019-09-17 05:47:49 +03:00
|
|
|
if (NS_WARN_IF(Destroyed())) {
|
2018-05-11 12:40:47 +03:00
|
|
|
return NS_ERROR_EDITOR_DESTROYED;
|
2018-03-15 15:25:41 +03:00
|
|
|
}
|
2020-03-18 04:14:42 +03:00
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
NS_SUCCEEDED(rvIgnored),
|
|
|
|
"EditorBase::CollapseSelectionToEnd() failed, but ignored");
|
2017-05-29 05:28:21 +03:00
|
|
|
}
|
2010-09-03 03:54:23 +04:00
|
|
|
|
2018-03-15 15:25:41 +03:00
|
|
|
// If we are at the end of the <textarea> element, we need to set the
|
2019-08-02 08:45:18 +03:00
|
|
|
// selection to stick to the padding <br> element for empty last line at the
|
|
|
|
// end of the <textarea>.
|
2018-05-08 11:30:05 +03:00
|
|
|
EditorRawDOMPoint selectionStartPoint(
|
2018-10-30 13:02:58 +03:00
|
|
|
EditorBase::GetStartPoint(*SelectionRefPtr()));
|
2018-03-15 15:25:41 +03:00
|
|
|
if (NS_WARN_IF(!selectionStartPoint.IsSet())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2016-10-24 05:27:45 +03:00
|
|
|
}
|
2010-07-20 00:19:27 +04:00
|
|
|
|
2018-03-15 15:25:41 +03:00
|
|
|
// Nothing to do if we're not at the end of the text node.
|
|
|
|
if (!selectionStartPoint.IsInTextNode() ||
|
|
|
|
!selectionStartPoint.IsEndOfContainer()) {
|
2010-11-04 23:45:39 +03:00
|
|
|
return NS_OK;
|
2016-10-24 05:27:45 +03:00
|
|
|
}
|
2010-07-20 00:19:27 +04:00
|
|
|
|
2019-09-17 05:47:49 +03:00
|
|
|
Element* anonymousDivElement = GetRoot();
|
|
|
|
if (NS_WARN_IF(!anonymousDivElement)) {
|
2017-05-29 05:28:21 +03:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
2018-03-15 15:25:41 +03:00
|
|
|
nsINode* parentNode = selectionStartPoint.GetContainer()->GetParentNode();
|
2019-09-17 05:47:49 +03:00
|
|
|
if (parentNode != anonymousDivElement) {
|
2016-10-24 05:27:45 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-07-18 18:12:51 +04:00
|
|
|
|
2018-03-15 15:25:41 +03:00
|
|
|
nsINode* nextNode = selectionStartPoint.GetContainer()->GetNextSibling();
|
2019-08-02 08:45:18 +03:00
|
|
|
if (!nextNode || !EditorBase::IsPaddingBRElementForEmptyLastLine(*nextNode)) {
|
2018-03-15 15:25:41 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-03-18 04:14:42 +03:00
|
|
|
EditorRawDOMPoint afterStartContainer(
|
|
|
|
EditorRawDOMPoint::After(*selectionStartPoint.GetContainer()));
|
|
|
|
if (NS_WARN_IF(!afterStartContainer.IsSet())) {
|
2018-03-15 15:25:41 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2020-03-18 04:14:42 +03:00
|
|
|
IgnoredErrorResult ignoredError;
|
|
|
|
SelectionRefPtr()->Collapse(afterStartContainer, ignoredError);
|
2019-09-17 05:47:49 +03:00
|
|
|
if (NS_WARN_IF(Destroyed())) {
|
2018-05-11 12:40:47 +03:00
|
|
|
return NS_ERROR_EDITOR_DESTROYED;
|
|
|
|
}
|
2020-03-18 04:14:42 +03:00
|
|
|
NS_WARNING_ASSERTION(!ignoredError.Failed(), "Selection::Collapse() failed");
|
|
|
|
return ignoredError.StealNSResult();
|
Preparation for ender-based text control
* added focus listener. Doesn't do much yet, but when focus notifications start appearing, we'll be ready for them. The code is in
place to hide selection when we lose focus and paint selection when we get focus. That's probably not quite right, but it's a start.
We will need to be able to determine the distinction between losing focus to another control within our app, and losing focus to
another app.
* added support for disabled and readonly states in the editor. This is accomplished by having flags set by the client, and letting the
rules system deal with those flags. The flags I added are:
TEXT_EDITOR_FLAG_PLAINTEXT 0x01 // only plain text editing is allowed
TEXT_EDITOR_FLAG_SINGLELINE 0x02 // enter key and CR-LF handled specially
TEXT_EDITOR_FLAG_PASSWORD 0x04 // text is not entered into content, only a representative character
TEXT_EDITOR_FLAG_READONLY 0x08 // editing events are disabled. Editor may still accept focus.
TEXT_EDITOR_FLAG_DISALBED 0x10 // all events are disabled (like scrolling). Editor will not accept focus.
* added WillInsertBreak/DidInsertBreak into text rules, so flags could be checked. This gets us readonly, disabled, and single line
behavior.
* cleaned up the code that allocates, registers, and destroys event listeners. Thanks to Kin and Simon for cleaning up the
ownership model on the listeners, it was a big help.
* added support for a max text length. You can now tell the text editor, be no bigger than n characters.
1999-05-29 01:24:18 +04:00
|
|
|
}
|
|
|
|
|
2019-09-13 05:40:09 +03:00
|
|
|
void TextEditor::HandleNewLinesInStringForSingleLineEditor(
|
|
|
|
nsString& aString) const {
|
2018-07-24 11:46:12 +03:00
|
|
|
static const char16_t kLF = static_cast<char16_t>('\n');
|
2019-09-13 05:40:09 +03:00
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
|
|
|
MOZ_ASSERT(IsPlaintextEditor());
|
2018-07-24 11:46:12 +03:00
|
|
|
MOZ_ASSERT(aString.FindChar(static_cast<uint16_t>('\r')) == kNotFound);
|
|
|
|
|
|
|
|
// First of all, check if aString contains '\n' since if the string
|
|
|
|
// does not include it, we don't need to do nothing here.
|
|
|
|
int32_t firstLF = aString.FindChar(kLF, 0);
|
|
|
|
if (firstLF == kNotFound) {
|
|
|
|
return;
|
2010-02-01 21:12:31 +03:00
|
|
|
}
|
|
|
|
|
2019-09-13 05:40:09 +03:00
|
|
|
switch (mNewlineHandling) {
|
2020-01-24 11:33:42 +03:00
|
|
|
case nsIEditor::eNewlinesReplaceWithSpaces:
|
2018-07-24 11:46:12 +03:00
|
|
|
// Default of Firefox:
|
2016-10-24 05:27:45 +03:00
|
|
|
// Strip trailing newlines first so we don't wind up with trailing spaces
|
2018-07-24 11:46:12 +03:00
|
|
|
aString.Trim(LFSTR, false, true);
|
|
|
|
aString.ReplaceChar(kLF, ' ');
|
2016-10-24 05:27:45 +03:00
|
|
|
break;
|
2020-01-24 11:33:42 +03:00
|
|
|
case nsIEditor::eNewlinesStrip:
|
2018-07-24 11:46:12 +03:00
|
|
|
aString.StripChar(kLF);
|
2016-10-24 05:27:45 +03:00
|
|
|
break;
|
2020-01-24 11:33:42 +03:00
|
|
|
case nsIEditor::eNewlinesPasteToFirst:
|
2016-10-24 05:27:45 +03:00
|
|
|
default: {
|
2010-02-01 21:12:31 +03:00
|
|
|
// we get first *non-empty* line.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t offset = 0;
|
2018-07-24 11:46:12 +03:00
|
|
|
while (firstLF == offset) {
|
2010-02-01 21:12:31 +03:00
|
|
|
offset++;
|
2018-07-24 11:46:12 +03:00
|
|
|
firstLF = aString.FindChar(kLF, offset);
|
2010-02-01 21:12:31 +03:00
|
|
|
}
|
2018-07-24 11:46:12 +03:00
|
|
|
if (firstLF > 0) {
|
|
|
|
aString.Truncate(firstLF);
|
2016-10-24 05:27:45 +03:00
|
|
|
}
|
|
|
|
if (offset > 0) {
|
2010-02-01 21:12:31 +03:00
|
|
|
aString.Cut(0, offset);
|
2016-10-24 05:27:45 +03:00
|
|
|
}
|
|
|
|
break;
|
2010-02-01 21:12:31 +03:00
|
|
|
}
|
2020-01-24 11:33:42 +03:00
|
|
|
case nsIEditor::eNewlinesReplaceWithCommas:
|
2018-07-24 11:46:12 +03:00
|
|
|
// Default of Thunderbird:
|
|
|
|
aString.Trim(LFSTR, true, true);
|
|
|
|
aString.ReplaceChar(kLF, ',');
|
2016-10-24 05:27:45 +03:00
|
|
|
break;
|
2020-01-24 11:33:42 +03:00
|
|
|
case nsIEditor::eNewlinesStripSurroundingWhitespace: {
|
2016-10-24 05:27:45 +03:00
|
|
|
nsAutoString result;
|
2013-01-16 03:44:16 +04:00
|
|
|
uint32_t offset = 0;
|
2016-10-24 05:27:45 +03:00
|
|
|
while (offset < aString.Length()) {
|
2018-07-24 11:46:12 +03:00
|
|
|
int32_t nextLF = !offset ? firstLF : aString.FindChar(kLF, offset);
|
|
|
|
if (nextLF < 0) {
|
2013-01-16 03:44:16 +04:00
|
|
|
result.Append(nsDependentSubstring(aString, offset));
|
|
|
|
break;
|
|
|
|
}
|
2018-07-24 11:46:12 +03:00
|
|
|
uint32_t wsBegin = nextLF;
|
2010-02-01 21:12:31 +03:00
|
|
|
// look backwards for the first non-whitespace char
|
2016-10-24 05:27:45 +03:00
|
|
|
while (wsBegin > offset && NS_IS_SPACE(aString[wsBegin - 1])) {
|
2010-02-01 21:12:31 +03:00
|
|
|
--wsBegin;
|
2016-10-24 05:27:45 +03:00
|
|
|
}
|
2013-01-16 03:44:16 +04:00
|
|
|
result.Append(nsDependentSubstring(aString, offset, wsBegin - offset));
|
2018-07-24 11:46:12 +03:00
|
|
|
offset = nextLF + 1;
|
2016-10-24 05:27:45 +03:00
|
|
|
while (offset < aString.Length() && NS_IS_SPACE(aString[offset])) {
|
2013-01-16 03:44:16 +04:00
|
|
|
++offset;
|
2016-10-24 05:27:45 +03:00
|
|
|
}
|
2010-02-01 21:12:31 +03:00
|
|
|
}
|
2013-01-16 03:44:16 +04:00
|
|
|
aString = result;
|
2016-10-24 05:27:45 +03:00
|
|
|
break;
|
2010-02-01 21:12:31 +03:00
|
|
|
}
|
2020-01-24 11:33:42 +03:00
|
|
|
case nsIEditor::eNewlinesPasteIntact:
|
2016-10-24 05:27:45 +03:00
|
|
|
// even if we're pasting newlines, don't paste leading/trailing ones
|
2018-07-24 11:46:12 +03:00
|
|
|
aString.Trim(LFSTR, true, true);
|
2016-10-24 05:27:45 +03:00
|
|
|
break;
|
2010-02-01 21:12:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-13 05:40:09 +03:00
|
|
|
EditActionResult TextEditor::HandleInsertText(
|
|
|
|
EditSubAction aEditSubAction, const nsAString& aInsertionString) {
|
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
|
|
|
MOZ_ASSERT(aEditSubAction == EditSubAction::eInsertText ||
|
|
|
|
aEditSubAction == EditSubAction::eInsertTextComingFromIME);
|
2018-04-26 18:09:10 +03:00
|
|
|
|
2019-09-13 05:40:09 +03:00
|
|
|
UndefineCaretBidiLevel();
|
1999-06-08 10:04:51 +04:00
|
|
|
|
2019-09-13 05:40:09 +03:00
|
|
|
if (aInsertionString.IsEmpty() &&
|
2018-05-28 15:44:39 +03:00
|
|
|
aEditSubAction != EditSubAction::eInsertTextComingFromIME) {
|
2000-02-07 04:42:14 +03:00
|
|
|
// HACK: this is a fix for bug 19395
|
|
|
|
// I can't outlaw all empty insertions
|
|
|
|
// because IME transaction depend on them
|
2015-05-28 18:58:42 +03:00
|
|
|
// There is more work to do to make the
|
2000-02-07 04:42:14 +03:00
|
|
|
// world safe for IME.
|
2019-09-13 05:40:09 +03:00
|
|
|
return EditActionCanceled();
|
2000-02-07 04:42:14 +03:00
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2019-09-13 05:40:09 +03:00
|
|
|
nsAutoString insertionString(aInsertionString);
|
|
|
|
if (mMaxTextLength >= 0) {
|
2019-09-12 10:48:29 +03:00
|
|
|
EditActionResult result =
|
2019-09-13 05:40:09 +03:00
|
|
|
TruncateInsertionStringForMaxLength(insertionString);
|
2020-03-18 04:14:42 +03:00
|
|
|
if (result.Failed()) {
|
|
|
|
NS_WARNING("TextEditor::TruncateInsertionStringForMaxLength() failed");
|
2019-09-13 05:40:09 +03:00
|
|
|
return result.MarkAsHandled();
|
2019-09-12 10:48:29 +03:00
|
|
|
}
|
|
|
|
// If we're exceeding the maxlength when composing IME, we need to clean up
|
|
|
|
// the composing text, so we shouldn't return early.
|
2019-09-13 05:40:09 +03:00
|
|
|
if (result.Handled() && insertionString.IsEmpty() &&
|
2019-09-12 10:48:29 +03:00
|
|
|
aEditSubAction != EditSubAction::eInsertTextComingFromIME) {
|
2019-09-13 05:40:09 +03:00
|
|
|
return EditActionCanceled();
|
2019-09-12 10:48:29 +03:00
|
|
|
}
|
2010-09-24 23:10:53 +04:00
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2017-03-09 22:44:45 +03:00
|
|
|
uint32_t start = 0;
|
2012-06-11 03:44:50 +04:00
|
|
|
if (IsPasswordEditor()) {
|
2019-09-13 05:40:09 +03:00
|
|
|
if (GetComposition() && !GetComposition()->String().IsEmpty()) {
|
|
|
|
start = GetComposition()->XPOffsetInTextNode();
|
2019-07-22 06:53:58 +03:00
|
|
|
} else {
|
|
|
|
uint32_t end = 0;
|
2019-09-13 05:40:09 +03:00
|
|
|
nsContentUtils::GetSelectionInTextControl(SelectionRefPtr(), GetRoot(),
|
|
|
|
start, end);
|
2019-07-22 06:53:58 +03:00
|
|
|
}
|
2000-03-29 17:45:08 +04:00
|
|
|
}
|
2000-02-07 05:48:36 +03:00
|
|
|
|
1999-11-29 11:28:46 +03:00
|
|
|
// if the selection isn't collapsed, delete it.
|
2018-10-30 13:02:58 +03:00
|
|
|
if (!SelectionRefPtr()->IsCollapsed()) {
|
2019-09-12 10:48:29 +03:00
|
|
|
nsresult rv =
|
2019-09-13 05:40:09 +03:00
|
|
|
DeleteSelectionAsSubAction(nsIEditor::eNone, nsIEditor::eStrip);
|
2020-03-18 04:14:42 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING(
|
|
|
|
"TextEditor::DeleteSelectionAsSubAction(eNone, eStrip) failed");
|
2019-09-13 05:40:09 +03:00
|
|
|
return EditActionHandled(rv);
|
2018-04-26 18:09:10 +03:00
|
|
|
}
|
1999-11-29 11:28:46 +03:00
|
|
|
}
|
|
|
|
|
2019-09-13 05:40:09 +03:00
|
|
|
// XXX Why don't we cancel here? Shouldn't we do this first?
|
2020-03-16 23:32:41 +03:00
|
|
|
CANCEL_OPERATION_AND_RETURN_EDIT_ACTION_RESULT_IF_READONLY_OF_DISABLED
|
2019-08-09 11:25:37 +03:00
|
|
|
|
2019-09-13 05:40:09 +03:00
|
|
|
MaybeDoAutoPasswordMasking();
|
2019-08-09 11:25:37 +03:00
|
|
|
|
2019-09-13 05:40:09 +03:00
|
|
|
nsresult rv = EnsureNoPaddingBRElementForEmptyEditor();
|
2020-03-18 04:14:42 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("EditorBase::EnsureNoPaddingBRElementForEmptyEditor() failed");
|
2019-09-13 05:40:09 +03:00
|
|
|
return EditActionHandled(rv);
|
2018-05-11 13:06:07 +03:00
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2000-09-02 01:12:43 +04:00
|
|
|
// People have lots of different ideas about what text fields
|
|
|
|
// should do with multiline pastes. See bugs 21032, 23485, 23485, 50935.
|
2005-12-20 23:12:54 +03:00
|
|
|
// The six possible options are:
|
2000-09-02 01:12:43 +04:00
|
|
|
// 0. paste newlines intact
|
2005-12-20 23:12:54 +03:00
|
|
|
// 1. paste up to the first newline (default)
|
2000-09-02 01:12:43 +04:00
|
|
|
// 2. replace newlines with spaces
|
|
|
|
// 3. strip newlines
|
2004-01-29 19:55:01 +03:00
|
|
|
// 4. replace with commas
|
2005-12-20 23:12:54 +03:00
|
|
|
// 5. strip newlines and surrounding whitespace
|
2000-09-02 01:12:43 +04:00
|
|
|
// So find out what we're expected to do:
|
2016-10-24 05:27:45 +03:00
|
|
|
if (IsSingleLineEditor()) {
|
2018-07-24 11:46:12 +03:00
|
|
|
// XXX Some callers of TextEditor::InsertTextAsAction() already make the
|
|
|
|
// string use only \n as a linebreaker. However, they are not hot
|
|
|
|
// path and nsContentUtils::PlatformToDOMLineBreaks() does nothing
|
|
|
|
// if the string doesn't include \r. So, let's convert linebreakers
|
|
|
|
// here. Note that there are too many callers of
|
|
|
|
// TextEditor::InsertTextAsAction(). So, it's difficult to keep
|
|
|
|
// maintaining all of them won't reach here without \r nor \r\n.
|
2019-09-13 05:40:09 +03:00
|
|
|
// XXX Should we handle do this before truncating the string for
|
|
|
|
// `maxlength`?
|
|
|
|
nsContentUtils::PlatformToDOMLineBreaks(insertionString);
|
|
|
|
HandleNewLinesInStringForSingleLineEditor(insertionString);
|
2001-12-11 10:21:10 +03:00
|
|
|
}
|
|
|
|
|
2000-03-29 16:53:23 +04:00
|
|
|
// get the (collapsed) selection location
|
2018-10-30 13:02:58 +03:00
|
|
|
nsRange* firstRange = SelectionRefPtr()->GetRangeAt(0);
|
2018-05-08 11:30:05 +03:00
|
|
|
if (NS_WARN_IF(!firstRange)) {
|
2019-09-13 05:40:09 +03:00
|
|
|
return EditActionHandled(NS_ERROR_FAILURE);
|
2018-05-08 11:30:05 +03:00
|
|
|
}
|
|
|
|
EditorRawDOMPoint atStartOfSelection(firstRange->StartRef());
|
2017-11-08 15:55:10 +03:00
|
|
|
if (NS_WARN_IF(!atStartOfSelection.IsSetAndValid())) {
|
2019-09-13 05:40:09 +03:00
|
|
|
return EditActionHandled(NS_ERROR_FAILURE);
|
2017-11-08 15:55:10 +03:00
|
|
|
}
|
2000-02-07 04:42:14 +03:00
|
|
|
|
2003-07-18 18:12:51 +04:00
|
|
|
// don't put text in places that can't have it
|
2017-12-07 12:45:52 +03:00
|
|
|
if (!atStartOfSelection.IsInTextNode() &&
|
2019-09-13 05:40:09 +03:00
|
|
|
!CanContainTag(*atStartOfSelection.GetContainer(),
|
|
|
|
*nsGkAtoms::textTagName)) {
|
2020-03-18 04:14:42 +03:00
|
|
|
NS_WARNING("Selection start container couldn't have text nodes");
|
2019-09-13 05:40:09 +03:00
|
|
|
return EditActionHandled(NS_ERROR_FAILURE);
|
2012-05-01 10:34:52 +04:00
|
|
|
}
|
2000-01-15 17:29:29 +03:00
|
|
|
|
2019-09-13 05:40:09 +03:00
|
|
|
RefPtr<Document> document = GetDocument();
|
|
|
|
if (NS_WARN_IF(!document)) {
|
|
|
|
return EditActionHandled(NS_ERROR_NOT_INITIALIZED);
|
2018-04-26 18:09:10 +03:00
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2018-05-28 15:44:39 +03:00
|
|
|
if (aEditSubAction == EditSubAction::eInsertTextComingFromIME) {
|
2019-09-13 05:40:09 +03:00
|
|
|
EditorRawDOMPoint compositionStartPoint = GetCompositionStartPoint();
|
Bug 1530649 - Improve composition string handling which ends with whitespaces r=m_kato
If insertion string ends with ASCII whitespace and there is no following
content in the block, `HTMLEditRules::AdjustWhitespaces()` needs to insert
`<br>` element. It's called only by `HTMLEditRules::AfterEditInner()` and
that does only simple things with `WSRunObject`. Therefore, this moves the
code into `AfterEditInner()`.
For making it adjust the whitespaces, `HTMLEditRules::WillInsertText()` needs
to notify `AfterEditInner()` of dirty range with `mDocChangeRange`. Therefore,
this patch makes it set `mDocChangeRange` manually after inserting composition
string.
On the other hand, there is another bug. `WSRunObject` was designed to treat
only inserting text for `WSRunObject::InsertText()`. I.e., not designed to
treat replacing existing composition string with new string. Therefore,
`WSRunObject::InsertText()` adjusts whitespaces only around start of
composition string. Therefore, if composition string ends with an ASCII
whitespace, it's not replaced with NBSP and that causes:
- failing `WSRunObject::AdjustWhitespaces()` inserts `<br>` element at
`AfterEditInner()` of committing composition.
- then, next composition's first `WSRunObject::InsertText()` removes the
last whitespace due to not followed by `<br>` nor any other content.
Therefore, this patch makes `WSRunObject` takes 2 DOM points to be able to
treat replaced range.
In strictly speaking, the latter change require more changes and tests for
supporting replacement with any other methods. However, it's risky and out
of scope of this bug.
Differential Revision: https://phabricator.services.mozilla.com/D26423
--HG--
extra : moz-landing-system : lando
2019-04-09 08:28:38 +03:00
|
|
|
if (!compositionStartPoint.IsSet()) {
|
2019-09-13 05:40:09 +03:00
|
|
|
compositionStartPoint = FindBetterInsertionPoint(atStartOfSelection);
|
2020-03-18 04:14:42 +03:00
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
compositionStartPoint.IsSet(),
|
|
|
|
"EditorBase::FindBetterInsertionPoint() failed, but ignored");
|
2015-06-04 20:06:09 +03:00
|
|
|
}
|
2019-09-13 05:40:09 +03:00
|
|
|
nsresult rv = InsertTextWithTransaction(*document, insertionString,
|
|
|
|
compositionStartPoint);
|
|
|
|
if (NS_WARN_IF(Destroyed())) {
|
|
|
|
return EditActionHandled(NS_ERROR_EDITOR_DESTROYED);
|
2018-05-11 12:29:54 +03:00
|
|
|
}
|
2020-03-18 04:14:42 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("EditorBase::InsertTextWithTransaction() failed");
|
2019-09-13 05:40:09 +03:00
|
|
|
return EditActionHandled(rv);
|
2018-04-26 18:09:10 +03:00
|
|
|
}
|
2012-05-05 22:52:29 +04:00
|
|
|
} else {
|
2019-09-13 05:40:09 +03:00
|
|
|
MOZ_ASSERT(aEditSubAction == EditSubAction::eInsertText);
|
2003-07-18 18:12:51 +04:00
|
|
|
|
2017-07-31 18:20:08 +03:00
|
|
|
// don't change my selection in subtransactions
|
2019-09-13 05:40:09 +03:00
|
|
|
AutoTransactionsConserveSelection dontChangeMySelection(*this);
|
2010-07-12 00:26:26 +04:00
|
|
|
|
2017-11-08 19:00:36 +03:00
|
|
|
EditorRawDOMPoint pointAfterStringInserted;
|
2019-09-13 05:40:09 +03:00
|
|
|
nsresult rv = InsertTextWithTransaction(*document, insertionString,
|
|
|
|
atStartOfSelection,
|
|
|
|
&pointAfterStringInserted);
|
|
|
|
if (NS_WARN_IF(Destroyed())) {
|
|
|
|
return EditActionHandled(NS_ERROR_EDITOR_DESTROYED);
|
2018-05-11 12:29:54 +03:00
|
|
|
}
|
2020-03-18 04:14:42 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("EditorBase::InsertTextWithTransaction() failed");
|
2019-09-13 05:40:09 +03:00
|
|
|
return EditActionHandled(rv);
|
2018-04-12 11:58:14 +03:00
|
|
|
}
|
2001-04-07 04:45:26 +04:00
|
|
|
|
2017-11-08 19:00:36 +03:00
|
|
|
if (pointAfterStringInserted.IsSet()) {
|
2015-05-28 18:58:42 +03:00
|
|
|
// Make the caret attach to the inserted text, unless this text ends with
|
2005-10-06 12:19:51 +04:00
|
|
|
// a LF, in which case make the caret attach to the next line.
|
2019-09-13 05:40:09 +03:00
|
|
|
bool endsWithLF =
|
|
|
|
!insertionString.IsEmpty() && insertionString.Last() == nsCRT::LF;
|
|
|
|
IgnoredErrorResult ignoredError;
|
|
|
|
SelectionRefPtr()->SetInterlinePosition(endsWithLF, ignoredError);
|
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
!ignoredError.Failed(),
|
|
|
|
"Selection::SetInterlinePosition() failed, but ignored");
|
2010-07-12 00:26:26 +04:00
|
|
|
|
2017-12-07 13:08:56 +03:00
|
|
|
MOZ_ASSERT(
|
|
|
|
!pointAfterStringInserted.GetChild(),
|
2017-11-08 19:00:36 +03:00
|
|
|
"After inserting text into a text node, pointAfterStringInserted."
|
2017-12-07 13:08:56 +03:00
|
|
|
"GetChild() should be nullptr");
|
2019-09-13 05:40:09 +03:00
|
|
|
ignoredError = IgnoredErrorResult();
|
|
|
|
SelectionRefPtr()->Collapse(pointAfterStringInserted, ignoredError);
|
|
|
|
if (NS_WARN_IF(Destroyed())) {
|
|
|
|
return EditActionHandled(NS_ERROR_EDITOR_DESTROYED);
|
2018-05-11 12:29:54 +03:00
|
|
|
}
|
2019-09-13 05:40:09 +03:00
|
|
|
NS_WARNING_ASSERTION(!ignoredError.Failed(),
|
|
|
|
"Selection::Collapse() failed, but ignored");
|
2005-10-06 12:19:51 +04:00
|
|
|
}
|
As a reminder, we decided to do this based strictly content. Some support for style-based text properties is written, but not used
anywhere any more.
* Cleaned up split and join undo/redo.
* Added TypeInState, a data struct that remembers things about text properties for collapsed selections, so you can type
* Ctrl-B with an insertion point and the next character will be bold.
* Added all the logic to handle inline vs. block elements when setting text properties.
* Added some support for italic and underline as well. Adding these things is pretty easy now. Ctrl-B, Ctrl-I, Ctrl-U for testing bold, italic, underline.
* Added all the logic to make sure we only add style tags where they're needed, so you should never get the same style tag nested within itself, except as needed for block elements.
* Added methods for testing a node to see if a particular style is set. This isn't 100% done yet, but with very little work we could have toolbar buttons that respond to selection changed notification that show the state of bold, italic, underline, etc. in real time. Supports tri-state: whole selection is bold, some of selection is bold, none of selection is bold, ...
* Fully undoable and redoable.
* Added some debug printfs to transactions and editors. all controlled by a gNoisy static in each module. helps me track down undo/redo problems. if the output bugs people enough, I'll shut it off and re-enable it in my local tree.
Noticably missing: make un-bold, make un-italic, etc. This is coming soon.
1999-04-01 21:58:07 +04:00
|
|
|
}
|
2019-07-22 06:53:58 +03:00
|
|
|
|
|
|
|
// Unmask inputted character(s) if necessary.
|
2019-09-13 05:40:09 +03:00
|
|
|
if (IsPasswordEditor() && IsMaskingPassword() && CanEchoPasswordNow()) {
|
|
|
|
nsresult rv = SetUnmaskRangeAndNotify(start, insertionString.Length(),
|
|
|
|
LookAndFeel::GetPasswordMaskDelay());
|
2020-03-18 04:14:42 +03:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"TextEditor::SetUnmaskRangeAndNotify() failed");
|
2019-09-13 05:40:09 +03:00
|
|
|
return EditActionHandled(rv);
|
2019-07-22 06:53:58 +03:00
|
|
|
}
|
|
|
|
|
2019-09-13 05:40:09 +03:00
|
|
|
return EditActionHandled();
|
As a reminder, we decided to do this based strictly content. Some support for style-based text properties is written, but not used
anywhere any more.
* Cleaned up split and join undo/redo.
* Added TypeInState, a data struct that remembers things about text properties for collapsed selections, so you can type
* Ctrl-B with an insertion point and the next character will be bold.
* Added all the logic to handle inline vs. block elements when setting text properties.
* Added some support for italic and underline as well. Adding these things is pretty easy now. Ctrl-B, Ctrl-I, Ctrl-U for testing bold, italic, underline.
* Added all the logic to make sure we only add style tags where they're needed, so you should never get the same style tag nested within itself, except as needed for block elements.
* Added methods for testing a node to see if a particular style is set. This isn't 100% done yet, but with very little work we could have toolbar buttons that respond to selection changed notification that show the state of bold, italic, underline, etc. in real time. Supports tri-state: whole selection is bold, some of selection is bold, none of selection is bold, ...
* Fully undoable and redoable.
* Added some debug printfs to transactions and editors. all controlled by a gNoisy static in each module. helps me track down undo/redo problems. if the output bugs people enough, I'll shut it off and re-enable it in my local tree.
Noticably missing: make un-bold, make un-italic, etc. This is coming soon.
1999-04-01 21:58:07 +04:00
|
|
|
}
|
|
|
|
|
2019-09-17 05:21:35 +03:00
|
|
|
EditActionResult TextEditor::SetTextWithoutTransaction(
|
|
|
|
const nsAString& aValue) {
|
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
|
|
|
MOZ_ASSERT(!AsHTMLEditor());
|
|
|
|
MOZ_ASSERT(IsPlaintextEditor());
|
|
|
|
MOZ_ASSERT(!IsIMEComposing());
|
|
|
|
MOZ_ASSERT(!IsUndoRedoEnabled());
|
|
|
|
MOZ_ASSERT(GetEditAction() != EditAction::eReplaceText);
|
|
|
|
MOZ_ASSERT(mMaxTextLength < 0);
|
|
|
|
MOZ_ASSERT(aValue.FindChar(static_cast<char16_t>('\r')) == kNotFound);
|
2017-05-25 08:30:50 +03:00
|
|
|
|
2019-09-17 05:21:35 +03:00
|
|
|
UndefineCaretBidiLevel();
|
2019-08-09 11:25:37 +03:00
|
|
|
|
2019-09-17 05:21:35 +03:00
|
|
|
// XXX If we're setting value, shouldn't we keep setting the new value here?
|
2020-03-16 23:32:41 +03:00
|
|
|
CANCEL_OPERATION_AND_RETURN_EDIT_ACTION_RESULT_IF_READONLY_OF_DISABLED
|
2019-09-15 02:32:09 +03:00
|
|
|
|
2019-09-17 05:21:35 +03:00
|
|
|
MaybeDoAutoPasswordMasking();
|
2019-09-15 02:32:09 +03:00
|
|
|
|
2019-09-17 05:21:35 +03:00
|
|
|
nsresult rv = EnsureNoPaddingBRElementForEmptyEditor();
|
2020-03-18 04:14:42 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("EditorBase::EnsureNoPaddingBRElementForEmptyEditor() failed");
|
2019-09-17 05:21:35 +03:00
|
|
|
return EditActionResult(rv);
|
2018-05-11 13:06:07 +03:00
|
|
|
}
|
2017-05-25 08:30:50 +03:00
|
|
|
|
2019-09-17 05:21:35 +03:00
|
|
|
RefPtr<Element> anonymousDivElement = GetRoot();
|
|
|
|
nsIContent* firstChild = anonymousDivElement->GetFirstChild();
|
2017-05-25 08:30:50 +03:00
|
|
|
|
Bug 1548751 - Make TextEditRules::WillSetText() use fast path even if it's for <textarea> element r=m_kato
As far as I've tested, `TextEditor` has the following structure patterns:
1. If it's for a non-empty `<input>` element, it has only one text node.
2. If it's for an empty `<input>` element, it has only bogus node.
3. If it's for a non-empty `<textarea>` element, it has a text node and
`moz-<br>` element. Additionally they are followed by `<scrollbar>` and
`<resizer>` elements.
4. If it's for an empty `<textarea>` element, it has a `moz-<br>` element
followed by `<scrollbar>` and `<resizer>` elements.
Additionally, `TextEditRules::WillInsert()` always removes bogus node if
there is. So, in the case #2, there is no children.
Fortunately, we don't support XUL addons anymore on Firefox. However, in
other products like Thunderbird, the tree may be changed as unexpected.
Therefore, we still need to keep checking the tree, but we can use the fast
path for `<textarea>` element too.
Differential Revision: https://phabricator.services.mozilla.com/D30012
--HG--
extra : moz-landing-system : lando
2019-05-07 08:07:14 +03:00
|
|
|
// We can use this fast path only when:
|
|
|
|
// - we need to insert a text node.
|
|
|
|
// - we need to replace content of existing text node.
|
|
|
|
// Additionally, for avoiding odd result, we should check whether we're in
|
|
|
|
// usual condition.
|
|
|
|
if (IsSingleLineEditor()) {
|
2019-08-02 08:44:40 +03:00
|
|
|
// If we're a single line text editor, i.e., <input>, there is only padding
|
Bug 1548751 - Make TextEditRules::WillSetText() use fast path even if it's for <textarea> element r=m_kato
As far as I've tested, `TextEditor` has the following structure patterns:
1. If it's for a non-empty `<input>` element, it has only one text node.
2. If it's for an empty `<input>` element, it has only bogus node.
3. If it's for a non-empty `<textarea>` element, it has a text node and
`moz-<br>` element. Additionally they are followed by `<scrollbar>` and
`<resizer>` elements.
4. If it's for an empty `<textarea>` element, it has a `moz-<br>` element
followed by `<scrollbar>` and `<resizer>` elements.
Additionally, `TextEditRules::WillInsert()` always removes bogus node if
there is. So, in the case #2, there is no children.
Fortunately, we don't support XUL addons anymore on Firefox. However, in
other products like Thunderbird, the tree may be changed as unexpected.
Therefore, we still need to keep checking the tree, but we can use the fast
path for `<textarea>` element too.
Differential Revision: https://phabricator.services.mozilla.com/D30012
--HG--
extra : moz-landing-system : lando
2019-05-07 08:07:14 +03:00
|
|
|
// <br> element. Otherwise, there should be only one text node. But note
|
2019-08-02 08:44:40 +03:00
|
|
|
// that even if there is a padding <br> element for empty editor, it's
|
2019-08-09 11:25:37 +03:00
|
|
|
// already been removed by `EnsureNoPaddingBRElementForEmptyEditor()`. So,
|
|
|
|
// at here, there should be only one text node or no children.
|
2019-09-17 05:21:35 +03:00
|
|
|
if (firstChild && (!firstChild->IsText() || firstChild->GetNextSibling())) {
|
|
|
|
return EditActionIgnored();
|
Bug 1548751 - Make TextEditRules::WillSetText() use fast path even if it's for <textarea> element r=m_kato
As far as I've tested, `TextEditor` has the following structure patterns:
1. If it's for a non-empty `<input>` element, it has only one text node.
2. If it's for an empty `<input>` element, it has only bogus node.
3. If it's for a non-empty `<textarea>` element, it has a text node and
`moz-<br>` element. Additionally they are followed by `<scrollbar>` and
`<resizer>` elements.
4. If it's for an empty `<textarea>` element, it has a `moz-<br>` element
followed by `<scrollbar>` and `<resizer>` elements.
Additionally, `TextEditRules::WillInsert()` always removes bogus node if
there is. So, in the case #2, there is no children.
Fortunately, we don't support XUL addons anymore on Firefox. However, in
other products like Thunderbird, the tree may be changed as unexpected.
Therefore, we still need to keep checking the tree, but we can use the fast
path for `<textarea>` element too.
Differential Revision: https://phabricator.services.mozilla.com/D30012
--HG--
extra : moz-landing-system : lando
2019-05-07 08:07:14 +03:00
|
|
|
}
|
|
|
|
} else {
|
2019-08-02 08:45:18 +03:00
|
|
|
// If we're a multiline text editor, i.e., <textarea>, there is a padding
|
|
|
|
// <br> element for empty last line followed by scrollbar/resizer elements.
|
|
|
|
// Otherwise, a text node is followed by them.
|
Bug 1548751 - Make TextEditRules::WillSetText() use fast path even if it's for <textarea> element r=m_kato
As far as I've tested, `TextEditor` has the following structure patterns:
1. If it's for a non-empty `<input>` element, it has only one text node.
2. If it's for an empty `<input>` element, it has only bogus node.
3. If it's for a non-empty `<textarea>` element, it has a text node and
`moz-<br>` element. Additionally they are followed by `<scrollbar>` and
`<resizer>` elements.
4. If it's for an empty `<textarea>` element, it has a `moz-<br>` element
followed by `<scrollbar>` and `<resizer>` elements.
Additionally, `TextEditRules::WillInsert()` always removes bogus node if
there is. So, in the case #2, there is no children.
Fortunately, we don't support XUL addons anymore on Firefox. However, in
other products like Thunderbird, the tree may be changed as unexpected.
Therefore, we still need to keep checking the tree, but we can use the fast
path for `<textarea>` element too.
Differential Revision: https://phabricator.services.mozilla.com/D30012
--HG--
extra : moz-landing-system : lando
2019-05-07 08:07:14 +03:00
|
|
|
if (!firstChild) {
|
2019-09-17 05:21:35 +03:00
|
|
|
return EditActionIgnored();
|
Bug 1548751 - Make TextEditRules::WillSetText() use fast path even if it's for <textarea> element r=m_kato
As far as I've tested, `TextEditor` has the following structure patterns:
1. If it's for a non-empty `<input>` element, it has only one text node.
2. If it's for an empty `<input>` element, it has only bogus node.
3. If it's for a non-empty `<textarea>` element, it has a text node and
`moz-<br>` element. Additionally they are followed by `<scrollbar>` and
`<resizer>` elements.
4. If it's for an empty `<textarea>` element, it has a `moz-<br>` element
followed by `<scrollbar>` and `<resizer>` elements.
Additionally, `TextEditRules::WillInsert()` always removes bogus node if
there is. So, in the case #2, there is no children.
Fortunately, we don't support XUL addons anymore on Firefox. However, in
other products like Thunderbird, the tree may be changed as unexpected.
Therefore, we still need to keep checking the tree, but we can use the fast
path for `<textarea>` element too.
Differential Revision: https://phabricator.services.mozilla.com/D30012
--HG--
extra : moz-landing-system : lando
2019-05-07 08:07:14 +03:00
|
|
|
}
|
2019-09-17 05:21:35 +03:00
|
|
|
if (firstChild->IsText()) {
|
Bug 1548751 - Make TextEditRules::WillSetText() use fast path even if it's for <textarea> element r=m_kato
As far as I've tested, `TextEditor` has the following structure patterns:
1. If it's for a non-empty `<input>` element, it has only one text node.
2. If it's for an empty `<input>` element, it has only bogus node.
3. If it's for a non-empty `<textarea>` element, it has a text node and
`moz-<br>` element. Additionally they are followed by `<scrollbar>` and
`<resizer>` elements.
4. If it's for an empty `<textarea>` element, it has a `moz-<br>` element
followed by `<scrollbar>` and `<resizer>` elements.
Additionally, `TextEditRules::WillInsert()` always removes bogus node if
there is. So, in the case #2, there is no children.
Fortunately, we don't support XUL addons anymore on Firefox. However, in
other products like Thunderbird, the tree may be changed as unexpected.
Therefore, we still need to keep checking the tree, but we can use the fast
path for `<textarea>` element too.
Differential Revision: https://phabricator.services.mozilla.com/D30012
--HG--
extra : moz-landing-system : lando
2019-05-07 08:07:14 +03:00
|
|
|
if (!firstChild->GetNextSibling() ||
|
2019-08-02 08:45:18 +03:00
|
|
|
!EditorBase::IsPaddingBRElementForEmptyLastLine(
|
|
|
|
*firstChild->GetNextSibling())) {
|
2019-09-17 05:21:35 +03:00
|
|
|
return EditActionIgnored();
|
Bug 1548751 - Make TextEditRules::WillSetText() use fast path even if it's for <textarea> element r=m_kato
As far as I've tested, `TextEditor` has the following structure patterns:
1. If it's for a non-empty `<input>` element, it has only one text node.
2. If it's for an empty `<input>` element, it has only bogus node.
3. If it's for a non-empty `<textarea>` element, it has a text node and
`moz-<br>` element. Additionally they are followed by `<scrollbar>` and
`<resizer>` elements.
4. If it's for an empty `<textarea>` element, it has a `moz-<br>` element
followed by `<scrollbar>` and `<resizer>` elements.
Additionally, `TextEditRules::WillInsert()` always removes bogus node if
there is. So, in the case #2, there is no children.
Fortunately, we don't support XUL addons anymore on Firefox. However, in
other products like Thunderbird, the tree may be changed as unexpected.
Therefore, we still need to keep checking the tree, but we can use the fast
path for `<textarea>` element too.
Differential Revision: https://phabricator.services.mozilla.com/D30012
--HG--
extra : moz-landing-system : lando
2019-05-07 08:07:14 +03:00
|
|
|
}
|
2019-08-02 08:45:18 +03:00
|
|
|
} else if (!EditorBase::IsPaddingBRElementForEmptyLastLine(*firstChild)) {
|
2019-09-17 05:21:35 +03:00
|
|
|
return EditActionIgnored();
|
Bug 1548751 - Make TextEditRules::WillSetText() use fast path even if it's for <textarea> element r=m_kato
As far as I've tested, `TextEditor` has the following structure patterns:
1. If it's for a non-empty `<input>` element, it has only one text node.
2. If it's for an empty `<input>` element, it has only bogus node.
3. If it's for a non-empty `<textarea>` element, it has a text node and
`moz-<br>` element. Additionally they are followed by `<scrollbar>` and
`<resizer>` elements.
4. If it's for an empty `<textarea>` element, it has a `moz-<br>` element
followed by `<scrollbar>` and `<resizer>` elements.
Additionally, `TextEditRules::WillInsert()` always removes bogus node if
there is. So, in the case #2, there is no children.
Fortunately, we don't support XUL addons anymore on Firefox. However, in
other products like Thunderbird, the tree may be changed as unexpected.
Therefore, we still need to keep checking the tree, but we can use the fast
path for `<textarea>` element too.
Differential Revision: https://phabricator.services.mozilla.com/D30012
--HG--
extra : moz-landing-system : lando
2019-05-07 08:07:14 +03:00
|
|
|
}
|
2017-05-25 08:30:50 +03:00
|
|
|
}
|
|
|
|
|
2019-07-22 06:53:58 +03:00
|
|
|
// XXX Password fields accept line breaks as normal characters with this code.
|
|
|
|
// Is this intentional?
|
2019-09-17 05:21:35 +03:00
|
|
|
nsAutoString sanitizedValue(aValue);
|
2019-07-22 06:53:58 +03:00
|
|
|
if (IsSingleLineEditor() && !IsPasswordEditor()) {
|
2019-09-17 05:21:35 +03:00
|
|
|
HandleNewLinesInStringForSingleLineEditor(sanitizedValue);
|
2017-05-25 08:30:50 +03:00
|
|
|
}
|
|
|
|
|
2019-09-17 05:21:35 +03:00
|
|
|
if (!firstChild || !firstChild->IsText()) {
|
|
|
|
if (sanitizedValue.IsEmpty()) {
|
|
|
|
return EditActionHandled();
|
2017-05-25 08:30:50 +03:00
|
|
|
}
|
2019-09-17 05:21:35 +03:00
|
|
|
RefPtr<Document> document = GetDocument();
|
|
|
|
if (NS_WARN_IF(!document)) {
|
|
|
|
return EditActionIgnored();
|
2017-05-25 08:30:50 +03:00
|
|
|
}
|
2019-09-17 05:21:35 +03:00
|
|
|
RefPtr<nsTextNode> newTextNode = CreateTextNode(sanitizedValue);
|
2020-03-18 04:14:42 +03:00
|
|
|
if (!newTextNode) {
|
|
|
|
NS_WARNING("EditorBase::CreateTextNode() failed");
|
2019-09-17 05:21:35 +03:00
|
|
|
return EditActionIgnored();
|
2017-05-25 08:30:50 +03:00
|
|
|
}
|
2019-09-17 05:21:35 +03:00
|
|
|
nsresult rv = InsertNodeWithTransaction(
|
|
|
|
*newTextNode, EditorDOMPoint(anonymousDivElement, 0));
|
|
|
|
if (NS_WARN_IF(Destroyed())) {
|
|
|
|
return EditActionResult(NS_ERROR_EDITOR_DESTROYED);
|
2018-05-11 11:33:55 +03:00
|
|
|
}
|
2020-03-18 04:14:42 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("EditorBase::InsertNodeWithTransaction() failed");
|
2019-09-17 05:21:35 +03:00
|
|
|
return EditActionResult(rv);
|
2017-05-25 08:30:50 +03:00
|
|
|
}
|
2019-09-17 05:21:35 +03:00
|
|
|
return EditActionHandled();
|
2017-05-25 08:30:50 +03:00
|
|
|
}
|
|
|
|
|
2019-09-17 05:21:35 +03:00
|
|
|
// TODO: If new value is empty string, we should only remove it.
|
Bug 1548751 - Make TextEditRules::WillSetText() use fast path even if it's for <textarea> element r=m_kato
As far as I've tested, `TextEditor` has the following structure patterns:
1. If it's for a non-empty `<input>` element, it has only one text node.
2. If it's for an empty `<input>` element, it has only bogus node.
3. If it's for a non-empty `<textarea>` element, it has a text node and
`moz-<br>` element. Additionally they are followed by `<scrollbar>` and
`<resizer>` elements.
4. If it's for an empty `<textarea>` element, it has a `moz-<br>` element
followed by `<scrollbar>` and `<resizer>` elements.
Additionally, `TextEditRules::WillInsert()` always removes bogus node if
there is. So, in the case #2, there is no children.
Fortunately, we don't support XUL addons anymore on Firefox. However, in
other products like Thunderbird, the tree may be changed as unexpected.
Therefore, we still need to keep checking the tree, but we can use the fast
path for `<textarea>` element too.
Differential Revision: https://phabricator.services.mozilla.com/D30012
--HG--
extra : moz-landing-system : lando
2019-05-07 08:07:14 +03:00
|
|
|
RefPtr<Text> textNode = firstChild->GetAsText();
|
2020-03-18 04:14:42 +03:00
|
|
|
if (MOZ_UNLIKELY(!textNode)) {
|
|
|
|
NS_WARNING("The first child was not a text node");
|
2019-09-17 05:21:35 +03:00
|
|
|
return EditActionIgnored();
|
2018-05-11 11:33:55 +03:00
|
|
|
}
|
2019-09-17 05:21:35 +03:00
|
|
|
rv = SetTextNodeWithoutTransaction(sanitizedValue, *textNode);
|
2020-03-18 04:14:42 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("EditorBase::SetTextNodeWithoutTransaction() failed");
|
2019-09-17 05:21:35 +03:00
|
|
|
return EditActionResult(rv);
|
2017-05-25 08:30:50 +03:00
|
|
|
}
|
|
|
|
|
Bug 1548751 - Make TextEditRules::WillSetText() use fast path even if it's for <textarea> element r=m_kato
As far as I've tested, `TextEditor` has the following structure patterns:
1. If it's for a non-empty `<input>` element, it has only one text node.
2. If it's for an empty `<input>` element, it has only bogus node.
3. If it's for a non-empty `<textarea>` element, it has a text node and
`moz-<br>` element. Additionally they are followed by `<scrollbar>` and
`<resizer>` elements.
4. If it's for an empty `<textarea>` element, it has a `moz-<br>` element
followed by `<scrollbar>` and `<resizer>` elements.
Additionally, `TextEditRules::WillInsert()` always removes bogus node if
there is. So, in the case #2, there is no children.
Fortunately, we don't support XUL addons anymore on Firefox. However, in
other products like Thunderbird, the tree may be changed as unexpected.
Therefore, we still need to keep checking the tree, but we can use the fast
path for `<textarea>` element too.
Differential Revision: https://phabricator.services.mozilla.com/D30012
--HG--
extra : moz-landing-system : lando
2019-05-07 08:07:14 +03:00
|
|
|
// If we replaced non-empty value with empty string, we need to delete the
|
|
|
|
// text node.
|
2019-09-17 05:21:35 +03:00
|
|
|
if (sanitizedValue.IsEmpty() && !textNode->Length()) {
|
|
|
|
nsresult rv = DeleteNodeWithTransaction(*textNode);
|
2019-09-12 09:51:26 +03:00
|
|
|
if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
|
2019-09-17 05:21:35 +03:00
|
|
|
return EditActionResult(NS_ERROR_EDITOR_DESTROYED);
|
2019-09-12 09:51:26 +03:00
|
|
|
}
|
2020-03-18 04:14:42 +03:00
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
NS_SUCCEEDED(rv),
|
|
|
|
"EditorBase::DeleteNodeWithTransaction() failed, but ignored");
|
2019-09-12 09:51:26 +03:00
|
|
|
// XXX I don't think this is necessary because the anonymous `<div>`
|
|
|
|
// element has now only padding `<br>` element even if there are
|
|
|
|
// something.
|
|
|
|
IgnoredErrorResult ignoredError;
|
|
|
|
SelectionRefPtr()->SetInterlinePosition(true, ignoredError);
|
|
|
|
NS_WARNING_ASSERTION(!ignoredError.Failed(),
|
2020-03-18 04:14:42 +03:00
|
|
|
"Selection::SetInterlinePoisition(true) failed");
|
Bug 1548751 - Make TextEditRules::WillSetText() use fast path even if it's for <textarea> element r=m_kato
As far as I've tested, `TextEditor` has the following structure patterns:
1. If it's for a non-empty `<input>` element, it has only one text node.
2. If it's for an empty `<input>` element, it has only bogus node.
3. If it's for a non-empty `<textarea>` element, it has a text node and
`moz-<br>` element. Additionally they are followed by `<scrollbar>` and
`<resizer>` elements.
4. If it's for an empty `<textarea>` element, it has a `moz-<br>` element
followed by `<scrollbar>` and `<resizer>` elements.
Additionally, `TextEditRules::WillInsert()` always removes bogus node if
there is. So, in the case #2, there is no children.
Fortunately, we don't support XUL addons anymore on Firefox. However, in
other products like Thunderbird, the tree may be changed as unexpected.
Therefore, we still need to keep checking the tree, but we can use the fast
path for `<textarea>` element too.
Differential Revision: https://phabricator.services.mozilla.com/D30012
--HG--
extra : moz-landing-system : lando
2019-05-07 08:07:14 +03:00
|
|
|
}
|
|
|
|
|
2019-09-17 05:21:35 +03:00
|
|
|
return EditActionHandled();
|
2017-05-25 08:30:50 +03:00
|
|
|
}
|
|
|
|
|
2019-09-13 05:49:43 +03:00
|
|
|
EditActionResult TextEditor::HandleDeleteSelection(
|
|
|
|
nsIEditor::EDirection aDirectionAndAmount,
|
|
|
|
nsIEditor::EStripWrappers aStripWrappers) {
|
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
2018-04-26 18:09:10 +03:00
|
|
|
|
2019-09-13 05:49:43 +03:00
|
|
|
UndefineCaretBidiLevel();
|
Preparation for ender-based text control
* added focus listener. Doesn't do much yet, but when focus notifications start appearing, we'll be ready for them. The code is in
place to hide selection when we lose focus and paint selection when we get focus. That's probably not quite right, but it's a start.
We will need to be able to determine the distinction between losing focus to another control within our app, and losing focus to
another app.
* added support for disabled and readonly states in the editor. This is accomplished by having flags set by the client, and letting the
rules system deal with those flags. The flags I added are:
TEXT_EDITOR_FLAG_PLAINTEXT 0x01 // only plain text editing is allowed
TEXT_EDITOR_FLAG_SINGLELINE 0x02 // enter key and CR-LF handled specially
TEXT_EDITOR_FLAG_PASSWORD 0x04 // text is not entered into content, only a representative character
TEXT_EDITOR_FLAG_READONLY 0x08 // editing events are disabled. Editor may still accept focus.
TEXT_EDITOR_FLAG_DISALBED 0x10 // all events are disabled (like scrolling). Editor will not accept focus.
* added WillInsertBreak/DidInsertBreak into text rules, so flags could be checked. This gets us readonly, disabled, and single line
behavior.
* cleaned up the code that allocates, registers, and destroys event listeners. Thanks to Kin and Simon for cleaning up the
ownership model on the listeners, it was a big help.
* added support for a max text length. You can now tell the text editor, be no bigger than n characters.
1999-05-29 01:24:18 +04:00
|
|
|
|
2020-03-16 23:32:41 +03:00
|
|
|
CANCEL_OPERATION_AND_RETURN_EDIT_ACTION_RESULT_IF_READONLY_OF_DISABLED
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2019-08-02 08:44:40 +03:00
|
|
|
// if there is only padding <br> element for empty editor, cancel the
|
|
|
|
// operation.
|
2019-09-17 09:58:06 +03:00
|
|
|
if (mPaddingBRElementForEmptyEditor) {
|
2019-09-13 05:49:43 +03:00
|
|
|
return EditActionCanceled();
|
1999-03-13 07:53:21 +03:00
|
|
|
}
|
2019-09-13 06:20:04 +03:00
|
|
|
EditActionResult result =
|
|
|
|
HandleDeleteSelectionInternal(aDirectionAndAmount, aStripWrappers);
|
2019-09-13 05:49:43 +03:00
|
|
|
// HandleDeleteSelectionInternal() creates SelectionBatcher. Therefore,
|
2018-05-11 11:15:53 +03:00
|
|
|
// quitting from it might cause having destroyed the editor.
|
2019-09-13 05:49:43 +03:00
|
|
|
if (NS_WARN_IF(Destroyed())) {
|
|
|
|
return result.SetResult(NS_ERROR_EDITOR_DESTROYED);
|
2018-05-11 11:15:53 +03:00
|
|
|
}
|
2019-09-13 05:49:43 +03:00
|
|
|
NS_WARNING_ASSERTION(result.Succeeded(),
|
2020-03-18 04:14:42 +03:00
|
|
|
"TextEditor::HandleDeleteSelectionInternal() failed");
|
2019-09-13 05:49:43 +03:00
|
|
|
return result;
|
2018-05-11 11:15:53 +03:00
|
|
|
}
|
|
|
|
|
2019-09-13 05:49:43 +03:00
|
|
|
EditActionResult TextEditor::HandleDeleteSelectionInternal(
|
2019-09-13 06:20:04 +03:00
|
|
|
nsIEditor::EDirection aDirectionAndAmount,
|
|
|
|
nsIEditor::EStripWrappers aStripWrappers) {
|
2019-09-13 05:49:43 +03:00
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
|
|
|
MOZ_ASSERT(!AsHTMLEditor());
|
2003-07-18 18:12:51 +04:00
|
|
|
|
2015-08-12 20:26:01 +03:00
|
|
|
// If the current selection is empty (e.g the user presses backspace with
|
|
|
|
// a collapsed selection), then we want to avoid sending the selectstart
|
|
|
|
// event to the user, so we hide selection changes. However, we still
|
|
|
|
// want to send a single selectionchange event to the document, so we
|
|
|
|
// batch the selectionchange events, such that a single event fires after
|
|
|
|
// the AutoHideSelectionChanges destructor has been run.
|
2018-10-30 13:02:58 +03:00
|
|
|
SelectionBatcher selectionBatcher(SelectionRefPtr());
|
|
|
|
AutoHideSelectionChanges hideSelection(SelectionRefPtr());
|
2013-02-23 03:40:09 +04:00
|
|
|
nsAutoScriptBlocker scriptBlocker;
|
2003-07-18 18:12:51 +04:00
|
|
|
|
2019-07-22 06:53:58 +03:00
|
|
|
if (IsPasswordEditor() && IsMaskingPassword()) {
|
2019-09-13 05:49:43 +03:00
|
|
|
MaskAllCharacters();
|
2016-10-24 05:27:45 +03:00
|
|
|
} else {
|
2018-03-15 15:25:41 +03:00
|
|
|
EditorRawDOMPoint selectionStartPoint(
|
2018-10-30 13:02:58 +03:00
|
|
|
EditorBase::GetStartPoint(*SelectionRefPtr()));
|
2018-03-15 15:25:41 +03:00
|
|
|
if (NS_WARN_IF(!selectionStartPoint.IsSet())) {
|
2019-09-13 05:49:43 +03:00
|
|
|
return EditActionResult(NS_ERROR_FAILURE);
|
2018-03-15 15:25:41 +03:00
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2018-10-30 13:02:58 +03:00
|
|
|
if (!SelectionRefPtr()->IsCollapsed()) {
|
2019-09-13 06:20:04 +03:00
|
|
|
nsresult rv =
|
|
|
|
DeleteSelectionWithTransaction(aDirectionAndAmount, aStripWrappers);
|
2020-03-18 04:14:42 +03:00
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
NS_SUCCEEDED(rv),
|
|
|
|
"TextEditor::DeleteSelectionWithTransaction() failed");
|
2019-09-13 06:20:04 +03:00
|
|
|
return EditActionHandled(rv);
|
2016-10-24 05:27:45 +03:00
|
|
|
}
|
2003-07-18 18:12:51 +04:00
|
|
|
|
2008-12-04 16:17:48 +03:00
|
|
|
// Test for distance between caret and text that will be deleted
|
2019-09-13 05:49:43 +03:00
|
|
|
EditActionResult result =
|
|
|
|
SetCaretBidiLevelForDeletion(selectionStartPoint, aDirectionAndAmount);
|
2020-03-18 04:14:42 +03:00
|
|
|
if (result.Failed() || result.Canceled()) {
|
|
|
|
NS_WARNING_ASSERTION(result.Succeeded(),
|
|
|
|
"EditorBase::SetCaretBidiLevelForDeletion() failed");
|
2019-09-13 05:49:43 +03:00
|
|
|
return result;
|
2016-10-24 05:27:45 +03:00
|
|
|
}
|
2019-07-22 06:53:58 +03:00
|
|
|
}
|
2003-09-27 08:18:26 +04:00
|
|
|
|
2019-09-13 05:49:43 +03:00
|
|
|
nsresult rv = ExtendSelectionForDelete(&aDirectionAndAmount);
|
2020-03-18 04:14:42 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("TextEditor::ExtendSelectionForDelete() failed");
|
2019-09-13 05:49:43 +03:00
|
|
|
return EditActionResult(rv);
|
2001-04-27 00:14:26 +04:00
|
|
|
}
|
|
|
|
|
2019-09-13 06:20:04 +03:00
|
|
|
rv = DeleteSelectionWithTransaction(aDirectionAndAmount, aStripWrappers);
|
2019-09-13 05:49:43 +03:00
|
|
|
if (NS_WARN_IF(Destroyed())) {
|
|
|
|
return EditActionResult(NS_ERROR_EDITOR_DESTROYED);
|
2018-05-11 11:15:53 +03:00
|
|
|
}
|
2019-09-13 05:49:43 +03:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
|
2020-03-18 04:14:42 +03:00
|
|
|
"TextEditor::DeleteSelectionWithTransaction() failed");
|
2019-09-13 05:49:43 +03:00
|
|
|
return EditActionHandled(rv);
|
1999-03-12 05:28:24 +03:00
|
|
|
}
|
|
|
|
|
2019-09-13 06:44:33 +03:00
|
|
|
EditActionResult TextEditor::ComputeValueFromTextNodeAndPaddingBRElement(
|
|
|
|
nsAString& aValue) const {
|
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
2017-06-16 13:08:10 +03:00
|
|
|
|
2019-08-02 08:44:40 +03:00
|
|
|
// If there is a padding <br> element, there's no content. So output empty
|
|
|
|
// string.
|
2019-09-17 09:58:06 +03:00
|
|
|
if (mPaddingBRElementForEmptyEditor) {
|
2019-09-13 06:44:33 +03:00
|
|
|
aValue.Truncate();
|
|
|
|
return EditActionHandled();
|
2017-06-16 13:08:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If it's neither <input type="text"> nor <textarea>, e.g., an HTML editor
|
|
|
|
// which is in plaintext mode (e.g., plaintext email composer on Thunderbird),
|
|
|
|
// it should be handled by the expensive path.
|
2019-09-13 06:44:33 +03:00
|
|
|
if (AsHTMLEditor()) {
|
|
|
|
return EditActionIgnored();
|
2017-06-16 13:08:10 +03:00
|
|
|
}
|
|
|
|
|
2019-09-13 06:44:33 +03:00
|
|
|
Element* anonymousDivElement = GetRoot();
|
|
|
|
if (!anonymousDivElement) {
|
|
|
|
// Don't warn this case, this is possible, e.g., 997805.html
|
|
|
|
aValue.Truncate();
|
|
|
|
return EditActionHandled();
|
2017-06-16 13:08:10 +03:00
|
|
|
}
|
|
|
|
|
2019-09-13 06:44:33 +03:00
|
|
|
nsIContent* textNodeOrPaddingBRElement = anonymousDivElement->GetFirstChild();
|
|
|
|
if (!textNodeOrPaddingBRElement) {
|
|
|
|
aValue.Truncate();
|
|
|
|
return EditActionHandled();
|
2017-06-16 13:08:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If it's an <input type="text"> element, the DOM tree should be:
|
2020-03-18 12:21:44 +03:00
|
|
|
// <div (::-moz-text-control-editing-root)>
|
2017-06-16 13:08:10 +03:00
|
|
|
// #text
|
|
|
|
// </div>
|
|
|
|
//
|
|
|
|
// If it's a <textarea> element, the DOM tree should be:
|
2020-03-18 12:21:44 +03:00
|
|
|
// <div (::-moz-text-control-editing-root)>
|
2017-06-16 13:08:10 +03:00
|
|
|
// #text (if there is)
|
|
|
|
// <br type="_moz">
|
|
|
|
// <scrollbar orient="horizontal">
|
|
|
|
// ...
|
|
|
|
// </div>
|
|
|
|
|
2019-09-13 06:44:33 +03:00
|
|
|
Text* textNode = textNodeOrPaddingBRElement->GetAsText();
|
|
|
|
if (!textNode) {
|
|
|
|
// If there is no text node in the expected DOM tree, we can say that it's
|
|
|
|
// just empty.
|
|
|
|
aValue.Truncate();
|
|
|
|
return EditActionHandled();
|
|
|
|
}
|
|
|
|
|
2017-06-16 13:08:10 +03:00
|
|
|
nsIContent* firstChildExceptText =
|
2019-09-13 06:44:33 +03:00
|
|
|
textNode ? textNodeOrPaddingBRElement->GetNextSibling()
|
|
|
|
: textNodeOrPaddingBRElement;
|
2017-06-16 13:08:10 +03:00
|
|
|
// If the DOM tree is unexpected, fall back to the expensive path.
|
|
|
|
bool isInput = IsSingleLineEditor();
|
|
|
|
bool isTextarea = !isInput;
|
|
|
|
if (NS_WARN_IF(isInput && firstChildExceptText) ||
|
|
|
|
NS_WARN_IF(isTextarea && !firstChildExceptText) ||
|
2019-08-02 08:45:18 +03:00
|
|
|
NS_WARN_IF(isTextarea &&
|
|
|
|
!EditorBase::IsPaddingBRElementForEmptyLastLine(
|
|
|
|
*firstChildExceptText) &&
|
2017-06-16 13:08:10 +03:00
|
|
|
!firstChildExceptText->IsXULElement(nsGkAtoms::scrollbar))) {
|
2019-09-13 06:44:33 +03:00
|
|
|
return EditActionIgnored();
|
2017-06-16 13:08:10 +03:00
|
|
|
}
|
|
|
|
|
2019-09-13 06:44:33 +03:00
|
|
|
// Otherwise, the text data is the value.
|
|
|
|
textNode->GetData(aValue);
|
|
|
|
return EditActionHandled();
|
1999-06-25 03:36:56 +04:00
|
|
|
}
|
|
|
|
|
2019-09-17 05:47:49 +03:00
|
|
|
nsresult TextEditor::EnsurePaddingBRElementInMultilineEditor() {
|
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
|
|
|
MOZ_ASSERT(IsPlaintextEditor());
|
|
|
|
MOZ_ASSERT(!IsSingleLineEditor());
|
2012-05-05 13:00:05 +04:00
|
|
|
|
2019-09-17 05:47:49 +03:00
|
|
|
Element* anonymousDivElement = GetRoot();
|
|
|
|
if (NS_WARN_IF(!anonymousDivElement)) {
|
2018-04-26 18:09:10 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-05-05 13:00:05 +04:00
|
|
|
|
2019-08-09 13:19:11 +03:00
|
|
|
// Assuming EditorBase::MaybeCreatePaddingBRElementForEmptyEditor() has been
|
2019-08-02 08:44:40 +03:00
|
|
|
// called first.
|
2019-08-09 13:19:11 +03:00
|
|
|
// XXX This assumption is wrong. This method may be called alone. Actually,
|
|
|
|
// we see this warning in mochitest log. So, we should fix this bug
|
|
|
|
// later.
|
2019-09-17 05:47:49 +03:00
|
|
|
if (NS_WARN_IF(!anonymousDivElement->GetLastChild())) {
|
2018-04-26 18:09:10 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2003-07-18 18:12:51 +04:00
|
|
|
|
2019-08-02 08:44:40 +03:00
|
|
|
RefPtr<HTMLBRElement> brElement =
|
2019-09-17 05:47:49 +03:00
|
|
|
HTMLBRElement::FromNode(anonymousDivElement->GetLastChild());
|
2019-08-02 08:44:40 +03:00
|
|
|
if (!brElement) {
|
2019-09-17 05:47:49 +03:00
|
|
|
AutoTransactionsConserveSelection dontChangeMySelection(*this);
|
|
|
|
EditorDOMPoint endOfAnonymousDiv(
|
|
|
|
EditorDOMPoint::AtEndOf(*anonymousDivElement));
|
2019-08-02 08:45:18 +03:00
|
|
|
CreateElementResult createPaddingBRResult =
|
2019-09-17 05:47:49 +03:00
|
|
|
InsertPaddingBRElementForEmptyLastLineWithTransaction(
|
|
|
|
endOfAnonymousDiv);
|
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
createPaddingBRResult.Succeeded(),
|
2020-03-18 04:14:42 +03:00
|
|
|
"EditorBase::InsertPaddingBRElementForEmptyLastLineWithTransaction() "
|
|
|
|
"failed");
|
2019-09-17 05:47:49 +03:00
|
|
|
return createPaddingBRResult.Rv();
|
2012-05-05 13:00:05 +04:00
|
|
|
}
|
2012-04-13 00:55:48 +04:00
|
|
|
|
2019-08-02 08:44:40 +03:00
|
|
|
// Check to see if the trailing BR is a former padding <br> element for empty
|
|
|
|
// editor - this will have stuck around if we previously morphed a trailing
|
|
|
|
// node into a padding <br> element.
|
|
|
|
if (!brElement->IsPaddingForEmptyEditor()) {
|
2012-05-05 13:00:05 +04:00
|
|
|
return NS_OK;
|
2002-04-06 23:07:47 +04:00
|
|
|
}
|
2012-05-05 13:00:05 +04:00
|
|
|
|
2019-08-02 08:45:18 +03:00
|
|
|
// Morph it back to a padding <br> element for empty last line.
|
2019-08-02 08:44:40 +03:00
|
|
|
brElement->UnsetFlags(NS_PADDING_FOR_EMPTY_EDITOR);
|
2019-08-02 08:45:18 +03:00
|
|
|
brElement->SetFlags(NS_PADDING_FOR_EMPTY_LAST_LINE);
|
|
|
|
|
2012-05-05 13:00:05 +04:00
|
|
|
return NS_OK;
|
2002-04-06 23:07:47 +04:00
|
|
|
}
|
2000-01-13 13:17:35 +03:00
|
|
|
|
2019-09-12 10:48:29 +03:00
|
|
|
EditActionResult TextEditor::TruncateInsertionStringForMaxLength(
|
2019-09-13 05:40:09 +03:00
|
|
|
nsAString& aInsertionString) {
|
2019-09-12 10:48:29 +03:00
|
|
|
MOZ_ASSERT(IsEditActionDataAvailable());
|
2019-09-13 05:40:09 +03:00
|
|
|
MOZ_ASSERT(mMaxTextLength >= 0);
|
2018-04-26 18:09:10 +03:00
|
|
|
|
2019-09-12 10:48:29 +03:00
|
|
|
if (!IsPlaintextEditor() || IsIMEComposing()) {
|
|
|
|
return EditActionIgnored();
|
2016-10-24 05:27:45 +03:00
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2019-09-12 10:48:29 +03:00
|
|
|
int32_t currentLength = INT32_MAX;
|
|
|
|
nsresult rv = GetTextLength(¤tLength);
|
|
|
|
if (NS_FAILED(rv)) {
|
2020-03-18 04:14:42 +03:00
|
|
|
NS_WARNING("TextEditor::GetTextLength() failed");
|
2019-09-12 10:48:29 +03:00
|
|
|
return EditActionResult(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t selectionStart, selectionEnd;
|
|
|
|
nsContentUtils::GetSelectionInTextControl(SelectionRefPtr(), GetRoot(),
|
|
|
|
selectionStart, selectionEnd);
|
|
|
|
|
|
|
|
TextComposition* composition = GetComposition();
|
|
|
|
const uint32_t kOldCompositionStringLength =
|
|
|
|
composition ? composition->String().Length() : 0;
|
|
|
|
|
|
|
|
const uint32_t kSelectionLength = selectionEnd - selectionStart;
|
|
|
|
// XXX This computation must be wrong. If we'll support non-collapsed
|
|
|
|
// selection even during composition for Korean IME, kSelectionLength
|
|
|
|
// is part of kOldCompositionStringLength.
|
|
|
|
const uint32_t kNewLength =
|
|
|
|
currentLength - kSelectionLength - kOldCompositionStringLength;
|
2019-09-13 05:40:09 +03:00
|
|
|
if (kNewLength >= static_cast<uint32_t>(mMaxTextLength)) {
|
2019-09-12 10:48:29 +03:00
|
|
|
aInsertionString.Truncate(); // Too long, we cannot accept new character.
|
|
|
|
return EditActionHandled();
|
|
|
|
}
|
|
|
|
|
2019-09-13 05:40:09 +03:00
|
|
|
if (aInsertionString.Length() + kNewLength <=
|
|
|
|
static_cast<uint32_t>(mMaxTextLength)) {
|
2019-09-12 10:48:29 +03:00
|
|
|
return EditActionIgnored(); // Enough short string.
|
|
|
|
}
|
|
|
|
|
2019-09-13 05:40:09 +03:00
|
|
|
int32_t newInsertionStringLength = mMaxTextLength - kNewLength;
|
2019-09-12 10:48:29 +03:00
|
|
|
MOZ_ASSERT(newInsertionStringLength > 0);
|
|
|
|
char16_t maybeHighSurrogate =
|
|
|
|
aInsertionString.CharAt(newInsertionStringLength - 1);
|
|
|
|
char16_t maybeLowSurrogate =
|
|
|
|
aInsertionString.CharAt(newInsertionStringLength);
|
|
|
|
// Don't split the surrogate pair.
|
2019-10-27 08:05:51 +03:00
|
|
|
if (NS_IS_SURROGATE_PAIR(maybeHighSurrogate, maybeLowSurrogate)) {
|
2019-09-12 10:48:29 +03:00
|
|
|
newInsertionStringLength--;
|
|
|
|
}
|
|
|
|
// XXX What should we do if we're removing IVS but its preceding
|
|
|
|
// character won't be removed?
|
|
|
|
aInsertionString.Truncate(newInsertionStringLength);
|
|
|
|
return EditActionHandled();
|
Preparation for ender-based text control
* added focus listener. Doesn't do much yet, but when focus notifications start appearing, we'll be ready for them. The code is in
place to hide selection when we lose focus and paint selection when we get focus. That's probably not quite right, but it's a start.
We will need to be able to determine the distinction between losing focus to another control within our app, and losing focus to
another app.
* added support for disabled and readonly states in the editor. This is accomplished by having flags set by the client, and letting the
rules system deal with those flags. The flags I added are:
TEXT_EDITOR_FLAG_PLAINTEXT 0x01 // only plain text editing is allowed
TEXT_EDITOR_FLAG_SINGLELINE 0x02 // enter key and CR-LF handled specially
TEXT_EDITOR_FLAG_PASSWORD 0x04 // text is not entered into content, only a representative character
TEXT_EDITOR_FLAG_READONLY 0x08 // editing events are disabled. Editor may still accept focus.
TEXT_EDITOR_FLAG_DISALBED 0x10 // all events are disabled (like scrolling). Editor will not accept focus.
* added WillInsertBreak/DidInsertBreak into text rules, so flags could be checked. This gets us readonly, disabled, and single line
behavior.
* cleaned up the code that allocates, registers, and destroys event listeners. Thanks to Kin and Simon for cleaning up the
ownership model on the listeners, it was a big help.
* added support for a max text length. You can now tell the text editor, be no bigger than n characters.
1999-05-29 01:24:18 +04:00
|
|
|
}
|
1999-07-01 17:42:03 +04:00
|
|
|
|
2019-09-13 05:40:09 +03:00
|
|
|
bool TextEditor::CanEchoPasswordNow() const {
|
|
|
|
if (!LookAndFeel::GetEchoPassword() ||
|
2020-01-24 11:33:42 +03:00
|
|
|
(mFlags & nsIEditor::eEditorDontEchoPassword)) {
|
2018-11-02 10:33:58 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-13 05:40:09 +03:00
|
|
|
return GetEditAction() != EditAction::eDrop &&
|
|
|
|
GetEditAction() != EditAction::ePaste;
|
2016-07-09 05:34:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|