fix: re-enable the spellchecker when new language list set (#26119)

* fix: re-enable the spellchecker when new language list set

Chromium recently added prefs logic to disable the spellchecker if the list of languages is empty, but the logic to re-enable if the languages are provided again lives in another part of Chromium.  This change makes it so our API re-enables the spellchecker correctly when required.

* chore: fix lint
This commit is contained in:
Samuel Attard 2020-10-23 00:34:19 -07:00 committed by GitHub
parent 6181c03df0
commit 9d18f9d54f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 3 deletions

View file

@ -957,6 +957,9 @@ void Session::SetSpellCheckerLanguages(
}
browser_context_->prefs()->Set(spellcheck::prefs::kSpellCheckDictionaries,
language_codes);
// Enable spellcheck if > 0 languages, disable if no languages set
browser_context_->prefs()->SetBoolean(spellcheck::prefs::kSpellCheckEnable,
!languages.empty());
}
void SetSpellCheckerDictionaryDownloadURL(gin_helper::ErrorThrower thrower,

View file

@ -2,9 +2,19 @@ import * as childProcess from 'child_process';
import * as path from 'path';
import * as http from 'http';
import * as v8 from 'v8';
import { SuiteFunction, TestFunction } from 'mocha';
export const ifit = (condition: boolean) => (condition ? it : it.skip);
export const ifdescribe = (condition: boolean) => (condition ? describe : describe.skip);
const addOnly = <T>(fn: Function): T => {
const wrapped = (...args: any[]) => {
return fn(...args);
};
(wrapped as any).only = wrapped;
(wrapped as any).skip = wrapped;
return wrapped as any;
};
export const ifit = (condition: boolean) => (condition ? it : addOnly<TestFunction>(it.skip));
export const ifdescribe = (condition: boolean) => (condition ? describe : addOnly<SuiteFunction>(describe.skip));
export const delay = (time: number = 0) => new Promise(resolve => setTimeout(resolve, time));

View file

@ -15,9 +15,11 @@ ifdescribe(features.isBuiltinSpellCheckerEnabled())('spellchecker', () => {
w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
nodeIntegration: true,
partition: `unique-spell-${Date.now()}`
}
});
w.webContents.session.setSpellCheckerLanguages(['en-US']);
await w.loadFile(path.resolve(__dirname, './fixtures/chromium/spellchecker.html'));
});
@ -65,6 +67,26 @@ ifdescribe(features.isBuiltinSpellCheckerEnabled())('spellchecker', () => {
expect(contextMenuParams.dictionarySuggestions).to.have.length.of.at.least(1);
});
ifit(shouldRun)('should detect incorrectly spelled words as incorrect after disabling all languages and re-enabling', async () => {
w.webContents.session.setSpellCheckerLanguages([]);
await delay(500);
w.webContents.session.setSpellCheckerLanguages(['en-US']);
await w.webContents.executeJavaScript('document.body.querySelector("textarea").value = "Beautifulllll asd asd"');
await w.webContents.executeJavaScript('document.body.querySelector("textarea").focus()');
const contextMenuPromise = emittedOnce(w.webContents, 'context-menu');
// Wait for spellchecker to load
await delay(500);
w.webContents.sendInputEvent({
type: 'mouseDown',
button: 'right',
x: 43,
y: 42
});
const contextMenuParams: Electron.ContextMenuParams = (await contextMenuPromise)[1];
expect(contextMenuParams.misspelledWord).to.eq('Beautifulllll');
expect(contextMenuParams.dictionarySuggestions).to.have.length.of.at.least(1);
});
ifit(shouldRun)('should expose webFrame spellchecker correctly', async () => {
await w.webContents.executeJavaScript('document.body.querySelector("textarea").value = "Beautifulllll asd asd"');
await w.webContents.executeJavaScript('document.body.querySelector("textarea").focus()');