editor/libeditor/base/JoinElementTxn.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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 "JoinElementTxn.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_NULL_POINTER, etc
michael@0 13 #include "nsIDOMCharacterData.h" // for nsIDOMCharacterData
michael@0 14 #include "nsIEditor.h" // for nsEditor::IsModifiableNode
michael@0 15 #include "nsINode.h" // for nsINode
michael@0 16 #include "nsISupportsImpl.h" // for EditTxn::QueryInterface, etc
michael@0 17
michael@0 18 JoinElementTxn::JoinElementTxn()
michael@0 19 : EditTxn()
michael@0 20 {
michael@0 21 }
michael@0 22
michael@0 23 NS_IMPL_CYCLE_COLLECTION_INHERITED(JoinElementTxn, EditTxn,
michael@0 24 mLeftNode,
michael@0 25 mRightNode,
michael@0 26 mParent)
michael@0 27
michael@0 28 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(JoinElementTxn)
michael@0 29 NS_INTERFACE_MAP_END_INHERITING(EditTxn)
michael@0 30
michael@0 31 NS_IMETHODIMP JoinElementTxn::Init(nsEditor *aEditor,
michael@0 32 nsIDOMNode *aLeftNode,
michael@0 33 nsIDOMNode *aRightNode)
michael@0 34 {
michael@0 35 NS_PRECONDITION((aEditor && aLeftNode && aRightNode), "null arg");
michael@0 36 if (!aEditor || !aLeftNode || !aRightNode) { return NS_ERROR_NULL_POINTER; }
michael@0 37 mEditor = aEditor;
michael@0 38 mLeftNode = do_QueryInterface(aLeftNode);
michael@0 39 nsCOMPtr<nsIDOMNode>leftParent;
michael@0 40 nsresult result = mLeftNode->GetParentNode(getter_AddRefs(leftParent));
michael@0 41 NS_ENSURE_SUCCESS(result, result);
michael@0 42 if (!mEditor->IsModifiableNode(leftParent)) {
michael@0 43 return NS_ERROR_FAILURE;
michael@0 44 }
michael@0 45 mRightNode = do_QueryInterface(aRightNode);
michael@0 46 mOffset = 0;
michael@0 47 return NS_OK;
michael@0 48 }
michael@0 49
michael@0 50 // After DoTransaction() and RedoTransaction(), the left node is removed from the content tree and right node remains.
michael@0 51 NS_IMETHODIMP JoinElementTxn::DoTransaction(void)
michael@0 52 {
michael@0 53 NS_PRECONDITION((mEditor && mLeftNode && mRightNode), "null arg");
michael@0 54 if (!mEditor || !mLeftNode || !mRightNode) { return NS_ERROR_NOT_INITIALIZED; }
michael@0 55
michael@0 56 // get the parent node
michael@0 57 nsCOMPtr<nsIDOMNode> leftParent;
michael@0 58 mLeftNode->GetParentNode(getter_AddRefs(leftParent));
michael@0 59 NS_ENSURE_TRUE(leftParent, NS_ERROR_NULL_POINTER);
michael@0 60
michael@0 61 // verify that mLeftNode and mRightNode have the same parent
michael@0 62 nsCOMPtr<nsIDOMNode> rightParent;
michael@0 63 mRightNode->GetParentNode(getter_AddRefs(rightParent));
michael@0 64 NS_ENSURE_TRUE(rightParent, NS_ERROR_NULL_POINTER);
michael@0 65
michael@0 66 if (leftParent != rightParent) {
michael@0 67 NS_ASSERTION(false, "2 nodes do not have same parent");
michael@0 68 return NS_ERROR_INVALID_ARG;
michael@0 69 }
michael@0 70
michael@0 71 // set this instance mParent.
michael@0 72 // Other methods will see a non-null mParent and know all is well
michael@0 73 mParent = leftParent;
michael@0 74 nsCOMPtr<nsINode> leftNode = do_QueryInterface(mLeftNode);
michael@0 75 nsCOMPtr<nsINode> rightNode = do_QueryInterface(mRightNode);
michael@0 76 nsCOMPtr<nsINode> parent = do_QueryInterface(mParent);
michael@0 77 NS_ENSURE_TRUE(leftNode && rightNode && parent, NS_ERROR_FAILURE);
michael@0 78 mOffset = leftNode->Length();
michael@0 79
michael@0 80 return mEditor->JoinNodesImpl(rightNode, leftNode, parent);
michael@0 81 }
michael@0 82
michael@0 83 //XXX: what if instead of split, we just deleted the unneeded children of mRight
michael@0 84 // and re-inserted mLeft?
michael@0 85 NS_IMETHODIMP JoinElementTxn::UndoTransaction(void)
michael@0 86 {
michael@0 87 NS_ASSERTION(mRightNode && mLeftNode && mParent, "bad state");
michael@0 88 if (!mRightNode || !mLeftNode || !mParent) { return NS_ERROR_NOT_INITIALIZED; }
michael@0 89 nsresult result;
michael@0 90 nsCOMPtr<nsIDOMNode>resultNode;
michael@0 91 // first, massage the existing node so it is in its post-split state
michael@0 92 nsCOMPtr<nsIDOMCharacterData>rightNodeAsText = do_QueryInterface(mRightNode);
michael@0 93 if (rightNodeAsText)
michael@0 94 {
michael@0 95 result = rightNodeAsText->DeleteData(0, mOffset);
michael@0 96 }
michael@0 97 else
michael@0 98 {
michael@0 99 nsCOMPtr<nsIDOMNode>child;
michael@0 100 result = mRightNode->GetFirstChild(getter_AddRefs(child));
michael@0 101 nsCOMPtr<nsIDOMNode>nextSibling;
michael@0 102 uint32_t i;
michael@0 103 for (i=0; i<mOffset; i++)
michael@0 104 {
michael@0 105 if (NS_FAILED(result)) {return result;}
michael@0 106 if (!child) {return NS_ERROR_NULL_POINTER;}
michael@0 107 child->GetNextSibling(getter_AddRefs(nextSibling));
michael@0 108 result = mLeftNode->AppendChild(child, getter_AddRefs(resultNode));
michael@0 109 child = do_QueryInterface(nextSibling);
michael@0 110 }
michael@0 111 }
michael@0 112 // second, re-insert the left node into the tree
michael@0 113 result = mParent->InsertBefore(mLeftNode, mRightNode, getter_AddRefs(resultNode));
michael@0 114 return result;
michael@0 115
michael@0 116 }
michael@0 117
michael@0 118 NS_IMETHODIMP JoinElementTxn::GetTxnDescription(nsAString& aString)
michael@0 119 {
michael@0 120 aString.AssignLiteral("JoinElementTxn");
michael@0 121 return NS_OK;
michael@0 122 }

mercurial