1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
|
/*
* Copyright (C) 2010, 2011 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "config.h"
#import "TextChecker.h"
#import "TextCheckerState.h"
#import <WebCore/NotImplemented.h>
#import <wtf/RetainPtr.h>
@interface NSSpellChecker (WebNSSpellCheckerDetails)
- (NSString *)languageForWordRange:(NSRange)range inString:(NSString *)string orthography:(NSOrthography *)orthography;
@end
static NSString* const WebAutomaticSpellingCorrectionEnabled = @"WebAutomaticSpellingCorrectionEnabled";
static NSString* const WebContinuousSpellCheckingEnabled = @"WebContinuousSpellCheckingEnabled";
static NSString* const WebGrammarCheckingEnabled = @"WebGrammarCheckingEnabled";
static NSString* const WebSmartInsertDeleteEnabled = @"WebSmartInsertDeleteEnabled";
static NSString* const WebAutomaticQuoteSubstitutionEnabled = @"WebAutomaticQuoteSubstitutionEnabled";
static NSString* const WebAutomaticDashSubstitutionEnabled = @"WebAutomaticDashSubstitutionEnabled";
static NSString* const WebAutomaticLinkDetectionEnabled = @"WebAutomaticLinkDetectionEnabled";
static NSString* const WebAutomaticTextReplacementEnabled = @"WebAutomaticTextReplacementEnabled";
using namespace WebCore;
namespace WebKit {
TextCheckerState textCheckerState;
static bool shouldAutomaticTextReplacementBeEnabled()
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults objectForKey:WebAutomaticTextReplacementEnabled])
return [NSSpellChecker isAutomaticTextReplacementEnabled];
return [defaults boolForKey:WebAutomaticTextReplacementEnabled];
}
static bool shouldAutomaticSpellingCorrectionBeEnabled()
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults objectForKey:WebAutomaticSpellingCorrectionEnabled])
return [NSSpellChecker isAutomaticTextReplacementEnabled];
return [defaults boolForKey:WebAutomaticSpellingCorrectionEnabled];
}
static bool shouldAutomaticQuoteSubstitutionBeEnabled()
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
if (![defaults objectForKey:WebAutomaticQuoteSubstitutionEnabled])
return [NSSpellChecker isAutomaticQuoteSubstitutionEnabled];
#endif
return [defaults boolForKey:WebAutomaticQuoteSubstitutionEnabled];
}
static bool shouldAutomaticDashSubstitutionBeEnabled()
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
if (![defaults objectForKey:WebAutomaticDashSubstitutionEnabled])
return [NSSpellChecker isAutomaticDashSubstitutionEnabled];
#endif
return [defaults boolForKey:WebAutomaticDashSubstitutionEnabled];
}
static void initializeState()
{
static bool didInitializeState = false;
if (didInitializeState)
return;
textCheckerState.isContinuousSpellCheckingEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:WebContinuousSpellCheckingEnabled] && TextChecker::isContinuousSpellCheckingAllowed();
textCheckerState.isGrammarCheckingEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:WebGrammarCheckingEnabled];
textCheckerState.isAutomaticTextReplacementEnabled = shouldAutomaticTextReplacementBeEnabled();
textCheckerState.isAutomaticSpellingCorrectionEnabled = shouldAutomaticSpellingCorrectionBeEnabled();
textCheckerState.isAutomaticQuoteSubstitutionEnabled = shouldAutomaticQuoteSubstitutionBeEnabled();
textCheckerState.isAutomaticDashSubstitutionEnabled = shouldAutomaticDashSubstitutionBeEnabled();
textCheckerState.isAutomaticLinkDetectionEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:WebAutomaticLinkDetectionEnabled];
didInitializeState = true;
}
const TextCheckerState& TextChecker::state()
{
initializeState();
return textCheckerState;
}
bool TextChecker::isContinuousSpellCheckingAllowed()
{
static bool allowContinuousSpellChecking = true;
static bool readAllowContinuousSpellCheckingDefault = false;
if (!readAllowContinuousSpellCheckingDefault) {
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"NSAllowContinuousSpellChecking"])
allowContinuousSpellChecking = [[NSUserDefaults standardUserDefaults] boolForKey:@"NSAllowContinuousSpellChecking"];
readAllowContinuousSpellCheckingDefault = true;
}
return allowContinuousSpellChecking;
}
void TextChecker::setContinuousSpellCheckingEnabled(bool isContinuousSpellCheckingEnabled)
{
if (state().isContinuousSpellCheckingEnabled == isContinuousSpellCheckingEnabled)
return;
textCheckerState.isContinuousSpellCheckingEnabled = isContinuousSpellCheckingEnabled;
[[NSUserDefaults standardUserDefaults] setBool:isContinuousSpellCheckingEnabled forKey:WebContinuousSpellCheckingEnabled];
// FIXME: preflight the spell checker.
}
void TextChecker::setGrammarCheckingEnabled(bool isGrammarCheckingEnabled)
{
if (state().isGrammarCheckingEnabled == isGrammarCheckingEnabled)
return;
textCheckerState.isGrammarCheckingEnabled = isGrammarCheckingEnabled;
[[NSUserDefaults standardUserDefaults] setBool:isGrammarCheckingEnabled forKey:WebGrammarCheckingEnabled];
[[NSSpellChecker sharedSpellChecker] updatePanels];
// We call preflightSpellChecker() when turning continuous spell checking on, but we don't need to do that here
// because grammar checking only occurs on code paths that already preflight spell checking appropriately.
}
void TextChecker::setAutomaticSpellingCorrectionEnabled(bool isAutomaticSpellingCorrectionEnabled)
{
if (state().isAutomaticSpellingCorrectionEnabled == isAutomaticSpellingCorrectionEnabled)
return;
textCheckerState.isAutomaticSpellingCorrectionEnabled = isAutomaticSpellingCorrectionEnabled;
[[NSUserDefaults standardUserDefaults] setBool:isAutomaticSpellingCorrectionEnabled forKey:WebAutomaticSpellingCorrectionEnabled];
[[NSSpellChecker sharedSpellChecker] updatePanels];
}
void TextChecker::setAutomaticQuoteSubstitutionEnabled(bool isAutomaticQuoteSubstitutionEnabled)
{
if (state().isAutomaticQuoteSubstitutionEnabled == isAutomaticQuoteSubstitutionEnabled)
return;
textCheckerState.isAutomaticQuoteSubstitutionEnabled = isAutomaticQuoteSubstitutionEnabled;
[[NSUserDefaults standardUserDefaults] setBool:isAutomaticQuoteSubstitutionEnabled forKey:WebAutomaticQuoteSubstitutionEnabled];
[[NSSpellChecker sharedSpellChecker] updatePanels];
}
void TextChecker::setAutomaticDashSubstitutionEnabled(bool isAutomaticDashSubstitutionEnabled)
{
if (state().isAutomaticDashSubstitutionEnabled == isAutomaticDashSubstitutionEnabled)
return;
textCheckerState.isAutomaticDashSubstitutionEnabled = isAutomaticDashSubstitutionEnabled;
[[NSUserDefaults standardUserDefaults] setBool:isAutomaticDashSubstitutionEnabled forKey:WebAutomaticDashSubstitutionEnabled];
[[NSSpellChecker sharedSpellChecker] updatePanels];
}
void TextChecker::setAutomaticLinkDetectionEnabled(bool isAutomaticLinkDetectionEnabled)
{
if (state().isAutomaticLinkDetectionEnabled == isAutomaticLinkDetectionEnabled)
return;
textCheckerState.isAutomaticLinkDetectionEnabled = isAutomaticLinkDetectionEnabled;
[[NSUserDefaults standardUserDefaults] setBool:isAutomaticLinkDetectionEnabled forKey:WebAutomaticLinkDetectionEnabled];
[[NSSpellChecker sharedSpellChecker] updatePanels];
}
void TextChecker::setAutomaticTextReplacementEnabled(bool isAutomaticTextReplacementEnabled)
{
if (state().isAutomaticTextReplacementEnabled == isAutomaticTextReplacementEnabled)
return;
textCheckerState.isAutomaticTextReplacementEnabled = isAutomaticTextReplacementEnabled;
[[NSUserDefaults standardUserDefaults] setBool:isAutomaticTextReplacementEnabled forKey:WebAutomaticTextReplacementEnabled];
[[NSSpellChecker sharedSpellChecker] updatePanels];
}
static bool smartInsertDeleteEnabled;
bool TextChecker::isSmartInsertDeleteEnabled()
{
static bool readSmartInsertDeleteEnabledDefault;
if (!readSmartInsertDeleteEnabledDefault) {
smartInsertDeleteEnabled = ![[NSUserDefaults standardUserDefaults] objectForKey:WebSmartInsertDeleteEnabled] || [[NSUserDefaults standardUserDefaults] boolForKey:WebSmartInsertDeleteEnabled];
readSmartInsertDeleteEnabledDefault = true;
}
return smartInsertDeleteEnabled;
}
void TextChecker::setSmartInsertDeleteEnabled(bool flag)
{
if (flag == isSmartInsertDeleteEnabled())
return;
smartInsertDeleteEnabled = flag;
[[NSUserDefaults standardUserDefaults] setBool:flag forKey:WebSmartInsertDeleteEnabled];
}
void TextChecker::didChangeAutomaticTextReplacementEnabled()
{
textCheckerState.isAutomaticTextReplacementEnabled = shouldAutomaticTextReplacementBeEnabled();
[[NSSpellChecker sharedSpellChecker] updatePanels];
}
void TextChecker::didChangeAutomaticSpellingCorrectionEnabled()
{
textCheckerState.isAutomaticSpellingCorrectionEnabled = shouldAutomaticSpellingCorrectionBeEnabled();
[[NSSpellChecker sharedSpellChecker] updatePanels];
}
void TextChecker::didChangeAutomaticQuoteSubstitutionEnabled()
{
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
textCheckerState.isAutomaticQuoteSubstitutionEnabled = shouldAutomaticQuoteSubstitutionBeEnabled();
[[NSSpellChecker sharedSpellChecker] updatePanels];
#endif
}
void TextChecker::didChangeAutomaticDashSubstitutionEnabled()
{
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
textCheckerState.isAutomaticDashSubstitutionEnabled = shouldAutomaticDashSubstitutionBeEnabled();
[[NSSpellChecker sharedSpellChecker] updatePanels];
#endif
}
bool TextChecker::substitutionsPanelIsShowing()
{
return [[[NSSpellChecker sharedSpellChecker] substitutionsPanel] isVisible];
}
void TextChecker::toggleSubstitutionsPanelIsShowing()
{
NSPanel *substitutionsPanel = [[NSSpellChecker sharedSpellChecker] substitutionsPanel];
if ([substitutionsPanel isVisible]) {
[substitutionsPanel orderOut:nil];
return;
}
[substitutionsPanel orderFront:nil];
}
void TextChecker::continuousSpellCheckingEnabledStateChanged(bool enabled)
{
textCheckerState.isContinuousSpellCheckingEnabled = enabled;
}
void TextChecker::grammarCheckingEnabledStateChanged(bool enabled)
{
textCheckerState.isGrammarCheckingEnabled = enabled;
}
int64_t TextChecker::uniqueSpellDocumentTag(WebPageProxy*)
{
return [NSSpellChecker uniqueSpellDocumentTag];
}
void TextChecker::closeSpellDocumentWithTag(int64_t tag)
{
[[NSSpellChecker sharedSpellChecker] closeSpellDocumentWithTag:tag];
}
#if USE(UNIFIED_TEXT_CHECKING)
Vector<TextCheckingResult> TextChecker::checkTextOfParagraph(int64_t spellDocumentTag, const UChar* text, int length, uint64_t checkingTypes)
{
Vector<TextCheckingResult> results;
RetainPtr<NSString> textString = adoptNS([[NSString alloc] initWithCharactersNoCopy:const_cast<UChar*>(text) length:length freeWhenDone:NO]);
NSArray *incomingResults = [[NSSpellChecker sharedSpellChecker] checkString:textString .get()
range:NSMakeRange(0, length)
types:checkingTypes | NSTextCheckingTypeOrthography
options:nil
inSpellDocumentWithTag:spellDocumentTag
orthography:NULL
wordCount:NULL];
for (NSTextCheckingResult *incomingResult in incomingResults) {
NSRange resultRange = [incomingResult range];
NSTextCheckingType resultType = [incomingResult resultType];
ASSERT(resultRange.location != NSNotFound);
ASSERT(resultRange.length > 0);
if (resultType == NSTextCheckingTypeSpelling && (checkingTypes & NSTextCheckingTypeSpelling)) {
TextCheckingResult result;
result.type = TextCheckingTypeSpelling;
result.location = resultRange.location;
result.length = resultRange.length;
results.append(result);
} else if (resultType == NSTextCheckingTypeGrammar && (checkingTypes & NSTextCheckingTypeGrammar)) {
TextCheckingResult result;
NSArray *details = [incomingResult grammarDetails];
result.type = TextCheckingTypeGrammar;
result.location = resultRange.location;
result.length = resultRange.length;
for (NSDictionary *incomingDetail in details) {
ASSERT(incomingDetail);
GrammarDetail detail;
NSValue *detailRangeAsNSValue = [incomingDetail objectForKey:NSGrammarRange];
ASSERT(detailRangeAsNSValue);
NSRange detailNSRange = [detailRangeAsNSValue rangeValue];
ASSERT(detailNSRange.location != NSNotFound);
ASSERT(detailNSRange.length > 0);
detail.location = detailNSRange.location;
detail.length = detailNSRange.length;
detail.userDescription = [incomingDetail objectForKey:NSGrammarUserDescription];
NSArray *guesses = [incomingDetail objectForKey:NSGrammarCorrections];
for (NSString *guess in guesses)
detail.guesses.append(String(guess));
result.details.append(detail);
}
results.append(result);
} else if (resultType == NSTextCheckingTypeLink && (checkingTypes & NSTextCheckingTypeLink)) {
TextCheckingResult result;
result.type = TextCheckingTypeLink;
result.location = resultRange.location;
result.length = resultRange.length;
result.replacement = [[incomingResult URL] absoluteString];
results.append(result);
} else if (resultType == NSTextCheckingTypeQuote && (checkingTypes & NSTextCheckingTypeQuote)) {
TextCheckingResult result;
result.type = TextCheckingTypeQuote;
result.location = resultRange.location;
result.length = resultRange.length;
result.replacement = [incomingResult replacementString];
results.append(result);
} else if (resultType == NSTextCheckingTypeDash && (checkingTypes & NSTextCheckingTypeDash)) {
TextCheckingResult result;
result.type = TextCheckingTypeDash;
result.location = resultRange.location;
result.length = resultRange.length;
result.replacement = [incomingResult replacementString];
results.append(result);
} else if (resultType == NSTextCheckingTypeReplacement && (checkingTypes & NSTextCheckingTypeReplacement)) {
TextCheckingResult result;
result.type = TextCheckingTypeReplacement;
result.location = resultRange.location;
result.length = resultRange.length;
result.replacement = [incomingResult replacementString];
results.append(result);
} else if (resultType == NSTextCheckingTypeCorrection && (checkingTypes & NSTextCheckingTypeCorrection)) {
TextCheckingResult result;
result.type = TextCheckingTypeCorrection;
result.location = resultRange.location;
result.length = resultRange.length;
result.replacement = [incomingResult replacementString];
results.append(result);
}
}
return results;
}
#endif
void TextChecker::checkSpellingOfString(int64_t, const UChar*, uint32_t, int32_t&, int32_t&)
{
// Mac uses checkTextOfParagraph instead.
notImplemented();
}
void TextChecker::checkGrammarOfString(int64_t, const UChar*, uint32_t, Vector<WebCore::GrammarDetail>&, int32_t&, int32_t&)
{
// Mac uses checkTextOfParagraph instead.
notImplemented();
}
bool TextChecker::spellingUIIsShowing()
{
return [[[NSSpellChecker sharedSpellChecker] spellingPanel] isVisible];
}
void TextChecker::toggleSpellingUIIsShowing()
{
NSPanel *spellingPanel = [[NSSpellChecker sharedSpellChecker] spellingPanel];
if ([spellingPanel isVisible])
[spellingPanel orderOut:nil];
else
[spellingPanel orderFront:nil];
}
void TextChecker::updateSpellingUIWithMisspelledWord(int64_t, const String& misspelledWord)
{
[[NSSpellChecker sharedSpellChecker] updateSpellingPanelWithMisspelledWord:misspelledWord];
}
void TextChecker::updateSpellingUIWithGrammarString(int64_t, const String& badGrammarPhrase, const GrammarDetail& grammarDetail)
{
RetainPtr<NSMutableArray> corrections = adoptNS([[NSMutableArray alloc] init]);
for (size_t i = 0; i < grammarDetail.guesses.size(); ++i) {
NSString *guess = grammarDetail.guesses[i];
[corrections.get() addObject:guess];
}
NSRange grammarRange = NSMakeRange(grammarDetail.location, grammarDetail.length);
NSString *grammarUserDescription = grammarDetail.userDescription;
RetainPtr<NSDictionary> grammarDetailDict = adoptNS([[NSDictionary alloc] initWithObjectsAndKeys:[NSValue valueWithRange:grammarRange], NSGrammarRange, grammarUserDescription, NSGrammarUserDescription, corrections.get(), NSGrammarCorrections, nil]);
[[NSSpellChecker sharedSpellChecker] updateSpellingPanelWithGrammarString:badGrammarPhrase detail:grammarDetailDict.get()];
}
void TextChecker::getGuessesForWord(int64_t spellDocumentTag, const String& word, const String& context, Vector<String>& guesses)
{
NSString* language = nil;
NSOrthography* orthography = nil;
NSSpellChecker *checker = [NSSpellChecker sharedSpellChecker];
if (context.length()) {
[checker checkString:context range:NSMakeRange(0, context.length()) types:NSTextCheckingTypeOrthography options:0 inSpellDocumentWithTag:spellDocumentTag orthography:&orthography wordCount:0];
language = [checker languageForWordRange:NSMakeRange(0, context.length()) inString:context orthography:orthography];
}
NSArray* stringsArray = [checker guessesForWordRange:NSMakeRange(0, word.length()) inString:word language:language inSpellDocumentWithTag:spellDocumentTag];
for (NSString *guess in stringsArray)
guesses.append(guess);
}
void TextChecker::learnWord(int64_t, const String& word)
{
[[NSSpellChecker sharedSpellChecker] learnWord:word];
}
void TextChecker::ignoreWord(int64_t spellDocumentTag, const String& word)
{
[[NSSpellChecker sharedSpellChecker] ignoreWord:word inSpellDocumentWithTag:spellDocumentTag];
}
void TextChecker::requestCheckingOfString(PassRefPtr<TextCheckerCompletion>)
{
notImplemented();
}
} // namespace WebKit
|