/editor/libeditor/base/SplitElementTxn.cpp

http://github.com/zpao/v8monkey · C++ · 276 lines · 203 code · 26 blank · 47 comment · 42 complexity · d86cba0486336272b165740735c8edfe MD5 · raw file

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is mozilla.org code.
  16. *
  17. * The Initial Developer of the Original Code is
  18. * Netscape Communications Corporation.
  19. * Portions created by the Initial Developer are Copyright (C) 1998-1999
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. *
  24. * Alternatively, the contents of this file may be used under the terms of
  25. * either of the GNU General Public License Version 2 or later (the "GPL"),
  26. * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. * in which case the provisions of the GPL or the LGPL are applicable instead
  28. * of those above. If you wish to allow use of your version of this file only
  29. * under the terms of either the GPL or the LGPL, and not to allow others to
  30. * use your version of this file under the terms of the MPL, indicate your
  31. * decision by deleting the provisions above and replace them with the notice
  32. * and other provisions required by the GPL or the LGPL. If you do not delete
  33. * the provisions above, a recipient may use your version of this file under
  34. * the terms of any one of the MPL, the GPL or the LGPL.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. #include "SplitElementTxn.h"
  38. #include "nsEditor.h"
  39. #include "nsIDOMNode.h"
  40. #include "nsISelection.h"
  41. #include "nsIDOMCharacterData.h"
  42. #ifdef NS_DEBUG
  43. static bool gNoisy = false;
  44. #endif
  45. // note that aEditor is not refcounted
  46. SplitElementTxn::SplitElementTxn()
  47. : EditTxn()
  48. {
  49. }
  50. NS_IMPL_CYCLE_COLLECTION_CLASS(SplitElementTxn)
  51. NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(SplitElementTxn, EditTxn)
  52. NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mParent)
  53. NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mNewLeftNode)
  54. NS_IMPL_CYCLE_COLLECTION_UNLINK_END
  55. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(SplitElementTxn, EditTxn)
  56. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mParent)
  57. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mNewLeftNode)
  58. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
  59. NS_IMPL_ADDREF_INHERITED(SplitElementTxn, EditTxn)
  60. NS_IMPL_RELEASE_INHERITED(SplitElementTxn, EditTxn)
  61. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SplitElementTxn)
  62. NS_INTERFACE_MAP_END_INHERITING(EditTxn)
  63. NS_IMETHODIMP SplitElementTxn::Init(nsEditor *aEditor,
  64. nsIDOMNode *aNode,
  65. PRInt32 aOffset)
  66. {
  67. NS_ASSERTION(aEditor && aNode, "bad args");
  68. if (!aEditor || !aNode) { return NS_ERROR_NOT_INITIALIZED; }
  69. mEditor = aEditor;
  70. mExistingRightNode = do_QueryInterface(aNode);
  71. mOffset = aOffset;
  72. return NS_OK;
  73. }
  74. NS_IMETHODIMP SplitElementTxn::DoTransaction(void)
  75. {
  76. #ifdef NS_DEBUG
  77. if (gNoisy)
  78. {
  79. printf("%p Do Split of node %p offset %d\n",
  80. static_cast<void*>(this),
  81. static_cast<void*>(mExistingRightNode.get()),
  82. mOffset);
  83. }
  84. #endif
  85. NS_ASSERTION(mExistingRightNode && mEditor, "bad state");
  86. if (!mExistingRightNode || !mEditor) { return NS_ERROR_NOT_INITIALIZED; }
  87. // create a new node
  88. nsresult result = mExistingRightNode->CloneNode(false, getter_AddRefs(mNewLeftNode));
  89. NS_ASSERTION(((NS_SUCCEEDED(result)) && (mNewLeftNode)), "could not create element.");
  90. NS_ENSURE_SUCCESS(result, result);
  91. NS_ENSURE_TRUE(mNewLeftNode, NS_ERROR_NULL_POINTER);
  92. mEditor->MarkNodeDirty(mExistingRightNode);
  93. #ifdef NS_DEBUG
  94. if (gNoisy)
  95. {
  96. printf(" created left node = %p\n",
  97. static_cast<void*>(mNewLeftNode.get()));
  98. }
  99. #endif
  100. // get the parent node
  101. result = mExistingRightNode->GetParentNode(getter_AddRefs(mParent));
  102. NS_ENSURE_SUCCESS(result, result);
  103. NS_ENSURE_TRUE(mParent, NS_ERROR_NULL_POINTER);
  104. // insert the new node
  105. result = mEditor->SplitNodeImpl(mExistingRightNode, mOffset, mNewLeftNode, mParent);
  106. if (mNewLeftNode) {
  107. bool bAdjustSelection;
  108. mEditor->ShouldTxnSetSelection(&bAdjustSelection);
  109. if (bAdjustSelection)
  110. {
  111. nsCOMPtr<nsISelection> selection;
  112. result = mEditor->GetSelection(getter_AddRefs(selection));
  113. NS_ENSURE_SUCCESS(result, result);
  114. NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
  115. result = selection->Collapse(mNewLeftNode, mOffset);
  116. }
  117. else
  118. {
  119. // do nothing - dom range gravity will adjust selection
  120. }
  121. }
  122. return result;
  123. }
  124. NS_IMETHODIMP SplitElementTxn::UndoTransaction(void)
  125. {
  126. #ifdef NS_DEBUG
  127. if (gNoisy) {
  128. printf("%p Undo Split of existing node %p and new node %p offset %d\n",
  129. static_cast<void*>(this),
  130. static_cast<void*>(mExistingRightNode.get()),
  131. static_cast<void*>(mNewLeftNode.get()),
  132. mOffset);
  133. }
  134. #endif
  135. NS_ASSERTION(mEditor && mExistingRightNode && mNewLeftNode && mParent, "bad state");
  136. if (!mEditor || !mExistingRightNode || !mNewLeftNode || !mParent) {
  137. return NS_ERROR_NOT_INITIALIZED;
  138. }
  139. // this assumes Do inserted the new node in front of the prior existing node
  140. nsresult result = mEditor->JoinNodesImpl(mExistingRightNode, mNewLeftNode, mParent, false);
  141. #ifdef NS_DEBUG
  142. if (gNoisy)
  143. {
  144. printf("** after join left child node %p into right node %p\n",
  145. static_cast<void*>(mNewLeftNode.get()),
  146. static_cast<void*>(mExistingRightNode.get()));
  147. if (gNoisy) {mEditor->DebugDumpContent(); } // DEBUG
  148. }
  149. if (NS_SUCCEEDED(result))
  150. {
  151. if (gNoisy)
  152. {
  153. printf(" left node = %p removed\n",
  154. static_cast<void*>(mNewLeftNode.get()));
  155. }
  156. }
  157. #endif
  158. return result;
  159. }
  160. /* redo cannot simply resplit the right node, because subsequent transactions
  161. * on the redo stack may depend on the left node existing in its previous state.
  162. */
  163. NS_IMETHODIMP SplitElementTxn::RedoTransaction(void)
  164. {
  165. NS_ASSERTION(mEditor && mExistingRightNode && mNewLeftNode && mParent, "bad state");
  166. if (!mEditor || !mExistingRightNode || !mNewLeftNode || !mParent) {
  167. return NS_ERROR_NOT_INITIALIZED;
  168. }
  169. #ifdef NS_DEBUG
  170. if (gNoisy) {
  171. printf("%p Redo Split of existing node %p and new node %p offset %d\n",
  172. static_cast<void*>(this),
  173. static_cast<void*>(mExistingRightNode.get()),
  174. static_cast<void*>(mNewLeftNode.get()),
  175. mOffset);
  176. if (gNoisy) {mEditor->DebugDumpContent(); } // DEBUG
  177. }
  178. #endif
  179. nsresult result;
  180. nsCOMPtr<nsIDOMNode>resultNode;
  181. // first, massage the existing node so it is in its post-split state
  182. nsCOMPtr<nsIDOMCharacterData>rightNodeAsText = do_QueryInterface(mExistingRightNode);
  183. if (rightNodeAsText)
  184. {
  185. result = rightNodeAsText->DeleteData(0, mOffset);
  186. #ifdef NS_DEBUG
  187. if (gNoisy)
  188. {
  189. printf("** after delete of text in right text node %p offset %d\n",
  190. static_cast<void*>(rightNodeAsText.get()),
  191. mOffset);
  192. mEditor->DebugDumpContent(); // DEBUG
  193. }
  194. #endif
  195. }
  196. else
  197. {
  198. nsCOMPtr<nsIDOMNode>child;
  199. nsCOMPtr<nsIDOMNode>nextSibling;
  200. result = mExistingRightNode->GetFirstChild(getter_AddRefs(child));
  201. PRInt32 i;
  202. for (i=0; i<mOffset; i++)
  203. {
  204. if (NS_FAILED(result)) {return result;}
  205. if (!child) {return NS_ERROR_NULL_POINTER;}
  206. child->GetNextSibling(getter_AddRefs(nextSibling));
  207. result = mExistingRightNode->RemoveChild(child, getter_AddRefs(resultNode));
  208. if (NS_SUCCEEDED(result))
  209. {
  210. result = mNewLeftNode->AppendChild(child, getter_AddRefs(resultNode));
  211. #ifdef NS_DEBUG
  212. if (gNoisy)
  213. {
  214. printf("** move child node %p from right node %p to left node %p\n",
  215. static_cast<void*>(child.get()),
  216. static_cast<void*>(mExistingRightNode.get()),
  217. static_cast<void*>(mNewLeftNode.get()));
  218. if (gNoisy) {mEditor->DebugDumpContent(); } // DEBUG
  219. }
  220. #endif
  221. }
  222. child = do_QueryInterface(nextSibling);
  223. }
  224. }
  225. // second, re-insert the left node into the tree
  226. result = mParent->InsertBefore(mNewLeftNode, mExistingRightNode, getter_AddRefs(resultNode));
  227. #ifdef NS_DEBUG
  228. if (gNoisy)
  229. {
  230. printf("** reinsert left child node %p before right node %p\n",
  231. static_cast<void*>(mNewLeftNode.get()),
  232. static_cast<void*>(mExistingRightNode.get()));
  233. if (gNoisy) {mEditor->DebugDumpContent(); } // DEBUG
  234. }
  235. #endif
  236. return result;
  237. }
  238. NS_IMETHODIMP SplitElementTxn::GetTxnDescription(nsAString& aString)
  239. {
  240. aString.AssignLiteral("SplitElementTxn");
  241. return NS_OK;
  242. }
  243. NS_IMETHODIMP SplitElementTxn::GetNewNode(nsIDOMNode **aNewNode)
  244. {
  245. NS_ENSURE_TRUE(aNewNode, NS_ERROR_NULL_POINTER);
  246. NS_ENSURE_TRUE(mNewLeftNode, NS_ERROR_NOT_INITIALIZED);
  247. *aNewNode = mNewLeftNode;
  248. NS_ADDREF(*aNewNode);
  249. return NS_OK;
  250. }