Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: third_party/WebKit/Source/core/editing/spellcheck/TextCheckingHelper.cpp

Issue 2246053003: Merge TextCheckingHelper into SpellChecker Part 1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@SpellCheckerDebugInfo
Patch Set: Add TODOs Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/core/editing/spellcheck/TextCheckingHelper.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 15 matching lines...) Expand all
26 26
27 #include "core/editing/spellcheck/TextCheckingHelper.h" 27 #include "core/editing/spellcheck/TextCheckingHelper.h"
28 28
29 #include "core/dom/Document.h" 29 #include "core/dom/Document.h"
30 #include "core/dom/Range.h" 30 #include "core/dom/Range.h"
31 #include "core/editing/VisiblePosition.h" 31 #include "core/editing/VisiblePosition.h"
32 #include "core/editing/VisibleUnits.h" 32 #include "core/editing/VisibleUnits.h"
33 #include "core/editing/iterators/CharacterIterator.h" 33 #include "core/editing/iterators/CharacterIterator.h"
34 #include "core/editing/iterators/WordAwareIterator.h" 34 #include "core/editing/iterators/WordAwareIterator.h"
35 #include "core/editing/markers/DocumentMarkerController.h" 35 #include "core/editing/markers/DocumentMarkerController.h"
36 #include "core/editing/spellcheck/SpellChecker.h"
36 #include "core/frame/LocalFrame.h" 37 #include "core/frame/LocalFrame.h"
37 #include "core/frame/Settings.h" 38 #include "core/frame/Settings.h"
38 #include "core/page/SpellCheckerClient.h" 39 #include "core/page/SpellCheckerClient.h"
39 #include "platform/text/TextBreakIterator.h" 40 #include "platform/text/TextBreakIterator.h"
40 #include "platform/text/TextCheckerClient.h" 41 #include "platform/text/TextCheckerClient.h"
41 42
42 namespace blink { 43 namespace blink {
43 44
44 static void findMisspellings(TextCheckerClient& client, const String& text, Vect or<TextCheckingResult>& results) 45 // TODO(xiaochengh): Move this function to SpellChecker.cpp
46 void SpellChecker::findMisspellings(const String& text, Vector<TextCheckingResul t>& results)
45 { 47 {
46 Vector<UChar> characters; 48 Vector<UChar> characters;
47 text.appendTo(characters); 49 text.appendTo(characters);
48 unsigned length = text.length(); 50 unsigned length = text.length();
49 51
50 TextBreakIterator* iterator = wordBreakIterator(characters.data(), length); 52 TextBreakIterator* iterator = wordBreakIterator(characters.data(), length);
51 if (!iterator) 53 if (!iterator)
52 return; 54 return;
53 55
54 int wordStart = iterator->current(); 56 int wordStart = iterator->current();
55 while (0 <= wordStart) { 57 while (0 <= wordStart) {
56 int wordEnd = iterator->next(); 58 int wordEnd = iterator->next();
57 if (wordEnd < 0) 59 if (wordEnd < 0)
58 break; 60 break;
59 int wordLength = wordEnd - wordStart; 61 int wordLength = wordEnd - wordStart;
60 int misspellingLocation = -1; 62 int misspellingLocation = -1;
61 int misspellingLength = 0; 63 int misspellingLength = 0;
62 client.checkSpellingOfString(String(characters.data() + wordStart, wordL ength), &misspellingLocation, &misspellingLength); 64 textChecker().checkSpellingOfString(String(characters.data() + wordStart , wordLength), &misspellingLocation, &misspellingLength);
63 if (0 < misspellingLength) { 65 if (0 < misspellingLength) {
64 DCHECK_LE(0, misspellingLocation); 66 DCHECK_LE(0, misspellingLocation);
65 DCHECK_LE(misspellingLocation, wordLength); 67 DCHECK_LE(misspellingLocation, wordLength);
66 DCHECK_LT(0, misspellingLength); 68 DCHECK_LT(0, misspellingLength);
67 DCHECK_LE(misspellingLocation + misspellingLength, wordLength); 69 DCHECK_LE(misspellingLocation + misspellingLength, wordLength);
68 TextCheckingResult misspelling; 70 TextCheckingResult misspelling;
69 misspelling.decoration = TextDecorationTypeSpelling; 71 misspelling.decoration = TextDecorationTypeSpelling;
70 misspelling.location = wordStart + misspellingLocation; 72 misspelling.location = wordStart + misspellingLocation;
71 misspelling.length = misspellingLength; 73 misspelling.length = misspellingLength;
72 results.append(misspelling); 74 results.append(misspelling);
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 } 216 }
215 217
216 int TextCheckingParagraph::checkingLength() const 218 int TextCheckingParagraph::checkingLength() const
217 { 219 {
218 DCHECK(m_checkingRange.isNotNull()); 220 DCHECK(m_checkingRange.isNotNull());
219 if (-1 == m_checkingLength) 221 if (-1 == m_checkingLength)
220 m_checkingLength = TextIterator::rangeLength(checkingRange().startPositi on(), checkingRange().endPosition()); 222 m_checkingLength = TextIterator::rangeLength(checkingRange().startPositi on(), checkingRange().endPosition());
221 return m_checkingLength; 223 return m_checkingLength;
222 } 224 }
223 225
224 TextCheckingHelper::TextCheckingHelper(SpellCheckerClient& client, const Positio n& start, const Position& end) 226 // TODO(xiaochengh): Move this function to SpellChecker.cpp
225 : m_client(&client) 227 String SpellChecker::findFirstMisspellingOrBadGrammar(const Position& start, con st Position& end, int& outFirstFoundOffset)
226 , m_start(start)
227 , m_end(end)
228 {
229 }
230
231 TextCheckingHelper::~TextCheckingHelper()
232 {
233 }
234
235 String TextCheckingHelper::findFirstMisspellingOrBadGrammar(int& outFirstFoundOf fset)
236 { 228 {
237 String firstFoundItem; 229 String firstFoundItem;
238 String misspelledWord; 230 String misspelledWord;
239 231
240 // Initialize out parameter; it will be updated if we find something to retu rn. 232 // Initialize out parameter; it will be updated if we find something to retu rn.
241 outFirstFoundOffset = 0; 233 outFirstFoundOffset = 0;
242 234
243 // Expand the search range to encompass entire paragraphs, since text checki ng needs that much context. 235 // Expand the search range to encompass entire paragraphs, since text checki ng needs that much context.
244 // Determine the character offset from the start of the paragraph to the sta rt of the original search range, 236 // Determine the character offset from the start of the paragraph to the sta rt of the original search range,
245 // since we will want to ignore results in this area. 237 // since we will want to ignore results in this area.
246 Position paragraphStart = startOfParagraph(createVisiblePosition(m_start)).t oParentAnchoredPosition(); 238 Position paragraphStart = startOfParagraph(createVisiblePosition(start)).toP arentAnchoredPosition();
247 Position paragraphEnd = m_end; 239 Position paragraphEnd = end;
248 int totalRangeLength = TextIterator::rangeLength(paragraphStart, paragraphEn d); 240 int totalRangeLength = TextIterator::rangeLength(paragraphStart, paragraphEn d);
249 paragraphEnd = endOfParagraph(createVisiblePosition(m_start)).toParentAnchor edPosition(); 241 paragraphEnd = endOfParagraph(createVisiblePosition(start)).toParentAnchored Position();
250 242
251 int rangeStartOffset = TextIterator::rangeLength(paragraphStart, m_start); 243 int rangeStartOffset = TextIterator::rangeLength(paragraphStart, start);
252 int totalLengthProcessed = 0; 244 int totalLengthProcessed = 0;
253 245
254 bool firstIteration = true; 246 bool firstIteration = true;
255 bool lastIteration = false; 247 bool lastIteration = false;
256 while (totalLengthProcessed < totalRangeLength) { 248 while (totalLengthProcessed < totalRangeLength) {
257 // Iterate through the search range by paragraphs, checking each one for spelling. 249 // Iterate through the search range by paragraphs, checking each one for spelling.
258 int currentLength = TextIterator::rangeLength(paragraphStart, paragraphE nd); 250 int currentLength = TextIterator::rangeLength(paragraphStart, paragraphE nd);
259 int currentStartOffset = firstIteration ? rangeStartOffset : 0; 251 int currentStartOffset = firstIteration ? rangeStartOffset : 0;
260 int currentEndOffset = currentLength; 252 int currentEndOffset = currentLength;
261 if (inSameParagraph(createVisiblePosition(paragraphStart), createVisible Position(m_end))) { 253 if (inSameParagraph(createVisiblePosition(paragraphStart), createVisible Position(end))) {
262 // Determine the character offset from the end of the original searc h range to the end of the paragraph, 254 // Determine the character offset from the end of the original searc h range to the end of the paragraph,
263 // since we will want to ignore results in this area. 255 // since we will want to ignore results in this area.
264 currentEndOffset = TextIterator::rangeLength(paragraphStart, m_end); 256 currentEndOffset = TextIterator::rangeLength(paragraphStart, end);
265 lastIteration = true; 257 lastIteration = true;
266 } 258 }
267 if (currentStartOffset < currentEndOffset) { 259 if (currentStartOffset < currentEndOffset) {
268 String paragraphString = plainText(EphemeralRange(paragraphStart, pa ragraphEnd)); 260 String paragraphString = plainText(EphemeralRange(paragraphStart, pa ragraphEnd));
269 if (paragraphString.length() > 0) { 261 if (paragraphString.length() > 0) {
270 int spellingLocation = 0; 262 int spellingLocation = 0;
271 263
272 Vector<TextCheckingResult> results; 264 Vector<TextCheckingResult> results;
273 findMisspellings(m_client->textChecker(), paragraphString, resul ts); 265 findMisspellings(paragraphString, results);
274 266
275 for (unsigned i = 0; i < results.size(); i++) { 267 for (unsigned i = 0; i < results.size(); i++) {
276 const TextCheckingResult* result = &results[i]; 268 const TextCheckingResult* result = &results[i];
277 if (result->decoration == TextDecorationTypeSpelling && resu lt->location >= currentStartOffset && result->location + result->length <= curre ntEndOffset) { 269 if (result->decoration == TextDecorationTypeSpelling && resu lt->location >= currentStartOffset && result->location + result->length <= curre ntEndOffset) {
278 DCHECK_GT(result->length, 0); 270 DCHECK_GT(result->length, 0);
279 DCHECK_GE(result->location, 0); 271 DCHECK_GE(result->location, 0);
280 spellingLocation = result->location; 272 spellingLocation = result->location;
281 misspelledWord = paragraphString.substring(result->locat ion, result->length); 273 misspelledWord = paragraphString.substring(result->locat ion, result->length);
282 DCHECK(misspelledWord.length()); 274 DCHECK(misspelledWord.length());
283 break; 275 break;
284 } 276 }
285 } 277 }
286 278
287 if (!misspelledWord.isEmpty()) { 279 if (!misspelledWord.isEmpty()) {
288 int spellingOffset = spellingLocation - currentStartOffset; 280 int spellingOffset = spellingLocation - currentStartOffset;
289 if (!firstIteration) 281 if (!firstIteration)
290 spellingOffset += TextIterator::rangeLength(m_start, par agraphStart); 282 spellingOffset += TextIterator::rangeLength(start, parag raphStart);
291 outFirstFoundOffset = spellingOffset; 283 outFirstFoundOffset = spellingOffset;
292 firstFoundItem = misspelledWord; 284 firstFoundItem = misspelledWord;
293 break; 285 break;
294 } 286 }
295 } 287 }
296 } 288 }
297 if (lastIteration || totalLengthProcessed + currentLength >= totalRangeL ength) 289 if (lastIteration || totalLengthProcessed + currentLength >= totalRangeL ength)
298 break; 290 break;
299 VisiblePosition newParagraphStart = startOfNextParagraph(createVisiblePo sition(paragraphEnd)); 291 VisiblePosition newParagraphStart = startOfNextParagraph(createVisiblePo sition(paragraphEnd));
300 paragraphStart = newParagraphStart.toParentAnchoredPosition(); 292 paragraphStart = newParagraphStart.toParentAnchoredPosition();
301 paragraphEnd = endOfParagraph(newParagraphStart).toParentAnchoredPositio n(); 293 paragraphEnd = endOfParagraph(newParagraphStart).toParentAnchoredPositio n();
302 firstIteration = false; 294 firstIteration = false;
303 totalLengthProcessed += currentLength; 295 totalLengthProcessed += currentLength;
304 } 296 }
305 return firstFoundItem; 297 return firstFoundItem;
306 } 298 }
307 299
308 } // namespace blink 300 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/editing/spellcheck/TextCheckingHelper.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698