| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved. | 2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies) | 3 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies) |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
| 7 * are met: | 7 * are met: |
| 8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 } | 279 } |
| 280 } | 280 } |
| 281 | 281 |
| 282 currentChunkOffset += length; | 282 currentChunkOffset += length; |
| 283 it.advance(); | 283 it.advance(); |
| 284 } | 284 } |
| 285 | 285 |
| 286 return firstMisspelling; | 286 return firstMisspelling; |
| 287 } | 287 } |
| 288 | 288 |
| 289 String TextCheckingHelper::findFirstMisspellingOrBadGrammar(bool& outIsSpelling,
int& outFirstFoundOffset, GrammarDetail& outGrammarDetail) | |
| 290 { | |
| 291 if (!unifiedTextCheckerEnabled()) | |
| 292 return ""; | |
| 293 | |
| 294 String firstFoundItem; | |
| 295 String misspelledWord; | |
| 296 String badGrammarPhrase; | |
| 297 | |
| 298 // Initialize out parameters; these will be updated if we find something to
return. | |
| 299 outIsSpelling = true; | |
| 300 outFirstFoundOffset = 0; | |
| 301 outGrammarDetail.location = -1; | |
| 302 outGrammarDetail.length = 0; | |
| 303 outGrammarDetail.guesses.clear(); | |
| 304 outGrammarDetail.userDescription = ""; | |
| 305 | |
| 306 // Expand the search range to encompass entire paragraphs, since text checki
ng needs that much context. | |
| 307 // Determine the character offset from the start of the paragraph to the sta
rt of the original search range, | |
| 308 // since we will want to ignore results in this area. | |
| 309 Position paragraphStart = startOfParagraph(createVisiblePosition(m_start)).t
oParentAnchoredPosition(); | |
| 310 Position paragraphEnd = m_end; | |
| 311 int totalRangeLength = TextIterator::rangeLength(paragraphStart, paragraphEn
d); | |
| 312 paragraphEnd = endOfParagraph(createVisiblePosition(m_start)).toParentAnchor
edPosition(); | |
| 313 | |
| 314 int rangeStartOffset = TextIterator::rangeLength(paragraphStart, m_start); | |
| 315 int totalLengthProcessed = 0; | |
| 316 | |
| 317 bool firstIteration = true; | |
| 318 bool lastIteration = false; | |
| 319 while (totalLengthProcessed < totalRangeLength) { | |
| 320 // Iterate through the search range by paragraphs, checking each one for
spelling. | |
| 321 int currentLength = TextIterator::rangeLength(paragraphStart, paragraphE
nd); | |
| 322 int currentStartOffset = firstIteration ? rangeStartOffset : 0; | |
| 323 int currentEndOffset = currentLength; | |
| 324 if (inSameParagraph(createVisiblePosition(paragraphStart), createVisible
Position(m_end))) { | |
| 325 // Determine the character offset from the end of the original searc
h range to the end of the paragraph, | |
| 326 // since we will want to ignore results in this area. | |
| 327 currentEndOffset = TextIterator::rangeLength(paragraphStart, m_end); | |
| 328 lastIteration = true; | |
| 329 } | |
| 330 if (currentStartOffset < currentEndOffset) { | |
| 331 String paragraphString = plainText(EphemeralRange(paragraphStart, pa
ragraphEnd)); | |
| 332 if (paragraphString.length() > 0) { | |
| 333 int spellingLocation = 0; | |
| 334 | |
| 335 Vector<TextCheckingResult> results; | |
| 336 TextCheckingTypeMask checkingTypes = TextCheckingTypeSpelling; | |
| 337 checkTextOfParagraph(m_client->textChecker(), paragraphString, c
heckingTypes, results); | |
| 338 | |
| 339 for (unsigned i = 0; i < results.size(); i++) { | |
| 340 const TextCheckingResult* result = &results[i]; | |
| 341 if (result->decoration == TextDecorationTypeSpelling && resu
lt->location >= currentStartOffset && result->location + result->length <= curre
ntEndOffset) { | |
| 342 DCHECK_GT(result->length, 0); | |
| 343 DCHECK_GE(result->location, 0); | |
| 344 spellingLocation = result->location; | |
| 345 misspelledWord = paragraphString.substring(result->locat
ion, result->length); | |
| 346 DCHECK(misspelledWord.length()); | |
| 347 break; | |
| 348 } | |
| 349 } | |
| 350 | |
| 351 if (!misspelledWord.isEmpty()) { | |
| 352 int spellingOffset = spellingLocation - currentStartOffset; | |
| 353 if (!firstIteration) | |
| 354 spellingOffset += TextIterator::rangeLength(m_start, par
agraphStart); | |
| 355 outIsSpelling = true; | |
| 356 outFirstFoundOffset = spellingOffset; | |
| 357 firstFoundItem = misspelledWord; | |
| 358 break; | |
| 359 } | |
| 360 } | |
| 361 } | |
| 362 if (lastIteration || totalLengthProcessed + currentLength >= totalRangeL
ength) | |
| 363 break; | |
| 364 VisiblePosition newParagraphStart = startOfNextParagraph(createVisiblePo
sition(paragraphEnd)); | |
| 365 paragraphStart = newParagraphStart.toParentAnchoredPosition(); | |
| 366 paragraphEnd = endOfParagraph(newParagraphStart).toParentAnchoredPositio
n(); | |
| 367 firstIteration = false; | |
| 368 totalLengthProcessed += currentLength; | |
| 369 } | |
| 370 return firstFoundItem; | |
| 371 } | |
| 372 | |
| 373 String TextCheckingHelper::findFirstBadGrammar(GrammarDetail& outGrammarDetail,
int& outGrammarPhraseOffset, bool markAll) | |
| 374 { | |
| 375 // Return empty result since there is no grammar checking. | |
| 376 outGrammarDetail.location = -1; | |
| 377 outGrammarDetail.length = 0; | |
| 378 outGrammarDetail.guesses.clear(); | |
| 379 outGrammarDetail.userDescription = ""; | |
| 380 outGrammarPhraseOffset = 0; | |
| 381 return ""; | |
| 382 } | |
| 383 | |
| 384 bool TextCheckingHelper::markAllMisspellings() | 289 bool TextCheckingHelper::markAllMisspellings() |
| 385 { | 290 { |
| 386 // Use the "markAll" feature of findFirstMisspelling. Ignore the return valu
e and the "out parameter"; | 291 // Use the "markAll" feature of findFirstMisspelling. Ignore the return valu
e and the "out parameter"; |
| 387 // all we need to do is mark every instance. | 292 // all we need to do is mark every instance. |
| 388 int ignoredOffset; | 293 int ignoredOffset; |
| 389 return findFirstMisspelling(ignoredOffset, true).isEmpty(); | 294 return findFirstMisspelling(ignoredOffset, true).isEmpty(); |
| 390 } | 295 } |
| 391 | 296 |
| 392 void TextCheckingHelper::markAllBadGrammar() | |
| 393 { | |
| 394 } | |
| 395 | |
| 396 bool TextCheckingHelper::unifiedTextCheckerEnabled() const | 297 bool TextCheckingHelper::unifiedTextCheckerEnabled() const |
| 397 { | 298 { |
| 398 DCHECK(m_start.isNotNull()); | 299 DCHECK(m_start.isNotNull()); |
| 399 Document& doc = m_start.computeContainerNode()->document(); | 300 Document& doc = m_start.computeContainerNode()->document(); |
| 400 return blink::unifiedTextCheckerEnabled(doc.frame()); | 301 return blink::unifiedTextCheckerEnabled(doc.frame()); |
| 401 } | 302 } |
| 402 | 303 |
| 403 void checkTextOfParagraph(TextCheckerClient& client, const String& text, TextChe
ckingTypeMask checkingTypes, Vector<TextCheckingResult>& results) | 304 void checkTextOfParagraph(TextCheckerClient& client, const String& text, TextChe
ckingTypeMask checkingTypes, Vector<TextCheckingResult>& results) |
| 404 { | 305 { |
| 405 Vector<UChar> characters; | 306 Vector<UChar> characters; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 420 return false; | 321 return false; |
| 421 | 322 |
| 422 const Settings* settings = frame->settings(); | 323 const Settings* settings = frame->settings(); |
| 423 if (!settings) | 324 if (!settings) |
| 424 return false; | 325 return false; |
| 425 | 326 |
| 426 return settings->unifiedTextCheckerEnabled(); | 327 return settings->unifiedTextCheckerEnabled(); |
| 427 } | 328 } |
| 428 | 329 |
| 429 } // namespace blink | 330 } // namespace blink |
| OLD | NEW |