Thu, 22 Jan 2015 13:21:57 +0100
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 "SplitElementTxn.h" |
michael@0 | 9 | #include "nsAString.h" |
michael@0 | 10 | #include "nsDebug.h" // for NS_ASSERTION, etc |
michael@0 | 11 | #include "nsEditor.h" // for nsEditor |
michael@0 | 12 | #include "nsError.h" // for NS_ERROR_NOT_INITIALIZED, etc |
michael@0 | 13 | #include "nsIDOMCharacterData.h" // for nsIDOMCharacterData |
michael@0 | 14 | #include "nsIDOMNode.h" // for nsIDOMNode |
michael@0 | 15 | #include "nsIEditor.h" // for nsEditor::DebugDumpContent, etc |
michael@0 | 16 | #include "nsISelection.h" // for nsISelection |
michael@0 | 17 | #include "nsISupportsUtils.h" // for NS_ADDREF |
michael@0 | 18 | |
michael@0 | 19 | // note that aEditor is not refcounted |
michael@0 | 20 | SplitElementTxn::SplitElementTxn() |
michael@0 | 21 | : EditTxn() |
michael@0 | 22 | { |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | NS_IMPL_CYCLE_COLLECTION_INHERITED(SplitElementTxn, EditTxn, |
michael@0 | 26 | mParent, |
michael@0 | 27 | mNewLeftNode) |
michael@0 | 28 | |
michael@0 | 29 | NS_IMPL_ADDREF_INHERITED(SplitElementTxn, EditTxn) |
michael@0 | 30 | NS_IMPL_RELEASE_INHERITED(SplitElementTxn, EditTxn) |
michael@0 | 31 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SplitElementTxn) |
michael@0 | 32 | NS_INTERFACE_MAP_END_INHERITING(EditTxn) |
michael@0 | 33 | |
michael@0 | 34 | NS_IMETHODIMP SplitElementTxn::Init(nsEditor *aEditor, |
michael@0 | 35 | nsIDOMNode *aNode, |
michael@0 | 36 | int32_t aOffset) |
michael@0 | 37 | { |
michael@0 | 38 | NS_ASSERTION(aEditor && aNode, "bad args"); |
michael@0 | 39 | if (!aEditor || !aNode) { return NS_ERROR_NOT_INITIALIZED; } |
michael@0 | 40 | |
michael@0 | 41 | mEditor = aEditor; |
michael@0 | 42 | mExistingRightNode = do_QueryInterface(aNode); |
michael@0 | 43 | mOffset = aOffset; |
michael@0 | 44 | return NS_OK; |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | NS_IMETHODIMP SplitElementTxn::DoTransaction(void) |
michael@0 | 48 | { |
michael@0 | 49 | NS_ASSERTION(mExistingRightNode && mEditor, "bad state"); |
michael@0 | 50 | if (!mExistingRightNode || !mEditor) { return NS_ERROR_NOT_INITIALIZED; } |
michael@0 | 51 | |
michael@0 | 52 | // create a new node |
michael@0 | 53 | nsresult result = mExistingRightNode->CloneNode(false, 1, getter_AddRefs(mNewLeftNode)); |
michael@0 | 54 | NS_ASSERTION(((NS_SUCCEEDED(result)) && (mNewLeftNode)), "could not create element."); |
michael@0 | 55 | NS_ENSURE_SUCCESS(result, result); |
michael@0 | 56 | NS_ENSURE_TRUE(mNewLeftNode, NS_ERROR_NULL_POINTER); |
michael@0 | 57 | mEditor->MarkNodeDirty(mExistingRightNode); |
michael@0 | 58 | |
michael@0 | 59 | // get the parent node |
michael@0 | 60 | result = mExistingRightNode->GetParentNode(getter_AddRefs(mParent)); |
michael@0 | 61 | NS_ENSURE_SUCCESS(result, result); |
michael@0 | 62 | NS_ENSURE_TRUE(mParent, NS_ERROR_NULL_POINTER); |
michael@0 | 63 | |
michael@0 | 64 | // insert the new node |
michael@0 | 65 | result = mEditor->SplitNodeImpl(mExistingRightNode, mOffset, mNewLeftNode, mParent); |
michael@0 | 66 | if (mNewLeftNode) { |
michael@0 | 67 | bool bAdjustSelection; |
michael@0 | 68 | mEditor->ShouldTxnSetSelection(&bAdjustSelection); |
michael@0 | 69 | if (bAdjustSelection) |
michael@0 | 70 | { |
michael@0 | 71 | nsCOMPtr<nsISelection> selection; |
michael@0 | 72 | result = mEditor->GetSelection(getter_AddRefs(selection)); |
michael@0 | 73 | NS_ENSURE_SUCCESS(result, result); |
michael@0 | 74 | NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER); |
michael@0 | 75 | result = selection->Collapse(mNewLeftNode, mOffset); |
michael@0 | 76 | } |
michael@0 | 77 | else |
michael@0 | 78 | { |
michael@0 | 79 | // do nothing - dom range gravity will adjust selection |
michael@0 | 80 | } |
michael@0 | 81 | } |
michael@0 | 82 | return result; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | NS_IMETHODIMP SplitElementTxn::UndoTransaction(void) |
michael@0 | 86 | { |
michael@0 | 87 | NS_ASSERTION(mEditor && mExistingRightNode && mNewLeftNode && mParent, "bad state"); |
michael@0 | 88 | if (!mEditor || !mExistingRightNode || !mNewLeftNode || !mParent) { |
michael@0 | 89 | return NS_ERROR_NOT_INITIALIZED; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | // this assumes Do inserted the new node in front of the prior existing node |
michael@0 | 93 | nsCOMPtr<nsINode> rightNode = do_QueryInterface(mExistingRightNode); |
michael@0 | 94 | nsCOMPtr<nsINode> leftNode = do_QueryInterface(mNewLeftNode); |
michael@0 | 95 | nsCOMPtr<nsINode> parent = do_QueryInterface(mParent); |
michael@0 | 96 | NS_ENSURE_TRUE(rightNode && leftNode && parent, NS_ERROR_FAILURE); |
michael@0 | 97 | return mEditor->JoinNodesImpl(rightNode, leftNode, parent); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | /* redo cannot simply resplit the right node, because subsequent transactions |
michael@0 | 101 | * on the redo stack may depend on the left node existing in its previous state. |
michael@0 | 102 | */ |
michael@0 | 103 | NS_IMETHODIMP SplitElementTxn::RedoTransaction(void) |
michael@0 | 104 | { |
michael@0 | 105 | NS_ASSERTION(mEditor && mExistingRightNode && mNewLeftNode && mParent, "bad state"); |
michael@0 | 106 | if (!mEditor || !mExistingRightNode || !mNewLeftNode || !mParent) { |
michael@0 | 107 | return NS_ERROR_NOT_INITIALIZED; |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | nsresult result; |
michael@0 | 111 | nsCOMPtr<nsIDOMNode>resultNode; |
michael@0 | 112 | // first, massage the existing node so it is in its post-split state |
michael@0 | 113 | nsCOMPtr<nsIDOMCharacterData>rightNodeAsText = do_QueryInterface(mExistingRightNode); |
michael@0 | 114 | if (rightNodeAsText) |
michael@0 | 115 | { |
michael@0 | 116 | nsresult result = rightNodeAsText->DeleteData(0, mOffset); |
michael@0 | 117 | NS_ENSURE_SUCCESS(result, result); |
michael@0 | 118 | } |
michael@0 | 119 | else |
michael@0 | 120 | { |
michael@0 | 121 | nsCOMPtr<nsIDOMNode>child; |
michael@0 | 122 | nsCOMPtr<nsIDOMNode>nextSibling; |
michael@0 | 123 | result = mExistingRightNode->GetFirstChild(getter_AddRefs(child)); |
michael@0 | 124 | int32_t i; |
michael@0 | 125 | for (i=0; i<mOffset; i++) |
michael@0 | 126 | { |
michael@0 | 127 | if (NS_FAILED(result)) {return result;} |
michael@0 | 128 | if (!child) {return NS_ERROR_NULL_POINTER;} |
michael@0 | 129 | child->GetNextSibling(getter_AddRefs(nextSibling)); |
michael@0 | 130 | result = mExistingRightNode->RemoveChild(child, getter_AddRefs(resultNode)); |
michael@0 | 131 | if (NS_SUCCEEDED(result)) |
michael@0 | 132 | { |
michael@0 | 133 | result = mNewLeftNode->AppendChild(child, getter_AddRefs(resultNode)); |
michael@0 | 134 | } |
michael@0 | 135 | child = do_QueryInterface(nextSibling); |
michael@0 | 136 | } |
michael@0 | 137 | } |
michael@0 | 138 | // second, re-insert the left node into the tree |
michael@0 | 139 | result = mParent->InsertBefore(mNewLeftNode, mExistingRightNode, getter_AddRefs(resultNode)); |
michael@0 | 140 | return result; |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | |
michael@0 | 144 | NS_IMETHODIMP SplitElementTxn::GetTxnDescription(nsAString& aString) |
michael@0 | 145 | { |
michael@0 | 146 | aString.AssignLiteral("SplitElementTxn"); |
michael@0 | 147 | return NS_OK; |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | NS_IMETHODIMP SplitElementTxn::GetNewNode(nsIDOMNode **aNewNode) |
michael@0 | 151 | { |
michael@0 | 152 | NS_ENSURE_TRUE(aNewNode, NS_ERROR_NULL_POINTER); |
michael@0 | 153 | NS_ENSURE_TRUE(mNewLeftNode, NS_ERROR_NOT_INITIALIZED); |
michael@0 | 154 | *aNewNode = mNewLeftNode; |
michael@0 | 155 | NS_ADDREF(*aNewNode); |
michael@0 | 156 | return NS_OK; |
michael@0 | 157 | } |