Home | History | Annotate | Download | only in web
      1 /*
      2  * Copyright (C) 2006, 2007 Apple, Inc.  All rights reserved.
      3  * Copyright (C) 2012 Google, Inc.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #include "config.h"
     28 #include "SpellCheckerClientImpl.h"
     29 
     30 #include "WebSpellCheckClient.h"
     31 #include "WebTextCheckingCompletionImpl.h"
     32 #include "WebTextCheckingResult.h"
     33 #include "WebViewImpl.h"
     34 #include "core/dom/DocumentMarkerController.h"
     35 #include "core/editing/Editor.h"
     36 #include "core/editing/SpellChecker.h"
     37 #include "core/frame/Frame.h"
     38 #include "core/page/Page.h"
     39 #include "core/frame/Settings.h"
     40 
     41 using namespace WebCore;
     42 
     43 namespace blink {
     44 
     45 SpellCheckerClientImpl::SpellCheckerClientImpl(WebViewImpl* webview)
     46     : m_webView(webview)
     47     , m_spellCheckThisFieldStatus(SpellCheckAutomatic)
     48 {
     49 }
     50 
     51 SpellCheckerClientImpl::~SpellCheckerClientImpl()
     52 {
     53 }
     54 
     55 bool SpellCheckerClientImpl::shouldSpellcheckByDefault()
     56 {
     57     // Spellcheck should be enabled for all editable areas (such as textareas,
     58     // contentEditable regions, designMode docs and inputs).
     59     const Frame* frame = m_webView->focusedWebCoreFrame();
     60     if (!frame)
     61         return false;
     62     if (frame->spellChecker().isSpellCheckingEnabledInFocusedNode())
     63         return true;
     64     const Document* document = frame->document();
     65     if (!document)
     66         return false;
     67     const Element* element = document->focusedElement();
     68     // If |element| is null, we default to allowing spellchecking. This is done
     69     // in order to mitigate the issue when the user clicks outside the textbox,
     70     // as a result of which |element| becomes null, resulting in all the spell
     71     // check markers being deleted. Also, the Frame will decide not to do
     72     // spellchecking if the user can't edit - so returning true here will not
     73     // cause any problems to the Frame's behavior.
     74     if (!element)
     75         return true;
     76     const RenderObject* renderer = element->renderer();
     77     if (!renderer)
     78         return false;
     79 
     80     return true;
     81 }
     82 
     83 bool SpellCheckerClientImpl::isContinuousSpellCheckingEnabled()
     84 {
     85     if (m_spellCheckThisFieldStatus == SpellCheckForcedOff)
     86         return false;
     87     if (m_spellCheckThisFieldStatus == SpellCheckForcedOn)
     88         return true;
     89     return shouldSpellcheckByDefault();
     90 }
     91 
     92 void SpellCheckerClientImpl::toggleContinuousSpellChecking()
     93 {
     94     if (isContinuousSpellCheckingEnabled()) {
     95         m_spellCheckThisFieldStatus = SpellCheckForcedOff;
     96         if (Page* page = m_webView->page()) {
     97             for (Frame* frame = page->mainFrame(); frame && frame->document(); frame = frame->tree().traverseNext()) {
     98                 frame->document()->markers()->removeMarkers(DocumentMarker::MisspellingMarkers());
     99             }
    100         }
    101     } else {
    102         m_spellCheckThisFieldStatus = SpellCheckForcedOn;
    103         if (Frame* frame = m_webView->focusedWebCoreFrame()) {
    104             VisibleSelection frameSelection = frame->selection().selection();
    105             // If a selection is in an editable element spell check its content.
    106             if (Element* rootEditableElement = frameSelection.rootEditableElement()) {
    107                 frame->spellChecker().didBeginEditing(rootEditableElement);
    108             }
    109         }
    110     }
    111 }
    112 
    113 bool SpellCheckerClientImpl::isGrammarCheckingEnabled()
    114 {
    115     const Frame* frame = m_webView->focusedWebCoreFrame();
    116     return frame && frame->settings() && (frame->settings()->asynchronousSpellCheckingEnabled() || frame->settings()->unifiedTextCheckerEnabled());
    117 }
    118 
    119 bool SpellCheckerClientImpl::shouldEraseMarkersAfterChangeSelection(TextCheckingType type) const
    120 {
    121     const Frame* frame = m_webView->focusedWebCoreFrame();
    122     return !frame || !frame->settings() || (!frame->settings()->asynchronousSpellCheckingEnabled() && !frame->settings()->unifiedTextCheckerEnabled());
    123 }
    124 
    125 void SpellCheckerClientImpl::checkSpellingOfString(const String& text, int* misspellingLocation, int* misspellingLength)
    126 {
    127     // SpellCheckWord will write (0, 0) into the output vars, which is what our
    128     // caller expects if the word is spelled correctly.
    129     int spellLocation = -1;
    130     int spellLength = 0;
    131 
    132     // Check to see if the provided text is spelled correctly.
    133     if (m_webView->spellCheckClient()) {
    134         m_webView->spellCheckClient()->spellCheck(text, spellLocation, spellLength, 0);
    135     } else {
    136         spellLocation = 0;
    137         spellLength = 0;
    138     }
    139 
    140     // Note: the Mac code checks if the pointers are null before writing to them,
    141     // so we do too.
    142     if (misspellingLocation)
    143         *misspellingLocation = spellLocation;
    144     if (misspellingLength)
    145         *misspellingLength = spellLength;
    146 }
    147 
    148 void SpellCheckerClientImpl::requestCheckingOfString(WTF::PassRefPtr<WebCore::TextCheckingRequest> request)
    149 {
    150     if (m_webView->spellCheckClient()) {
    151         const String& text = request->data().text();
    152         const Vector<uint32_t>& markers = request->data().markers();
    153         const Vector<unsigned>& markerOffsets = request->data().offsets();
    154         m_webView->spellCheckClient()->requestCheckingOfText(text, markers, markerOffsets, new WebTextCheckingCompletionImpl(request));
    155     }
    156 }
    157 
    158 String SpellCheckerClientImpl::getAutoCorrectSuggestionForMisspelledWord(const String& misspelledWord)
    159 {
    160     if (!(isContinuousSpellCheckingEnabled() && m_webView->client()))
    161         return String();
    162 
    163     // Do not autocorrect words with capital letters in it except the
    164     // first letter. This will remove cases changing "IMB" to "IBM".
    165     for (size_t i = 1; i < misspelledWord.length(); i++) {
    166         if (u_isupper(static_cast<UChar32>(misspelledWord[i])))
    167             return String();
    168     }
    169 
    170     if (m_webView->spellCheckClient())
    171         return m_webView->spellCheckClient()->autoCorrectWord(WebString(misspelledWord));
    172     return String();
    173 }
    174 
    175 void SpellCheckerClientImpl::checkGrammarOfString(const String& text, WTF::Vector<GrammarDetail>& details, int* badGrammarLocation, int* badGrammarLength)
    176 {
    177     if (badGrammarLocation)
    178         *badGrammarLocation = -1;
    179     if (badGrammarLength)
    180         *badGrammarLength = 0;
    181 
    182     if (!m_webView->spellCheckClient())
    183         return;
    184     WebVector<WebTextCheckingResult> webResults;
    185     m_webView->spellCheckClient()->checkTextOfParagraph(text, WebTextCheckingTypeGrammar, &webResults);
    186     if (!webResults.size())
    187         return;
    188 
    189     // Convert a list of WebTextCheckingResults to a list of GrammarDetails. If
    190     // the converted vector of GrammarDetails has grammar errors, we set
    191     // badGrammarLocation and badGrammarLength to tell WebKit that the input
    192     // text has grammar errors.
    193     for (size_t i = 0; i < webResults.size(); ++i) {
    194         if (webResults[i].decoration == WebTextDecorationTypeGrammar) {
    195             GrammarDetail detail;
    196             detail.location = webResults[i].location;
    197             detail.length = webResults[i].length;
    198             detail.userDescription = webResults[i].replacement;
    199             details.append(detail);
    200         }
    201     }
    202     if (!details.size())
    203         return;
    204     if (badGrammarLocation)
    205         *badGrammarLocation = 0;
    206     if (badGrammarLength)
    207         *badGrammarLength = text.length();
    208 }
    209 
    210 void SpellCheckerClientImpl::updateSpellingUIWithMisspelledWord(const String& misspelledWord)
    211 {
    212     if (m_webView->spellCheckClient())
    213         m_webView->spellCheckClient()->updateSpellingUIWithMisspelledWord(WebString(misspelledWord));
    214 }
    215 
    216 void SpellCheckerClientImpl::showSpellingUI(bool show)
    217 {
    218     if (m_webView->spellCheckClient())
    219         m_webView->spellCheckClient()->showSpellingUI(show);
    220 }
    221 
    222 bool SpellCheckerClientImpl::spellingUIIsShowing()
    223 {
    224     if (m_webView->spellCheckClient())
    225         return m_webView->spellCheckClient()->isShowingSpellingUI();
    226     return false;
    227 }
    228 
    229 } // namesace WebKit
    230