editor/libeditor/base/CreateElementTxn.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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>
michael@0 7
michael@0 8 #include "CreateElementTxn.h"
michael@0 9 #include "mozilla/dom/Element.h"
michael@0 10 #include "nsAlgorithm.h"
michael@0 11 #include "nsDebug.h"
michael@0 12 #include "nsEditor.h"
michael@0 13 #include "nsError.h"
michael@0 14 #include "nsIContent.h"
michael@0 15 #include "nsIDOMCharacterData.h"
michael@0 16 #include "nsIEditor.h"
michael@0 17 #include "nsINode.h"
michael@0 18 #include "nsISelection.h"
michael@0 19 #include "nsISupportsUtils.h"
michael@0 20 #include "nsMemory.h"
michael@0 21 #include "nsReadableUtils.h"
michael@0 22 #include "nsStringFwd.h"
michael@0 23 #include "nsString.h"
michael@0 24 #include "nsAString.h"
michael@0 25 #include <algorithm>
michael@0 26
michael@0 27 using namespace mozilla;
michael@0 28
michael@0 29 CreateElementTxn::CreateElementTxn()
michael@0 30 : EditTxn()
michael@0 31 {
michael@0 32 }
michael@0 33
michael@0 34 NS_IMPL_CYCLE_COLLECTION_INHERITED(CreateElementTxn, EditTxn,
michael@0 35 mParent,
michael@0 36 mNewNode,
michael@0 37 mRefNode)
michael@0 38
michael@0 39 NS_IMPL_ADDREF_INHERITED(CreateElementTxn, EditTxn)
michael@0 40 NS_IMPL_RELEASE_INHERITED(CreateElementTxn, EditTxn)
michael@0 41 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CreateElementTxn)
michael@0 42 NS_INTERFACE_MAP_END_INHERITING(EditTxn)
michael@0 43 NS_IMETHODIMP CreateElementTxn::Init(nsEditor *aEditor,
michael@0 44 const nsAString &aTag,
michael@0 45 nsIDOMNode *aParent,
michael@0 46 uint32_t aOffsetInParent)
michael@0 47 {
michael@0 48 NS_ASSERTION(aEditor&&aParent, "null args");
michael@0 49 if (!aEditor || !aParent) { return NS_ERROR_NULL_POINTER; }
michael@0 50
michael@0 51 mEditor = aEditor;
michael@0 52 mTag = aTag;
michael@0 53 mParent = do_QueryInterface(aParent);
michael@0 54 mOffsetInParent = aOffsetInParent;
michael@0 55 return NS_OK;
michael@0 56 }
michael@0 57
michael@0 58
michael@0 59 NS_IMETHODIMP CreateElementTxn::DoTransaction(void)
michael@0 60 {
michael@0 61 NS_ASSERTION(mEditor && mParent, "bad state");
michael@0 62 NS_ENSURE_TRUE(mEditor && mParent, NS_ERROR_NOT_INITIALIZED);
michael@0 63
michael@0 64 nsCOMPtr<dom::Element> newContent;
michael@0 65
michael@0 66 //new call to use instead to get proper HTML element, bug# 39919
michael@0 67 nsresult result = mEditor->CreateHTMLContent(mTag, getter_AddRefs(newContent));
michael@0 68 NS_ENSURE_SUCCESS(result, result);
michael@0 69 NS_ENSURE_STATE(newContent);
michael@0 70
michael@0 71 mNewNode = newContent->AsDOMNode();
michael@0 72 // Try to insert formatting whitespace for the new node:
michael@0 73 mEditor->MarkNodeDirty(mNewNode);
michael@0 74
michael@0 75 // insert the new node
michael@0 76 if (CreateElementTxn::eAppend == int32_t(mOffsetInParent)) {
michael@0 77 nsCOMPtr<nsIDOMNode> resultNode;
michael@0 78 return mParent->AppendChild(mNewNode, getter_AddRefs(resultNode));
michael@0 79 }
michael@0 80
michael@0 81 nsCOMPtr<nsINode> parent = do_QueryInterface(mParent);
michael@0 82 NS_ENSURE_STATE(parent);
michael@0 83
michael@0 84 mOffsetInParent = XPCOM_MIN(mOffsetInParent, parent->GetChildCount());
michael@0 85
michael@0 86 // note, it's ok for mRefNode to be null. that means append
michael@0 87 nsIContent* refNode = parent->GetChildAt(mOffsetInParent);
michael@0 88 mRefNode = refNode ? refNode->AsDOMNode() : nullptr;
michael@0 89
michael@0 90 nsCOMPtr<nsIDOMNode> resultNode;
michael@0 91 result = mParent->InsertBefore(mNewNode, mRefNode, getter_AddRefs(resultNode));
michael@0 92 NS_ENSURE_SUCCESS(result, result);
michael@0 93
michael@0 94 // only set selection to insertion point if editor gives permission
michael@0 95 bool bAdjustSelection;
michael@0 96 mEditor->ShouldTxnSetSelection(&bAdjustSelection);
michael@0 97 if (!bAdjustSelection) {
michael@0 98 // do nothing - dom range gravity will adjust selection
michael@0 99 return NS_OK;
michael@0 100 }
michael@0 101
michael@0 102 nsCOMPtr<nsISelection> selection;
michael@0 103 result = mEditor->GetSelection(getter_AddRefs(selection));
michael@0 104 NS_ENSURE_SUCCESS(result, result);
michael@0 105 NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
michael@0 106
michael@0 107 nsCOMPtr<nsIContent> parentContent = do_QueryInterface(mParent);
michael@0 108 NS_ENSURE_STATE(parentContent);
michael@0 109
michael@0 110 result = selection->CollapseNative(parentContent,
michael@0 111 parentContent->IndexOf(newContent) + 1);
michael@0 112 NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after insert.");
michael@0 113 return result;
michael@0 114 }
michael@0 115
michael@0 116 NS_IMETHODIMP CreateElementTxn::UndoTransaction(void)
michael@0 117 {
michael@0 118 NS_ASSERTION(mEditor && mParent, "bad state");
michael@0 119 NS_ENSURE_TRUE(mEditor && mParent, NS_ERROR_NOT_INITIALIZED);
michael@0 120
michael@0 121 nsCOMPtr<nsIDOMNode> resultNode;
michael@0 122 return mParent->RemoveChild(mNewNode, getter_AddRefs(resultNode));
michael@0 123 }
michael@0 124
michael@0 125 NS_IMETHODIMP CreateElementTxn::RedoTransaction(void)
michael@0 126 {
michael@0 127 NS_ASSERTION(mEditor && mParent, "bad state");
michael@0 128 NS_ENSURE_TRUE(mEditor && mParent, NS_ERROR_NOT_INITIALIZED);
michael@0 129
michael@0 130 // first, reset mNewNode so it has no attributes or content
michael@0 131 nsCOMPtr<nsIDOMCharacterData>nodeAsText = do_QueryInterface(mNewNode);
michael@0 132 if (nodeAsText)
michael@0 133 {
michael@0 134 nodeAsText->SetData(EmptyString());
michael@0 135 }
michael@0 136
michael@0 137 // now, reinsert mNewNode
michael@0 138 nsCOMPtr<nsIDOMNode> resultNode;
michael@0 139 return mParent->InsertBefore(mNewNode, mRefNode, getter_AddRefs(resultNode));
michael@0 140 }
michael@0 141
michael@0 142 NS_IMETHODIMP CreateElementTxn::GetTxnDescription(nsAString& aString)
michael@0 143 {
michael@0 144 aString.AssignLiteral("CreateElementTxn: ");
michael@0 145 aString += mTag;
michael@0 146 return NS_OK;
michael@0 147 }
michael@0 148
michael@0 149 NS_IMETHODIMP CreateElementTxn::GetNewNode(nsIDOMNode **aNewNode)
michael@0 150 {
michael@0 151 NS_ENSURE_TRUE(aNewNode, NS_ERROR_NULL_POINTER);
michael@0 152 NS_ENSURE_TRUE(mNewNode, NS_ERROR_NOT_INITIALIZED);
michael@0 153 *aNewNode = mNewNode;
michael@0 154 NS_ADDREF(*aNewNode);
michael@0 155 return NS_OK;
michael@0 156 }

mercurial