/src/mozilla-central/editor/libeditor/TextEditRulesBidi.cpp
Line | Count | Source (jump to first uncovered line) |
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/. */ |
5 | | |
6 | | #include "mozilla/TextEditRules.h" |
7 | | |
8 | | #include "mozilla/TextEditor.h" |
9 | | #include "mozilla/dom/Selection.h" |
10 | | #include "nsCOMPtr.h" |
11 | | #include "nsDebug.h" |
12 | | #include "nsError.h" |
13 | | #include "nsFrameSelection.h" |
14 | | #include "nsIContent.h" |
15 | | #include "nsIEditor.h" |
16 | | #include "nsIPresShell.h" |
17 | | #include "nsISupportsImpl.h" |
18 | | #include "nsPresContext.h" |
19 | | #include "nscore.h" |
20 | | |
21 | | namespace mozilla { |
22 | | |
23 | | using namespace dom; |
24 | | |
25 | | // Test for distance between caret and text that will be deleted |
26 | | nsresult |
27 | | TextEditRules::CheckBidiLevelForDeletion( |
28 | | const EditorRawDOMPoint& aSelectionPoint, |
29 | | nsIEditor::EDirection aAction, |
30 | | bool* aCancel) |
31 | 0 | { |
32 | 0 | MOZ_ASSERT(IsEditorDataAvailable()); |
33 | 0 |
|
34 | 0 | if (NS_WARN_IF(!aCancel)) { |
35 | 0 | return NS_ERROR_INVALID_ARG; |
36 | 0 | } |
37 | 0 | |
38 | 0 | *aCancel = false; |
39 | 0 |
|
40 | 0 | RefPtr<nsPresContext> presContext = TextEditorRef().GetPresContext(); |
41 | 0 | if (NS_WARN_IF(!presContext)) { |
42 | 0 | return NS_ERROR_FAILURE; |
43 | 0 | } |
44 | 0 | |
45 | 0 | if (!presContext->BidiEnabled()) { |
46 | 0 | return NS_OK; |
47 | 0 | } |
48 | 0 | |
49 | 0 | if (!aSelectionPoint.GetContainerAsContent()) { |
50 | 0 | return NS_ERROR_FAILURE; |
51 | 0 | } |
52 | 0 | |
53 | 0 | RefPtr<nsFrameSelection> frameSelection = SelectionRef().GetFrameSelection(); |
54 | 0 | if (NS_WARN_IF(!frameSelection)) { |
55 | 0 | return NS_ERROR_FAILURE; |
56 | 0 | } |
57 | 0 | |
58 | 0 | nsPrevNextBidiLevels levels = frameSelection-> |
59 | 0 | GetPrevNextBidiLevels(aSelectionPoint.GetContainerAsContent(), |
60 | 0 | aSelectionPoint.Offset(), true); |
61 | 0 |
|
62 | 0 | nsBidiLevel levelBefore = levels.mLevelBefore; |
63 | 0 | nsBidiLevel levelAfter = levels.mLevelAfter; |
64 | 0 |
|
65 | 0 | nsBidiLevel currentCaretLevel = frameSelection->GetCaretBidiLevel(); |
66 | 0 |
|
67 | 0 | nsBidiLevel levelOfDeletion; |
68 | 0 | levelOfDeletion = |
69 | 0 | (nsIEditor::eNext==aAction || nsIEditor::eNextWord==aAction) ? |
70 | 0 | levelAfter : levelBefore; |
71 | 0 |
|
72 | 0 | if (currentCaretLevel == levelOfDeletion) { |
73 | 0 | return NS_OK; // perform the deletion |
74 | 0 | } |
75 | 0 | |
76 | 0 | if (!mDeleteBidiImmediately && levelBefore != levelAfter) { |
77 | 0 | *aCancel = true; |
78 | 0 | } |
79 | 0 |
|
80 | 0 | // Set the bidi level of the caret to that of the |
81 | 0 | // character that will be (or would have been) deleted |
82 | 0 | frameSelection->SetCaretBidiLevel(levelOfDeletion); |
83 | 0 | return NS_OK; |
84 | 0 | } |
85 | | |
86 | | void |
87 | | TextEditRules::UndefineCaretBidiLevel() |
88 | 0 | { |
89 | 0 | MOZ_ASSERT(IsEditorDataAvailable()); |
90 | 0 |
|
91 | 0 | /** |
92 | 0 | * After inserting text the caret Bidi level must be set to the level of the |
93 | 0 | * inserted text.This is difficult, because we cannot know what the level is |
94 | 0 | * until after the Bidi algorithm is applied to the whole paragraph. |
95 | 0 | * |
96 | 0 | * So we set the caret Bidi level to UNDEFINED here, and the caret code will |
97 | 0 | * set it correctly later |
98 | 0 | */ |
99 | 0 | RefPtr<nsFrameSelection> frameSelection = SelectionRef().GetFrameSelection(); |
100 | 0 | if (frameSelection) { |
101 | 0 | frameSelection->UndefineCaretBidiLevel(); |
102 | 0 | } |
103 | 0 | } |
104 | | |
105 | | } // namespace mozilla |