Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Firefox/editor/libeditor/     Datei vom 27.6.2026 mit Größe 29 kB image not shown  

Quelle  TextEditSubActionHandler.cpp  Sprache: C


     
 * License, v. 2.0. If areturnNS_ERROR_EDITOR_DESTROYED
 obtainone http/mozillaorg//./ /

#include "ErrorList.h"
"

#include "
"EditorBase:), )
#include "EditAction.h"
#include " }
#include "
EditorUtils.h"
#include "
HTMLEditor.h"

#include "
mozilla/Assertions.h"
#include "
mozilla/Logging.h"
#include "
mozilla/LookAndFeel.h"
#include "
mozilla/Preferences.h"
#include "
mozilla/StaticPrefs_editor.h"
#include "
mozilla/TextComposition.h"
#include "
mozilla/dom/Element.h"
#include "
mozilla/dom/HTMLBRElement.h"
#include "
mozilla/dom/NodeFilterBinding.h"
#include "
mozilla/dom/NodeIterator.h"
#include "
mozilla/domjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0

#include "nsAString.h"
#include "nsCOMPtr.h"
#"sCRT."
#include "nsCRTGlue.h"
#include "nsComponentManagerUtils.java.lang.StringIndexOutOfBoundsException: Index 34 out of bounds for length 24
# nhjava.lang.StringIndexOutOfBoundsException: Index 27 out of bounds for length 27
i"
sDebug"
#include "nsError.h"
#include java.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 5
ijava.lang.StringIndexOutOfBoundsException: Range [9, 8) out of bounds for length 36
#include "nsIContent.h"
#include "nsINode.h"
include"."
#include "java.lang.StringIndexOutOfBoundsException: Index 18 out of bounds for length 0
include"
h
#include "nsPrintfCString.h"
#include .java.lang.StringIndexOutOfBoundsException: Index 23 out of bounds for length 23
#h"

namespace mozilla {

extern LazyLogModule gTextInputLog;    MOZ_ASSERT(!IsIMEComposing());

using namespace dom;

#define   MOZ_ASSERT(GetEditAction() != EditAction:;
  if (MOZ_ASSERT<)
    return   MOZ_ASSERT(aValuestatic_castchar16_t(\' =;
  }

  UndefineCaretBidiLevel();
    EditSubAction aTopLevelEditSubAction,
    :: java.lang.StringIndexOutOfBoundsException: Range [73, 72) out of bounds for length 80
  MOZ_ASSERT);
  MOZ_ASSERT(!aRv.Failed());

  EditorBase::OnStartToHandleTopLevelEditSubAction(
      aTopLevelEditSubAction  

  MOZ_ASSERT(java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
  MOZ_ASSERT
             const RefPtr<Text> textNode = GetTextNode();

  if (NS_WARN_IF(java.lang.StringIndexOutOfBoundsException: Index 26 out of bounds for length 23
    aRv  
    java.lang.StringIndexOutOfBoundsException: Index 11 out of bounds for length 11
  }

  if (NS_WARN_IF(!//  - we need to replaceof java.lang.StringIndexOutOfBoundsException: Range [57, 58) out of bounds for length 57
    return;
  }

  if (aTopLevelEditSubAction == EditSubAction  (java.lang.StringIndexOutOfBoundsException: Range [26, 25) out of bounds for length 30
    // SetText replaces all text, so spell checker handles starting from the
    // start of new value.
    SetSpellCheckRestartPoint(EditorDOMPoint(mRootElement
    returnjava.lang.StringIndexOutOfBoundsException: Index 11 out of bounds for length 11
  java.lang.StringIndexOutOfBoundsException: Range [3, 4) out of bounds for length 3

  if (aTopLevelEditSubAction == EditSubAction::eInsertText ||
       =:java.lang.StringIndexOutOfBoundsException: Range [73, 71) out of bounds for length 74
/java.lang.StringIndexOutOfBoundsException: Index 71 out of bounds for length 71
    // possible. If anchor is root of editor, it may become invalid offset
    // after inserting text.
    const java.lang.StringIndexOutOfBoundsException: Index 26 out of bounds for length 0
        FindBetterInsertionPoint(EditorRawDOMPoint  /    this?
  java.lang.StringIndexOutOfBoundsException: Range [15, 14) out of bounds for length 38
pjava.lang.StringIndexOutOfBoundsException: Range [38, 37) out of bounds for length 39
      ;
    }
    java.lang.StringIndexOutOfBoundsException: Index 12 out of bounds for length 3
  rv=java.lang.StringIndexOutOfBoundsException: Range [46, 45) out of bounds for length 73
  if ( NS_FAILEDrv){
    SetSpellCheckRestartPoint"
java.lang.StringIndexOutOfBoundsException: Range [27, 26) out of bounds for length 69
  }
  ()

nsresult   java.lang.StringIndexOutOfBoundsException: Index 3 out of bounds for length 3
();

  nsresult rv;
  while (true) {
    if (}
      rv = java.lang.StringIndexOutOfBoundsException: Index 19 out of bounds for length 0
      break
    }

    // XXX Probably, we should spellcheck again after edit action (not top-level:E,
    //     sub-action) is handled because the ranges can be referred only by
    //     users.
  OZ_ASSERT= :)
      NS_WARNING("
      break;
    }

    if (!java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
        NS_FAILED(rv java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
      NS_WARNING(
   E:java.lang.StringIndexOutOfBoundsException: Range [64, 62) out of bounds for length 74
      break}
    }

      Resu<   java.lang.StringIndexOutOfBoundsException: Index 45 out of bounds for length 45
     ( =){
      break;
    }
    NS_WARNING_ASSERTION(
java.lang.StringIndexOutOfBoundsException: Index 25 out of bounds for length 25
        "
// quitting    
    rv =   if (NS_WARN_I())java.lang.StringIndexOutOfBoundsException: Range [32, 33) out of bounds for length 32
    break;
  }
  java.lang.StringIndexOutOfBoundsException: Index 3 out of bounds for length 3
      EditorBase:      .isOk)
       :java.lang.StringIndexOutOfBoundsException: Range [49, 48) out of bounds for length 68
      return result;
      "EditorBase::OnEndHandlingTopLevelEditSubAction() failed, but java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
elEditSubAction);
  MOZ_ASSERT(nsIEditor::EDirection aDirectionAndAmount,
  return rv;
}

 :java.lang.StringIndexOutOfBoundsException: Range [30, 29) out of bounds for length 47
  ())

  if (   =:;
    return java.lang.StringIndexOutOfBoundsException: Index 25 out of bounds for length 0
  }

  IgnoredErrorResult ignoredError;
  /java.lang.StringIndexOutOfBoundsException: Index 74 out of bounds for length 74
      *this, EditSubAction  selection changes.However,we 
  if   // want to send a single selectionchange event to the document, so we
    return ignoredError.StealNSResult()
  }
  NS_WARNING_ASSERTION(
      !ignoredError.Failed(),
      "
TextEditor  /  .

  Result ) _;
      InsertLineFeedCharacterAtSelection();
  if (MOZ_UNLIKELY(result  S);
    nsAutoScriptBscriptBlocker
        "TextEditorjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
    return result.unwrapErr();
  }
  return NS_OK;
}

Result<EditActionResult, nsresult>
const autojava.lang.StringIndexOutOfBoundsException: Range [35, 34) out of bounds for length 36
 java.lang.StringIndexOutOfBoundsException: Range [13, 12) out of bounds for length 42
  MOZ_ASSERT(!IsSingleLineEditor    if NS_WARN_IF(selectionStartPoint.sSet() java.lang.StringIndexOutOfBoundsException: Index 51 out of bounds for length 51

  UndefineCaretBidiLevel();

  java.lang.StringIndexOutOfBoundsException: Index 16 out of bounds for length 5

  if      (SelectionRef().IsCollapsed()) {
    nsAutoString insertionString(u"
\n"_ns);
    ,nsresult>result java.lang.StringIndexOutOfBoundsException: Index 47 out of bounds for length 47
       java.lang.StringIndexOutOfBoundsException: Range [49, 48) out of bounds for length 66
        ,
      NS_WARNING(
          "
TextEditor::MaybeTruncateInsertionStringForMaxLength"For  throw java.lang.StringIndexOutOfBoundsException: Range [57, 56) out of bounds for length 72
      return result;
    }
    if (result.inspect(                     `xecCommand`,fix the caller"
;
      // Don't return as handled since we stopped inserting the line break.
      return EditActionResult::CanceledResult();
    }
  }

  // if the selection isn't collapsed, delete it.
  if (!SelectionRef().IsCollapsed()) {
    nsresult rv =
        DeleteSelectionAsSubAction(nsIEditor::eNone, nsIEditor::eNoStrip);
    if (NS_FAILED(rv)) {
      NS_WARNING(
          "EditorBase::DeleteSelectionAsSubAction(eNone, eNoStrip) failed");
      return Err(rv);
    }
  }

  const auto pointToInsert = GetFirstSelectionStartPoint<EditorDOMPoint>();
  if (NS_WARN_IF(!pointToInsert.IsSet())) {
    return Err(NS_ERROR_FAILURE);
  }
  MOZ_ASSERT(pointToInsert.IsSetAndValid());
  MOZ_ASSERT(!pointToInsert.IsContainerHTMLElement(nsGkAtoms::br));

  // Insert a linefeed character.
  Result<InsertTextResult, nsresult> insertTextResult =
      InsertTextWithTransaction(u"\n"_ns, pointToInsert,
                                InsertTextTo::ExistingTextNodeIfAvailable);
  if (MOZ_UNLIKELY(insertTextResult.isErr())) {
    NS_WARNING("TextEditor::InsertTextWithTransaction(\"\\n\") failed");
    return insertTextResult.propagateErr();
  }
  insertTextResult.inspect().IgnoreCaretPointSuggestion();
  EditorDOMPoint pointToPutCaret =
      insertTextResult.inspect().Handled()
          ? insertTextResult.inspect().EndOfInsertedTextRef()
          : pointToInsert;
  if (NS_WARN_IF(!pointToPutCaret.IsSetAndValid())) {
    return Err(NS_ERROR_FAILURE);
  }
  // XXX I don't think we still need this.  This must have been required when
  //     `<textarea>` was implemented with text nodes and `<br>` elements.
  // 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.
  pointToPutCaret.SetInterlinePosition(InterlinePosition::StartOfNextLine);
  nsresult rv = CollapseSelectionTo(pointToPutCaret);
  if (NS_FAILED(rv)) {
    NS_WARNING("EditorBase::CollapseSelectionTo() failed");
    return Err(rv);
  }
  return EditActionResult::HandledResult();
}

nsresult TextEditor::EnsureCaretNotAtEndOfTextNode() {
  MOZ_ASSERT(IsEditActionDataAvailable());

  // If there is no selection ranges, we should set to the end of the editor.
  // This is usually performed in InitEditorContentAndSelection(), however,
  // if the editor is reframed, this may be called by
  // OnEndHandlingTopLevelEditSubAction().
  if (SelectionRef().RangeCount()) {
    return NS_OK;
  }

  nsresult rv = CollapseSelectionToEndOfTextNode();
  if (MOZ_UNLIKELY(rv == NS_ERROR_EDITOR_DESTROYED)) {
    NS_WARNING(
        "TextEditor::CollapseSelectionToEndOfTextNode() caused destroying the "
        "editor");
    return NS_ERROR_EDITOR_DESTROYED;
  }
  NS_WARNING_ASSERTION(
      NS_SUCCEEDED(rv),
      "TextEditor::CollapseSelectionToEndOfTextNode() failed, but ignored");

  return NS_OK;
}

void TextEditor::HandleNewLinesInStringForSingleLineEditor(
    nsString& aString) const {
  static const char16_t kLF = static_cast<char16_t>('\n');
  MOZ_ASSERT(IsEditActionDataAvailable());
  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;
  }

  switch (mNewlineHandling) {
    case nsIEditor::eNewlinesReplaceWithSpaces:
      // Default of Firefox:
      // Strip trailing newlines first so we don't wind up with trailing spaces
      aString.Trim(LFSTR, false, true);
      aString.ReplaceChar(kLF, ' ');
      break;
    case nsIEditor::eNewlinesStrip:
      aString.StripChar(kLF);
      break;
    case nsIEditor::eNewlinesPasteToFirst:
    default: {
      // we get first *non-empty* line.
      int32_t offset = 0;
      while (firstLF == offset) {
        offset++;
        firstLF = aString.FindChar(kLF, offset);
      }
      if (firstLF > 0) {
        aString.Truncate(firstLF);
      }
      if (offset > 0) {
        aString.Cut(0, offset);
      }
      break;
    }
    case nsIEditor::eNewlinesReplaceWithCommas:
      // Default of Thunderbird:
      aString.Trim(LFSTR, true, true);
      aString.ReplaceChar(kLF, ',');
      break;
    case nsIEditor::eNewlinesStripSurroundingWhitespace: {
      nsAutoString result;
      uint32_t offset = 0;
      while (offset < aString.Length()) {
        int32_t nextLF = !offset ? firstLF : aString.FindChar(kLF, offset);
        if (nextLF < 0) {
          result.Append(nsDependentSubstring(aString, offset));
          break;
        }
        uint32_t wsBegin = nextLF;
        // look backwards for the first non-white-space char
        while (wsBegin > offset && NS_IS_SPACE(aString[wsBegin - 1])) {
          --wsBegin;
        }
        result.Append(nsDependentSubstring(aString, offset, wsBegin - offset));
        offset = nextLF + 1;
        while (offset < aString.Length() && NS_IS_SPACE(aString[offset])) {
          ++offset;
        }
      }
      aString = std::move(result);
      break;
    }
    case nsIEditor::eNewlinesPasteIntact:
      // even if we're pasting newlines, don't paste leading/trailing ones
      aString.Trim(LFSTR, true, true);
      break;
  }
}

Result<EditActionResult, nsresult> TextEditor::HandleInsertText(
    const nsAString& aInsertionString, InsertTextFor aPurpose) {
  MOZ_ASSERT(IsEditActionDataAvailable());

  MOZ_LOG(
      gTextInputLog, LogLevel::Info,
      ("%p TextEditor::HandleInsertText(aInsertionString=\"%s\", aPurpose=%s)",
       this, NS_ConvertUTF16toUTF8(aInsertionString).get(),
       ToString(aPurpose).c_str()));

  UndefineCaretBidiLevel();

  nsAutoString insertionString(aInsertionString);
  if (!aInsertionString.IsEmpty() && mMaxTextLength >= 0) {
    Result<EditActionResult, nsresult> result =
        MaybeTruncateInsertionStringForMaxLength(insertionString);
    if (MOZ_UNLIKELY(result.isErr())) {
      NS_WARNING(
          "TextEditor::MaybeTruncateInsertionStringForMaxLength() failed");
      EditActionResult unwrappedResult = result.unwrap();
      unwrappedResult.MarkAsHandled();
      return unwrappedResult;
    }
    // If we're exceeding the maxlength when composing IME, we need to clean up
    // the composing text, so we shouldn't return early.
    if (result.inspect().Handled() && insertionString.IsEmpty() &&
        NothingToDoIfInsertingEmptyText(aPurpose)) {
      return EditActionResult::CanceledResult();
    }
  }

  uint32_t start = 0;
  if (IsPasswordEditor()) {
    if (GetComposition() && !GetComposition()->String().IsEmpty()) {
      start = GetComposition()->ClampedStartOffsetInTextNode();
    } else {
      uint32_t end = 0;
      nsContentUtils::GetSelectionInTextControl(&SelectionRef(), GetRoot(),
                                                start, end);
    }
  }

  // if the selection isn't collapsed, delete it.
  if (!SelectionRef().IsCollapsed() &&
      !InsertingTextForExtantComposition(aPurpose)) {
    nsresult rv =
        DeleteSelectionAsSubAction(nsIEditor::eNone, nsIEditor::eNoStrip);
    if (NS_FAILED(rv)) {
      NS_WARNING(
          "EditorBase::DeleteSelectionAsSubAction(eNone, eNoStrip) failed");
      return Err(rv);
    }
  }

  if (aInsertionString.IsEmpty() && NothingToDoIfInsertingEmptyText(aPurpose)) {
    // HACK: this is a fix for bug 19395
    // I can't outlaw all empty insertions
    // because IME transaction depend on them
    // There is more work to do to make the
    // world safe for IME.
    return EditActionResult::CanceledResult();
  }

  // XXX Why don't we cancel here?  Shouldn't we do this first?
  CANCEL_OPERATION_AND_RETURN_EDIT_ACTION_RESULT_IF_READONLY

  MaybeDoAutoPasswordMasking();

  // People have lots of different ideas about what text fields
  // should do with multiline pastes.  See bugs 21032, 23485, 23485, 50935.
  // The six possible options are:
  // 0. paste newlines intact
  // 1. paste up to the first newline (default)
  // 2. replace newlines with spaces
  // 3. strip newlines
  // 4. replace with commas
  // 5. strip newlines and surrounding white-space
  // So find out what we're expected to do:
  if (IsSingleLineEditor()) {
    // 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.
    // XXX Should we handle do this before truncating the string for
    //     `maxlength`?
    nsContentUtils::PlatformToDOMLineBreaks(insertionString);
    HandleNewLinesInStringForSingleLineEditor(insertionString);
  }

  const auto atStartOfSelection = GetFirstSelectionStartPoint<EditorDOMPoint>();
  if (NS_WARN_IF(!atStartOfSelection.IsSetAndValid())) {
    return Err(NS_ERROR_FAILURE);
  }
  MOZ_ASSERT(!atStartOfSelection.IsContainerHTMLElement(nsGkAtoms::br));

  if (InsertingTextForComposition(aPurpose)) {
    EditorDOMPoint compositionStartPoint =
        GetFirstIMESelectionStartPoint<EditorDOMPoint>();
    if (!compositionStartPoint.IsSet()) {
      compositionStartPoint = FindBetterInsertionPoint(atStartOfSelection);
      NS_WARNING_ASSERTION(
          compositionStartPoint.IsSet(),
          "TextEditor::FindBetterInsertionPoint() failed, but ignored");
    }
    Result<InsertTextResult, nsresult> insertTextResult =
        InsertTextWithTransaction(insertionString, compositionStartPoint,
                                  InsertTextTo::ExistingTextNodeIfAvailable);
    if (MOZ_UNLIKELY(insertTextResult.isErr())) {
      NS_WARNING("EditorBase::InsertTextWithTransaction() failed");
      return insertTextResult.propagateErr();
    }
    nsresult rv = insertTextResult.unwrap().SuggestCaretPointTo(
        *this, {SuggestCaret::OnlyIfHasSuggestion,
                SuggestCaret::OnlyIfTransactionsAllowedToDoIt,
                SuggestCaret::AndIgnoreTrivialError});
    if (NS_FAILED(rv)) {
      NS_WARNING("CaretPoint::SuggestCaretPointTo() failed");
      return Err(rv);
    }
    NS_WARNING_ASSERTION(
        rv != NS_SUCCESS_EDITOR_BUT_IGNORED_TRIVIAL_ERROR,
        "CaretPoint::SuggestCaretPointTo() failed, but ignored");
  } else {
    MOZ_ASSERT(!InsertingTextForComposition(aPurpose));

    Result<InsertTextResult, nsresult> insertTextResult =
        InsertTextWithTransaction(insertionString, atStartOfSelection,
                                  InsertTextTo::ExistingTextNodeIfAvailable);
    if (MOZ_UNLIKELY(insertTextResult.isErr())) {
      NS_WARNING("EditorBase::InsertTextWithTransaction() failed");
      return insertTextResult.propagateErr();
    }
    // Ignore caret suggestion because there was
    // AutoTransactionsConserveSelection.
    insertTextResult.inspect().IgnoreCaretPointSuggestion();
    if (insertTextResult.inspect().Handled()) {
      // Make the caret attach to the inserted text, unless this text ends with
      // a LF, in which case make the caret attach to the next line.
      const bool endsWithLF =
          !insertionString.IsEmpty() && insertionString.Last() == nsCRT::LF;
      EditorDOMPoint pointToPutCaret =
          insertTextResult.inspect().EndOfInsertedTextRef();
      pointToPutCaret.SetInterlinePosition(
          endsWithLF ? InterlinePosition::StartOfNextLine
                     : InterlinePosition::EndOfLine);
      MOZ_ASSERT(pointToPutCaret.IsInTextNode(),
                 "After inserting text into a text node, insertTextResult "
                 "should return a point in a text node");
      nsresult rv = CollapseSelectionTo(pointToPutCaret);
      if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
        return Err(NS_ERROR_EDITOR_DESTROYED);
      }
      NS_WARNING_ASSERTION(
          NS_SUCCEEDED(rv),
          "EditorBase::CollapseSelectionTo() failed, but ignored");
    }
  }

  // Unmask inputted character(s) if necessary.
  if (IsPasswordEditor() && IsMaskingPassword() && CanEchoPasswordNow()) {
    nsresult rv = SetUnmaskRangeAndNotify(start, insertionString.Length(),
                                          LookAndFeel::GetPasswordMaskDelay());
    if (NS_FAILED(rv)) {
      NS_WARNING("TextEditor::SetUnmaskRangeAndNotify() failed");
      return Err(rv);
    }
    return EditActionResult::HandledResult();
  }

  return EditActionResult::HandledResult();
}

Result<EditActionResult, nsresult> TextEditor::SetTextWithoutTransaction(
    const nsAString& aValue) {
  MOZ_ASSERT(IsEditActionDataAvailable());
  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);

  UndefineCaretBidiLevel();

  // XXX If we're setting value, shouldn't we keep setting the new value here?
  CANCEL_OPERATION_AND_RETURN_EDIT_ACTION_RESULT_IF_READONLY

  MaybeDoAutoPasswordMasking();

  const RefPtr<Text> textNode = GetTextNode();
  MOZ_ASSERT(textNode);

  // 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()) {
    // 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.
    if (!textNode->GetNextSibling() ||
        !EditorUtils::IsPaddingBRElementForEmptyLastLine(
            *textNode->GetNextSibling())) {
      return EditActionResult::IgnoredResult();
    }
  }

  // XXX Password fields accept line breaks as normal characters with this code.
  //     Is this intentional?
  nsAutoString sanitizedValue(aValue);
  if (IsSingleLineEditor() && !IsPasswordEditor()) {
    HandleNewLinesInStringForSingleLineEditor(sanitizedValue);
  }

  nsresult rv = SetTextNodeWithoutTransaction(sanitizedValue, *textNode);
  if (NS_FAILED(rv)) {
    NS_WARNING("EditorBase::SetTextNodeWithoutTransaction() failed");
    return Err(rv);
  }

  return EditActionResult::HandledResult();
}

Result<EditActionResult, nsresult> TextEditor::HandleDeleteSelection(
    nsIEditor::EDirection aDirectionAndAmount,
    nsIEditor::EStripWrappers aStripWrappers) {
  MOZ_ASSERT(IsEditActionDataAvailable());
  MOZ_ASSERT(aStripWrappers == nsIEditor::eNoStrip);

  UndefineCaretBidiLevel();

  CANCEL_OPERATION_AND_RETURN_EDIT_ACTION_RESULT_IF_READONLY

  if (IsEmpty()) {
    return EditActionResult::CanceledResult();
  }
  Result<EditActionResult, nsresult> result =
      HandleDeleteSelectionInternal(aDirectionAndAmount, nsIEditor::eNoStrip);
  // HandleDeleteSelectionInternal() creates SelectionBatcher.  Therefore,
  // quitting from it might cause having destroyed the editor.
  if (NS_WARN_IF(Destroyed())) {
    return Err(NS_ERROR_EDITOR_DESTROYED);
  }
  NS_WARNING_ASSERTION(
      result.isOk(),
      "TextEditor::HandleDeleteSelectionInternal(eNoStrip) failed");
  return result;
}

Result<EditActionResult, nsresult> TextEditor::HandleDeleteSelectionInternal(
    nsIEditor::EDirection aDirectionAndAmount,
    nsIEditor::EStripWrappers aStripWrappers) {
  MOZ_ASSERT(IsEditActionDataAvailable());
  MOZ_ASSERT(aStripWrappers == nsIEditor::eNoStrip);

  // If the current selection is empty (e.g the user presses backspace with
  // a collapsed selection), then we want to avoid sending the selectstart
  // event to the user, so we hide selection changes. However, we still
  // want to send a single selectionchange event to the document, so we
  // batch the selectionchange events, such that a single event fires after
  // the AutoHideSelectionChanges destructor has been run.
  SelectionBatcher selectionBatcher(SelectionRef(), __FUNCTION__);
  AutoHideSelectionChanges hideSelection(SelectionRef());
  nsAutoScriptBlocker scriptBlocker;

  if (IsPasswordEditor() && IsMaskingPassword()) {
    MaskAllCharacters();
  } else {
    const auto selectionStartPoint =
        GetFirstSelectionStartPoint<EditorRawDOMPoint>();
    if (NS_WARN_IF(!selectionStartPoint.IsSet())) {
      return Err(NS_ERROR_FAILURE);
    }

    if (!SelectionRef().IsCollapsed()) {
      AutoClonedSelectionRangeArray rangesToDelete(SelectionRef());
      if (NS_WARN_IF(rangesToDelete.Ranges().IsEmpty())) {
        NS_ASSERTION(false,
                     "For avoiding to throw incompatible exception for "
                     "`execCommand`, fix the caller");
        return Err(NS_ERROR_FAILURE);
      }

      if (const Text* const textNode = GetTextNode()) {
        rangesToDelete.EnsureRangesInTextNode(*textNode);
      }

      Result<CaretPoint, nsresult> caretPointOrError =
          DeleteRangesWithTransaction(aDirectionAndAmount, aStripWrappers,
                                      rangesToDelete);
      if (MOZ_UNLIKELY(caretPointOrError.isErr())) {
        NS_WARNING("EditorBase::DeleteRangesWithTransaction() failed");
        return caretPointOrError.propagateErr();
      }
      nsresult rv = caretPointOrError.inspect().SuggestCaretPointTo(
          *this, {SuggestCaret::OnlyIfHasSuggestion,
                  SuggestCaret::OnlyIfTransactionsAllowedToDoIt,
                  SuggestCaret::AndIgnoreTrivialError});
      if (NS_FAILED(rv)) {
        NS_WARNING("CaretPoint::SuggestCaretPointTo() failed");
      }
      NS_WARNING_ASSERTION(
          rv != NS_SUCCESS_EDITOR_BUT_IGNORED_TRIVIAL_ERROR,
          "CaretPoint::SuggestCaretPointTo() failed, but ignored");
      return EditActionResult::HandledResult();
    }

    // Test for distance between caret and text that will be deleted
    AutoCaretBidiLevelManager bidiLevelManager(*this, aDirectionAndAmount,
                                               selectionStartPoint);
    if (MOZ_UNLIKELY(bidiLevelManager.Failed())) {
      NS_WARNING("EditorBase::AutoCaretBidiLevelManager() failed");
      return Err(NS_ERROR_FAILURE);
    }
    bidiLevelManager.MaybeUpdateCaretBidiLevel(*this);
    if (bidiLevelManager.Canceled()) {
      return EditActionResult::CanceledResult();
    }
  }

  AutoClonedSelectionRangeArray rangesToDelete(SelectionRef());
  Result<nsIEditor::EDirection, nsresult> result =
      rangesToDelete.ExtendAnchorFocusRangeFor(*this, aDirectionAndAmount);
  if (result.isErr()) {
    NS_WARNING(
        "AutoClonedSelectionRangeArray::ExtendAnchorFocusRangeFor() failed");
    return result.propagateErr();
  }
  if (const Text* theTextNode = GetTextNode()) {
    rangesToDelete.EnsureRangesInTextNode(*theTextNode);
  }

  Result<CaretPoint, nsresult> caretPointOrError = DeleteRangesWithTransaction(
      result.unwrap(), nsIEditor::eNoStrip, rangesToDelete);
  if (MOZ_UNLIKELY(caretPointOrError.isErr())) {
    NS_WARNING("EditorBase::DeleteRangesWithTransaction(eNoStrip) failed");
    return caretPointOrError.propagateErr();
  }

  nsresult rv = caretPointOrError.inspect().SuggestCaretPointTo(
      *this, {SuggestCaret::OnlyIfHasSuggestion,
              SuggestCaret::OnlyIfTransactionsAllowedToDoIt,
              SuggestCaret::AndIgnoreTrivialError});
  if (NS_FAILED(rv)) {
    NS_WARNING("CaretPoint::SuggestCaretPointTo() failed");
    return Err(rv);
  }
  NS_WARNING_ASSERTION(rv != NS_SUCCESS_EDITOR_BUT_IGNORED_TRIVIAL_ERROR,
                       "CaretPoint::SuggestCaretPointTo() failed, but ignored");

  return EditActionResult::HandledResult();
}

Result<EditActionResult, nsresult>
TextEditor::ComputeValueFromTextNodeAndBRElement(nsAString& aValue) const {
  MOZ_ASSERT(IsEditActionDataAvailable());
  MOZ_ASSERT(!IsHTMLEditor());

  Element* anonymousDivElement = GetRoot();
  if (MOZ_UNLIKELY(!anonymousDivElement)) {
    // Don't warn this case, this is possible, e.g., 997805.html
    aValue.Truncate();
    return EditActionResult::HandledResult();
  }

  const Text* const textNode = GetTextNode();
  MOZ_ASSERT(textNode);

  if (!textNode->Length()) {
    aValue.Truncate();
    return EditActionResult::HandledResult();
  }

  nsIContent* firstChildExceptText = textNode->GetNextSibling();
  // If the DOM tree is unexpected, fall back to the expensive path.
  bool isInput = IsSingleLineEditor();
  bool isTextarea = !isInput;
  if (NS_WARN_IF(isInput && firstChildExceptText) ||
      NS_WARN_IF(isTextarea && !firstChildExceptText) ||
      NS_WARN_IF(isTextarea &&
                 !EditorUtils::IsPaddingBRElementForEmptyLastLine(
                     *firstChildExceptText) &&
                 !firstChildExceptText->IsXULElement(nsGkAtoms::scrollbar))) {
    return EditActionResult::IgnoredResult();
  }

  // Otherwise, the text data is the value.
  textNode->GetData(aValue);
  return EditActionResult::HandledResult();
}

Result<EditActionResult, nsresult>
TextEditor::MaybeTruncateInsertionStringForMaxLength(
    nsAString& aInsertionString) {
  MOZ_ASSERT(IsEditActionDataAvailable());
  MOZ_ASSERT(mMaxTextLength >= 0);

  if (IsIMEComposing()) {
    return EditActionResult::IgnoredResult();
  }

  // Ignore user pastes
  switch (GetEditAction()) {
    case EditAction::ePaste:
    case EditAction::ePasteAsQuotation:
    case EditAction::eDrop:
    case EditAction::eReplaceText:
      // EditActionPrinciple() is non-null iff the edit was requested by
      // javascript.
      if (!GetEditActionPrincipal()) {
        // By now we are certain that this is a user paste, before we ignore it,
        // lets check if the user explictly enabled truncating user pastes.
        if (!StaticPrefs::editor_truncate_user_pastes()) {
          return EditActionResult::IgnoredResult();
        }
      }
      [[fallthrough]];
    default:
      break;
  }

  uint32_t currentLength = UINT32_MAX;
  nsresult rv = GetTextLength(¤tLength);
  if (NS_FAILED(rv)) {
    NS_WARNING("TextEditor::GetTextLength() failed");
    return Err(rv);
  }

  uint32_t selectionStart, selectionEnd;
  nsContentUtils::GetSelectionInTextControl(&SelectionRef(), 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;
  if (kNewLength >= AssertedCast<uint32_t>(mMaxTextLength)) {
    aInsertionString.Truncate();  // Too long, we cannot accept new character.
    return EditActionResult::HandledResult();
  }

  if (aInsertionString.Length() + kNewLength <=
      AssertedCast<uint32_t>(mMaxTextLength)) {
    return EditActionResult::IgnoredResult();  // Enough short string.
  }

  int32_t newInsertionStringLength = mMaxTextLength - kNewLength;
  MOZ_ASSERT(newInsertionStringLength > 0);
  char16_t maybeHighSurrogate =
      aInsertionString.CharAt(newInsertionStringLength - 1);
  char16_t maybeLowSurrogate =
      aInsertionString.CharAt(newInsertionStringLength);
  // Don't split the surrogate pair.
  if (NS_IS_SURROGATE_PAIR(maybeHighSurrogate, maybeLowSurrogate)) {
    newInsertionStringLength--;
  }
  // XXX What should we do if we're removing IVS but its preceding
  //     character won't be removed?
  aInsertionString.Truncate(newInsertionStringLength);
  return EditActionResult::HandledResult();
}

bool TextEditor::CanEchoPasswordNow() const {
  if (!LookAndFeel::GetEchoPassword() || EchoingPasswordPrevented()) {
    return false;
  }

  return GetEditAction() != EditAction::eDrop &&
         GetEditAction() != EditAction::ePaste;
}

}  // namespace mozilla

Messung
CHZG
909810096

¤ Dauer der Verarbeitung: 0.23 Sekunden  ¤

*© Formatika GbR, Deutschland