2002-10-12 03:46:57 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2014-10-03 18:52:37 +04:00
|
|
|
/* vim: set ts=2 sts=2 sw=2 tw=80: */
|
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/. */
|
2002-10-12 03:46:57 +04:00
|
|
|
|
2018-01-18 15:01:13 +03:00
|
|
|
#include "EditorSpellCheck.h"
|
2002-10-12 03:46:57 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
#include "mozilla/Attributes.h" // for final
|
2018-01-18 15:01:13 +03:00
|
|
|
#include "mozilla/EditorBase.h" // for EditorBase
|
2018-04-06 08:53:05 +03:00
|
|
|
#include "mozilla/HTMLEditor.h" // for HTMLEditor
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "mozilla/dom/Element.h" // for Element
|
2014-11-02 15:04:13 +03:00
|
|
|
#include "mozilla/dom/Selection.h"
|
2017-03-15 02:09:54 +03:00
|
|
|
#include "mozilla/intl/LocaleService.h" // for retrieving app locale
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "mozilla/mozalloc.h" // for operator delete, etc
|
2018-01-18 14:06:49 +03:00
|
|
|
#include "mozilla/mozSpellChecker.h" // for mozSpellChecker
|
2018-01-18 11:32:23 +03:00
|
|
|
#include "mozilla/Preferences.h" // for Preferences
|
|
|
|
#include "mozilla/TextServicesDocument.h" // for TextServicesDocument
|
2017-03-10 05:17:23 +03:00
|
|
|
#include "nsAString.h" // for nsAString::IsEmpty, etc
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsComponentManagerUtils.h" // for do_CreateInstance
|
|
|
|
#include "nsDebug.h" // for NS_ENSURE_TRUE, etc
|
|
|
|
#include "nsDependentSubstring.h" // for Substring
|
|
|
|
#include "nsError.h" // for NS_ERROR_NOT_INITIALIZED, etc
|
|
|
|
#include "nsIContent.h" // for nsIContent
|
2013-06-06 04:07:54 +04:00
|
|
|
#include "nsIContentPrefService2.h" // for nsIContentPrefService2, etc
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsIDocument.h" // for nsIDocument
|
|
|
|
#include "nsIEditor.h" // for nsIEditor
|
2012-06-30 18:50:07 +04:00
|
|
|
#include "nsILoadContext.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsISupportsBase.h" // for nsISupports
|
|
|
|
#include "nsISupportsUtils.h" // for NS_ADDREF
|
|
|
|
#include "nsIURI.h" // for nsIURI
|
2017-06-10 07:24:46 +03:00
|
|
|
#include "nsThreadUtils.h" // for GetMainThreadSerialEventTarget
|
2015-10-07 18:17:42 +03:00
|
|
|
#include "nsVariant.h" // for nsIWritableVariant, etc
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsLiteralString.h" // for NS_LITERAL_STRING, etc
|
|
|
|
#include "nsMemory.h" // for nsMemory
|
2014-11-02 15:04:13 +03:00
|
|
|
#include "nsRange.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsReadableUtils.h" // for ToNewUnicode, EmptyString, etc
|
|
|
|
#include "nsServiceManagerUtils.h" // for do_GetService
|
|
|
|
#include "nsString.h" // for nsAutoString, nsString, etc
|
|
|
|
#include "nsStringFwd.h" // for nsAFlatString
|
|
|
|
#include "nsStyleUtil.h" // for nsStyleUtil
|
2014-07-02 02:24:27 +04:00
|
|
|
#include "nsXULAppAPI.h" // for XRE_GetProcessType
|
2015-04-28 12:01:00 +03:00
|
|
|
#include "nsIPlaintextEditor.h" // for editor flags
|
2011-06-17 04:59:29 +04:00
|
|
|
|
2018-01-18 15:01:13 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace dom;
|
|
|
|
using intl::LocaleService;
|
|
|
|
|
|
|
|
class UpdateDictionaryHolder
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
EditorSpellCheck* mSpellCheck;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit UpdateDictionaryHolder(EditorSpellCheck* esc)
|
|
|
|
: mSpellCheck(esc)
|
|
|
|
{
|
|
|
|
if (mSpellCheck) {
|
|
|
|
mSpellCheck->BeginUpdateDictionary();
|
2011-08-15 21:54:03 +04:00
|
|
|
}
|
2018-01-18 15:01:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
~UpdateDictionaryHolder() {
|
|
|
|
if (mSpellCheck) {
|
|
|
|
mSpellCheck->EndUpdateDictionary();
|
2011-08-15 21:54:03 +04:00
|
|
|
}
|
2018-01-18 15:01:13 +03:00
|
|
|
}
|
2011-08-15 21:54:03 +04:00
|
|
|
};
|
|
|
|
|
2011-08-23 23:03:33 +04:00
|
|
|
#define CPS_PREF_NAME NS_LITERAL_STRING("spellcheck.lang")
|
|
|
|
|
2013-06-06 04:07:54 +04:00
|
|
|
/**
|
|
|
|
* Gets the URI of aEditor's document.
|
|
|
|
*/
|
2018-04-06 08:53:05 +03:00
|
|
|
static nsIURI*
|
|
|
|
GetDocumentURI(EditorBase* aEditor)
|
2011-08-23 23:03:33 +04:00
|
|
|
{
|
2018-04-06 08:53:05 +03:00
|
|
|
MOZ_ASSERT(aEditor);
|
2011-08-23 23:03:33 +04:00
|
|
|
|
2018-04-06 08:53:05 +03:00
|
|
|
nsIDocument* doc = aEditor->AsEditorBase()->GetDocument();
|
|
|
|
if (NS_WARN_IF(!doc)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2011-08-23 23:03:33 +04:00
|
|
|
|
2018-04-06 08:53:05 +03:00
|
|
|
return doc->GetDocumentURI();
|
2011-08-23 23:03:33 +04:00
|
|
|
}
|
|
|
|
|
2018-04-06 08:53:05 +03:00
|
|
|
static nsILoadContext*
|
2012-06-30 18:50:07 +04:00
|
|
|
GetLoadContext(nsIEditor* aEditor)
|
|
|
|
{
|
2018-04-06 08:53:05 +03:00
|
|
|
nsIDocument* doc = aEditor->AsEditorBase()->GetDocument();
|
|
|
|
if (NS_WARN_IF(!doc)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-06-30 18:50:07 +04:00
|
|
|
|
2018-04-06 08:53:05 +03:00
|
|
|
return doc->GetLoadContext();
|
2012-06-30 18:50:07 +04:00
|
|
|
}
|
|
|
|
|
2013-06-06 04:07:54 +04:00
|
|
|
/**
|
|
|
|
* Fetches the dictionary stored in content prefs and maintains state during the
|
|
|
|
* fetch, which is asynchronous.
|
|
|
|
*/
|
2015-03-21 19:28:04 +03:00
|
|
|
class DictionaryFetcher final : public nsIContentPrefCallback2
|
2013-06-06 04:07:54 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2018-01-18 15:01:13 +03:00
|
|
|
DictionaryFetcher(EditorSpellCheck* aSpellCheck,
|
2013-06-06 04:07:54 +04:00
|
|
|
nsIEditorSpellCheckCallback* aCallback,
|
|
|
|
uint32_t aGroup)
|
2018-01-18 15:01:13 +03:00
|
|
|
: mCallback(aCallback)
|
|
|
|
, mGroup(aGroup)
|
|
|
|
, mSpellCheck(aSpellCheck)
|
|
|
|
{
|
|
|
|
}
|
2013-06-06 04:07:54 +04:00
|
|
|
|
|
|
|
NS_IMETHOD Fetch(nsIEditor* aEditor);
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD HandleResult(nsIContentPref* aPref) override
|
2013-06-06 04:07:54 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIVariant> value;
|
|
|
|
nsresult rv = aPref->GetValue(getter_AddRefs(value));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
value->GetAsAString(mDictionary);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD HandleCompletion(uint16_t reason) override
|
2013-06-06 04:07:54 +04:00
|
|
|
{
|
|
|
|
mSpellCheck->DictionaryFetched(this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD HandleError(nsresult error) override
|
2013-06-06 04:07:54 +04:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIEditorSpellCheckCallback> mCallback;
|
|
|
|
uint32_t mGroup;
|
|
|
|
nsString mRootContentLang;
|
|
|
|
nsString mRootDocContentLang;
|
|
|
|
nsString mDictionary;
|
|
|
|
|
|
|
|
private:
|
2014-06-24 02:40:02 +04:00
|
|
|
~DictionaryFetcher() {}
|
|
|
|
|
2018-01-18 15:01:13 +03:00
|
|
|
RefPtr<EditorSpellCheck> mSpellCheck;
|
2013-06-06 04:07:54 +04:00
|
|
|
};
|
2018-01-18 15:01:13 +03:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(DictionaryFetcher, nsIContentPrefCallback2)
|
2013-06-06 04:07:54 +04:00
|
|
|
|
2017-09-04 13:57:45 +03:00
|
|
|
class ContentPrefInitializerRunnable final : public Runnable
|
2011-08-23 23:03:33 +04:00
|
|
|
{
|
2017-09-04 13:57:45 +03:00
|
|
|
public:
|
|
|
|
ContentPrefInitializerRunnable(nsIEditor* aEditor,
|
|
|
|
nsIContentPrefCallback2* aCallback)
|
|
|
|
: Runnable("ContentPrefInitializerRunnable")
|
2018-04-06 08:53:05 +03:00
|
|
|
, mEditorBase(aEditor->AsEditorBase())
|
2017-09-04 13:57:45 +03:00
|
|
|
, mCallback(aCallback)
|
|
|
|
{
|
|
|
|
}
|
2011-08-23 23:03:33 +04:00
|
|
|
|
2017-09-04 13:57:45 +03:00
|
|
|
NS_IMETHOD Run() override
|
|
|
|
{
|
2018-04-06 08:53:05 +03:00
|
|
|
if (mEditorBase->Destroyed()) {
|
2017-09-04 13:57:45 +03:00
|
|
|
mCallback->HandleError(NS_ERROR_NOT_AVAILABLE);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-08-23 23:03:33 +04:00
|
|
|
|
2017-09-04 13:57:45 +03:00
|
|
|
nsCOMPtr<nsIContentPrefService2> contentPrefService =
|
|
|
|
do_GetService(NS_CONTENT_PREF_SERVICE_CONTRACTID);
|
|
|
|
if (NS_WARN_IF(!contentPrefService)) {
|
|
|
|
mCallback->HandleError(NS_ERROR_NOT_AVAILABLE);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-08-23 23:03:33 +04:00
|
|
|
|
2018-04-06 08:53:05 +03:00
|
|
|
nsCOMPtr<nsIURI> docUri = GetDocumentURI(mEditorBase);
|
|
|
|
if (NS_WARN_IF(!docUri)) {
|
|
|
|
mCallback->HandleError(NS_ERROR_FAILURE);
|
2017-09-04 13:57:45 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-06-06 04:07:54 +04:00
|
|
|
|
2017-09-04 13:57:45 +03:00
|
|
|
nsAutoCString docUriSpec;
|
2018-04-06 08:53:05 +03:00
|
|
|
nsresult rv = docUri->GetSpec(docUriSpec);
|
2017-09-04 13:57:45 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
mCallback->HandleError(rv);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-08-23 23:03:33 +04:00
|
|
|
|
2017-09-04 13:57:45 +03:00
|
|
|
rv = contentPrefService->GetByDomainAndName(
|
|
|
|
NS_ConvertUTF8toUTF16(docUriSpec),
|
2018-04-06 08:53:05 +03:00
|
|
|
CPS_PREF_NAME,
|
|
|
|
GetLoadContext(mEditorBase),
|
2017-09-04 13:57:45 +03:00
|
|
|
mCallback);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
mCallback->HandleError(rv);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-04-06 08:53:05 +03:00
|
|
|
RefPtr<EditorBase> mEditorBase;
|
2017-09-04 13:57:45 +03:00
|
|
|
nsCOMPtr<nsIContentPrefCallback2> mCallback;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DictionaryFetcher::Fetch(nsIEditor* aEditor)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aEditor);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
|
|
|
new ContentPrefInitializerRunnable(aEditor, this);
|
|
|
|
NS_IdleDispatchToCurrentThread(runnable.forget(), 1000);
|
2011-08-23 23:03:33 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-06-06 04:07:54 +04:00
|
|
|
/**
|
|
|
|
* Stores the current dictionary for aEditor's document URL.
|
|
|
|
*/
|
|
|
|
static nsresult
|
2018-04-06 08:53:05 +03:00
|
|
|
StoreCurrentDictionary(EditorBase* aEditorBase, const nsAString& aDictionary)
|
2011-08-23 23:03:33 +04:00
|
|
|
{
|
2018-04-06 08:53:05 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aEditorBase);
|
2011-08-23 23:03:33 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2018-04-06 08:53:05 +03:00
|
|
|
nsCOMPtr<nsIURI> docUri = GetDocumentURI(aEditorBase);
|
|
|
|
if (NS_WARN_IF(!docUri)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2011-08-23 23:03:33 +04:00
|
|
|
|
2013-06-06 04:07:54 +04:00
|
|
|
nsAutoCString docUriSpec;
|
|
|
|
rv = docUri->GetSpec(docUriSpec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-08-23 23:03:33 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsVariant> prefValue = new nsVariant();
|
2011-08-23 23:03:33 +04:00
|
|
|
prefValue->SetAsAString(aDictionary);
|
|
|
|
|
2013-06-06 04:07:54 +04:00
|
|
|
nsCOMPtr<nsIContentPrefService2> contentPrefService =
|
2011-08-23 23:03:33 +04:00
|
|
|
do_GetService(NS_CONTENT_PREF_SERVICE_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(contentPrefService, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
2013-06-06 04:07:54 +04:00
|
|
|
return contentPrefService->Set(NS_ConvertUTF8toUTF16(docUriSpec),
|
2018-04-06 08:53:05 +03:00
|
|
|
CPS_PREF_NAME, prefValue,
|
|
|
|
GetLoadContext(aEditorBase),
|
2013-06-06 04:07:54 +04:00
|
|
|
nullptr);
|
2011-08-23 23:03:33 +04:00
|
|
|
}
|
|
|
|
|
2013-06-06 04:07:54 +04:00
|
|
|
/**
|
|
|
|
* Forgets the current dictionary stored for aEditor's document URL.
|
|
|
|
*/
|
|
|
|
static nsresult
|
2018-04-06 08:53:05 +03:00
|
|
|
ClearCurrentDictionary(EditorBase* aEditorBase)
|
2011-08-23 23:03:33 +04:00
|
|
|
{
|
2018-04-06 08:53:05 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aEditorBase);
|
2011-08-23 23:03:33 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2018-04-06 08:53:05 +03:00
|
|
|
nsCOMPtr<nsIURI> docUri = GetDocumentURI(aEditorBase);
|
|
|
|
if (NS_WARN_IF(!docUri)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2011-08-23 23:03:33 +04:00
|
|
|
|
2013-06-06 04:07:54 +04:00
|
|
|
nsAutoCString docUriSpec;
|
|
|
|
rv = docUri->GetSpec(docUriSpec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-08-23 23:03:33 +04:00
|
|
|
|
2013-06-06 04:07:54 +04:00
|
|
|
nsCOMPtr<nsIContentPrefService2> contentPrefService =
|
2011-08-23 23:03:33 +04:00
|
|
|
do_GetService(NS_CONTENT_PREF_SERVICE_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(contentPrefService, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
2013-06-06 04:07:54 +04:00
|
|
|
return contentPrefService->RemoveByDomainAndName(
|
2018-04-06 08:53:05 +03:00
|
|
|
NS_ConvertUTF8toUTF16(docUriSpec), CPS_PREF_NAME,
|
|
|
|
GetLoadContext(aEditorBase), nullptr);
|
2011-08-23 23:03:33 +04:00
|
|
|
}
|
|
|
|
|
2018-01-18 15:01:13 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(EditorSpellCheck)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(EditorSpellCheck)
|
2010-06-02 22:33:47 +04:00
|
|
|
|
2018-01-18 15:01:13 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN(EditorSpellCheck)
|
2010-06-02 22:33:47 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIEditorSpellCheck)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIEditorSpellCheck)
|
2018-01-18 15:01:13 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(EditorSpellCheck)
|
2010-06-02 22:33:47 +04:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2018-01-18 15:01:13 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION(EditorSpellCheck,
|
2014-04-25 20:49:00 +04:00
|
|
|
mEditor,
|
2018-09-08 20:31:25 +03:00
|
|
|
mSpellChecker)
|
2002-10-12 03:46:57 +04:00
|
|
|
|
2018-01-18 15:01:13 +03:00
|
|
|
EditorSpellCheck::EditorSpellCheck()
|
2018-09-08 20:31:25 +03:00
|
|
|
: mTxtSrvFilterType(0)
|
|
|
|
, mSuggestedWordIndex(0)
|
2002-10-12 03:46:57 +04:00
|
|
|
, mDictionaryIndex(0)
|
2013-06-06 04:07:54 +04:00
|
|
|
, mDictionaryFetcherGroup(0)
|
2011-10-17 18:59:28 +04:00
|
|
|
, mUpdateDictionaryRunning(false)
|
2002-10-12 03:46:57 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-01-18 15:01:13 +03:00
|
|
|
EditorSpellCheck::~EditorSpellCheck()
|
2002-10-12 03:46:57 +04:00
|
|
|
{
|
|
|
|
// Make sure we blow the spellchecker away, just in
|
|
|
|
// case it hasn't been destroyed already.
|
2012-07-30 18:20:58 +04:00
|
|
|
mSpellChecker = nullptr;
|
2002-10-12 03:46:57 +04:00
|
|
|
}
|
|
|
|
|
2018-01-18 15:38:50 +03:00
|
|
|
mozSpellChecker*
|
|
|
|
EditorSpellCheck::GetSpellChecker()
|
|
|
|
{
|
|
|
|
return mSpellChecker;
|
|
|
|
}
|
|
|
|
|
2005-12-05 21:07:33 +03:00
|
|
|
// The problem is that if the spell checker does not exist, we can not tell
|
|
|
|
// which dictionaries are installed. This function works around the problem,
|
|
|
|
// allowing callers to ask if we can spell check without actually doing so (and
|
|
|
|
// enabling or disabling UI as necessary). This just creates a spellcheck
|
|
|
|
// object if needed and asks it for the dictionary list.
|
|
|
|
NS_IMETHODIMP
|
2018-01-18 15:01:13 +03:00
|
|
|
EditorSpellCheck::CanSpellCheck(bool* aCanSpellCheck)
|
2005-12-05 21:07:33 +03:00
|
|
|
{
|
2018-01-18 14:06:49 +03:00
|
|
|
RefPtr<mozSpellChecker> spellChecker = mSpellChecker;
|
|
|
|
if (!spellChecker) {
|
2018-09-15 19:08:06 +03:00
|
|
|
spellChecker = mozSpellChecker::Create();
|
|
|
|
MOZ_ASSERT(spellChecker);
|
2005-12-05 21:07:33 +03:00
|
|
|
}
|
2009-01-18 23:14:14 +03:00
|
|
|
nsTArray<nsString> dictList;
|
2018-01-18 14:06:49 +03:00
|
|
|
nsresult rv = spellChecker->GetDictionaryList(&dictList);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2005-12-05 21:07:33 +03:00
|
|
|
|
2018-01-18 15:01:13 +03:00
|
|
|
*aCanSpellCheck = !dictList.IsEmpty();
|
2005-12-05 21:07:33 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-06-06 04:07:54 +04:00
|
|
|
// Instances of this class can be used as either runnables or RAII helpers.
|
2016-04-26 03:23:21 +03:00
|
|
|
class CallbackCaller final : public Runnable
|
2013-06-06 04:07:54 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit CallbackCaller(nsIEditorSpellCheckCallback* aCallback)
|
2017-06-12 22:34:10 +03:00
|
|
|
: mozilla::Runnable("CallbackCaller")
|
|
|
|
, mCallback(aCallback)
|
|
|
|
{
|
|
|
|
}
|
2013-06-06 04:07:54 +04:00
|
|
|
|
|
|
|
~CallbackCaller()
|
|
|
|
{
|
|
|
|
Run();
|
|
|
|
}
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override
|
2013-06-06 04:07:54 +04:00
|
|
|
{
|
|
|
|
if (mCallback) {
|
|
|
|
mCallback->EditorSpellCheckDone();
|
|
|
|
mCallback = nullptr;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIEditorSpellCheckCallback> mCallback;
|
|
|
|
};
|
|
|
|
|
2015-05-28 18:58:42 +03:00
|
|
|
NS_IMETHODIMP
|
2018-01-18 15:01:13 +03:00
|
|
|
EditorSpellCheck::InitSpellChecker(nsIEditor* aEditor,
|
|
|
|
bool aEnableSelectionChecking,
|
|
|
|
nsIEditorSpellCheckCallback* aCallback)
|
2002-10-12 03:46:57 +04:00
|
|
|
{
|
2011-08-23 23:03:33 +04:00
|
|
|
NS_ENSURE_TRUE(aEditor, NS_ERROR_NULL_POINTER);
|
2018-04-06 08:53:05 +03:00
|
|
|
mEditor = aEditor->AsEditorBase();
|
2011-08-23 23:03:33 +04:00
|
|
|
|
2018-04-06 08:53:05 +03:00
|
|
|
nsCOMPtr<nsIDocument> doc = mEditor->GetDocument();
|
|
|
|
if (NS_WARN_IF(!doc)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2017-03-29 21:43:28 +03:00
|
|
|
|
2002-10-12 03:46:57 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// We can spell check with any editor type
|
2018-01-18 11:32:23 +03:00
|
|
|
RefPtr<TextServicesDocument> textServicesDocument =
|
|
|
|
new TextServicesDocument();
|
2018-09-08 20:31:25 +03:00
|
|
|
textServicesDocument->SetFilterType(mTxtSrvFilterType);
|
2002-12-10 18:03:04 +03:00
|
|
|
|
2018-01-18 17:46:03 +03:00
|
|
|
// EditorBase::AddEditActionListener() needs to access mSpellChecker and
|
|
|
|
// mSpellChecker->GetTextServicesDocument(). Therefore, we need to
|
|
|
|
// initialize them before calling TextServicesDocument::InitWithEditor()
|
|
|
|
// since it calls EditorBase::AddEditActionListener().
|
2018-09-15 19:08:06 +03:00
|
|
|
mSpellChecker = mozSpellChecker::Create();
|
|
|
|
MOZ_ASSERT(mSpellChecker);
|
2018-01-18 17:46:03 +03:00
|
|
|
rv = mSpellChecker->SetDocument(textServicesDocument, true);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2002-10-12 03:46:57 +04:00
|
|
|
// Pass the editor to the text services document
|
2018-01-18 11:32:23 +03:00
|
|
|
rv = textServicesDocument->InitWithEditor(aEditor);
|
2002-10-12 03:46:57 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2003-04-17 18:29:08 +04:00
|
|
|
if (aEnableSelectionChecking) {
|
|
|
|
// Find out if the section is collapsed or not.
|
|
|
|
// If it isn't, we want to spellcheck just the selection.
|
|
|
|
|
2018-05-08 20:52:36 +03:00
|
|
|
RefPtr<Selection> selection;
|
|
|
|
aEditor->GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_WARN_IF(!selection)) {
|
2016-06-17 16:32:49 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2003-04-17 18:29:08 +04:00
|
|
|
|
2017-08-28 08:52:25 +03:00
|
|
|
if (selection->RangeCount()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsRange> range = selection->GetRangeAt(0);
|
2014-11-02 15:04:13 +03:00
|
|
|
NS_ENSURE_STATE(range);
|
2003-04-17 18:29:08 +04:00
|
|
|
|
2017-09-01 06:19:11 +03:00
|
|
|
if (!range->Collapsed()) {
|
2003-04-17 18:29:08 +04:00
|
|
|
// We don't want to touch the range in the selection,
|
|
|
|
// so create a new copy of it.
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsRange> rangeBounds = range->CloneRange();
|
2003-04-17 18:29:08 +04:00
|
|
|
|
|
|
|
// Make sure the new range spans complete words.
|
|
|
|
|
2018-01-18 11:32:23 +03:00
|
|
|
rv = textServicesDocument->ExpandRangeToWordBoundaries(rangeBounds);
|
2003-04-17 18:29:08 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Now tell the text services that you only want
|
|
|
|
// to iterate over the text in this range.
|
|
|
|
|
2018-01-18 11:32:23 +03:00
|
|
|
rv = textServicesDocument->SetExtent(rangeBounds);
|
2003-04-17 18:29:08 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-12 23:12:45 +04:00
|
|
|
// do not fail if UpdateCurrentDictionary fails because this method may
|
|
|
|
// succeed later.
|
2013-06-06 04:07:54 +04:00
|
|
|
rv = UpdateCurrentDictionary(aCallback);
|
|
|
|
if (NS_FAILED(rv) && aCallback) {
|
|
|
|
// However, if it does fail, we still need to call the callback since we
|
|
|
|
// discard the failure. Do it asynchronously so that the caller is always
|
|
|
|
// guaranteed async behavior.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CallbackCaller> caller = new CallbackCaller(aCallback);
|
2017-07-26 11:13:35 +03:00
|
|
|
rv = doc->Dispatch(TaskCategory::Other, caller.forget());
|
2013-06-06 04:07:54 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2002-10-12 03:46:57 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-05-28 18:58:42 +03:00
|
|
|
NS_IMETHODIMP
|
2018-01-18 15:01:13 +03:00
|
|
|
EditorSpellCheck::GetNextMisspelledWord(nsAString& aNextMisspelledWord)
|
2002-10-12 03:46:57 +04:00
|
|
|
{
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(mSpellChecker, NS_ERROR_NOT_INITIALIZED);
|
2002-10-12 03:46:57 +04:00
|
|
|
|
|
|
|
DeleteSuggestedWordList();
|
2008-02-28 18:28:37 +03:00
|
|
|
// Beware! This may flush notifications via synchronous
|
|
|
|
// ScrollSelectionIntoView.
|
2017-12-13 13:14:27 +03:00
|
|
|
return mSpellChecker->NextMisspelledWord(aNextMisspelledWord,
|
|
|
|
&mSuggestedWordList);
|
2002-10-12 03:46:57 +04:00
|
|
|
}
|
|
|
|
|
2015-05-28 18:58:42 +03:00
|
|
|
NS_IMETHODIMP
|
2018-01-18 15:01:13 +03:00
|
|
|
EditorSpellCheck::GetSuggestedWord(nsAString& aSuggestedWord)
|
2002-10-12 03:46:57 +04:00
|
|
|
{
|
2016-10-24 05:27:45 +03:00
|
|
|
// XXX This is buggy if mSuggestedWordList.Length() is over INT32_MAX.
|
|
|
|
if (mSuggestedWordIndex < static_cast<int32_t>(mSuggestedWordList.Length())) {
|
2017-12-13 13:14:27 +03:00
|
|
|
aSuggestedWord = mSuggestedWordList[mSuggestedWordIndex];
|
2002-10-12 03:46:57 +04:00
|
|
|
mSuggestedWordIndex++;
|
|
|
|
} else {
|
|
|
|
// A blank string signals that there are no more strings
|
2017-12-13 13:14:27 +03:00
|
|
|
aSuggestedWord.Truncate();
|
2002-10-12 03:46:57 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-05-28 18:58:42 +03:00
|
|
|
NS_IMETHODIMP
|
2018-01-18 15:01:13 +03:00
|
|
|
EditorSpellCheck::CheckCurrentWord(const nsAString& aSuggestedWord,
|
|
|
|
bool* aIsMisspelled)
|
2002-10-12 03:46:57 +04:00
|
|
|
{
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(mSpellChecker, NS_ERROR_NOT_INITIALIZED);
|
2002-10-12 03:46:57 +04:00
|
|
|
|
|
|
|
DeleteSuggestedWordList();
|
2017-12-13 13:14:27 +03:00
|
|
|
return mSpellChecker->CheckWord(aSuggestedWord,
|
2003-12-20 16:06:44 +03:00
|
|
|
aIsMisspelled, &mSuggestedWordList);
|
2002-10-12 03:46:57 +04:00
|
|
|
}
|
|
|
|
|
2015-05-28 18:58:42 +03:00
|
|
|
NS_IMETHODIMP
|
2018-01-18 15:01:13 +03:00
|
|
|
EditorSpellCheck::CheckCurrentWordNoSuggest(const nsAString& aSuggestedWord,
|
|
|
|
bool* aIsMisspelled)
|
2005-02-02 00:12:53 +03:00
|
|
|
{
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(mSpellChecker, NS_ERROR_NOT_INITIALIZED);
|
2005-02-02 00:12:53 +03:00
|
|
|
|
2017-12-13 13:14:27 +03:00
|
|
|
return mSpellChecker->CheckWord(aSuggestedWord,
|
2012-07-30 18:20:58 +04:00
|
|
|
aIsMisspelled, nullptr);
|
2005-02-02 00:12:53 +03:00
|
|
|
}
|
|
|
|
|
2015-05-28 18:58:42 +03:00
|
|
|
NS_IMETHODIMP
|
2018-01-18 15:01:13 +03:00
|
|
|
EditorSpellCheck::ReplaceWord(const nsAString& aMisspelledWord,
|
|
|
|
const nsAString& aReplaceWord,
|
|
|
|
bool aAllOccurrences)
|
2002-10-12 03:46:57 +04:00
|
|
|
{
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(mSpellChecker, NS_ERROR_NOT_INITIALIZED);
|
2002-10-12 03:46:57 +04:00
|
|
|
|
2017-12-13 13:14:27 +03:00
|
|
|
return mSpellChecker->Replace(aMisspelledWord,
|
2018-01-18 15:01:13 +03:00
|
|
|
aReplaceWord, aAllOccurrences);
|
2002-10-12 03:46:57 +04:00
|
|
|
}
|
|
|
|
|
2015-05-28 18:58:42 +03:00
|
|
|
NS_IMETHODIMP
|
2018-01-18 15:01:13 +03:00
|
|
|
EditorSpellCheck::IgnoreWordAllOccurrences(const nsAString& aWord)
|
2002-10-12 03:46:57 +04:00
|
|
|
{
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(mSpellChecker, NS_ERROR_NOT_INITIALIZED);
|
2002-10-12 03:46:57 +04:00
|
|
|
|
2017-12-13 13:14:27 +03:00
|
|
|
return mSpellChecker->IgnoreAll(aWord);
|
2002-10-12 03:46:57 +04:00
|
|
|
}
|
|
|
|
|
2015-05-28 18:58:42 +03:00
|
|
|
NS_IMETHODIMP
|
2018-01-18 15:01:13 +03:00
|
|
|
EditorSpellCheck::GetPersonalDictionary()
|
2002-10-12 03:46:57 +04:00
|
|
|
{
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(mSpellChecker, NS_ERROR_NOT_INITIALIZED);
|
2002-10-12 03:46:57 +04:00
|
|
|
|
|
|
|
// We can spell check with any editor type
|
|
|
|
mDictionaryList.Clear();
|
|
|
|
mDictionaryIndex = 0;
|
|
|
|
return mSpellChecker->GetPersonalDictionary(&mDictionaryList);
|
|
|
|
}
|
|
|
|
|
2015-05-28 18:58:42 +03:00
|
|
|
NS_IMETHODIMP
|
2018-01-18 15:01:13 +03:00
|
|
|
EditorSpellCheck::GetPersonalDictionaryWord(nsAString& aDictionaryWord)
|
2002-10-12 03:46:57 +04:00
|
|
|
{
|
2016-10-24 05:27:45 +03:00
|
|
|
// XXX This is buggy if mDictionaryList.Length() is over INT32_MAX.
|
|
|
|
if (mDictionaryIndex < static_cast<int32_t>(mDictionaryList.Length())) {
|
2017-12-13 13:14:27 +03:00
|
|
|
aDictionaryWord = mDictionaryList[mDictionaryIndex];
|
2002-10-12 03:46:57 +04:00
|
|
|
mDictionaryIndex++;
|
|
|
|
} else {
|
|
|
|
// A blank string signals that there are no more strings
|
2017-12-13 13:14:27 +03:00
|
|
|
aDictionaryWord.Truncate();
|
2002-10-12 03:46:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-05-28 18:58:42 +03:00
|
|
|
NS_IMETHODIMP
|
2018-01-18 15:01:13 +03:00
|
|
|
EditorSpellCheck::AddWordToDictionary(const nsAString& aWord)
|
2002-10-12 03:46:57 +04:00
|
|
|
{
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(mSpellChecker, NS_ERROR_NOT_INITIALIZED);
|
2002-10-12 03:46:57 +04:00
|
|
|
|
2017-12-13 13:14:27 +03:00
|
|
|
return mSpellChecker->AddWordToPersonalDictionary(aWord);
|
2002-10-12 03:46:57 +04:00
|
|
|
}
|
|
|
|
|
2015-05-28 18:58:42 +03:00
|
|
|
NS_IMETHODIMP
|
2018-01-18 15:01:13 +03:00
|
|
|
EditorSpellCheck::RemoveWordFromDictionary(const nsAString& aWord)
|
2002-10-12 03:46:57 +04:00
|
|
|
{
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(mSpellChecker, NS_ERROR_NOT_INITIALIZED);
|
2002-10-12 03:46:57 +04:00
|
|
|
|
2017-12-13 13:14:27 +03:00
|
|
|
return mSpellChecker->RemoveWordFromPersonalDictionary(aWord);
|
2002-10-12 03:46:57 +04:00
|
|
|
}
|
|
|
|
|
2015-05-28 18:58:42 +03:00
|
|
|
NS_IMETHODIMP
|
2018-01-18 15:01:13 +03:00
|
|
|
EditorSpellCheck::GetDictionaryList(char16_t*** aDictionaryList,
|
|
|
|
uint32_t* aCount)
|
2002-10-12 03:46:57 +04:00
|
|
|
{
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(mSpellChecker, NS_ERROR_NOT_INITIALIZED);
|
2002-10-12 03:46:57 +04:00
|
|
|
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(aDictionaryList && aCount, NS_ERROR_NULL_POINTER);
|
2002-10-12 03:46:57 +04:00
|
|
|
|
|
|
|
*aDictionaryList = 0;
|
|
|
|
*aCount = 0;
|
|
|
|
|
2009-01-18 23:14:14 +03:00
|
|
|
nsTArray<nsString> dictList;
|
2002-10-12 03:46:57 +04:00
|
|
|
|
|
|
|
nsresult rv = mSpellChecker->GetDictionaryList(&dictList);
|
|
|
|
|
2010-06-18 00:44:35 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-10-12 03:46:57 +04:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t **tmpPtr = 0;
|
2002-10-12 03:46:57 +04:00
|
|
|
|
2016-10-24 05:27:45 +03:00
|
|
|
if (dictList.IsEmpty()) {
|
2002-10-12 03:46:57 +04:00
|
|
|
// If there are no dictionaries, return an array containing
|
|
|
|
// one element and a count of one.
|
|
|
|
|
2015-03-27 03:01:12 +03:00
|
|
|
tmpPtr = (char16_t **)moz_xmalloc(sizeof(char16_t *));
|
2002-10-12 03:46:57 +04:00
|
|
|
|
|
|
|
*tmpPtr = 0;
|
|
|
|
*aDictionaryList = tmpPtr;
|
|
|
|
*aCount = 0;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-27 03:01:12 +03:00
|
|
|
tmpPtr = (char16_t **)moz_xmalloc(sizeof(char16_t *) * dictList.Length());
|
2002-10-12 03:46:57 +04:00
|
|
|
|
|
|
|
*aDictionaryList = tmpPtr;
|
2009-01-18 23:14:14 +03:00
|
|
|
*aCount = dictList.Length();
|
2002-10-12 03:46:57 +04:00
|
|
|
|
2016-10-24 05:27:45 +03:00
|
|
|
for (uint32_t i = 0; i < *aCount; i++) {
|
2009-01-18 23:14:14 +03:00
|
|
|
tmpPtr[i] = ToNewUnicode(dictList[i]);
|
2002-10-12 03:46:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-05-28 18:58:42 +03:00
|
|
|
NS_IMETHODIMP
|
2018-01-18 15:01:13 +03:00
|
|
|
EditorSpellCheck::GetCurrentDictionary(nsAString& aDictionary)
|
2002-10-12 03:46:57 +04:00
|
|
|
{
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(mSpellChecker, NS_ERROR_NOT_INITIALIZED);
|
2012-07-17 08:01:15 +04:00
|
|
|
|
2011-08-15 21:55:02 +04:00
|
|
|
return mSpellChecker->GetCurrentDictionary(aDictionary);
|
2002-10-12 03:46:57 +04:00
|
|
|
}
|
|
|
|
|
2015-05-28 18:58:42 +03:00
|
|
|
NS_IMETHODIMP
|
2018-01-18 15:01:13 +03:00
|
|
|
EditorSpellCheck::SetCurrentDictionary(const nsAString& aDictionary)
|
2002-10-12 03:46:57 +04:00
|
|
|
{
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(mSpellChecker, NS_ERROR_NOT_INITIALIZED);
|
2002-10-12 03:46:57 +04:00
|
|
|
|
2018-01-18 15:01:13 +03:00
|
|
|
RefPtr<EditorSpellCheck> kungFuDeathGrip = this;
|
2012-07-18 07:02:00 +04:00
|
|
|
|
2013-06-06 04:07:54 +04:00
|
|
|
// The purpose of mUpdateDictionaryRunning is to avoid doing all of this if
|
|
|
|
// UpdateCurrentDictionary's helper method DictionaryFetched, which calls us,
|
2015-09-07 10:06:00 +03:00
|
|
|
// is on the stack. In other words: Only do this, if the user manually selected a
|
|
|
|
// dictionary to use.
|
2011-08-15 21:54:03 +04:00
|
|
|
if (!mUpdateDictionaryRunning) {
|
2011-08-23 23:03:33 +04:00
|
|
|
|
2013-06-06 04:07:54 +04:00
|
|
|
// Ignore pending dictionary fetchers by increasing this number.
|
|
|
|
mDictionaryFetcherGroup++;
|
|
|
|
|
2015-04-28 12:01:00 +03:00
|
|
|
uint32_t flags = 0;
|
|
|
|
mEditor->GetFlags(&flags);
|
|
|
|
if (!(flags & nsIPlaintextEditor::eEditorMailMask)) {
|
2015-09-15 11:34:00 +03:00
|
|
|
if (!aDictionary.IsEmpty() && (mPreferredLang.IsEmpty() ||
|
|
|
|
!mPreferredLang.Equals(aDictionary,
|
|
|
|
nsCaseInsensitiveStringComparator()))) {
|
2015-04-28 12:01:00 +03:00
|
|
|
// When user sets dictionary manually, we store this value associated
|
2015-09-02 06:12:00 +03:00
|
|
|
// with editor url, if it doesn't match the document language exactly.
|
|
|
|
// For example on "en" sites, we need to store "en-GB", otherwise
|
|
|
|
// the language might jump back to en-US although the user explicitly
|
|
|
|
// chose otherwise.
|
2015-04-28 12:01:00 +03:00
|
|
|
StoreCurrentDictionary(mEditor, aDictionary);
|
2015-09-07 10:06:00 +03:00
|
|
|
#ifdef DEBUG_DICT
|
|
|
|
printf("***** Writing content preferences for |%s|\n",
|
|
|
|
NS_ConvertUTF16toUTF8(aDictionary).get());
|
|
|
|
#endif
|
2015-04-28 12:01:00 +03:00
|
|
|
} else {
|
2015-09-02 06:12:00 +03:00
|
|
|
// If user sets a dictionary matching the language defined by
|
2015-04-28 12:01:00 +03:00
|
|
|
// document, we consider content pref has been canceled, and we clear it.
|
|
|
|
ClearCurrentDictionary(mEditor);
|
2015-09-07 10:06:00 +03:00
|
|
|
#ifdef DEBUG_DICT
|
|
|
|
printf("***** Clearing content preferences for |%s|\n",
|
|
|
|
NS_ConvertUTF16toUTF8(aDictionary).get());
|
|
|
|
#endif
|
2015-04-28 12:01:00 +03:00
|
|
|
}
|
2011-08-23 23:03:33 +04:00
|
|
|
|
2015-09-07 10:06:00 +03:00
|
|
|
// Also store it in as a preference, so we can use it as a fallback.
|
|
|
|
// We don't want this for mail composer because it uses
|
|
|
|
// "spellchecker.dictionary" as a preference.
|
2017-11-13 01:19:55 +03:00
|
|
|
//
|
|
|
|
// XXX: Prefs can only be set in the parent process, so this condition is
|
|
|
|
// necessary to stop libpref from throwing errors. But this should
|
|
|
|
// probably be handled in a better way.
|
|
|
|
if (XRE_IsParentProcess()) {
|
|
|
|
Preferences::SetString("spellchecker.dictionary", aDictionary);
|
2015-09-07 10:06:00 +03:00
|
|
|
#ifdef DEBUG_DICT
|
2017-11-13 01:19:55 +03:00
|
|
|
printf("***** Possibly storing spellchecker.dictionary |%s|\n",
|
|
|
|
NS_ConvertUTF16toUTF8(aDictionary).get());
|
2015-09-07 10:06:00 +03:00
|
|
|
#endif
|
2017-11-13 01:19:55 +03:00
|
|
|
}
|
2015-04-28 12:01:00 +03:00
|
|
|
}
|
2011-08-15 21:54:03 +04:00
|
|
|
}
|
2011-08-15 21:55:02 +04:00
|
|
|
return mSpellChecker->SetCurrentDictionary(aDictionary);
|
2002-10-12 03:46:57 +04:00
|
|
|
}
|
|
|
|
|
2015-05-28 18:58:42 +03:00
|
|
|
NS_IMETHODIMP
|
2018-01-18 15:01:13 +03:00
|
|
|
EditorSpellCheck::UninitSpellChecker()
|
2002-10-12 03:46:57 +04:00
|
|
|
{
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(mSpellChecker, NS_ERROR_NOT_INITIALIZED);
|
2002-10-12 03:46:57 +04:00
|
|
|
|
2006-05-13 03:15:17 +04:00
|
|
|
// Cleanup - kill the spell checker
|
|
|
|
DeleteSuggestedWordList();
|
|
|
|
mDictionaryList.Clear();
|
|
|
|
mDictionaryIndex = 0;
|
2018-03-20 08:27:06 +03:00
|
|
|
mDictionaryFetcherGroup++;
|
2016-11-10 06:11:27 +03:00
|
|
|
mSpellChecker = nullptr;
|
2006-05-13 03:15:17 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-28 18:58:42 +03:00
|
|
|
NS_IMETHODIMP
|
2018-09-08 20:31:25 +03:00
|
|
|
EditorSpellCheck::SetFilterType(uint32_t aFilterType)
|
2002-12-10 18:03:04 +03:00
|
|
|
{
|
2018-09-08 20:31:25 +03:00
|
|
|
mTxtSrvFilterType = aFilterType;
|
2002-12-10 18:03:04 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-05-28 18:58:42 +03:00
|
|
|
nsresult
|
2018-01-18 15:01:13 +03:00
|
|
|
EditorSpellCheck::DeleteSuggestedWordList()
|
2002-10-12 03:46:57 +04:00
|
|
|
{
|
|
|
|
mSuggestedWordList.Clear();
|
|
|
|
mSuggestedWordIndex = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-08-12 23:12:45 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2018-01-18 15:01:13 +03:00
|
|
|
EditorSpellCheck::UpdateCurrentDictionary(
|
|
|
|
nsIEditorSpellCheckCallback* aCallback)
|
2011-08-12 23:12:45 +04:00
|
|
|
{
|
2017-04-12 11:43:51 +03:00
|
|
|
if (NS_WARN_IF(!mSpellChecker)) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2011-08-12 23:12:45 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
2018-01-18 15:01:13 +03:00
|
|
|
RefPtr<EditorSpellCheck> kungFuDeathGrip = this;
|
2012-07-18 07:02:00 +04:00
|
|
|
|
2011-08-23 23:03:33 +04:00
|
|
|
// Get language with html5 algorithm
|
2011-08-12 23:12:45 +04:00
|
|
|
nsCOMPtr<nsIContent> rootContent;
|
2018-04-06 08:53:05 +03:00
|
|
|
HTMLEditor* htmlEditor = mEditor->AsHTMLEditor();
|
2011-08-12 23:12:45 +04:00
|
|
|
if (htmlEditor) {
|
|
|
|
rootContent = htmlEditor->GetActiveEditingHost();
|
|
|
|
} else {
|
2018-04-06 08:53:05 +03:00
|
|
|
rootContent = mEditor->GetRoot();
|
2011-08-12 23:12:45 +04:00
|
|
|
}
|
2015-04-28 12:01:00 +03:00
|
|
|
|
|
|
|
// Try to get topmost document's document element for embedded mail editor.
|
|
|
|
uint32_t flags = 0;
|
|
|
|
mEditor->GetFlags(&flags);
|
|
|
|
if (flags & nsIPlaintextEditor::eEditorMailMask) {
|
|
|
|
nsCOMPtr<nsIDocument> ownerDoc = rootContent->OwnerDoc();
|
|
|
|
NS_ENSURE_TRUE(ownerDoc, NS_ERROR_FAILURE);
|
|
|
|
nsIDocument* parentDoc = ownerDoc->GetParentDocument();
|
|
|
|
if (parentDoc) {
|
2018-10-02 00:38:01 +03:00
|
|
|
rootContent = parentDoc->GetDocumentElement();
|
2015-04-28 12:01:00 +03:00
|
|
|
}
|
|
|
|
}
|
2015-07-22 19:24:59 +03:00
|
|
|
|
|
|
|
if (!rootContent) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2011-08-12 23:12:45 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DictionaryFetcher> fetcher =
|
2014-10-03 18:52:37 +04:00
|
|
|
new DictionaryFetcher(this, aCallback, mDictionaryFetcherGroup);
|
2013-06-06 04:07:54 +04:00
|
|
|
rootContent->GetLang(fetcher->mRootContentLang);
|
2018-03-06 03:34:40 +03:00
|
|
|
nsCOMPtr<nsIDocument> doc = rootContent->GetComposedDoc();
|
2013-06-06 04:07:54 +04:00
|
|
|
NS_ENSURE_STATE(doc);
|
|
|
|
doc->GetContentLanguage(fetcher->mRootDocContentLang);
|
|
|
|
|
2014-10-03 18:52:37 +04:00
|
|
|
rv = fetcher->Fetch(mEditor);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-06-06 04:07:54 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-07 10:06:00 +03:00
|
|
|
// Helper function that iterates over the list of dictionaries and sets the one
|
|
|
|
// that matches based on a given comparison type.
|
2017-04-11 11:05:19 +03:00
|
|
|
void
|
2018-01-18 15:01:13 +03:00
|
|
|
EditorSpellCheck::BuildDictionaryList(const nsAString& aDictName,
|
|
|
|
const nsTArray<nsString>& aDictList,
|
|
|
|
enum dictCompare aCompareType,
|
|
|
|
nsTArray<nsString>& aOutList)
|
2015-09-07 10:06:00 +03:00
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < aDictList.Length(); i++) {
|
|
|
|
nsAutoString dictStr(aDictList.ElementAt(i));
|
|
|
|
bool equals = false;
|
|
|
|
switch (aCompareType) {
|
|
|
|
case DICT_NORMAL_COMPARE:
|
|
|
|
equals = aDictName.Equals(dictStr);
|
|
|
|
break;
|
|
|
|
case DICT_COMPARE_CASE_INSENSITIVE:
|
|
|
|
equals = aDictName.Equals(dictStr, nsCaseInsensitiveStringComparator());
|
|
|
|
break;
|
|
|
|
case DICT_COMPARE_DASHMATCH:
|
|
|
|
equals = nsStyleUtil::DashMatchCompare(dictStr, aDictName, nsCaseInsensitiveStringComparator());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (equals) {
|
2017-04-11 11:05:19 +03:00
|
|
|
aOutList.AppendElement(dictStr);
|
2015-09-07 10:06:00 +03:00
|
|
|
#ifdef DEBUG_DICT
|
2017-04-11 11:05:19 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
printf("***** Trying |%s|.\n", NS_ConvertUTF16toUTF8(dictStr).get());
|
|
|
|
}
|
2015-09-07 10:06:00 +03:00
|
|
|
#endif
|
|
|
|
// We always break here. We tried to set the dictionary to an existing
|
|
|
|
// dictionary from the list. This must work, if it doesn't, there is
|
|
|
|
// no point trying another one.
|
2017-04-11 11:05:19 +03:00
|
|
|
return;
|
2015-09-07 10:06:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-06 04:07:54 +04:00
|
|
|
nsresult
|
2018-01-18 15:01:13 +03:00
|
|
|
EditorSpellCheck::DictionaryFetched(DictionaryFetcher* aFetcher)
|
2013-06-06 04:07:54 +04:00
|
|
|
{
|
2014-10-03 18:52:37 +04:00
|
|
|
MOZ_ASSERT(aFetcher);
|
2018-01-18 15:01:13 +03:00
|
|
|
RefPtr<EditorSpellCheck> kungFuDeathGrip = this;
|
2011-08-23 23:03:33 +04:00
|
|
|
|
2017-04-11 11:05:19 +03:00
|
|
|
BeginUpdateDictionary();
|
2013-06-06 04:07:54 +04:00
|
|
|
|
|
|
|
if (aFetcher->mGroup < mDictionaryFetcherGroup) {
|
|
|
|
// SetCurrentDictionary was called after the fetch started. Don't overwrite
|
|
|
|
// that dictionary with the fetched one.
|
2017-04-11 11:05:19 +03:00
|
|
|
EndUpdateDictionary();
|
2017-05-18 08:50:25 +03:00
|
|
|
if (aFetcher->mCallback) {
|
|
|
|
aFetcher->mCallback->EditorSpellCheckDone();
|
|
|
|
}
|
2013-06-06 04:07:54 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-07 10:06:00 +03:00
|
|
|
/*
|
|
|
|
* We try to derive the dictionary to use based on the following priorities:
|
|
|
|
* 1) Content preference, so the language the user set for the site before.
|
|
|
|
* (Introduced in bug 678842 and corrected in bug 717433.)
|
|
|
|
* 2) Language set by the website, or any other dictionary that partly
|
|
|
|
* matches that. (Introduced in bug 338427.)
|
|
|
|
* Eg. if the website is "en-GB", a user who only has "en-US" will get
|
|
|
|
* that. If the website is generic "en", the user will get one of the
|
|
|
|
* "en-*" installed, (almost) at random.
|
|
|
|
* However, we prefer what is stored in "spellchecker.dictionary",
|
|
|
|
* so if the user chose "en-AU" before, they will get "en-AU" on a plain
|
|
|
|
* "en" site. (Introduced in bug 682564.)
|
|
|
|
* 3) The value of "spellchecker.dictionary" which reflects a previous
|
|
|
|
* language choice of the user (on another site).
|
|
|
|
* (This was the original behaviour before the aforementioned bugs
|
|
|
|
* landed).
|
|
|
|
* 4) The user's locale.
|
|
|
|
* 5) Use the current dictionary that is currently set.
|
|
|
|
* 6) The content of the "LANG" environment variable (if set).
|
|
|
|
* 7) The first spell check dictionary installed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Get the language from the element or its closest parent according to:
|
|
|
|
// https://html.spec.whatwg.org/#attr-lang
|
|
|
|
// This is used in SetCurrentDictionary.
|
2013-06-06 04:07:54 +04:00
|
|
|
mPreferredLang.Assign(aFetcher->mRootContentLang);
|
2015-09-07 10:06:00 +03:00
|
|
|
#ifdef DEBUG_DICT
|
|
|
|
printf("***** mPreferredLang (element) |%s|\n",
|
|
|
|
NS_ConvertUTF16toUTF8(mPreferredLang).get());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// If no luck, try the "Content-Language" header.
|
|
|
|
if (mPreferredLang.IsEmpty()) {
|
|
|
|
mPreferredLang.Assign(aFetcher->mRootDocContentLang);
|
|
|
|
#ifdef DEBUG_DICT
|
|
|
|
printf("***** mPreferredLang (content-language) |%s|\n",
|
|
|
|
NS_ConvertUTF16toUTF8(mPreferredLang).get());
|
|
|
|
#endif
|
|
|
|
}
|
2013-06-06 04:07:54 +04:00
|
|
|
|
2015-09-21 22:51:00 +03:00
|
|
|
// We obtain a list of available dictionaries.
|
2017-04-11 11:05:19 +03:00
|
|
|
AutoTArray<nsString, 8> dictList;
|
|
|
|
nsresult rv = mSpellChecker->GetDictionaryList(&dictList);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
EndUpdateDictionary();
|
2017-05-18 08:50:25 +03:00
|
|
|
if (aFetcher->mCallback) {
|
|
|
|
aFetcher->mCallback->EditorSpellCheckDone();
|
|
|
|
}
|
2017-04-11 11:05:19 +03:00
|
|
|
return rv;
|
|
|
|
}
|
2015-09-21 22:51:00 +03:00
|
|
|
|
2015-09-07 10:06:00 +03:00
|
|
|
// Priority 1:
|
2013-06-06 04:07:54 +04:00
|
|
|
// If we successfully fetched a dictionary from content prefs, do not go
|
2011-08-23 23:03:33 +04:00
|
|
|
// further. Use this exact dictionary.
|
2015-04-28 12:01:00 +03:00
|
|
|
// Don't use content preferences for editor with eEditorMailMask flag.
|
|
|
|
nsAutoString dictName;
|
|
|
|
uint32_t flags;
|
|
|
|
mEditor->GetFlags(&flags);
|
|
|
|
if (!(flags & nsIPlaintextEditor::eEditorMailMask)) {
|
|
|
|
dictName.Assign(aFetcher->mDictionary);
|
|
|
|
if (!dictName.IsEmpty()) {
|
2017-04-11 11:05:19 +03:00
|
|
|
AutoTArray<nsString, 1> tryDictList;
|
|
|
|
BuildDictionaryList(dictName, dictList, DICT_NORMAL_COMPARE, tryDictList);
|
|
|
|
|
2018-01-18 15:01:13 +03:00
|
|
|
RefPtr<EditorSpellCheck> self = this;
|
2017-04-11 11:05:19 +03:00
|
|
|
RefPtr<DictionaryFetcher> fetcher = aFetcher;
|
|
|
|
mSpellChecker->SetCurrentDictionaryFromList(tryDictList)->Then(
|
2017-06-10 07:24:46 +03:00
|
|
|
GetMainThreadSerialEventTarget(),
|
2017-04-11 11:05:19 +03:00
|
|
|
__func__,
|
|
|
|
[self, fetcher]() {
|
2015-09-07 10:06:00 +03:00
|
|
|
#ifdef DEBUG_DICT
|
2017-04-11 11:05:19 +03:00
|
|
|
printf("***** Assigned from content preferences |%s|\n",
|
|
|
|
NS_ConvertUTF16toUTF8(dictName).get());
|
2015-09-07 10:06:00 +03:00
|
|
|
#endif
|
2017-04-11 11:05:19 +03:00
|
|
|
// We take an early exit here, so let's not forget to clear the word
|
|
|
|
// list.
|
|
|
|
self->DeleteSuggestedWordList();
|
|
|
|
|
|
|
|
self->EndUpdateDictionary();
|
2017-05-18 08:50:25 +03:00
|
|
|
if (fetcher->mCallback) {
|
|
|
|
fetcher->mCallback->EditorSpellCheckDone();
|
|
|
|
}
|
2017-04-11 11:05:19 +03:00
|
|
|
},
|
2018-04-20 00:33:18 +03:00
|
|
|
[self, fetcher](nsresult aError) {
|
|
|
|
if (aError == NS_ERROR_ABORT) {
|
|
|
|
return;
|
|
|
|
}
|
2017-04-11 11:05:19 +03:00
|
|
|
// May be dictionary was uninstalled ?
|
|
|
|
// Clear the content preference and continue.
|
|
|
|
ClearCurrentDictionary(self->mEditor);
|
|
|
|
|
|
|
|
// Priority 2 or later will handled by the following
|
|
|
|
self->SetFallbackDictionary(fetcher);
|
|
|
|
});
|
|
|
|
return NS_OK;
|
2011-08-23 23:03:33 +04:00
|
|
|
}
|
|
|
|
}
|
2017-04-11 11:05:19 +03:00
|
|
|
SetFallbackDictionary(aFetcher);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-18 15:01:13 +03:00
|
|
|
EditorSpellCheck::SetFallbackDictionary(DictionaryFetcher* aFetcher)
|
2017-04-11 11:05:19 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mUpdateDictionaryRunning);
|
|
|
|
|
|
|
|
AutoTArray<nsString, 6> tryDictList;
|
|
|
|
|
|
|
|
// We obtain a list of available dictionaries.
|
|
|
|
AutoTArray<nsString, 8> dictList;
|
|
|
|
nsresult rv = mSpellChecker->GetDictionaryList(&dictList);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
EndUpdateDictionary();
|
2017-05-18 08:50:25 +03:00
|
|
|
if (aFetcher->mCallback) {
|
|
|
|
aFetcher->mCallback->EditorSpellCheckDone();
|
|
|
|
}
|
2017-04-11 11:05:19 +03:00
|
|
|
return;
|
|
|
|
}
|
2011-08-12 23:12:45 +04:00
|
|
|
|
2015-09-07 10:06:00 +03:00
|
|
|
// Priority 2:
|
|
|
|
// After checking the content preferences, we use the language of the element
|
|
|
|
// or document.
|
2017-04-11 11:05:19 +03:00
|
|
|
nsAutoString dictName(mPreferredLang);
|
2015-09-07 10:06:00 +03:00
|
|
|
#ifdef DEBUG_DICT
|
|
|
|
printf("***** Assigned from element/doc |%s|\n",
|
|
|
|
NS_ConvertUTF16toUTF8(dictName).get());
|
|
|
|
#endif
|
2011-08-12 23:12:45 +04:00
|
|
|
|
2015-09-07 10:06:00 +03:00
|
|
|
// Get the preference value.
|
|
|
|
nsAutoString preferredDict;
|
2017-07-31 07:23:50 +03:00
|
|
|
Preferences::GetLocalizedString("spellchecker.dictionary", preferredDict);
|
2011-08-12 23:12:45 +04:00
|
|
|
|
2015-09-07 10:06:00 +03:00
|
|
|
if (!dictName.IsEmpty()) {
|
|
|
|
// RFC 5646 explicitly states that matches should be case-insensitive.
|
2017-04-11 11:05:19 +03:00
|
|
|
BuildDictionaryList(dictName, dictList, DICT_COMPARE_CASE_INSENSITIVE,
|
|
|
|
tryDictList);
|
2014-04-10 13:28:23 +04:00
|
|
|
|
2015-09-07 10:06:00 +03:00
|
|
|
#ifdef DEBUG_DICT
|
2017-04-11 11:05:19 +03:00
|
|
|
printf("***** Trying from element/doc |%s| \n",
|
|
|
|
NS_ConvertUTF16toUTF8(dictName).get());
|
2015-09-07 10:06:00 +03:00
|
|
|
#endif
|
2014-04-10 13:28:23 +04:00
|
|
|
|
2017-04-11 11:05:19 +03:00
|
|
|
// Required dictionary was not available. Try to get a dictionary
|
|
|
|
// matching at least language part of dictName.
|
|
|
|
nsAutoString langCode;
|
|
|
|
int32_t dashIdx = dictName.FindChar('-');
|
|
|
|
if (dashIdx != -1) {
|
|
|
|
langCode.Assign(Substring(dictName, 0, dashIdx));
|
|
|
|
} else {
|
|
|
|
langCode.Assign(dictName);
|
|
|
|
}
|
2011-09-04 17:00:20 +04:00
|
|
|
|
2017-04-11 11:05:19 +03:00
|
|
|
// Try dictionary.spellchecker preference, if it starts with langCode,
|
|
|
|
// so we don't just get any random dictionary matching the language.
|
|
|
|
if (!preferredDict.IsEmpty() &&
|
|
|
|
nsStyleUtil::DashMatchCompare(preferredDict, langCode, nsDefaultStringComparator())) {
|
2015-09-07 10:06:00 +03:00
|
|
|
#ifdef DEBUG_DICT
|
2017-04-11 11:05:19 +03:00
|
|
|
printf("***** Trying preference value |%s| since it matches language code\n",
|
|
|
|
NS_ConvertUTF16toUTF8(preferredDict).get());
|
2015-09-07 10:06:00 +03:00
|
|
|
#endif
|
2017-04-11 11:05:19 +03:00
|
|
|
BuildDictionaryList(preferredDict, dictList,
|
|
|
|
DICT_COMPARE_CASE_INSENSITIVE, tryDictList);
|
|
|
|
}
|
2014-04-10 13:28:23 +04:00
|
|
|
|
2017-04-11 11:05:19 +03:00
|
|
|
// Use any dictionary with the required language.
|
2015-09-07 10:06:00 +03:00
|
|
|
#ifdef DEBUG_DICT
|
2017-04-11 11:05:19 +03:00
|
|
|
printf("***** Trying to find match for language code |%s|\n",
|
|
|
|
NS_ConvertUTF16toUTF8(langCode).get());
|
2015-09-07 10:06:00 +03:00
|
|
|
#endif
|
2017-04-11 11:05:19 +03:00
|
|
|
BuildDictionaryList(langCode, dictList, DICT_COMPARE_DASHMATCH,
|
|
|
|
tryDictList);
|
2015-09-07 10:06:00 +03:00
|
|
|
}
|
2015-09-07 10:06:00 +03:00
|
|
|
|
2015-09-07 10:06:00 +03:00
|
|
|
// Priority 3:
|
|
|
|
// If the document didn't supply a dictionary or the setting failed,
|
|
|
|
// try the user preference next.
|
2017-04-11 11:05:19 +03:00
|
|
|
if (!preferredDict.IsEmpty()) {
|
2015-09-07 10:06:00 +03:00
|
|
|
#ifdef DEBUG_DICT
|
2017-04-11 11:05:19 +03:00
|
|
|
printf("***** Trying preference value |%s|\n",
|
|
|
|
NS_ConvertUTF16toUTF8(preferredDict).get());
|
2015-09-07 10:06:00 +03:00
|
|
|
#endif
|
2017-04-11 11:05:19 +03:00
|
|
|
BuildDictionaryList(preferredDict, dictList, DICT_NORMAL_COMPARE,
|
|
|
|
tryDictList);
|
2015-09-07 10:06:00 +03:00
|
|
|
}
|
|
|
|
|
2015-09-07 10:06:00 +03:00
|
|
|
// Priority 4:
|
|
|
|
// As next fallback, try the current locale.
|
2017-04-11 11:05:19 +03:00
|
|
|
nsAutoCString utf8DictName;
|
|
|
|
LocaleService::GetInstance()->GetAppLocaleAsLangTag(utf8DictName);
|
2017-03-15 02:09:54 +03:00
|
|
|
|
2017-04-11 11:05:19 +03:00
|
|
|
CopyUTF8toUTF16(utf8DictName, dictName);
|
2015-09-07 10:06:00 +03:00
|
|
|
#ifdef DEBUG_DICT
|
2017-04-11 11:05:19 +03:00
|
|
|
printf("***** Trying locale |%s|\n",
|
|
|
|
NS_ConvertUTF16toUTF8(dictName).get());
|
2015-09-07 10:06:00 +03:00
|
|
|
#endif
|
2017-04-11 11:05:19 +03:00
|
|
|
BuildDictionaryList(dictName, dictList, DICT_COMPARE_CASE_INSENSITIVE,
|
|
|
|
tryDictList);
|
2015-09-07 10:06:00 +03:00
|
|
|
|
|
|
|
// Priority 5:
|
2017-04-11 11:05:19 +03:00
|
|
|
// If we have a current dictionary and we don't have no item in try list,
|
|
|
|
// don't try anything else.
|
|
|
|
nsAutoString currentDictionary;
|
|
|
|
GetCurrentDictionary(currentDictionary);
|
|
|
|
if (!currentDictionary.IsEmpty() && tryDictList.IsEmpty()) {
|
2015-09-07 10:06:00 +03:00
|
|
|
#ifdef DEBUG_DICT
|
2017-04-11 11:05:19 +03:00
|
|
|
printf("***** Retrieved current dict |%s|\n",
|
|
|
|
NS_ConvertUTF16toUTF8(currentDictionary).get());
|
2015-09-07 10:06:00 +03:00
|
|
|
#endif
|
2017-04-11 11:05:19 +03:00
|
|
|
EndUpdateDictionary();
|
2017-05-18 08:50:25 +03:00
|
|
|
if (aFetcher->mCallback) {
|
|
|
|
aFetcher->mCallback->EditorSpellCheckDone();
|
|
|
|
}
|
2017-04-11 11:05:19 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-09-07 10:06:00 +03:00
|
|
|
|
2017-04-11 11:05:19 +03:00
|
|
|
// Priority 6:
|
|
|
|
// Try to get current dictionary from environment variable LANG.
|
|
|
|
// LANG = language[_territory][.charset]
|
|
|
|
char* env_lang = getenv("LANG");
|
|
|
|
if (env_lang) {
|
|
|
|
nsString lang = NS_ConvertUTF8toUTF16(env_lang);
|
|
|
|
// Strip trailing charset, if there is any.
|
|
|
|
int32_t dot_pos = lang.FindChar('.');
|
|
|
|
if (dot_pos != -1) {
|
|
|
|
lang = Substring(lang, 0, dot_pos);
|
|
|
|
}
|
2015-09-07 10:06:00 +03:00
|
|
|
|
2017-04-11 11:05:19 +03:00
|
|
|
int32_t underScore = lang.FindChar('_');
|
|
|
|
if (underScore != -1) {
|
|
|
|
lang.Replace(underScore, 1, '-');
|
2015-09-07 10:06:00 +03:00
|
|
|
#ifdef DEBUG_DICT
|
2017-04-11 11:05:19 +03:00
|
|
|
printf("***** Trying LANG from environment |%s|\n",
|
|
|
|
NS_ConvertUTF16toUTF8(lang).get());
|
2016-10-24 05:27:45 +03:00
|
|
|
#endif
|
2017-04-11 11:05:19 +03:00
|
|
|
BuildDictionaryList(lang, dictList, DICT_COMPARE_CASE_INSENSITIVE,
|
|
|
|
tryDictList);
|
2011-08-12 23:12:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-11 11:05:19 +03:00
|
|
|
// Priority 7:
|
|
|
|
// If it does not work, pick the first one.
|
|
|
|
if (!dictList.IsEmpty()) {
|
|
|
|
BuildDictionaryList(dictList[0], dictList, DICT_NORMAL_COMPARE,
|
|
|
|
tryDictList);
|
|
|
|
#ifdef DEBUG_DICT
|
|
|
|
printf("***** Trying first of list |%s|\n",
|
|
|
|
NS_ConvertUTF16toUTF8(dictList[0]).get());
|
|
|
|
#endif
|
|
|
|
}
|
2011-08-12 23:12:45 +04:00
|
|
|
|
2018-01-18 15:01:13 +03:00
|
|
|
RefPtr<EditorSpellCheck> self = this;
|
2017-04-11 11:05:19 +03:00
|
|
|
RefPtr<DictionaryFetcher> fetcher = aFetcher;
|
|
|
|
mSpellChecker->SetCurrentDictionaryFromList(tryDictList)->Then(
|
2017-06-10 07:24:46 +03:00
|
|
|
GetMainThreadSerialEventTarget(),
|
2017-04-11 11:05:19 +03:00
|
|
|
__func__,
|
|
|
|
[self, fetcher]() {
|
|
|
|
// If an error was thrown while setting the dictionary, just
|
|
|
|
// fail silently so that the spellchecker dialog is allowed to come
|
|
|
|
// up. The user can manually reset the language to their choice on
|
|
|
|
// the dialog if it is wrong.
|
|
|
|
self->DeleteSuggestedWordList();
|
|
|
|
self->EndUpdateDictionary();
|
2017-05-18 08:50:25 +03:00
|
|
|
if (fetcher->mCallback) {
|
|
|
|
fetcher->mCallback->EditorSpellCheckDone();
|
|
|
|
}
|
2017-04-11 11:05:19 +03:00
|
|
|
});
|
2011-08-12 23:12:45 +04:00
|
|
|
}
|
2018-01-18 15:01:13 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|