DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (95ddb3213aec)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "SplitNodeTransaction.h"

#include "mozilla/EditorBase.h"      // for EditorBase
#include "mozilla/EditorDOMPoint.h"  // for RangeBoundary, EditorRawDOMPoint
#include "mozilla/dom/Selection.h"
#include "nsAString.h"
#include "nsDebug.h"     // for NS_ASSERTION, etc.
#include "nsError.h"     // for NS_ERROR_NOT_INITIALIZED, etc.
#include "nsIContent.h"  // for nsIContent

namespace mozilla {

using namespace dom;

template already_AddRefed<SplitNodeTransaction> SplitNodeTransaction::Create(
    EditorBase& aEditorBase, const EditorDOMPoint& aStartOfRightNode);
template already_AddRefed<SplitNodeTransaction> SplitNodeTransaction::Create(
    EditorBase& aEditorBase, const EditorRawDOMPoint& aStartOfRightNode);

// static
template <typename PT, typename CT>
already_AddRefed<SplitNodeTransaction> SplitNodeTransaction::Create(
    EditorBase& aEditorBase,
    const EditorDOMPointBase<PT, CT>& aStartOfRightNode) {
  RefPtr<SplitNodeTransaction> transaction =
      new SplitNodeTransaction(aEditorBase, aStartOfRightNode);
  return transaction.forget();
}

template <typename PT, typename CT>
SplitNodeTransaction::SplitNodeTransaction(
    EditorBase& aEditorBase,
    const EditorDOMPointBase<PT, CT>& aStartOfRightNode)
    : mEditorBase(&aEditorBase), mStartOfRightNode(aStartOfRightNode) {
  MOZ_DIAGNOSTIC_ASSERT(aStartOfRightNode.IsSet());
  MOZ_DIAGNOSTIC_ASSERT(aStartOfRightNode.GetContainerAsContent());
}

NS_IMPL_CYCLE_COLLECTION_INHERITED(SplitNodeTransaction, EditTransactionBase,
                                   mEditorBase, mStartOfRightNode, mParent,
                                   mNewLeftNode)

NS_IMPL_ADDREF_INHERITED(SplitNodeTransaction, EditTransactionBase)
NS_IMPL_RELEASE_INHERITED(SplitNodeTransaction, EditTransactionBase)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SplitNodeTransaction)
NS_INTERFACE_MAP_END_INHERITING(EditTransactionBase)

MOZ_CAN_RUN_SCRIPT_BOUNDARY NS_IMETHODIMP
SplitNodeTransaction::DoTransaction() {
  if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mStartOfRightNode.IsSet())) {
    return NS_ERROR_NOT_INITIALIZED;
  }
  MOZ_ASSERT(mStartOfRightNode.IsSetAndValid());

  // Create a new node
  ErrorResult error;
  // Don't use .downcast directly because AsContent has an assertion we want
  nsCOMPtr<nsINode> cloneOfRightContainer =
      mStartOfRightNode.GetContainer()->CloneNode(false, error);
  if (error.Failed()) {
    NS_WARNING("nsINode::CloneNode() failed");
    return error.StealNSResult();
  }
  if (NS_WARN_IF(!cloneOfRightContainer)) {
    return NS_ERROR_UNEXPECTED;
  }

  RefPtr<EditorBase> editorBase(mEditorBase);

  mNewLeftNode = cloneOfRightContainer->AsContent();
  if (RefPtr<Element> startOfRightNode =
          mStartOfRightNode.GetContainerAsElement()) {
    nsresult rv = editorBase->MarkElementDirty(*startOfRightNode);
    if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
      return EditorBase::ToGenericNSResult(rv);
    }
    NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                         "EditorBase::MarkElementDirty() failed, but ignored");
  }

  // Get the parent node
  mParent = mStartOfRightNode.GetContainerParent();
  if (!mParent) {
    NS_WARNING("Right container was an orphan node");
    return NS_ERROR_FAILURE;
  }

  // Insert the new node
  nsCOMPtr<nsIContent> newLeftNode = mNewLeftNode;
  editorBase->DoSplitNode(EditorDOMPoint(mStartOfRightNode), *newLeftNode,
                          error);
  if (error.Failed()) {
    NS_WARNING("EditorBase::DoSplitNode() failed");
    return error.StealNSResult();
  }

  if (!editorBase->AllowsTransactionsToChangeSelection()) {
    return NS_OK;
  }

  NS_WARNING_ASSERTION(
      !editorBase->Destroyed(),
      "The editor has gone but SplitNodeTransaction keeps trying to modify "
      "Selection");
  RefPtr<Selection> selection = editorBase->GetSelection();
  if (NS_WARN_IF(!selection)) {
    return NS_ERROR_FAILURE;
  }
  EditorRawDOMPoint atEndOfLeftNode(EditorRawDOMPoint::AtEndOf(mNewLeftNode));
  selection->Collapse(atEndOfLeftNode, error);
  NS_WARNING_ASSERTION(!error.Failed(), "Selection::Collapse() failed");
  return error.StealNSResult();
}

MOZ_CAN_RUN_SCRIPT_BOUNDARY NS_IMETHODIMP
SplitNodeTransaction::UndoTransaction() {
  if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mNewLeftNode) ||
      NS_WARN_IF(!mParent) || NS_WARN_IF(!mStartOfRightNode.IsSet())) {
    return NS_ERROR_NOT_INITIALIZED;
  }

  // This assumes Do inserted the new node in front of the prior existing node
  // XXX Perhaps, we should reset mStartOfRightNode with current first child
  //     of the right node.
  RefPtr<EditorBase> editorBase = mEditorBase;
  nsCOMPtr<nsINode> container = mStartOfRightNode.GetContainer();
  nsCOMPtr<nsINode> newLeftNode = mNewLeftNode;
  nsCOMPtr<nsINode> parent = mParent;
  nsresult rv = editorBase->DoJoinNodes(container, newLeftNode, parent);
  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "EditorBase::DoJoinNodes() failed");
  return rv;
}

/* Redo cannot simply resplit the right node, because subsequent transactions
 * on the redo stack may depend on the left node existing in its previous
 * state.
 */
MOZ_CAN_RUN_SCRIPT_BOUNDARY NS_IMETHODIMP
SplitNodeTransaction::RedoTransaction() {
  if (NS_WARN_IF(!mNewLeftNode) || NS_WARN_IF(!mParent) ||
      NS_WARN_IF(!mStartOfRightNode.IsSet()) || NS_WARN_IF(!mEditorBase)) {
    return NS_ERROR_NOT_INITIALIZED;
  }

  // First, massage the existing node so it is in its post-split state
  ErrorResult error;
  if (mStartOfRightNode.IsInTextNode()) {
    RefPtr<EditorBase> editorBase = mEditorBase;
    RefPtr<Text> rightNodeAsText = mStartOfRightNode.GetContainerAsText();
    MOZ_DIAGNOSTIC_ASSERT(rightNodeAsText);
    editorBase->DoDeleteText(*rightNodeAsText, 0, mStartOfRightNode.Offset(),
                             error);
    if (error.Failed()) {
      NS_WARNING("EditorBase::DoDeleteText() failed");
      return error.StealNSResult();
    }
  } else {
    nsCOMPtr<nsIContent> child =
        mStartOfRightNode.GetContainer()->GetFirstChild();
    nsCOMPtr<nsIContent> nextSibling;
    ErrorResult error;
    for (uint32_t i = 0; i < mStartOfRightNode.Offset(); i++) {
      // XXX This must be bad behavior.  Perhaps, we should work with
      //     mStartOfRightNode::GetChild().  Even if some children
      //     before the right node have been inserted or removed, we should
      //     move all children before the right node because user must focus
      //     on the right node, so, it must be the expected behavior.
      if (NS_WARN_IF(!child)) {
        return NS_ERROR_NULL_POINTER;
      }
      nextSibling = child->GetNextSibling();
      mStartOfRightNode.GetContainer()->RemoveChild(*child, error);
      if (error.Failed()) {
        NS_WARNING("nsINode::RemoveChild() failed");
        return error.StealNSResult();
      }
      mNewLeftNode->AppendChild(*child, error);
      if (error.Failed()) {
        NS_WARNING("nsINode::AppendChild() failed");
        return error.StealNSResult();
      }
      child = nextSibling;
    }
  }
  MOZ_ASSERT(!error.Failed());
  // Second, re-insert the left node into the tree
  mParent->InsertBefore(*mNewLeftNode, mStartOfRightNode.GetContainer(), error);
  NS_WARNING_ASSERTION(!error.Failed(), "nsINode::InsertBefore() failed");
  return error.StealNSResult();
}

nsIContent* SplitNodeTransaction::GetNewNode() { return mNewLeftNode; }

}  // namespace mozilla