summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/ReplaceTextTransaction.cpp
blob: ac1ed2b31d96220d5418c27b9b84b4e6cfafdb9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "ReplaceTextTransaction.h"

#include "HTMLEditUtils.h"

#include "mozilla/Logging.h"
#include "mozilla/OwningNonNull.h"
#include "mozilla/ToString.h"

namespace mozilla {

using namespace dom;

std::ostream& operator<<(std::ostream& aStream,
                         const ReplaceTextTransaction& aTransaction) {
  aStream << "{ mTextNode=" << aTransaction.mTextNode.get();
  if (aTransaction.mTextNode) {
    aStream << " (" << *aTransaction.mTextNode << ")";
  }
  aStream << ", mStringToInsert=\""
          << NS_ConvertUTF16toUTF8(aTransaction.mStringToInsert).get() << "\""
          << ", mStringToBeReplaced=\""
          << NS_ConvertUTF16toUTF8(aTransaction.mStringToBeReplaced).get()
          << "\", mOffset=" << aTransaction.mOffset
          << ", mEditorBase=" << aTransaction.mEditorBase.get() << " }";
  return aStream;
}

NS_IMPL_CYCLE_COLLECTION_INHERITED(ReplaceTextTransaction, EditTransactionBase,
                                   mEditorBase, mTextNode)

NS_IMPL_ADDREF_INHERITED(ReplaceTextTransaction, EditTransactionBase)
NS_IMPL_RELEASE_INHERITED(ReplaceTextTransaction, EditTransactionBase)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ReplaceTextTransaction)
NS_INTERFACE_MAP_END_INHERITING(EditTransactionBase)

NS_IMETHODIMP ReplaceTextTransaction::DoTransaction() {
  MOZ_LOG(GetLogModule(), LogLevel::Info,
          ("%p ReplaceTextTransaction::%s this=%s", this, __FUNCTION__,
           ToString(*this).c_str()));

  if (MOZ_UNLIKELY(
          NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mTextNode) ||
          NS_WARN_IF(!HTMLEditUtils::IsSimplyEditableNode(*mTextNode)))) {
    return NS_ERROR_NOT_AVAILABLE;
  }

  OwningNonNull<EditorBase> editorBase = *mEditorBase;
  OwningNonNull<Text> textNode = *mTextNode;

  IgnoredErrorResult error;
  editorBase->DoReplaceText(textNode, mOffset, mStringToBeReplaced.Length(),
                            mStringToInsert, error);
  if (MOZ_UNLIKELY(error.Failed())) {
    NS_WARNING("EditorBase::DoReplaceText() failed");
    return error.StealNSResult();
  }
  // XXX What should we do if mutation event listener changed the node?
  editorBase->RangeUpdaterRef().SelAdjReplaceText(textNode, mOffset,
                                                  mStringToBeReplaced.Length(),
                                                  mStringToInsert.Length());

  if (!editorBase->AllowsTransactionsToChangeSelection()) {
    return NS_OK;
  }

  // XXX Should we stop setting selection when mutation event listener
  //     modifies the text node?
  editorBase->CollapseSelectionTo(
      EditorRawDOMPoint(textNode, mOffset + mStringToInsert.Length()), error);
  if (MOZ_UNLIKELY(error.ErrorCodeIs(NS_ERROR_EDITOR_DESTROYED))) {
    NS_WARNING(
        "EditorBase::CollapseSelectionTo() caused destroying the editor");
    return NS_ERROR_EDITOR_DESTROYED;
  }
  NS_ASSERTION(!error.Failed(),
               "EditorBase::CollapseSelectionTo() failed, but ignored");
  return NS_OK;
}

NS_IMETHODIMP ReplaceTextTransaction::UndoTransaction() {
  MOZ_LOG(GetLogModule(), LogLevel::Info,
          ("%p ReplaceTextTransaction::%s this=%s", this, __FUNCTION__,
           ToString(*this).c_str()));

  if (MOZ_UNLIKELY(
          NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mTextNode) ||
          NS_WARN_IF(!HTMLEditUtils::IsSimplyEditableNode(*mTextNode)))) {
    return NS_ERROR_NOT_AVAILABLE;
  }

  IgnoredErrorResult error;
  nsAutoString insertedString;
  mTextNode->SubstringData(mOffset, mStringToInsert.Length(), insertedString,
                           error);
  if (MOZ_UNLIKELY(error.Failed())) {
    NS_WARNING("CharacterData::SubstringData() failed");
    return error.StealNSResult();
  }
  if (MOZ_UNLIKELY(insertedString != mStringToInsert)) {
    NS_WARNING(
        "ReplaceTextTransaction::UndoTransaction() did nothing due to "
        "unexpected text");
    return NS_OK;
  }

  OwningNonNull<EditorBase> editorBase = *mEditorBase;
  OwningNonNull<Text> textNode = *mTextNode;

  editorBase->DoReplaceText(textNode, mOffset, mStringToInsert.Length(),
                            mStringToBeReplaced, error);
  if (MOZ_UNLIKELY(error.Failed())) {
    NS_WARNING("EditorBase::DoReplaceText() failed");
    return error.StealNSResult();
  }
  // XXX What should we do if mutation event listener changed the node?
  editorBase->RangeUpdaterRef().SelAdjReplaceText(textNode, mOffset,
                                                  mStringToInsert.Length(),
                                                  mStringToBeReplaced.Length());

  if (!editorBase->AllowsTransactionsToChangeSelection()) {
    return NS_OK;
  }

  // XXX Should we stop setting selection when mutation event listener
  //     modifies the text node?
  editorBase->CollapseSelectionTo(
      EditorRawDOMPoint(textNode, mOffset + mStringToBeReplaced.Length()),
      error);
  if (MOZ_UNLIKELY(error.ErrorCodeIs(NS_ERROR_EDITOR_DESTROYED))) {
    NS_WARNING(
        "EditorBase::CollapseSelectionTo() caused destroying the editor");
    return NS_ERROR_EDITOR_DESTROYED;
  }
  NS_ASSERTION(!error.Failed(),
               "EditorBase::CollapseSelectionTo() failed, but ignored");
  return NS_OK;
}

NS_IMETHODIMP ReplaceTextTransaction::RedoTransaction() {
  MOZ_LOG(GetLogModule(), LogLevel::Info,
          ("%p ReplaceTextTransaction::%s this=%s", this, __FUNCTION__,
           ToString(*this).c_str()));

  if (MOZ_UNLIKELY(
          NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mTextNode) ||
          NS_WARN_IF(!HTMLEditUtils::IsSimplyEditableNode(*mTextNode)))) {
    return NS_ERROR_NOT_AVAILABLE;
  }

  IgnoredErrorResult error;
  nsAutoString undoneString;
  mTextNode->SubstringData(mOffset, mStringToBeReplaced.Length(), undoneString,
                           error);
  if (MOZ_UNLIKELY(error.Failed())) {
    NS_WARNING("CharacterData::SubstringData() failed");
    return error.StealNSResult();
  }
  if (MOZ_UNLIKELY(undoneString != mStringToBeReplaced)) {
    NS_WARNING(
        "ReplaceTextTransaction::RedoTransaction() did nothing due to "
        "unexpected text");
    return NS_OK;
  }

  OwningNonNull<EditorBase> editorBase = *mEditorBase;
  OwningNonNull<Text> textNode = *mTextNode;

  editorBase->DoReplaceText(textNode, mOffset, mStringToBeReplaced.Length(),
                            mStringToInsert, error);
  if (MOZ_UNLIKELY(error.Failed())) {
    NS_WARNING("EditorBase::DoReplaceText() failed");
    return error.StealNSResult();
  }
  // XXX What should we do if mutation event listener changed the node?
  editorBase->RangeUpdaterRef().SelAdjReplaceText(textNode, mOffset,
                                                  mStringToBeReplaced.Length(),
                                                  mStringToInsert.Length());

  if (!editorBase->AllowsTransactionsToChangeSelection()) {
    return NS_OK;
  }

  // XXX Should we stop setting selection when mutation event listener
  //     modifies the text node?
  editorBase->CollapseSelectionTo(
      EditorRawDOMPoint(textNode, mOffset + mStringToInsert.Length()), error);
  if (MOZ_UNLIKELY(error.ErrorCodeIs(NS_ERROR_EDITOR_DESTROYED))) {
    NS_WARNING(
        "EditorBase::CollapseSelectionTo() caused destroying the editor");
    return NS_ERROR_EDITOR_DESTROYED;
  }
  NS_ASSERTION(!error.Failed(),
               "EditorBase::CollapseSelectionTo() failed, but ignored");
  return NS_OK;
}

}  // namespace mozilla