Refactoring: Editor::markAllMisspellingsAndBadGrammarInRanges should be refactored.
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 7 Dec 2011 05:00:57 +0000 (05:00 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 7 Dec 2011 05:00:57 +0000 (05:00 +0000)
https://bugs.webkit.org/show_bug.cgi?id=73628

Patch by Shinya Kawanaka <shinyak@google.com> on 2011-12-06
Reviewed by Hajime Morita.

Extracted a code for adding markers and replacing misspelled words from WebCore::Editor::markAllMisspellingsAndBadGrammarInRanges.

No new tests. covered by existing tests.

* editing/Editor.cpp:
(WebCore::Editor::markAllMisspellingsAndBadGrammarInRanges):
  Extracted a code for adding markers and replacing missplled words, and moved to markAndReplaceFor.
(WebCore::Editor::markAndReplaceFor):
* editing/Editor.h:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@102218 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/editing/Editor.cpp
Source/WebCore/editing/Editor.h

index 37dec90..d260fcb 100644 (file)
@@ -1,3 +1,20 @@
+2011-12-06  Shinya Kawanaka  <shinyak@google.com>
+
+        Refactoring: Editor::markAllMisspellingsAndBadGrammarInRanges should be refactored.
+        https://bugs.webkit.org/show_bug.cgi?id=73628
+
+        Reviewed by Hajime Morita.
+
+        Extracted a code for adding markers and replacing misspelled words from WebCore::Editor::markAllMisspellingsAndBadGrammarInRanges.
+
+        No new tests. covered by existing tests.
+
+        * editing/Editor.cpp:
+        (WebCore::Editor::markAllMisspellingsAndBadGrammarInRanges):
+          Extracted a code for adding markers and replacing missplled words, and moved to markAndReplaceFor.
+        (WebCore::Editor::markAndReplaceFor):
+        * editing/Editor.h:
+
 2011-12-06  Julien Chaffraix  <jchaffraix@webkit.org>
 
         Avoid calling calculateRects in RenderLayer::paintLayer when the rectangles are not needed
index eff9002..1b3bdab 100644 (file)
@@ -1995,14 +1995,10 @@ void Editor::markAllMisspellingsAndBadGrammarInRanges(TextCheckingTypeMask textC
     // There shouldn't be pending autocorrection at this moment.
     ASSERT(!m_spellingCorrector->hasPendingCorrection());
 
-    bool shouldMarkSpelling = textCheckingOptions & TextCheckingTypeSpelling;
     bool shouldMarkGrammar = textCheckingOptions & TextCheckingTypeGrammar;
-    bool shouldPerformReplacement = textCheckingOptions & TextCheckingTypeReplacement;
     bool shouldShowCorrectionPanel = textCheckingOptions & TextCheckingTypeShowCorrectionPanel;
-    bool shouldCheckForCorrection = shouldShowCorrectionPanel || (textCheckingOptions & TextCheckingTypeCorrection);
 
     // This function is called with selections already expanded to word boundaries.
-    ExceptionCode ec = 0;
     if (!client() || !spellingRange || (shouldMarkGrammar && !grammarRange))
         return;
 
@@ -2014,36 +2010,12 @@ void Editor::markAllMisspellingsAndBadGrammarInRanges(TextCheckingTypeMask textC
     if (!isSpellCheckingEnabledFor(editableNode))
         return;
 
-    // Expand the range to encompass entire paragraphs, since text checking needs that much context.
-    int selectionOffset = 0;
-    int ambiguousBoundaryOffset = -1;
-    bool selectionChanged = false;
-    bool restoreSelectionAfterChange = false;
-    bool adjustSelectionForParagraphBoundaries = false;
-
     TextCheckingParagraph spellingParagraph(spellingRange);
     TextCheckingParagraph grammarParagraph(shouldMarkGrammar ? grammarRange : 0);
 
     if (shouldMarkGrammar ? (spellingParagraph.isRangeEmpty() && grammarParagraph.isEmpty()) : spellingParagraph.isEmpty())
         return;
 
-    if (shouldPerformReplacement || shouldMarkSpelling || shouldCheckForCorrection) {
-        if (m_frame->selection()->selectionType() == VisibleSelection::CaretSelection) {
-            // Attempt to save the caret position so we can restore it later if needed
-            Position caretPosition = m_frame->selection()->end();
-            int offset = spellingParagraph.offsetTo(caretPosition, ec);
-            if (!ec) {
-                selectionOffset = offset;
-                restoreSelectionAfterChange = true;
-                if (selectionOffset > 0 && (selectionOffset > spellingParagraph.textLength() || spellingParagraph.textCharAt(selectionOffset - 1) == newlineCharacter))
-                    adjustSelectionForParagraphBoundaries = true;
-                if (selectionOffset > 0 && selectionOffset <= spellingParagraph.textLength() && isAmbiguousBoundaryCharacter(spellingParagraph.textCharAt(selectionOffset - 1)))
-                    ambiguousBoundaryOffset = selectionOffset - 1;
-            }
-        }
-    }
-
-
     bool asynchronous = m_frame && m_frame->settings() && m_frame->settings()->asynchronousSpellCheckingEnabled() && !shouldShowCorrectionPanel;
     if (asynchronous) {
         if (shouldMarkGrammar)
@@ -2060,7 +2032,37 @@ void Editor::markAllMisspellingsAndBadGrammarInRanges(TextCheckingTypeMask textC
     else
         checkTextOfParagraph(textChecker(), spellingParagraph.textCharacters(), spellingParagraph.textLength(), 
                                             resolveTextCheckingTypeMask(textCheckingOptions), results);
-        
+
+    markAndReplaceFor(textCheckingOptions, results, spellingParagraph, grammarParagraph);
+}
+
+void Editor::markAndReplaceFor(TextCheckingTypeMask textCheckingOptions, const Vector<TextCheckingResult>& results, const TextCheckingParagraph& spellingParagraph, const TextCheckingParagraph& grammarParagraph)
+{
+    bool shouldMarkSpelling = textCheckingOptions & TextCheckingTypeSpelling;
+    bool shouldMarkGrammar = textCheckingOptions & TextCheckingTypeGrammar;
+    bool shouldPerformReplacement = textCheckingOptions & TextCheckingTypeReplacement;
+    bool shouldShowCorrectionPanel = textCheckingOptions & TextCheckingTypeShowCorrectionPanel;
+    bool shouldCheckForCorrection = shouldShowCorrectionPanel || (textCheckingOptions & TextCheckingTypeCorrection);
+
+    // Expand the range to encompass entire paragraphs, since text checking needs that much context.
+    int selectionOffset = 0;
+    int ambiguousBoundaryOffset = -1;
+    bool selectionChanged = false;
+    bool restoreSelectionAfterChange = false;
+    bool adjustSelectionForParagraphBoundaries = false;
+
+    if (shouldPerformReplacement || shouldMarkSpelling || shouldCheckForCorrection) {
+        if (m_frame->selection()->selectionType() == VisibleSelection::CaretSelection) {
+            // Attempt to save the caret position so we can restore it later if needed
+            Position caretPosition = m_frame->selection()->end();
+            selectionOffset = spellingParagraph.offsetTo(caretPosition, ASSERT_NO_EXCEPTION);
+            restoreSelectionAfterChange = true;
+            if (selectionOffset > 0 && (selectionOffset > spellingParagraph.textLength() || spellingParagraph.textCharAt(selectionOffset - 1) == newlineCharacter))
+                adjustSelectionForParagraphBoundaries = true;
+            if (selectionOffset > 0 && selectionOffset <= spellingParagraph.textLength() && isAmbiguousBoundaryCharacter(spellingParagraph.textCharAt(selectionOffset - 1)))
+                ambiguousBoundaryOffset = selectionOffset - 1;
+        }
+    }
 
     // If this checking is only for showing correction panel, we shouldn't bother to mark misspellings.
     if (shouldShowCorrectionPanel)
@@ -2085,7 +2087,7 @@ void Editor::markAllMisspellingsAndBadGrammarInRanges(TextCheckingTypeMask textC
             RefPtr<Range> misspellingRange = spellingParagraph.subrange(resultLocation, resultLength);
             if (!m_spellingCorrector->isSpellingMarkerAllowed(misspellingRange))
                 continue;
-            misspellingRange->startContainer(ec)->document()->markers()->addMarker(misspellingRange.get(), DocumentMarker::Spelling);
+            misspellingRange->startContainer()->document()->markers()->addMarker(misspellingRange.get(), DocumentMarker::Spelling);
         } else if (shouldMarkGrammar && result->type == TextCheckingTypeGrammar && grammarParagraph.checkingRangeCovers(resultLocation, resultLength)) {
             ASSERT(resultLength > 0 && resultLocation >= 0);
             for (unsigned j = 0; j < result->details.size(); j++) {
@@ -2093,7 +2095,7 @@ void Editor::markAllMisspellingsAndBadGrammarInRanges(TextCheckingTypeMask textC
                 ASSERT(detail->length > 0 && detail->location >= 0);
                 if (grammarParagraph.checkingRangeCovers(resultLocation + detail->location, detail->length)) {
                     RefPtr<Range> badGrammarRange = grammarParagraph.subrange(resultLocation + detail->location, detail->length);
-                    grammarRange->startContainer(ec)->document()->markers()->addMarker(badGrammarRange.get(), DocumentMarker::Grammar, detail->userDescription);
+                    badGrammarRange->startContainer()->document()->markers()->addMarker(badGrammarRange.get(), DocumentMarker::Grammar, detail->userDescription);
                 }
             }
         } else if (resultLocation + resultLength <= spellingRangeEndOffset && resultLocation + resultLength >= spellingParagraph.checkingStart()
@@ -2179,10 +2181,11 @@ void Editor::markAllMisspellingsAndBadGrammarInRanges(TextCheckingTypeMask textC
     }
 
     if (selectionChanged) {
+        TextCheckingParagraph extendedParagraph(spellingParagraph);
         // Restore the caret position if we have made any replacements
-        spellingParagraph.expandRangeToNextEnd();
-        if (restoreSelectionAfterChange && selectionOffset >= 0 && selectionOffset <= spellingParagraph.rangeLength()) {
-            RefPtr<Range> selectionRange = spellingParagraph.subrange(0, selectionOffset);
+        extendedParagraph.expandRangeToNextEnd();
+        if (restoreSelectionAfterChange && selectionOffset >= 0 && selectionOffset <= extendedParagraph.rangeLength()) {
+            RefPtr<Range> selectionRange = extendedParagraph.subrange(0, selectionOffset);
             m_frame->selection()->moveTo(selectionRange->endPosition(), DOWNSTREAM);
             if (adjustSelectionForParagraphBoundaries)
                 m_frame->selection()->modify(FrameSelection::AlterationMove, DirectionForward, CharacterGranularity);
index 10d7098..86cae4d 100644 (file)
@@ -62,6 +62,8 @@ class SimpleFontData;
 class SpellChecker;
 class Text;
 class TextCheckerClient;
+class TextCheckingParagraph;
+struct TextCheckingResult;
 class TextEvent;
 
 struct CompositionUnderline {
@@ -403,6 +405,7 @@ private:
     void writeSelectionToPasteboard(Pasteboard*);
     void revealSelectionAfterEditingOperation();
     void markMisspellingsOrBadGrammar(const VisibleSelection&, bool checkSpelling, RefPtr<Range>& firstMisspellingRange);
+    void markAndReplaceFor(TextCheckingTypeMask, const Vector<TextCheckingResult>&, const TextCheckingParagraph& spellingParagraph, const TextCheckingParagraph& grammarParagraph);
     TextCheckingTypeMask resolveTextCheckingTypeMask(TextCheckingTypeMask);
 
     void selectComposition();