NS_IMETHODIMP InsertNodeTransaction::DoTransaction() { if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mContentToInsert) || NS_WARN_IF(!mPointToInsert.IsSet())) { return NS_ERROR_NOT_INITIALIZED; } if (!mPointToInsert.IsSetAndValid()) { // It seems that DOM tree has been changed after first DoTransaction() // and current RedoTranaction() call. if (mPointToInsert.GetChild()) { EditorDOMPoint newPointToInsert(mPointToInsert.GetChild()); if (!newPointToInsert.IsSet()) { // The insertion point has been removed from the DOM tree. // In this case, we should append the node to the container instead. newPointToInsert.SetToEndOf(mPointToInsert.GetContainer()); if (NS_WARN_IF(!newPointToInsert.IsSet())) { return NS_ERROR_FAILURE; } } mPointToInsert = newPointToInsert; } else { mPointToInsert.SetToEndOf(mPointToInsert.GetContainer()); if (NS_WARN_IF(!mPointToInsert.IsSet())) { return NS_ERROR_FAILURE; } } } mEditorBase->MarkNodeDirty(GetAsDOMNode(mContentToInsert)); ErrorResult error; mPointToInsert.GetContainer()->InsertBefore(*mContentToInsert, mPointToInsert.GetChild(), error); error.WouldReportJSException(); if (NS_WARN_IF(error.Failed())) { return error.StealNSResult(); } // Only set selection to insertion point if editor gives permission if (mEditorBase->GetShouldTxnSetSelection()) { RefPtr<Selection> selection = mEditorBase->GetSelection(); if (NS_WARN_IF(!selection)) { return NS_ERROR_FAILURE; } // Place the selection just after the inserted element EditorRawDOMPoint afterInsertedNode(mContentToInsert); DebugOnly<bool> advanced = afterInsertedNode.AdvanceOffset(); NS_WARNING_ASSERTION(advanced, "Failed to advance offset after the inserted node"); selection->Collapse(afterInsertedNode, error); if (NS_WARN_IF(error.Failed())) { error.SuppressException(); } } return NS_OK; }
NS_IMETHODIMP InsertTextTransaction::DoTransaction() { if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mTextNode)) { return NS_ERROR_NOT_AVAILABLE; } ErrorResult rv; mTextNode->InsertData(mOffset, mStringToInsert, rv); if (NS_WARN_IF(rv.Failed())) { return rv.StealNSResult(); } // Only set selection to insertion point if editor gives permission if (mEditorBase->GetShouldTxnSetSelection()) { RefPtr<Selection> selection = mEditorBase->GetSelection(); if (NS_WARN_IF(!selection)) { return NS_ERROR_FAILURE; } DebugOnly<nsresult> rv = selection->Collapse(mTextNode, mOffset + mStringToInsert.Length()); NS_ASSERTION(NS_SUCCEEDED(rv), "Selection could not be collapsed after insert"); } else { // Do nothing - DOM Range gravity will adjust selection } mEditorBase->RangeUpdaterRef(). SelAdjInsertText(*mTextNode, mOffset, mStringToInsert); return NS_OK; }