Bug 1766823 - Select multiple dictionaries if document has multiple Content-Language values. r=dminor,smaug

Persist dictionary preference in document content language field for mail editors so multiple dictionary selection can persist.

Differential Revision: https://phabricator.services.mozilla.com/D144935
This commit is contained in:
Martin Giger 2022-05-12 08:29:36 +00:00
parent d806a68833
commit 86738ee648
6 changed files with 392 additions and 148 deletions

View File

@ -602,10 +602,22 @@ EditorSpellCheck::SetCurrentDictionaries(
uint32_t flags = 0;
mEditor->GetFlags(&flags);
if (!(flags & nsIEditor::eEditorMailMask)) {
if (!aDictionaries.IsEmpty() &&
(mPreferredLang.IsEmpty() || aDictionaries.Length() > 1 ||
!mPreferredLang.Equals(aDictionaries[0],
nsCaseInsensitiveCStringComparator))) {
bool contentPrefMatchesUserPref = true;
// Check if aDictionaries has the same languages as mPreferredLangs.
if (!aDictionaries.IsEmpty()) {
if (aDictionaries.Length() != mPreferredLangs.Length()) {
contentPrefMatchesUserPref = false;
} else {
for (const auto& dictName : aDictionaries) {
if (mPreferredLangs.IndexOf(dictName) ==
nsTArray<nsCString>::NoIndex) {
contentPrefMatchesUserPref = false;
break;
}
}
}
}
if (!contentPrefMatchesUserPref) {
// When user sets dictionary manually, we store this value associated
// with editor url, if it doesn't match the document language exactly.
// For example on "en" sites, we need to store "en-GB", otherwise
@ -642,6 +654,24 @@ EditorSpellCheck::SetCurrentDictionaries(
asString.Data());
#endif
}
} else {
MOZ_ASSERT(flags & nsIEditor::eEditorMailMask);
// Since the mail editor can only influence the language selection by the
// html lang attribute, set the content-language document to persist
// multi language selections.
nsCOMPtr<nsIContent> rootContent;
if (HTMLEditor* htmlEditor = mEditor->GetAsHTMLEditor()) {
rootContent = htmlEditor->GetActiveEditingHost();
} else {
rootContent = mEditor->GetRoot();
}
RefPtr<Document> ownerDoc = rootContent->OwnerDoc();
Document* parentDoc = ownerDoc->GetInProcessParentDocument();
if (parentDoc) {
parentDoc->SetHeaderData(
nsGkAtoms::headerContentLanguage,
NS_ConvertUTF8toUTF16(DictionariesToString(aDictionaries)));
}
}
}
@ -751,7 +781,7 @@ EditorSpellCheck::UpdateCurrentDictionary(
// Helper function that iterates over the list of dictionaries and sets the one
// that matches based on a given comparison type.
void EditorSpellCheck::BuildDictionaryList(const nsACString& aDictName,
bool EditorSpellCheck::BuildDictionaryList(const nsACString& aDictName,
const nsTArray<nsCString>& aDictList,
enum dictCompare aCompareType,
nsTArray<nsCString>& aOutList) {
@ -771,16 +801,20 @@ void EditorSpellCheck::BuildDictionaryList(const nsACString& aDictName,
break;
}
if (equals) {
aOutList.AppendElement(dictStr);
// Avoid adding duplicates to aOutList.
if (aOutList.IndexOf(dictStr) == nsTArray<nsCString>::NoIndex) {
aOutList.AppendElement(dictStr);
}
#ifdef DEBUG_DICT
printf("***** Trying |%s|.\n", dictStr.get());
#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.
return;
return true;
}
}
return false;
}
nsresult EditorSpellCheck::DictionaryFetched(DictionaryFetcher* aFetcher) {
@ -812,6 +846,9 @@ nsresult EditorSpellCheck::DictionaryFetched(DictionaryFetcher* aFetcher) {
* 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.)
* If the site has multiple languages declared in its Content-Language
* header and there is no more specific lang tag in HTML, we try to
* enable a dictionary for every content language.
* 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
@ -824,19 +861,24 @@ nsresult EditorSpellCheck::DictionaryFetched(DictionaryFetcher* aFetcher) {
// Get the language from the element or its closest parent according to:
// https://html.spec.whatwg.org/#attr-lang
// This is used in SetCurrentDictionary.
CopyUTF16toUTF8(aFetcher->mRootContentLang, mPreferredLang);
// This is used in SetCurrentDictionaries.
nsCString contentLangs;
// Reset mPreferredLangs so we only get the current state.
mPreferredLangs.Clear();
CopyUTF16toUTF8(aFetcher->mRootContentLang, contentLangs);
#ifdef DEBUG_DICT
printf("***** mPreferredLang (element) |%s|\n", mPreferredLang.get());
printf("***** mPreferredLangs (element) |%s|\n", contentLangs.get());
#endif
// If no luck, try the "Content-Language" header.
if (mPreferredLang.IsEmpty()) {
CopyUTF16toUTF8(aFetcher->mRootDocContentLang, mPreferredLang);
if (!contentLangs.IsEmpty()) {
mPreferredLangs.AppendElement(contentLangs);
} else {
// If no luck, try the "Content-Language" header.
CopyUTF16toUTF8(aFetcher->mRootDocContentLang, contentLangs);
#ifdef DEBUG_DICT
printf("***** mPreferredLang (content-language) |%s|\n",
mPreferredLang.get());
printf("***** mPreferredLangs (content-language) |%s|\n",
contentLangs.get());
#endif
StringToDictionaries(contentLangs, mPreferredLangs);
}
// We obtain a list of available dictionaries.
@ -921,12 +963,8 @@ void EditorSpellCheck::SetFallbackDictionary(DictionaryFetcher* aFetcher) {
}
// Priority 2:
// After checking the content preferences, we use the language of the element
// After checking the content preferences, we use the languages of the element
// or document.
nsAutoCString dictName(mPreferredLang);
#ifdef DEBUG_DICT
printf("***** Assigned from element/doc |%s|\n", dictName.get());
#endif
// Get the preference value.
nsAutoCString prefDictionariesAsString;
@ -936,14 +974,17 @@ void EditorSpellCheck::SetFallbackDictionary(DictionaryFetcher* aFetcher) {
StringToDictionaries(prefDictionariesAsString, prefDictionaries);
nsAutoCString appLocaleStr;
if (!dictName.IsEmpty()) {
// We pick one dictionary for every language that the element or document
// indicates it contains.
for (const auto& dictName : mPreferredLangs) {
// RFC 5646 explicitly states that matches should be case-insensitive.
BuildDictionaryList(dictName, dictList, DICT_COMPARE_CASE_INSENSITIVE,
tryDictList);
if (BuildDictionaryList(dictName, dictList, DICT_COMPARE_CASE_INSENSITIVE,
tryDictList)) {
#ifdef DEBUG_DICT
printf("***** Trying from element/doc |%s| \n", dictName.get());
printf("***** Trying from element/doc |%s| \n", dictName.get());
#endif
continue;
}
// Required dictionary was not available. Try to get a dictionary
// matching at least language part of dictName.
@ -955,6 +996,7 @@ void EditorSpellCheck::SetFallbackDictionary(DictionaryFetcher* aFetcher) {
// Try dictionary.spellchecker preference, if it starts with langCode,
// so we don't just get any random dictionary matching the language.
bool didAppend = false;
for (const auto& dictionary : prefDictionaries) {
if (nsStyleUtil::DashMatchCompare(NS_ConvertUTF8toUTF16(dictionary),
NS_ConvertUTF8toUTF16(langCode),
@ -965,39 +1007,46 @@ void EditorSpellCheck::SetFallbackDictionary(DictionaryFetcher* aFetcher) {
"code\n",
dictionary.Data());
#endif
BuildDictionaryList(dictionary, dictList,
DICT_COMPARE_CASE_INSENSITIVE, tryDictList);
break;
if (BuildDictionaryList(dictionary, dictList,
DICT_COMPARE_CASE_INSENSITIVE, tryDictList)) {
didAppend = true;
break;
}
}
}
if (didAppend) {
continue;
}
// Use the application locale dictionary when the required language
// equals applocation locale language.
LocaleService::GetInstance()->GetAppLocaleAsBCP47(appLocaleStr);
if (!appLocaleStr.IsEmpty()) {
mozilla::intl::Locale appLoc;
auto result =
mozilla::intl::LocaleParser::TryParse(appLocaleStr, appLoc);
if (result.isOk() && loc.Canonicalize().isOk() &&
loc.Language().Span() == appLoc.Language().Span()) {
if (BuildDictionaryList(appLocaleStr, dictList,
DICT_COMPARE_CASE_INSENSITIVE, tryDictList)) {
continue;
}
}
}
if (tryDictList.IsEmpty()) {
// Use the application locale dictionary when the required language
// equals applocation locale language.
LocaleService::GetInstance()->GetAppLocaleAsBCP47(appLocaleStr);
if (!appLocaleStr.IsEmpty()) {
mozilla::intl::Locale appLoc;
auto result =
mozilla::intl::LocaleParser::TryParse(appLocaleStr, appLoc);
if (result.isOk() && loc.Canonicalize().isOk() &&
loc.Language().Span() == appLoc.Language().Span()) {
BuildDictionaryList(appLocaleStr, dictList,
DICT_COMPARE_CASE_INSENSITIVE, tryDictList);
}
}
// Use the system locale dictionary when the required language equlas
// system locale language.
nsAutoCString sysLocaleStr;
OSPreferences::GetInstance()->GetSystemLocale(sysLocaleStr);
if (!sysLocaleStr.IsEmpty()) {
mozilla::intl::Locale sysLoc;
auto result =
mozilla::intl::LocaleParser::TryParse(sysLocaleStr, sysLoc);
if (result.isOk() && loc.Canonicalize().isOk() &&
loc.Language().Span() == sysLoc.Language().Span()) {
BuildDictionaryList(sysLocaleStr, dictList,
DICT_COMPARE_CASE_INSENSITIVE, tryDictList);
// Use the system locale dictionary when the required language equlas
// system locale language.
nsAutoCString sysLocaleStr;
OSPreferences::GetInstance()->GetSystemLocale(sysLocaleStr);
if (!sysLocaleStr.IsEmpty()) {
mozilla::intl::Locale sysLoc;
auto result =
mozilla::intl::LocaleParser::TryParse(sysLocaleStr, sysLoc);
if (result.isOk() && loc.Canonicalize().isOk() &&
loc.Language().Span() == sysLoc.Language().Span()) {
if (BuildDictionaryList(sysLocaleStr, dictList,
DICT_COMPARE_CASE_INSENSITIVE, tryDictList)) {
continue;
}
}
}
@ -1014,113 +1063,116 @@ void EditorSpellCheck::SetFallbackDictionary(DictionaryFetcher* aFetcher) {
RefPtr<EditorSpellCheck> self = this;
RefPtr<DictionaryFetcher> fetcher = aFetcher;
RefPtr<GenericPromise> promise;
if (tryDictList.IsEmpty()) {
// Proceed to priority 3 if the list of dictionaries is empty.
promise = GenericPromise::CreateAndReject(NS_ERROR_INVALID_ARG, __func__);
} else {
promise = mSpellChecker->SetCurrentDictionaries(tryDictList);
}
// 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.
mSpellChecker->SetCurrentDictionaryFromList(tryDictList)
->Then(
GetMainThreadSerialEventTarget(), __func__,
[self, fetcher]() { self->SetDictionarySucceeded(fetcher); },
[prefDictionaries = prefDictionaries.Clone(),
dictList = dictList.Clone(), self, fetcher]() {
// Build tryDictList with dictionaries for priorities 4 through 7.
// We'll use this list if there is no user preference or trying
// the user preference fails.
AutoTArray<nsCString, 6> tryDictList;
promise->Then(
GetMainThreadSerialEventTarget(), __func__,
[self, fetcher]() { self->SetDictionarySucceeded(fetcher); },
[prefDictionaries = prefDictionaries.Clone(), dictList = dictList.Clone(),
self, fetcher]() {
// Build tryDictList with dictionaries for priorities 4 through 7.
// We'll use this list if there is no user preference or trying
// the user preference fails.
AutoTArray<nsCString, 6> tryDictList;
// Priority 4:
// As next fallback, try the current locale.
nsAutoCString appLocaleStr;
LocaleService::GetInstance()->GetAppLocaleAsBCP47(appLocaleStr);
// Priority 4:
// As next fallback, try the current locale.
nsAutoCString appLocaleStr;
LocaleService::GetInstance()->GetAppLocaleAsBCP47(appLocaleStr);
#ifdef DEBUG_DICT
printf("***** Trying locale |%s|\n", appLocaleStr.get());
printf("***** Trying locale |%s|\n", appLocaleStr.get());
#endif
self->BuildDictionaryList(appLocaleStr, dictList,
DICT_COMPARE_CASE_INSENSITIVE,
tryDictList);
self->BuildDictionaryList(appLocaleStr, dictList,
DICT_COMPARE_CASE_INSENSITIVE, tryDictList);
// Priority 5:
// If we have a current dictionary and we don't have no item in try
// list, don't try anything else.
nsTArray<nsCString> currentDictionaries;
self->GetCurrentDictionaries(currentDictionaries);
if (!currentDictionaries.IsEmpty() && tryDictList.IsEmpty()) {
// Priority 5:
// If we have a current dictionary and we don't have no item in try
// list, don't try anything else.
nsTArray<nsCString> currentDictionaries;
self->GetCurrentDictionaries(currentDictionaries);
if (!currentDictionaries.IsEmpty() && tryDictList.IsEmpty()) {
#ifdef DEBUG_DICT
printf("***** Retrieved current dict |%s|\n",
DictionariesToString(currentDictionaries).Data());
printf("***** Retrieved current dict |%s|\n",
DictionariesToString(currentDictionaries).Data());
#endif
self->EndUpdateDictionary();
if (fetcher->mCallback) {
fetcher->mCallback->EditorSpellCheckDone();
}
return;
}
self->EndUpdateDictionary();
if (fetcher->mCallback) {
fetcher->mCallback->EditorSpellCheckDone();
}
return;
}
// Priority 6:
// Try to get current dictionary from environment variable LANG.
// LANG = language[_territory][.charset]
char* env_lang = getenv("LANG");
if (env_lang) {
nsAutoCString lang(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);
}
// Priority 6:
// Try to get current dictionary from environment variable LANG.
// LANG = language[_territory][.charset]
char* env_lang = getenv("LANG");
if (env_lang) {
nsAutoCString lang(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);
}
int32_t underScore = lang.FindChar('_');
if (underScore != -1) {
lang.Replace(underScore, 1, '-');
int32_t underScore = lang.FindChar('_');
if (underScore != -1) {
lang.Replace(underScore, 1, '-');
#ifdef DEBUG_DICT
printf("***** Trying LANG from environment |%s|\n", lang.get());
printf("***** Trying LANG from environment |%s|\n", lang.get());
#endif
self->BuildDictionaryList(
lang, dictList, DICT_COMPARE_CASE_INSENSITIVE, tryDictList);
}
}
self->BuildDictionaryList(
lang, dictList, DICT_COMPARE_CASE_INSENSITIVE, tryDictList);
}
}
// Priority 7:
// If it does not work, pick the first one.
if (!dictList.IsEmpty()) {
self->BuildDictionaryList(dictList[0], dictList,
DICT_NORMAL_COMPARE, tryDictList);
// Priority 7:
// If it does not work, pick the first one.
if (!dictList.IsEmpty()) {
self->BuildDictionaryList(dictList[0], dictList, DICT_NORMAL_COMPARE,
tryDictList);
#ifdef DEBUG_DICT
printf("***** Trying first of list |%s|\n", dictList[0].get());
printf("***** Trying first of list |%s|\n", dictList[0].get());
#endif
}
}
// Priority 3:
// If the document didn't supply a dictionary or the setting
// failed, try the user preference next.
if (!prefDictionaries.IsEmpty()) {
self->mSpellChecker->SetCurrentDictionaries(prefDictionaries)
->Then(
GetMainThreadSerialEventTarget(), __func__,
[self, fetcher]() {
self->SetDictionarySucceeded(fetcher);
},
// Priority 3 failed, we'll use the list we built of
// priorities 4 to 7.
[tryDictList = tryDictList.Clone(), self, fetcher]() {
self->mSpellChecker
->SetCurrentDictionaryFromList(tryDictList)
->Then(GetMainThreadSerialEventTarget(), __func__,
[self, fetcher]() {
self->SetDictionarySucceeded(fetcher);
});
});
} else {
// We don't have a user preference, so we'll try the list we
// built of priorities 4 to 7.
self->mSpellChecker->SetCurrentDictionaryFromList(tryDictList)
->Then(GetMainThreadSerialEventTarget(), __func__,
[self, fetcher]() {
self->SetDictionarySucceeded(fetcher);
});
}
});
// Priority 3:
// If the document didn't supply a dictionary or the setting
// failed, try the user preference next.
if (!prefDictionaries.IsEmpty()) {
self->mSpellChecker->SetCurrentDictionaries(prefDictionaries)
->Then(
GetMainThreadSerialEventTarget(), __func__,
[self, fetcher]() { self->SetDictionarySucceeded(fetcher); },
// Priority 3 failed, we'll use the list we built of
// priorities 4 to 7.
[tryDictList = tryDictList.Clone(), self, fetcher]() {
self->mSpellChecker
->SetCurrentDictionaryFromList(tryDictList)
->Then(GetMainThreadSerialEventTarget(), __func__,
[self, fetcher]() {
self->SetDictionarySucceeded(fetcher);
});
});
} else {
// We don't have a user preference, so we'll try the list we
// built of priorities 4 to 7.
self->mSpellChecker->SetCurrentDictionaryFromList(tryDictList)
->Then(
GetMainThreadSerialEventTarget(), __func__,
[self, fetcher]() { self->SetDictionarySucceeded(fetcher); });
}
});
}
} // namespace mozilla

View File

@ -68,7 +68,7 @@ class EditorSpellCheck final : public nsIEditorSpellCheck {
// GetPersonalDictionary must be called to load them.
nsTArray<nsString> mDictionaryList;
nsCString mPreferredLang;
nsTArray<nsCString> mPreferredLangs;
uint32_t mTxtSrvFilterType;
int32_t mSuggestedWordIndex;
@ -79,7 +79,7 @@ class EditorSpellCheck final : public nsIEditorSpellCheck {
nsresult DeleteSuggestedWordList();
void BuildDictionaryList(const nsACString& aDictName,
bool BuildDictionaryList(const nsACString& aDictName,
const nsTArray<nsCString>& aDictList,
enum dictCompare aCompareType,
nsTArray<nsCString>& aOutList);

View File

@ -10,6 +10,7 @@ support-files =
bug1200533_subframe.html
bug1204147_subframe.html
bug1204147_subframe2.html
multiple_content_languages_subframe.html
en-GB/en_GB.dic
en-GB/en_GB.aff
en-AU/en_AU.dic
@ -42,6 +43,7 @@ skip-if = e10s
[test_bug1497480.html]
[test_bug1602526.html]
[test_bug1761273.html]
[test_multiple_content_languages.html]
[test_spellcheck_after_edit.html]
[test_spellcheck_after_pressing_navigation_key.html]
[test_suggest.html]

View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Language" content="en-US, en-GB, ko, en-CA">
</head>
<body>
<textarea id="none">root en-US and en-GB</textarea>
<textarea id="en-GB" lang="en-GB">root multiple, but element only en-GB</textarea>
<textarea id="en-ZA-not-avail" lang="en-ZA">root multiple en, but element en-ZA (which is not installed)</textarea>
<textarea id="en" lang="en">root multiple en, but element en</textarea>
<textarea id="ko-not-avail" lang="ko">root multiple en, but element ko (which is not installed)</textarea>
</body>
</html>

View File

@ -124,8 +124,9 @@ async function handlePopup() {
// Check that the English dictionary is loaded and that the spell check has worked.
is(currentDictionaries.length, 2, "expected two dictionaries");
is(currentDictionaries[0], "de-DE", "expected de-DE");
is(currentDictionaries[1], "en-US", "expected en-US");
let dictionaryArray = Array.from(currentDictionaries);
ok(dictionaryArray.includes("de-DE"), "expected de-DE");
ok(dictionaryArray.includes("en-US"), "expected en-US");
is(getMisspelledWords(editor_de), "", "No misspelled words expected");
// Remove the fake de_DE dictionary again.

View File

@ -0,0 +1,176 @@
<!DOCTYPE html>
<html>
<head>
<title>Test for multiple Content-Language values</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" href="/tests/SimpleTest/test.css">
</head>
<body>
<p id="display"></p>
<iframe id="content"></iframe>
<pre id="test">
<script class="testbody">
/** Test for multiple Content-Language values **/
/** Visit the elements defined above and check the dictionaries we got **/
SimpleTest.waitForExplicitFinish();
var content = document.getElementById("content");
var tests = [
// text area, value of spellchecker.dictionary, result.
// Result: Document language.
[ "none", "", ["en-US", "en-GB"] ],
// Result: Element language.
[ "en-GB", "", ["en-GB"] ],
// Result: Random en-* or en-US (if application locale is en-US).
[ "en-ZA-not-avail", "", ["*"] ],
[ "en", "", ["*"] ],
// Result: Locale.
[ "ko-not-avail", "", ["en-US"] ],
// Result: Document language, plus preference value in all cases.
[ "none", "en-AU", ["en-US", "en-GB", "en-AU"] ],
[ "en-ZA-not-avail", "en-AU", ["en-AU"] ],
[ "ko-not-avail", "en-AU", ["en-AU"] ],
// Result: Document language, plus first matching preference language.
[ "none", "en-AU,en-US", ["en-US", "en-GB", "en-AU"] ],
// Result: First matching preference language.
[ "en-ZA-not-avail", "en-AU,en-US", ["en-AU"] ],
// Result: Fall back to preference languages.
[ "ko-not-avail", "en-AU,en-US", ["en-AU", "en-US"] ],
// Result: Random en-*.
[ "en-ZA-not-avail", "de-DE", ["*"] ],
// Result: Preference value.
[ "ko-not-avail", "de-DE", ["de-DE"] ],
];
var loadCount = 0;
var retrying = false;
var script;
var loadListener = async function(evt) {
if (loadCount == 0) {
/* eslint-env mozilla/frame-script */
script = SpecialPowers.loadChromeScript(function() {
// eslint-disable-next-line mozilla/use-services
var dir = Cc["@mozilla.org/file/directory_service;1"]
.getService(Ci.nsIProperties)
.get("CurWorkD", Ci.nsIFile);
dir.append("tests");
dir.append("editor");
dir.append("spellchecker");
dir.append("tests");
var hunspell = Cc["@mozilla.org/spellchecker/engine;1"]
.getService(Ci.mozISpellCheckingEngine);
// Install en-GB, en-AU and de-DE dictionaries.
var en_GB = dir.clone();
var en_AU = dir.clone();
var de_DE = dir.clone();
en_GB.append("en-GB");
en_AU.append("en-AU");
de_DE.append("de-DE");
hunspell.addDirectory(en_GB);
hunspell.addDirectory(en_AU);
hunspell.addDirectory(de_DE);
addMessageListener("check-existence",
() => [en_GB.exists(), en_AU.exists(),
de_DE.exists()]);
addMessageListener("destroy", () => {
hunspell.removeDirectory(en_GB);
hunspell.removeDirectory(en_AU);
hunspell.removeDirectory(de_DE);
});
});
var existenceChecks = await script.sendQuery("check-existence");
is(existenceChecks[0], true, "true expected (en-GB directory should exist)");
is(existenceChecks[1], true, "true expected (en-AU directory should exist)");
is(existenceChecks[2], true, "true expected (de-DE directory should exist)");
}
SpecialPowers.pushPrefEnv({set: [["spellchecker.dictionary", tests[loadCount][1]]]},
function() { continueTest(evt); });
};
function continueTest(evt) {
var doc = evt.target.contentDocument;
var elem = doc.getElementById(tests[loadCount][0]);
var editor = SpecialPowers.wrap(elem).editor;
editor.setSpellcheckUserOverride(true);
var inlineSpellChecker = editor.getInlineSpellChecker(true);
const is_en_US = SpecialPowers.Services.locale.appLocaleAsBCP47 == "en-US";
const { onSpellCheck } = SpecialPowers.ChromeUtils.import(
"resource://testing-common/AsyncSpellCheckTestHelper.jsm"
);
onSpellCheck(elem, async function() {
var spellchecker = inlineSpellChecker.spellChecker;
let currentDictionaries;
try {
currentDictionaries = spellchecker.getCurrentDictionaries();
} catch (e) {}
if (!currentDictionaries && !retrying) {
// It's possible for an asynchronous font-list update to cause a reflow
// that disrupts the async spell-check and results in not getting a
// current dictionary here; if that happens, we retry the same testcase
// by reloading the iframe without bumping loadCount.
info(`No current dictionary: retrying testcase ${loadCount}`);
retrying = true;
} else {
let expectedDictionaries = tests[loadCount][2];
let dictionaryArray = Array.from(currentDictionaries);
is(
dictionaryArray.length,
expectedDictionaries.length,
"Expected matching dictionary count"
);
if (expectedDictionaries[0] != "*") {
ok(
dictionaryArray.every(dict => expectedDictionaries.includes(dict)),
"active dictionaries should match expectation"
);
} else if (is_en_US && tests[loadCount][0].startsWith("en")) {
// Current application locale is en-US and content lang is en or
// en-unknown, so we should use en-US dictionary as default.
is(
dictionaryArray[0],
"en-US",
"expected en-US that is application locale"
);
} else {
let dict = dictionaryArray[0];
var gotEn = (dict == "en-GB" || dict == "en-AU" || dict == "en-US");
is(gotEn, true, "expected en-AU or en-GB or en-US");
}
loadCount++;
retrying = false;
}
if (loadCount < tests.length) {
// Load the iframe again.
content.src = "http://mochi.test:8888/tests/editor/spellchecker/tests/multiple_content_languages_subframe.html?firstload=false";
} else {
// Remove the fake dictionaries again, since it's otherwise picked up by later tests.
await script.sendQuery("destroy");
SimpleTest.finish();
}
});
}
content.addEventListener("load", loadListener);
content.src = "http://mochi.test:8888/tests/editor/spellchecker/tests/multiple_content_languages_subframe.html?firstload=true";
</script>
</pre>
</body>
</html>