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

Side by Side Diff: Source/core/editing/TextCheckingHelper.cpp

Issue 23819007: Have Node::document() return a reference instead of a pointer (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Rebase on master Created 7 years, 3 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 | Annotate | Revision Log
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 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 { 174 {
175 // Both predicates should have same result, but we check both just for sure. 175 // Both predicates should have same result, but we check both just for sure.
176 // We need to investigate to remove this redundancy. 176 // We need to investigate to remove this redundancy.
177 return isRangeEmpty() || isTextEmpty(); 177 return isRangeEmpty() || isTextEmpty();
178 } 178 }
179 179
180 PassRefPtr<Range> TextCheckingParagraph::offsetAsRange() const 180 PassRefPtr<Range> TextCheckingParagraph::offsetAsRange() const
181 { 181 {
182 ASSERT(m_checkingRange); 182 ASSERT(m_checkingRange);
183 if (!m_offsetAsRange) 183 if (!m_offsetAsRange)
184 m_offsetAsRange = Range::create(paragraphRange()->startContainer()->docu ment(), paragraphRange()->startPosition(), checkingRange()->startPosition()); 184 m_offsetAsRange = Range::create(&paragraphRange()->startContainer()->doc ument(), paragraphRange()->startPosition(), checkingRange()->startPosition());
185 185
186 return m_offsetAsRange; 186 return m_offsetAsRange;
187 } 187 }
188 188
189 const String& TextCheckingParagraph::text() const 189 const String& TextCheckingParagraph::text() const
190 { 190 {
191 ASSERT(m_checkingRange); 191 ASSERT(m_checkingRange);
192 if (m_text.isEmpty()) 192 if (m_text.isEmpty())
193 m_text = plainText(paragraphRange().get()); 193 m_text = plainText(paragraphRange().get());
194 return m_text; 194 return m_text;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 RefPtr<Range> misspellingRange = TextIterator::subrange(m_range. get(), currentChunkOffset + misspellingLocation, misspellingLength); 263 RefPtr<Range> misspellingRange = TextIterator::subrange(m_range. get(), currentChunkOffset + misspellingLocation, misspellingLength);
264 264
265 // Remember first-encountered misspelling and its offset. 265 // Remember first-encountered misspelling and its offset.
266 if (!firstMisspelling) { 266 if (!firstMisspelling) {
267 firstMisspellingOffset = currentChunkOffset + misspellingLoc ation; 267 firstMisspellingOffset = currentChunkOffset + misspellingLoc ation;
268 firstMisspelling = it.substring(misspellingLocation, misspel lingLength); 268 firstMisspelling = it.substring(misspellingLocation, misspel lingLength);
269 firstMisspellingRange = misspellingRange; 269 firstMisspellingRange = misspellingRange;
270 } 270 }
271 271
272 // Store marker for misspelled word. 272 // Store marker for misspelled word.
273 misspellingRange->startContainer()->document()->markers()->addMa rker(misspellingRange.get(), DocumentMarker::Spelling); 273 misspellingRange->startContainer()->document().markers()->addMar ker(misspellingRange.get(), DocumentMarker::Spelling);
274 274
275 // Bail out if we're marking only the first misspelling, and not all instances. 275 // Bail out if we're marking only the first misspelling, and not all instances.
276 if (!markAll) 276 if (!markAll)
277 break; 277 break;
278 } 278 }
279 } 279 }
280 280
281 currentChunkOffset += length; 281 currentChunkOffset += length;
282 it.advance(); 282 it.advance();
283 } 283 }
(...skipping 19 matching lines...) Expand all
303 outGrammarDetail.userDescription = ""; 303 outGrammarDetail.userDescription = "";
304 304
305 // Expand the search range to encompass entire paragraphs, since text checki ng needs that much context. 305 // Expand the search range to encompass entire paragraphs, since text checki ng needs that much context.
306 // Determine the character offset from the start of the paragraph to the sta rt of the original search range, 306 // Determine the character offset from the start of the paragraph to the sta rt of the original search range,
307 // since we will want to ignore results in this area. 307 // since we will want to ignore results in this area.
308 RefPtr<Range> paragraphRange = m_range->cloneRange(IGNORE_EXCEPTION); 308 RefPtr<Range> paragraphRange = m_range->cloneRange(IGNORE_EXCEPTION);
309 setStart(paragraphRange.get(), startOfParagraph(m_range->startPosition())); 309 setStart(paragraphRange.get(), startOfParagraph(m_range->startPosition()));
310 int totalRangeLength = TextIterator::rangeLength(paragraphRange.get()); 310 int totalRangeLength = TextIterator::rangeLength(paragraphRange.get());
311 setEnd(paragraphRange.get(), endOfParagraph(m_range->startPosition())); 311 setEnd(paragraphRange.get(), endOfParagraph(m_range->startPosition()));
312 312
313 RefPtr<Range> offsetAsRange = Range::create(paragraphRange->startContainer() ->document(), paragraphRange->startPosition(), m_range->startPosition()); 313 RefPtr<Range> offsetAsRange = Range::create(&paragraphRange->startContainer( )->document(), paragraphRange->startPosition(), m_range->startPosition());
314 int rangeStartOffset = TextIterator::rangeLength(offsetAsRange.get()); 314 int rangeStartOffset = TextIterator::rangeLength(offsetAsRange.get());
315 int totalLengthProcessed = 0; 315 int totalLengthProcessed = 0;
316 316
317 bool firstIteration = true; 317 bool firstIteration = true;
318 bool lastIteration = false; 318 bool lastIteration = false;
319 while (totalLengthProcessed < totalRangeLength) { 319 while (totalLengthProcessed < totalRangeLength) {
320 // Iterate through the search range by paragraphs, checking each one for spelling and grammar. 320 // Iterate through the search range by paragraphs, checking each one for spelling and grammar.
321 int currentLength = TextIterator::rangeLength(paragraphRange.get()); 321 int currentLength = TextIterator::rangeLength(paragraphRange.get());
322 int currentStartOffset = firstIteration ? rangeStartOffset : 0; 322 int currentStartOffset = firstIteration ? rangeStartOffset : 0;
323 int currentEndOffset = currentLength; 323 int currentEndOffset = currentLength;
324 if (inSameParagraph(paragraphRange->startPosition(), m_range->endPositio n())) { 324 if (inSameParagraph(paragraphRange->startPosition(), m_range->endPositio n())) {
325 // Determine the character offset from the end of the original searc h range to the end of the paragraph, 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. 326 // since we will want to ignore results in this area.
327 RefPtr<Range> endOffsetAsRange = Range::create(paragraphRange->start Container()->document(), paragraphRange->startPosition(), m_range->endPosition() ); 327 RefPtr<Range> endOffsetAsRange = Range::create(&paragraphRange->star tContainer()->document(), paragraphRange->startPosition(), m_range->endPosition( ));
328 currentEndOffset = TextIterator::rangeLength(endOffsetAsRange.get()) ; 328 currentEndOffset = TextIterator::rangeLength(endOffsetAsRange.get()) ;
329 lastIteration = true; 329 lastIteration = true;
330 } 330 }
331 if (currentStartOffset < currentEndOffset) { 331 if (currentStartOffset < currentEndOffset) {
332 String paragraphString = plainText(paragraphRange.get()); 332 String paragraphString = plainText(paragraphRange.get());
333 if (paragraphString.length() > 0) { 333 if (paragraphString.length() > 0) {
334 bool foundGrammar = false; 334 bool foundGrammar = false;
335 int spellingLocation = 0; 335 int spellingLocation = 0;
336 int grammarPhraseLocation = 0; 336 int grammarPhraseLocation = 0;
337 int grammarDetailLocation = 0; 337 int grammarDetailLocation = 0;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 outGrammarDetail = result->details[grammarDetailInde x]; 370 outGrammarDetail = result->details[grammarDetailInde x];
371 badGrammarPhrase = paragraphString.substring(result- >location, result->length); 371 badGrammarPhrase = paragraphString.substring(result- >location, result->length);
372 ASSERT(badGrammarPhrase.length()); 372 ASSERT(badGrammarPhrase.length());
373 } 373 }
374 } 374 }
375 } 375 }
376 376
377 if (!misspelledWord.isEmpty() && (!checkGrammar || badGrammarPhr ase.isEmpty() || spellingLocation <= grammarDetailLocation)) { 377 if (!misspelledWord.isEmpty() && (!checkGrammar || badGrammarPhr ase.isEmpty() || spellingLocation <= grammarDetailLocation)) {
378 int spellingOffset = spellingLocation - currentStartOffset; 378 int spellingOffset = spellingLocation - currentStartOffset;
379 if (!firstIteration) { 379 if (!firstIteration) {
380 RefPtr<Range> paragraphOffsetAsRange = Range::create(par agraphRange->startContainer()->document(), m_range->startPosition(), paragraphRa nge->startPosition()); 380 RefPtr<Range> paragraphOffsetAsRange = Range::create(&pa ragraphRange->startContainer()->document(), m_range->startPosition(), paragraphR ange->startPosition());
381 spellingOffset += TextIterator::rangeLength(paragraphOff setAsRange.get()); 381 spellingOffset += TextIterator::rangeLength(paragraphOff setAsRange.get());
382 } 382 }
383 outIsSpelling = true; 383 outIsSpelling = true;
384 outFirstFoundOffset = spellingOffset; 384 outFirstFoundOffset = spellingOffset;
385 firstFoundItem = misspelledWord; 385 firstFoundItem = misspelledWord;
386 break; 386 break;
387 } 387 }
388 if (checkGrammar && !badGrammarPhrase.isEmpty()) { 388 if (checkGrammar && !badGrammarPhrase.isEmpty()) {
389 int grammarPhraseOffset = grammarPhraseLocation - currentSta rtOffset; 389 int grammarPhraseOffset = grammarPhraseLocation - currentSta rtOffset;
390 if (!firstIteration) { 390 if (!firstIteration) {
391 RefPtr<Range> paragraphOffsetAsRange = Range::create(par agraphRange->startContainer()->document(), m_range->startPosition(), paragraphRa nge->startPosition()); 391 RefPtr<Range> paragraphOffsetAsRange = Range::create(&pa ragraphRange->startContainer()->document(), m_range->startPosition(), paragraphR ange->startPosition());
392 grammarPhraseOffset += TextIterator::rangeLength(paragra phOffsetAsRange.get()); 392 grammarPhraseOffset += TextIterator::rangeLength(paragra phOffsetAsRange.get());
393 } 393 }
394 outIsSpelling = false; 394 outIsSpelling = false;
395 outFirstFoundOffset = grammarPhraseOffset; 395 outFirstFoundOffset = grammarPhraseOffset;
396 firstFoundItem = badGrammarPhrase; 396 firstFoundItem = badGrammarPhrase;
397 break; 397 break;
398 } 398 }
399 } 399 }
400 } 400 }
401 if (lastIteration || totalLengthProcessed + currentLength >= totalRangeL ength) 401 if (lastIteration || totalLengthProcessed + currentLength >= totalRangeL ength)
(...skipping 22 matching lines...) Expand all
424 // Skip this detail if it starts before the original search range 424 // Skip this detail if it starts before the original search range
425 if (detailStartOffsetInParagraph < startOffset) 425 if (detailStartOffsetInParagraph < startOffset)
426 continue; 426 continue;
427 427
428 // Skip this detail if it starts after the original search range 428 // Skip this detail if it starts after the original search range
429 if (detailStartOffsetInParagraph >= endOffset) 429 if (detailStartOffsetInParagraph >= endOffset)
430 continue; 430 continue;
431 431
432 if (markAll) { 432 if (markAll) {
433 RefPtr<Range> badGrammarRange = TextIterator::subrange(m_range.get() , badGrammarPhraseLocation - startOffset + detail->location, detail->length); 433 RefPtr<Range> badGrammarRange = TextIterator::subrange(m_range.get() , badGrammarPhraseLocation - startOffset + detail->location, detail->length);
434 badGrammarRange->startContainer()->document()->markers()->addMarker( badGrammarRange.get(), DocumentMarker::Grammar, detail->userDescription); 434 badGrammarRange->startContainer()->document().markers()->addMarker(b adGrammarRange.get(), DocumentMarker::Grammar, detail->userDescription);
435 } 435 }
436 436
437 // Remember this detail only if it's earlier than our current candidate (the details aren't in a guaranteed order) 437 // Remember this detail only if it's earlier than our current candidate (the details aren't in a guaranteed order)
438 if (earliestDetailIndex < 0 || earliestDetailLocationSoFar > detail->loc ation) { 438 if (earliestDetailIndex < 0 || earliestDetailLocationSoFar > detail->loc ation) {
439 earliestDetailIndex = i; 439 earliestDetailIndex = i;
440 earliestDetailLocationSoFar = detail->location; 440 earliestDetailLocationSoFar = detail->location;
441 } 441 }
442 } 442 }
443 443
444 return earliestDetailIndex; 444 return earliestDetailIndex;
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
571 return false; 571 return false;
572 572
573 const Settings* settings = frame->settings(); 573 const Settings* settings = frame->settings();
574 if (!settings) 574 if (!settings)
575 return false; 575 return false;
576 576
577 return settings->unifiedTextCheckerEnabled(); 577 return settings->unifiedTextCheckerEnabled();
578 } 578 }
579 579
580 } 580 }
OLDNEW
« no previous file with comments | « Source/core/editing/SplitTextNodeContainingElementCommand.cpp ('k') | Source/core/editing/TextIterator.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698