This source file includes following definitions.
- m_spellCheckThisFieldStatus
- shouldSpellcheckByDefault
- isContinuousSpellCheckingEnabled
- toggleContinuousSpellChecking
- isGrammarCheckingEnabled
- shouldEraseMarkersAfterChangeSelection
- checkSpellingOfString
- requestCheckingOfString
- getAutoCorrectSuggestionForMisspelledWord
- checkGrammarOfString
- updateSpellingUIWithMisspelledWord
- showSpellingUI
- spellingUIIsShowing
#include "config.h"
#include "SpellCheckerClientImpl.h"
#include "WebSpellCheckClient.h"
#include "WebTextCheckingCompletionImpl.h"
#include "WebTextCheckingResult.h"
#include "WebViewImpl.h"
#include "core/dom/DocumentMarkerController.h"
#include "core/editing/Editor.h"
#include "core/editing/SpellChecker.h"
#include "core/frame/LocalFrame.h"
#include "core/page/Page.h"
#include "core/frame/Settings.h"
using namespace WebCore;
namespace blink {
SpellCheckerClientImpl::SpellCheckerClientImpl(WebViewImpl* webview)
: m_webView(webview)
, m_spellCheckThisFieldStatus(SpellCheckAutomatic)
{
}
SpellCheckerClientImpl::~SpellCheckerClientImpl()
{
}
bool SpellCheckerClientImpl::shouldSpellcheckByDefault()
{
if (!m_webView->focusedWebCoreFrame()->isLocalFrame())
return false;
const LocalFrame* frame = toLocalFrame(m_webView->focusedWebCoreFrame());
if (!frame)
return false;
if (frame->spellChecker().isSpellCheckingEnabledInFocusedNode())
return true;
const Document* document = frame->document();
if (!document)
return false;
const Element* element = document->focusedElement();
if (!element)
return true;
const RenderObject* renderer = element->renderer();
if (!renderer)
return false;
return true;
}
bool SpellCheckerClientImpl::isContinuousSpellCheckingEnabled()
{
if (m_spellCheckThisFieldStatus == SpellCheckForcedOff)
return false;
if (m_spellCheckThisFieldStatus == SpellCheckForcedOn)
return true;
return shouldSpellcheckByDefault();
}
void SpellCheckerClientImpl::toggleContinuousSpellChecking()
{
if (isContinuousSpellCheckingEnabled()) {
m_spellCheckThisFieldStatus = SpellCheckForcedOff;
if (Page* page = m_webView->page()) {
for (LocalFrame* frame = page->mainFrame(); frame && frame->document(); frame = frame->tree().traverseNext()) {
frame->document()->markers().removeMarkers(DocumentMarker::MisspellingMarkers());
}
}
} else {
m_spellCheckThisFieldStatus = SpellCheckForcedOn;
if (m_webView->focusedWebCoreFrame()->isLocalFrame()) {
if (LocalFrame* frame = toLocalFrame(m_webView->focusedWebCoreFrame())) {
VisibleSelection frameSelection = frame->selection().selection();
if (Element* rootEditableElement = frameSelection.rootEditableElement()) {
frame->spellChecker().didBeginEditing(rootEditableElement);
}
}
}
}
}
bool SpellCheckerClientImpl::isGrammarCheckingEnabled()
{
const LocalFrame* frame = toLocalFrame(m_webView->focusedWebCoreFrame());
return frame && frame->settings() && (frame->settings()->asynchronousSpellCheckingEnabled() || frame->settings()->unifiedTextCheckerEnabled());
}
bool SpellCheckerClientImpl::shouldEraseMarkersAfterChangeSelection(TextCheckingType type) const
{
const Frame* frame = m_webView->focusedWebCoreFrame();
return !frame || !frame->settings() || (!frame->settings()->asynchronousSpellCheckingEnabled() && !frame->settings()->unifiedTextCheckerEnabled());
}
void SpellCheckerClientImpl::checkSpellingOfString(const String& text, int* misspellingLocation, int* misspellingLength)
{
int spellLocation = -1;
int spellLength = 0;
if (m_webView->spellCheckClient()) {
m_webView->spellCheckClient()->spellCheck(text, spellLocation, spellLength, 0);
} else {
spellLocation = 0;
spellLength = 0;
}
if (misspellingLocation)
*misspellingLocation = spellLocation;
if (misspellingLength)
*misspellingLength = spellLength;
}
void SpellCheckerClientImpl::requestCheckingOfString(WTF::PassRefPtr<WebCore::TextCheckingRequest> request)
{
if (m_webView->spellCheckClient()) {
const String& text = request->data().text();
const Vector<uint32_t>& markers = request->data().markers();
const Vector<unsigned>& markerOffsets = request->data().offsets();
m_webView->spellCheckClient()->requestCheckingOfText(text, markers, markerOffsets, new WebTextCheckingCompletionImpl(request));
}
}
String SpellCheckerClientImpl::getAutoCorrectSuggestionForMisspelledWord(const String& misspelledWord)
{
if (!(isContinuousSpellCheckingEnabled() && m_webView->client()))
return String();
for (size_t i = 1; i < misspelledWord.length(); i++) {
if (u_isupper(static_cast<UChar32>(misspelledWord[i])))
return String();
}
if (m_webView->spellCheckClient())
return m_webView->spellCheckClient()->autoCorrectWord(WebString(misspelledWord));
return String();
}
void SpellCheckerClientImpl::checkGrammarOfString(const String& text, WTF::Vector<GrammarDetail>& details, int* badGrammarLocation, int* badGrammarLength)
{
if (badGrammarLocation)
*badGrammarLocation = -1;
if (badGrammarLength)
*badGrammarLength = 0;
if (!m_webView->spellCheckClient())
return;
WebVector<WebTextCheckingResult> webResults;
m_webView->spellCheckClient()->checkTextOfParagraph(text, WebTextCheckingTypeGrammar, &webResults);
if (!webResults.size())
return;
for (size_t i = 0; i < webResults.size(); ++i) {
if (webResults[i].decoration == WebTextDecorationTypeGrammar) {
GrammarDetail detail;
detail.location = webResults[i].location;
detail.length = webResults[i].length;
detail.userDescription = webResults[i].replacement;
details.append(detail);
}
}
if (!details.size())
return;
if (badGrammarLocation)
*badGrammarLocation = 0;
if (badGrammarLength)
*badGrammarLength = text.length();
}
void SpellCheckerClientImpl::updateSpellingUIWithMisspelledWord(const String& misspelledWord)
{
if (m_webView->spellCheckClient())
m_webView->spellCheckClient()->updateSpellingUIWithMisspelledWord(WebString(misspelledWord));
}
void SpellCheckerClientImpl::showSpellingUI(bool show)
{
if (m_webView->spellCheckClient())
m_webView->spellCheckClient()->showSpellingUI(show);
}
bool SpellCheckerClientImpl::spellingUIIsShowing()
{
if (m_webView->spellCheckClient())
return m_webView->spellCheckClient()->isShowingSpellingUI();
return false;
}
}