2001-09-26 02:53:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2001-05-22 03:57:55 +04:00
|
|
|
|
2016-07-09 05:34:41 +03:00
|
|
|
#include "mozilla/TextEditRules.h"
|
|
|
|
|
2016-07-09 05:54:50 +03:00
|
|
|
#include "mozilla/TextEditor.h"
|
|
|
|
#include "mozilla/dom/Selection.h"
|
2001-05-22 03:57:55 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsFrameSelection.h"
|
2001-05-22 03:57:55 +04:00
|
|
|
#include "nsIContent.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsIEditor.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nscore.h"
|
2001-05-22 03:57:55 +04:00
|
|
|
|
2016-07-09 05:34:41 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace dom;
|
2013-02-16 00:09:28 +04:00
|
|
|
|
2001-05-22 03:57:55 +04:00
|
|
|
// Test for distance between caret and text that will be deleted
|
2018-03-15 12:38:46 +03:00
|
|
|
nsresult TextEditRules::CheckBidiLevelForDeletion(
|
|
|
|
const EditorRawDOMPoint& aSelectionPoint, nsIEditor::EDirection aAction,
|
|
|
|
bool* aCancel) {
|
2018-04-26 18:09:10 +03:00
|
|
|
MOZ_ASSERT(IsEditorDataAvailable());
|
2001-05-22 03:57:55 +04:00
|
|
|
|
2018-04-26 18:09:10 +03:00
|
|
|
if (NS_WARN_IF(!aCancel)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2018-04-26 18:09:10 +03:00
|
|
|
*aCancel = false;
|
|
|
|
|
|
|
|
RefPtr<nsPresContext> presContext = TextEditorRef().GetPresContext();
|
|
|
|
if (NS_WARN_IF(!presContext)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2018-04-26 18:09:10 +03:00
|
|
|
if (!presContext->BidiEnabled()) {
|
2001-05-22 03:57:55 +04:00
|
|
|
return NS_OK;
|
2016-10-24 05:27:45 +03:00
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2018-03-15 12:38:46 +03:00
|
|
|
if (!aSelectionPoint.GetContainerAsContent()) {
|
2018-04-26 18:09:10 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2018-02-28 05:32:28 +03:00
|
|
|
}
|
2006-04-26 05:57:22 +04:00
|
|
|
|
2018-10-30 13:02:58 +03:00
|
|
|
RefPtr<nsFrameSelection> frameSelection =
|
|
|
|
SelectionRefPtr()->GetFrameSelection();
|
2018-04-26 19:42:22 +03:00
|
|
|
if (NS_WARN_IF(!frameSelection)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2006-04-26 05:57:22 +04:00
|
|
|
nsPrevNextBidiLevels levels = frameSelection->GetPrevNextBidiLevels(
|
2018-03-15 12:38:46 +03:00
|
|
|
aSelectionPoint.GetContainerAsContent(), aSelectionPoint.Offset(), true);
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2018-04-26 19:42:22 +03:00
|
|
|
nsBidiLevel levelBefore = levels.mLevelBefore;
|
|
|
|
nsBidiLevel levelAfter = levels.mLevelAfter;
|
2006-08-08 09:14:52 +04:00
|
|
|
|
2014-11-20 13:45:22 +03:00
|
|
|
nsBidiLevel currentCaretLevel = frameSelection->GetCaretBidiLevel();
|
2003-07-18 18:12:51 +04:00
|
|
|
|
2014-11-20 13:45:22 +03:00
|
|
|
nsBidiLevel levelOfDeletion;
|
2008-10-16 11:44:32 +04:00
|
|
|
levelOfDeletion =
|
|
|
|
(nsIEditor::eNext == aAction || nsIEditor::eNextWord == aAction)
|
|
|
|
? levelAfter
|
|
|
|
: levelBefore;
|
2001-05-22 03:57:55 +04:00
|
|
|
|
2016-10-24 05:27:45 +03:00
|
|
|
if (currentCaretLevel == levelOfDeletion) {
|
|
|
|
return NS_OK; // perform the deletion
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mDeleteBidiImmediately && levelBefore != levelAfter) {
|
|
|
|
*aCancel = true;
|
2001-05-22 03:57:55 +04:00
|
|
|
}
|
2016-10-24 05:27:45 +03:00
|
|
|
|
|
|
|
// Set the bidi level of the caret to that of the
|
|
|
|
// character that will be (or would have been) deleted
|
|
|
|
frameSelection->SetCaretBidiLevel(levelOfDeletion);
|
2001-05-22 03:57:55 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-05-10 09:03:21 +03:00
|
|
|
void TextEditRules::UndefineCaretBidiLevel() {
|
2018-04-26 19:42:22 +03:00
|
|
|
MOZ_ASSERT(IsEditorDataAvailable());
|
|
|
|
|
2015-06-01 22:19:57 +03:00
|
|
|
/**
|
|
|
|
* After inserting text the caret Bidi level must be set to the level of the
|
|
|
|
* inserted text.This is difficult, because we cannot know what the level is
|
|
|
|
* until after the Bidi algorithm is applied to the whole paragraph.
|
|
|
|
*
|
|
|
|
* So we set the caret Bidi level to UNDEFINED here, and the caret code will
|
|
|
|
* set it correctly later
|
|
|
|
*/
|
2018-10-30 13:02:58 +03:00
|
|
|
RefPtr<nsFrameSelection> frameSelection =
|
|
|
|
SelectionRefPtr()->GetFrameSelection();
|
2015-06-01 22:19:57 +03:00
|
|
|
if (frameSelection) {
|
|
|
|
frameSelection->UndefineCaretBidiLevel();
|
|
|
|
}
|
|
|
|
}
|
2016-07-09 05:34:41 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|