editor/libeditor/base/InsertElementTxn.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include <stdio.h> // for printf
michael@0 7
michael@0 8 #include "InsertElementTxn.h"
michael@0 9 #include "nsAString.h"
michael@0 10 #include "nsDebug.h" // for NS_ENSURE_TRUE, etc
michael@0 11 #include "nsError.h" // for NS_ERROR_NULL_POINTER, etc
michael@0 12 #include "nsIContent.h" // for nsIContent
michael@0 13 #include "nsIEditor.h" // for nsIEditor
michael@0 14 #include "nsINode.h" // for nsINode
michael@0 15 #include "nsISelection.h" // for nsISelection
michael@0 16 #include "nsMemory.h" // for nsMemory
michael@0 17 #include "nsReadableUtils.h" // for ToNewCString
michael@0 18 #include "nsString.h" // for nsString
michael@0 19
michael@0 20 InsertElementTxn::InsertElementTxn()
michael@0 21 : EditTxn()
michael@0 22 {
michael@0 23 }
michael@0 24
michael@0 25 NS_IMPL_CYCLE_COLLECTION_INHERITED(InsertElementTxn, EditTxn,
michael@0 26 mNode,
michael@0 27 mParent)
michael@0 28
michael@0 29 NS_IMPL_ADDREF_INHERITED(InsertElementTxn, EditTxn)
michael@0 30 NS_IMPL_RELEASE_INHERITED(InsertElementTxn, EditTxn)
michael@0 31 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(InsertElementTxn)
michael@0 32 NS_INTERFACE_MAP_END_INHERITING(EditTxn)
michael@0 33
michael@0 34 NS_IMETHODIMP InsertElementTxn::Init(nsIDOMNode *aNode,
michael@0 35 nsIDOMNode *aParent,
michael@0 36 int32_t aOffset,
michael@0 37 nsIEditor *aEditor)
michael@0 38 {
michael@0 39 NS_ASSERTION(aNode && aParent && aEditor, "bad arg");
michael@0 40 NS_ENSURE_TRUE(aNode && aParent && aEditor, NS_ERROR_NULL_POINTER);
michael@0 41
michael@0 42 mNode = do_QueryInterface(aNode);
michael@0 43 mParent = do_QueryInterface(aParent);
michael@0 44 mOffset = aOffset;
michael@0 45 mEditor = aEditor;
michael@0 46 NS_ENSURE_TRUE(mNode && mParent && mEditor, NS_ERROR_INVALID_ARG);
michael@0 47 return NS_OK;
michael@0 48 }
michael@0 49
michael@0 50
michael@0 51 NS_IMETHODIMP InsertElementTxn::DoTransaction(void)
michael@0 52 {
michael@0 53 NS_ENSURE_TRUE(mNode && mParent, NS_ERROR_NOT_INITIALIZED);
michael@0 54
michael@0 55 nsCOMPtr<nsINode> parent = do_QueryInterface(mParent);
michael@0 56 NS_ENSURE_STATE(parent);
michael@0 57
michael@0 58 uint32_t count = parent->GetChildCount();
michael@0 59 if (mOffset > int32_t(count) || mOffset == -1) {
michael@0 60 // -1 is sentinel value meaning "append at end"
michael@0 61 mOffset = count;
michael@0 62 }
michael@0 63
michael@0 64 nsCOMPtr<nsIContent> refContent = parent->GetChildAt(mOffset);
michael@0 65 // note, it's ok for refNode to be null. that means append
michael@0 66 nsCOMPtr<nsIDOMNode> refNode = refContent ? refContent->AsDOMNode() : nullptr;
michael@0 67
michael@0 68 mEditor->MarkNodeDirty(mNode);
michael@0 69
michael@0 70 nsCOMPtr<nsIDOMNode> resultNode;
michael@0 71 nsresult result = mParent->InsertBefore(mNode, refNode, getter_AddRefs(resultNode));
michael@0 72 NS_ENSURE_SUCCESS(result, result);
michael@0 73 NS_ENSURE_TRUE(resultNode, NS_ERROR_NULL_POINTER);
michael@0 74
michael@0 75 // only set selection to insertion point if editor gives permission
michael@0 76 bool bAdjustSelection;
michael@0 77 mEditor->ShouldTxnSetSelection(&bAdjustSelection);
michael@0 78 if (bAdjustSelection)
michael@0 79 {
michael@0 80 nsCOMPtr<nsISelection> selection;
michael@0 81 result = mEditor->GetSelection(getter_AddRefs(selection));
michael@0 82 NS_ENSURE_SUCCESS(result, result);
michael@0 83 NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
michael@0 84 // place the selection just after the inserted element
michael@0 85 selection->Collapse(mParent, mOffset+1);
michael@0 86 }
michael@0 87 else
michael@0 88 {
michael@0 89 // do nothing - dom range gravity will adjust selection
michael@0 90 }
michael@0 91 return result;
michael@0 92 }
michael@0 93
michael@0 94 NS_IMETHODIMP InsertElementTxn::UndoTransaction(void)
michael@0 95 {
michael@0 96 NS_ENSURE_TRUE(mNode && mParent, NS_ERROR_NOT_INITIALIZED);
michael@0 97
michael@0 98 nsCOMPtr<nsIDOMNode> resultNode;
michael@0 99 return mParent->RemoveChild(mNode, getter_AddRefs(resultNode));
michael@0 100 }
michael@0 101
michael@0 102 NS_IMETHODIMP InsertElementTxn::GetTxnDescription(nsAString& aString)
michael@0 103 {
michael@0 104 aString.AssignLiteral("InsertElementTxn");
michael@0 105 return NS_OK;
michael@0 106 }

mercurial