Bug 1773802 - Ignore empty strings when spellchecking; r=smaug

This adds a check to see if the encoded word is not empty and does not start
with the null character. Hunspell accepts C-style strings and marks the empty
string as correctly spelled. This prevents other dictionaries from detecting
misspelled strings in languages that use a different charset, which has lead
to problems when using the en-US dictionary packaged with Firefox and Greek,
Hebrew and Russian dictionaries, where misspellings are not detected in the
non-English language.

Differential Revision: https://phabricator.services.mozilla.com/D149899
This commit is contained in:
Dan Minor 2022-06-23 15:34:20 +00:00
parent 3ab693b367
commit 32b9c84af0
5 changed files with 115 additions and 1 deletions

View File

@ -17,6 +17,8 @@ support-files =
en-AU/en_AU.aff
de-DE/de_DE.dic
de-DE/de_DE.aff
ru-RU/ru_RU.dic
ru-RU/ru_RU.aff
spellcheck.js
[test_async_UpdateCurrentDictionary.html]
@ -43,6 +45,7 @@ skip-if = e10s
[test_bug1497480.html]
[test_bug1602526.html]
[test_bug1761273.html]
[test_bug1773802.html]
[test_multiple_content_languages.html]
[test_spellcheck_after_edit.html]
[test_spellcheck_after_pressing_navigation_key.html]

View File

@ -0,0 +1 @@
SET KOI8-R

View File

@ -0,0 +1,2 @@
1
правильный

View File

@ -0,0 +1,96 @@
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1773802
-->
<head>
<title>Test for Bug 1773802</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" href="/tests/SimpleTest/test.css">
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1773802">Mozilla Bug 1773802</a>
<p id="display"></p>
</div>
<textarea id="editor">correct правильный, incarrect непровильный</textarea>
<pre id="test">
<script class="testbody" type="text/javascript">
const Ci = SpecialPowers.Ci;
let { maybeOnSpellCheck } = SpecialPowers.ChromeUtils.import(
"resource://testing-common/AsyncSpellCheckTestHelper.jsm"
);
function getMisspelledWords(editor) {
return editor.selectionController.getSelection(Ci.nsISelectionController.SELECTION_SPELLCHECK).toString();
}
/** Test for Bug 1773802 **/
SimpleTest.waitForExplicitFinish();
addLoadEvent(start);
async function start() {
/* global actorParent */
/* eslint-env mozilla/frame-script */
let script = SpecialPowers.loadChromeScript(() => {
// eslint-disable-next-line mozilla/use-services
let 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");
let hunspell = Cc["@mozilla.org/spellchecker/engine;1"]
.getService(Ci.mozISpellCheckingEngine);
// Install ru-RU dictionary.
let ru_RU = dir.clone();
ru_RU.append("ru-RU");
hunspell.addDirectory(ru_RU);
addMessageListener("destroy", () => hunspell.removeDirectory(ru_RU));
addMessageListener("ru_RU-exists", () => ru_RU.exists());
});
is(await script.sendQuery("ru_RU-exists"), true,
"true expected (ru_RU directory should exist)");
let textarea = document.getElementById("editor");
let editor = SpecialPowers.wrap(textarea).editor;
textarea.focus();
maybeOnSpellCheck(textarea, () => {
let isc = SpecialPowers.wrap(textarea).editor.getInlineSpellChecker(false);
ok(isc, "Inline spell checker should exist after focus and spell check");
let spellchecker = isc.spellChecker;
spellchecker.setCurrentDictionaries(["en-US", "ru-RU"]).then(async () => {
textarea.blur();
textarea.focus();
maybeOnSpellCheck(textarea, async function() {
let currentDictionaries = spellchecker.getCurrentDictionaries();
is(currentDictionaries.length, 2, "expected two dictionaries");
is(currentDictionaries[0], "en-US", "expected en-US");
is(currentDictionaries[1], "ru-RU", "expected ru-RU");
is(getMisspelledWords(editor), "incarrectнепровильный", "some misspelled words expected: incarrect непровильный");
// Remove the fake ru_RU dictionary again.
await script.sendQuery("destroy");
// This will clear the content preferences and reset "spellchecker.dictionary".
spellchecker.setCurrentDictionaries([]).then(() => {
SimpleTest.finish();
});
});
});
});
}
</script>
</pre>
</body>
</html>

View File

@ -453,6 +453,7 @@ mozHunspell::Check(const nsAString& aWord, bool* aResult) {
return NS_ERROR_FAILURE;
}
*aResult = true;
for (auto iter = mHunspells.Iter(); !iter.Done(); iter.Next()) {
if (!iter.Data().mEnabled) {
continue;
@ -465,7 +466,18 @@ mozHunspell::Check(const nsAString& aWord, bool* aResult) {
std::string charsetWord;
rv = iter.Data().ConvertCharset(aWord, charsetWord);
NS_ENSURE_SUCCESS(rv, rv);
if (NS_FAILED(rv)) {
continue;
}
// Depending upon the encoding, we might end up with a string that begins
// with the null byte. Since the hunspell interface uses C-style strings,
// this appears like an empty string, and hunspell marks empty strings as
// spelled correctly. Skip these cases to allow another dictionary to have
// the chance to spellcheck them.
if (charsetWord.empty() || charsetWord[0] == 0) {
continue;
}
*aResult = iter.Data().mHunspell->spell(charsetWord);
if (*aResult) {