editor/libeditor/text/nsTextEditRulesBidi.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/editor/libeditor/text/nsTextEditRulesBidi.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,83 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsAutoPtr.h"
    1.10 +#include "nsCOMPtr.h"
    1.11 +#include "nsDebug.h"
    1.12 +#include "nsError.h"
    1.13 +#include "nsFrameSelection.h"
    1.14 +#include "nsIContent.h"
    1.15 +#include "nsIDOMNode.h"
    1.16 +#include "nsIEditor.h"
    1.17 +#include "nsIPresShell.h"
    1.18 +#include "mozilla/dom/Selection.h"
    1.19 +#include "nsISelectionPrivate.h"
    1.20 +#include "nsISupportsImpl.h"
    1.21 +#include "nsPlaintextEditor.h"
    1.22 +#include "nsPresContext.h"
    1.23 +#include "nsTextEditRules.h"
    1.24 +#include "nscore.h"
    1.25 +
    1.26 +using namespace mozilla;
    1.27 +using namespace mozilla::dom;
    1.28 +
    1.29 +// Test for distance between caret and text that will be deleted
    1.30 +nsresult
    1.31 +nsTextEditRules::CheckBidiLevelForDeletion(nsISelection         *aSelection,
    1.32 +                                           nsIDOMNode           *aSelNode, 
    1.33 +                                           int32_t               aSelOffset, 
    1.34 +                                           nsIEditor::EDirection aAction,
    1.35 +                                           bool                 *aCancel)
    1.36 +{
    1.37 +  NS_ENSURE_ARG_POINTER(aCancel);
    1.38 +  *aCancel = false;
    1.39 +
    1.40 +  nsCOMPtr<nsIPresShell> shell = mEditor->GetPresShell();
    1.41 +  NS_ENSURE_TRUE(shell, NS_ERROR_NOT_INITIALIZED);
    1.42 +  
    1.43 +  nsPresContext *context = shell->GetPresContext();
    1.44 +  NS_ENSURE_TRUE(context, NS_ERROR_NULL_POINTER);
    1.45 +  
    1.46 +  if (!context->BidiEnabled())
    1.47 +    return NS_OK;
    1.48 +  
    1.49 +  nsCOMPtr<nsIContent> content = do_QueryInterface(aSelNode);
    1.50 +  NS_ENSURE_TRUE(content, NS_ERROR_NULL_POINTER);
    1.51 +
    1.52 +  uint8_t levelBefore;
    1.53 +  uint8_t levelAfter;
    1.54 +  nsRefPtr<nsFrameSelection> frameSelection =
    1.55 +    static_cast<Selection*>(aSelection)->GetFrameSelection();
    1.56 +  NS_ENSURE_TRUE(frameSelection, NS_ERROR_NULL_POINTER);
    1.57 +  
    1.58 +  nsPrevNextBidiLevels levels = frameSelection->
    1.59 +    GetPrevNextBidiLevels(content, aSelOffset, true);
    1.60 +    
    1.61 +  levelBefore = levels.mLevelBefore;
    1.62 +  levelAfter = levels.mLevelAfter;
    1.63 +
    1.64 +  uint8_t currentCaretLevel = frameSelection->GetCaretBidiLevel();
    1.65 +
    1.66 +  uint8_t levelOfDeletion;
    1.67 +  levelOfDeletion =
    1.68 +    (nsIEditor::eNext==aAction || nsIEditor::eNextWord==aAction) ?
    1.69 +    levelAfter : levelBefore;
    1.70 +
    1.71 +  if (currentCaretLevel == levelOfDeletion)
    1.72 +    ; // perform the deletion
    1.73 +  else
    1.74 +  {
    1.75 +    if (mDeleteBidiImmediately || levelBefore == levelAfter)
    1.76 +      ; // perform the deletion
    1.77 +    else
    1.78 +      *aCancel = true;
    1.79 +
    1.80 +    // Set the bidi level of the caret to that of the
    1.81 +    // character that will be (or would have been) deleted
    1.82 +    frameSelection->SetCaretBidiLevel(levelOfDeletion);
    1.83 +  }
    1.84 +  return NS_OK;
    1.85 +}
    1.86 +

mercurial