editor/libeditor/base/InsertElementTxn.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.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include <stdio.h>                      // for printf
     8 #include "InsertElementTxn.h"
     9 #include "nsAString.h"
    10 #include "nsDebug.h"                    // for NS_ENSURE_TRUE, etc
    11 #include "nsError.h"                    // for NS_ERROR_NULL_POINTER, etc
    12 #include "nsIContent.h"                 // for nsIContent
    13 #include "nsIEditor.h"                  // for nsIEditor
    14 #include "nsINode.h"                    // for nsINode
    15 #include "nsISelection.h"               // for nsISelection
    16 #include "nsMemory.h"                   // for nsMemory
    17 #include "nsReadableUtils.h"            // for ToNewCString
    18 #include "nsString.h"                   // for nsString
    20 InsertElementTxn::InsertElementTxn()
    21   : EditTxn()
    22 {
    23 }
    25 NS_IMPL_CYCLE_COLLECTION_INHERITED(InsertElementTxn, EditTxn,
    26                                    mNode,
    27                                    mParent)
    29 NS_IMPL_ADDREF_INHERITED(InsertElementTxn, EditTxn)
    30 NS_IMPL_RELEASE_INHERITED(InsertElementTxn, EditTxn)
    31 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(InsertElementTxn)
    32 NS_INTERFACE_MAP_END_INHERITING(EditTxn)
    34 NS_IMETHODIMP InsertElementTxn::Init(nsIDOMNode *aNode,
    35                                      nsIDOMNode *aParent,
    36                                      int32_t     aOffset,
    37                                      nsIEditor  *aEditor)
    38 {
    39   NS_ASSERTION(aNode && aParent && aEditor, "bad arg");
    40   NS_ENSURE_TRUE(aNode && aParent && aEditor, NS_ERROR_NULL_POINTER);
    42   mNode = do_QueryInterface(aNode);
    43   mParent = do_QueryInterface(aParent);
    44   mOffset = aOffset;
    45   mEditor = aEditor;
    46   NS_ENSURE_TRUE(mNode && mParent && mEditor, NS_ERROR_INVALID_ARG);
    47   return NS_OK;
    48 }
    51 NS_IMETHODIMP InsertElementTxn::DoTransaction(void)
    52 {
    53   NS_ENSURE_TRUE(mNode && mParent, NS_ERROR_NOT_INITIALIZED);
    55   nsCOMPtr<nsINode> parent = do_QueryInterface(mParent);
    56   NS_ENSURE_STATE(parent);
    58   uint32_t count = parent->GetChildCount();
    59   if (mOffset > int32_t(count) || mOffset == -1) {
    60     // -1 is sentinel value meaning "append at end"
    61     mOffset = count;
    62   }
    64   nsCOMPtr<nsIContent> refContent = parent->GetChildAt(mOffset);
    65   // note, it's ok for refNode to be null.  that means append
    66   nsCOMPtr<nsIDOMNode> refNode = refContent ? refContent->AsDOMNode() : nullptr;
    68   mEditor->MarkNodeDirty(mNode);
    70   nsCOMPtr<nsIDOMNode> resultNode;
    71   nsresult result = mParent->InsertBefore(mNode, refNode, getter_AddRefs(resultNode));
    72   NS_ENSURE_SUCCESS(result, result);
    73   NS_ENSURE_TRUE(resultNode, NS_ERROR_NULL_POINTER);
    75   // only set selection to insertion point if editor gives permission
    76   bool bAdjustSelection;
    77   mEditor->ShouldTxnSetSelection(&bAdjustSelection);
    78   if (bAdjustSelection)
    79   {
    80     nsCOMPtr<nsISelection> selection;
    81     result = mEditor->GetSelection(getter_AddRefs(selection));
    82     NS_ENSURE_SUCCESS(result, result);
    83     NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
    84     // place the selection just after the inserted element
    85     selection->Collapse(mParent, mOffset+1);
    86   }
    87   else
    88   {
    89     // do nothing - dom range gravity will adjust selection
    90   }
    91   return result;
    92 }
    94 NS_IMETHODIMP InsertElementTxn::UndoTransaction(void)
    95 {
    96   NS_ENSURE_TRUE(mNode && mParent, NS_ERROR_NOT_INITIALIZED);
    98   nsCOMPtr<nsIDOMNode> resultNode;
    99   return mParent->RemoveChild(mNode, getter_AddRefs(resultNode));
   100 }
   102 NS_IMETHODIMP InsertElementTxn::GetTxnDescription(nsAString& aString)
   103 {
   104   aString.AssignLiteral("InsertElementTxn");
   105   return NS_OK;
   106 }

mercurial