editor/libeditor/base/InsertTextTxn.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 "InsertTextTxn.h"
michael@0 9 #include "nsAString.h"
michael@0 10 #include "nsDebug.h" // for NS_ASSERTION, etc
michael@0 11 #include "nsError.h" // for NS_OK, etc
michael@0 12 #include "nsIDOMCharacterData.h" // for nsIDOMCharacterData
michael@0 13 #include "nsIEditor.h" // for nsIEditor
michael@0 14 #include "nsISelection.h" // for nsISelection
michael@0 15 #include "nsISupportsUtils.h" // for NS_ADDREF_THIS, NS_RELEASE
michael@0 16 #include "nsITransaction.h" // for nsITransaction
michael@0 17
michael@0 18 InsertTextTxn::InsertTextTxn()
michael@0 19 : EditTxn()
michael@0 20 {
michael@0 21 }
michael@0 22
michael@0 23 NS_IMPL_CYCLE_COLLECTION_INHERITED(InsertTextTxn, EditTxn,
michael@0 24 mElement)
michael@0 25
michael@0 26 NS_IMPL_ADDREF_INHERITED(InsertTextTxn, EditTxn)
michael@0 27 NS_IMPL_RELEASE_INHERITED(InsertTextTxn, EditTxn)
michael@0 28 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(InsertTextTxn)
michael@0 29 if (aIID.Equals(InsertTextTxn::GetCID())) {
michael@0 30 *aInstancePtr = (void*)(InsertTextTxn*)this;
michael@0 31 NS_ADDREF_THIS();
michael@0 32 return NS_OK;
michael@0 33 } else
michael@0 34 NS_INTERFACE_MAP_END_INHERITING(EditTxn)
michael@0 35
michael@0 36 NS_IMETHODIMP InsertTextTxn::Init(nsIDOMCharacterData *aElement,
michael@0 37 uint32_t aOffset,
michael@0 38 const nsAString &aStringToInsert,
michael@0 39 nsIEditor *aEditor)
michael@0 40 {
michael@0 41 #if 0
michael@0 42 nsAutoString text;
michael@0 43 aElement->GetData(text);
michael@0 44 printf("InsertTextTxn: Offset to insert at = %d. Text of the node to insert into:\n", aOffset);
michael@0 45 wprintf(text.get());
michael@0 46 printf("\n");
michael@0 47 #endif
michael@0 48
michael@0 49 NS_ASSERTION(aElement && aEditor, "bad args");
michael@0 50 NS_ENSURE_TRUE(aElement && aEditor, NS_ERROR_NULL_POINTER);
michael@0 51
michael@0 52 mElement = do_QueryInterface(aElement);
michael@0 53 mOffset = aOffset;
michael@0 54 mStringToInsert = aStringToInsert;
michael@0 55 mEditor = aEditor;
michael@0 56 return NS_OK;
michael@0 57 }
michael@0 58
michael@0 59 NS_IMETHODIMP InsertTextTxn::DoTransaction(void)
michael@0 60 {
michael@0 61 NS_ASSERTION(mElement && mEditor, "bad state");
michael@0 62 if (!mElement || !mEditor) { return NS_ERROR_NOT_INITIALIZED; }
michael@0 63
michael@0 64 nsresult result = mElement->InsertData(mOffset, mStringToInsert);
michael@0 65 NS_ENSURE_SUCCESS(result, result);
michael@0 66
michael@0 67 // only set selection to insertion point if editor gives permission
michael@0 68 bool bAdjustSelection;
michael@0 69 mEditor->ShouldTxnSetSelection(&bAdjustSelection);
michael@0 70 if (bAdjustSelection)
michael@0 71 {
michael@0 72 nsCOMPtr<nsISelection> selection;
michael@0 73 result = mEditor->GetSelection(getter_AddRefs(selection));
michael@0 74 NS_ENSURE_SUCCESS(result, result);
michael@0 75 NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
michael@0 76 result = selection->Collapse(mElement, mOffset+mStringToInsert.Length());
michael@0 77 NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after insert.");
michael@0 78 }
michael@0 79 else
michael@0 80 {
michael@0 81 // do nothing - dom range gravity will adjust selection
michael@0 82 }
michael@0 83
michael@0 84 return result;
michael@0 85 }
michael@0 86
michael@0 87 NS_IMETHODIMP InsertTextTxn::UndoTransaction(void)
michael@0 88 {
michael@0 89 NS_ASSERTION(mElement && mEditor, "bad state");
michael@0 90 if (!mElement || !mEditor) { return NS_ERROR_NOT_INITIALIZED; }
michael@0 91
michael@0 92 uint32_t length = mStringToInsert.Length();
michael@0 93 return mElement->DeleteData(mOffset, length);
michael@0 94 }
michael@0 95
michael@0 96 NS_IMETHODIMP InsertTextTxn::Merge(nsITransaction *aTransaction, bool *aDidMerge)
michael@0 97 {
michael@0 98 // set out param default value
michael@0 99 if (aDidMerge)
michael@0 100 *aDidMerge = false;
michael@0 101 nsresult result = NS_OK;
michael@0 102 if (aDidMerge && aTransaction)
michael@0 103 {
michael@0 104 // if aTransaction is a InsertTextTxn, and if the selection hasn't changed,
michael@0 105 // then absorb it
michael@0 106 InsertTextTxn *otherInsTxn = nullptr;
michael@0 107 aTransaction->QueryInterface(InsertTextTxn::GetCID(), (void **)&otherInsTxn);
michael@0 108 if (otherInsTxn)
michael@0 109 {
michael@0 110 if (IsSequentialInsert(otherInsTxn))
michael@0 111 {
michael@0 112 nsAutoString otherData;
michael@0 113 otherInsTxn->GetData(otherData);
michael@0 114 mStringToInsert += otherData;
michael@0 115 *aDidMerge = true;
michael@0 116 }
michael@0 117 NS_RELEASE(otherInsTxn);
michael@0 118 }
michael@0 119 }
michael@0 120 return result;
michael@0 121 }
michael@0 122
michael@0 123 NS_IMETHODIMP InsertTextTxn::GetTxnDescription(nsAString& aString)
michael@0 124 {
michael@0 125 aString.AssignLiteral("InsertTextTxn: ");
michael@0 126 aString += mStringToInsert;
michael@0 127 return NS_OK;
michael@0 128 }
michael@0 129
michael@0 130 /* ============ protected methods ================== */
michael@0 131
michael@0 132 NS_IMETHODIMP InsertTextTxn::GetData(nsString& aResult)
michael@0 133 {
michael@0 134 aResult = mStringToInsert;
michael@0 135 return NS_OK;
michael@0 136 }
michael@0 137
michael@0 138 bool InsertTextTxn::IsSequentialInsert(InsertTextTxn *aOtherTxn)
michael@0 139 {
michael@0 140 NS_ASSERTION(aOtherTxn, "null param");
michael@0 141 if (aOtherTxn && aOtherTxn->mElement == mElement)
michael@0 142 {
michael@0 143 // here, we need to compare offsets.
michael@0 144 int32_t length = mStringToInsert.Length();
michael@0 145 if (aOtherTxn->mOffset == (mOffset + length))
michael@0 146 return true;
michael@0 147 }
michael@0 148 return false;
michael@0 149 }

mercurial