michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include // for printf michael@0: michael@0: #include "InsertElementTxn.h" michael@0: #include "nsAString.h" michael@0: #include "nsDebug.h" // for NS_ENSURE_TRUE, etc michael@0: #include "nsError.h" // for NS_ERROR_NULL_POINTER, etc michael@0: #include "nsIContent.h" // for nsIContent michael@0: #include "nsIEditor.h" // for nsIEditor michael@0: #include "nsINode.h" // for nsINode michael@0: #include "nsISelection.h" // for nsISelection michael@0: #include "nsMemory.h" // for nsMemory michael@0: #include "nsReadableUtils.h" // for ToNewCString michael@0: #include "nsString.h" // for nsString michael@0: michael@0: InsertElementTxn::InsertElementTxn() michael@0: : EditTxn() michael@0: { michael@0: } michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_INHERITED(InsertElementTxn, EditTxn, michael@0: mNode, michael@0: mParent) michael@0: michael@0: NS_IMPL_ADDREF_INHERITED(InsertElementTxn, EditTxn) michael@0: NS_IMPL_RELEASE_INHERITED(InsertElementTxn, EditTxn) michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(InsertElementTxn) michael@0: NS_INTERFACE_MAP_END_INHERITING(EditTxn) michael@0: michael@0: NS_IMETHODIMP InsertElementTxn::Init(nsIDOMNode *aNode, michael@0: nsIDOMNode *aParent, michael@0: int32_t aOffset, michael@0: nsIEditor *aEditor) michael@0: { michael@0: NS_ASSERTION(aNode && aParent && aEditor, "bad arg"); michael@0: NS_ENSURE_TRUE(aNode && aParent && aEditor, NS_ERROR_NULL_POINTER); michael@0: michael@0: mNode = do_QueryInterface(aNode); michael@0: mParent = do_QueryInterface(aParent); michael@0: mOffset = aOffset; michael@0: mEditor = aEditor; michael@0: NS_ENSURE_TRUE(mNode && mParent && mEditor, NS_ERROR_INVALID_ARG); michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP InsertElementTxn::DoTransaction(void) michael@0: { michael@0: NS_ENSURE_TRUE(mNode && mParent, NS_ERROR_NOT_INITIALIZED); michael@0: michael@0: nsCOMPtr parent = do_QueryInterface(mParent); michael@0: NS_ENSURE_STATE(parent); michael@0: michael@0: uint32_t count = parent->GetChildCount(); michael@0: if (mOffset > int32_t(count) || mOffset == -1) { michael@0: // -1 is sentinel value meaning "append at end" michael@0: mOffset = count; michael@0: } michael@0: michael@0: nsCOMPtr refContent = parent->GetChildAt(mOffset); michael@0: // note, it's ok for refNode to be null. that means append michael@0: nsCOMPtr refNode = refContent ? refContent->AsDOMNode() : nullptr; michael@0: michael@0: mEditor->MarkNodeDirty(mNode); michael@0: michael@0: nsCOMPtr resultNode; michael@0: nsresult result = mParent->InsertBefore(mNode, refNode, getter_AddRefs(resultNode)); michael@0: NS_ENSURE_SUCCESS(result, result); michael@0: NS_ENSURE_TRUE(resultNode, NS_ERROR_NULL_POINTER); michael@0: michael@0: // only set selection to insertion point if editor gives permission michael@0: bool bAdjustSelection; michael@0: mEditor->ShouldTxnSetSelection(&bAdjustSelection); michael@0: if (bAdjustSelection) michael@0: { michael@0: nsCOMPtr selection; michael@0: result = mEditor->GetSelection(getter_AddRefs(selection)); michael@0: NS_ENSURE_SUCCESS(result, result); michael@0: NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER); michael@0: // place the selection just after the inserted element michael@0: selection->Collapse(mParent, mOffset+1); michael@0: } michael@0: else michael@0: { michael@0: // do nothing - dom range gravity will adjust selection michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: NS_IMETHODIMP InsertElementTxn::UndoTransaction(void) michael@0: { michael@0: NS_ENSURE_TRUE(mNode && mParent, NS_ERROR_NOT_INITIALIZED); michael@0: michael@0: nsCOMPtr resultNode; michael@0: return mParent->RemoveChild(mNode, getter_AddRefs(resultNode)); michael@0: } michael@0: michael@0: NS_IMETHODIMP InsertElementTxn::GetTxnDescription(nsAString& aString) michael@0: { michael@0: aString.AssignLiteral("InsertElementTxn"); michael@0: return NS_OK; michael@0: }