Bumping gaia.json for 1 gaia revision(s) a=gaia-bump
[gecko.git] / editor / libeditor / SplitElementTxn.cpp
blob2299c599c735b9dd505d69cf11a801eb8a9ab488
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include <stdio.h> // for printf
8 #include "SplitElementTxn.h"
9 #include "nsAString.h"
10 #include "nsDebug.h" // for NS_ASSERTION, etc
11 #include "nsEditor.h" // for nsEditor
12 #include "nsError.h" // for NS_ERROR_NOT_INITIALIZED, etc
13 #include "nsIContent.h" // for nsIContent
14 #include "nsIDOMCharacterData.h" // for nsIDOMCharacterData
15 #include "nsIEditor.h" // for nsEditor::DebugDumpContent, etc
16 #include "nsISelection.h" // for nsISelection
17 #include "nsISupportsUtils.h" // for NS_ADDREF
19 using namespace mozilla;
21 // note that aEditor is not refcounted
22 SplitElementTxn::SplitElementTxn()
23 : EditTxn()
27 SplitElementTxn::~SplitElementTxn()
31 NS_IMPL_CYCLE_COLLECTION_INHERITED(SplitElementTxn, EditTxn,
32 mParent,
33 mNewLeftNode)
35 NS_IMPL_ADDREF_INHERITED(SplitElementTxn, EditTxn)
36 NS_IMPL_RELEASE_INHERITED(SplitElementTxn, EditTxn)
37 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SplitElementTxn)
38 NS_INTERFACE_MAP_END_INHERITING(EditTxn)
40 NS_IMETHODIMP SplitElementTxn::Init(nsEditor *aEditor,
41 nsIDOMNode *aNode,
42 int32_t aOffset)
44 NS_ASSERTION(aEditor && aNode, "bad args");
45 if (!aEditor || !aNode) { return NS_ERROR_NOT_INITIALIZED; }
47 mEditor = aEditor;
48 mExistingRightNode = do_QueryInterface(aNode);
49 mOffset = aOffset;
50 return NS_OK;
53 NS_IMETHODIMP SplitElementTxn::DoTransaction(void)
55 NS_ASSERTION(mExistingRightNode && mEditor, "bad state");
56 if (!mExistingRightNode || !mEditor) { return NS_ERROR_NOT_INITIALIZED; }
58 // create a new node
59 nsresult result = mExistingRightNode->CloneNode(false, 1, getter_AddRefs(mNewLeftNode));
60 NS_ASSERTION(((NS_SUCCEEDED(result)) && (mNewLeftNode)), "could not create element.");
61 NS_ENSURE_SUCCESS(result, result);
62 mEditor->MarkNodeDirty(mExistingRightNode);
64 // get the parent node
65 result = mExistingRightNode->GetParentNode(getter_AddRefs(mParent));
66 NS_ENSURE_SUCCESS(result, result);
67 NS_ENSURE_TRUE(mParent, NS_ERROR_NULL_POINTER);
69 // insert the new node
70 result = mEditor->SplitNodeImpl(mExistingRightNode, mOffset, mNewLeftNode, mParent);
71 if (mNewLeftNode) {
72 bool bAdjustSelection;
73 mEditor->ShouldTxnSetSelection(&bAdjustSelection);
74 if (bAdjustSelection)
76 nsCOMPtr<nsISelection> selection;
77 result = mEditor->GetSelection(getter_AddRefs(selection));
78 NS_ENSURE_SUCCESS(result, result);
79 NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
80 result = selection->Collapse(mNewLeftNode, mOffset);
82 else
84 // do nothing - dom range gravity will adjust selection
87 return result;
90 NS_IMETHODIMP SplitElementTxn::UndoTransaction(void)
92 NS_ASSERTION(mEditor && mExistingRightNode && mNewLeftNode && mParent, "bad state");
93 if (!mEditor || !mExistingRightNode || !mNewLeftNode || !mParent) {
94 return NS_ERROR_NOT_INITIALIZED;
97 // this assumes Do inserted the new node in front of the prior existing node
98 nsCOMPtr<nsINode> right = do_QueryInterface(mExistingRightNode);
99 nsCOMPtr<nsINode> left = do_QueryInterface(mNewLeftNode);
100 nsCOMPtr<nsINode> parent = do_QueryInterface(mParent);
101 return mEditor->JoinNodesImpl(right, left, parent);
104 /* redo cannot simply resplit the right node, because subsequent transactions
105 * on the redo stack may depend on the left node existing in its previous state.
107 NS_IMETHODIMP SplitElementTxn::RedoTransaction(void)
109 NS_ASSERTION(mEditor && mExistingRightNode && mNewLeftNode && mParent, "bad state");
110 if (!mEditor || !mExistingRightNode || !mNewLeftNode || !mParent) {
111 return NS_ERROR_NOT_INITIALIZED;
114 nsresult result;
115 nsCOMPtr<nsIDOMNode>resultNode;
116 // first, massage the existing node so it is in its post-split state
117 nsCOMPtr<nsIDOMCharacterData>rightNodeAsText = do_QueryInterface(mExistingRightNode);
118 if (rightNodeAsText)
120 nsresult result = rightNodeAsText->DeleteData(0, mOffset);
121 NS_ENSURE_SUCCESS(result, result);
123 else
125 nsCOMPtr<nsIDOMNode>child;
126 nsCOMPtr<nsIDOMNode>nextSibling;
127 result = mExistingRightNode->GetFirstChild(getter_AddRefs(child));
128 int32_t i;
129 for (i=0; i<mOffset; i++)
131 if (NS_FAILED(result)) {return result;}
132 if (!child) {return NS_ERROR_NULL_POINTER;}
133 child->GetNextSibling(getter_AddRefs(nextSibling));
134 result = mExistingRightNode->RemoveChild(child, getter_AddRefs(resultNode));
135 if (NS_SUCCEEDED(result))
137 result = mNewLeftNode->AppendChild(child, getter_AddRefs(resultNode));
139 child = do_QueryInterface(nextSibling);
142 // second, re-insert the left node into the tree
143 result = mParent->InsertBefore(mNewLeftNode, mExistingRightNode, getter_AddRefs(resultNode));
144 return result;
148 NS_IMETHODIMP SplitElementTxn::GetTxnDescription(nsAString& aString)
150 aString.AssignLiteral("SplitElementTxn");
151 return NS_OK;
154 NS_IMETHODIMP SplitElementTxn::GetNewNode(nsIDOMNode **aNewNode)
156 NS_ENSURE_TRUE(aNewNode, NS_ERROR_NULL_POINTER);
157 NS_ENSURE_TRUE(mNewLeftNode, NS_ERROR_NOT_INITIALIZED);
158 *aNewNode = mNewLeftNode;
159 NS_ADDREF(*aNewNode);
160 return NS_OK;