This source file includes following definitions.
- isSelectionInTextField
- create
- emptySpellCheckerClient
- spellCheckerClient
- textChecker
- m_spellCheckRequester
- isContinuousSpellCheckingEnabled
- toggleContinuousSpellChecking
- isGrammarCheckingEnabled
- didBeginEditing
- ignoreSpelling
- advanceToNextMisspelling
- showSpellingGuessPanel
- clearMisspellingsAndBadGrammar
- markMisspellingsAndBadGrammar
- markMisspellingsAfterTypingToWord
- markMisspellingsOrBadGrammar
- isSpellCheckingEnabledFor
- isSpellCheckingEnabledInFocusedNode
- markMisspellings
- markBadGrammar
- markAllMisspellingsAndBadGrammarInRanges
- chunkAndMarkAllMisspellingsAndBadGrammar
- chunkAndMarkAllMisspellingsAndBadGrammar
- markAllMisspellingsAndBadGrammarInRanges
- markAndReplaceFor
- markMisspellingsAndBadGrammar
- updateMarkersForWordsAffectedByEditing
- didEndEditingOnTextField
- respondToChangedSelection
- spellCheckAfterBlur
- spellCheckOldSelection
- findFirstMarkable
- selectionStartHasMarkerFor
- resolveTextCheckingTypeMask
- unifiedTextCheckerEnabled
- cancelCheck
- requestTextChecking
#include "config.h"
#include "core/editing/SpellChecker.h"
#include "HTMLNames.h"
#include "core/dom/Document.h"
#include "core/dom/DocumentMarkerController.h"
#include "core/dom/Element.h"
#include "core/dom/NodeTraversal.h"
#include "core/editing/Editor.h"
#include "core/editing/SpellCheckRequester.h"
#include "core/editing/TextCheckingHelper.h"
#include "core/editing/VisibleUnits.h"
#include "core/editing/htmlediting.h"
#include "core/frame/LocalFrame.h"
#include "core/html/HTMLInputElement.h"
#include "core/loader/EmptyClients.h"
#include "core/page/Page.h"
#include "core/frame/Settings.h"
#include "core/page/SpellCheckerClient.h"
#include "core/rendering/RenderTextControl.h"
#include "platform/text/TextCheckerClient.h"
namespace WebCore {
using namespace HTMLNames;
namespace {
bool isSelectionInTextField(const VisibleSelection& selection)
{
HTMLTextFormControlElement* textControl = enclosingTextFormControl(selection.start());
return isHTMLInputElement(textControl) && toHTMLInputElement(textControl)->isTextField();
}
}
PassOwnPtr<SpellChecker> SpellChecker::create(LocalFrame& frame)
{
return adoptPtr(new SpellChecker(frame));
}
static SpellCheckerClient& emptySpellCheckerClient()
{
DEFINE_STATIC_LOCAL(EmptySpellCheckerClient, client, ());
return client;
}
SpellCheckerClient& SpellChecker::spellCheckerClient() const
{
if (Page* page = m_frame.page())
return page->spellCheckerClient();
return emptySpellCheckerClient();
}
TextCheckerClient& SpellChecker::textChecker() const
{
return spellCheckerClient().textChecker();
}
SpellChecker::SpellChecker(LocalFrame& frame)
: m_frame(frame)
, m_spellCheckRequester(adoptPtr(new SpellCheckRequester(frame)))
{
}
SpellChecker::~SpellChecker()
{
}
bool SpellChecker::isContinuousSpellCheckingEnabled() const
{
return spellCheckerClient().isContinuousSpellCheckingEnabled();
}
void SpellChecker::toggleContinuousSpellChecking()
{
spellCheckerClient().toggleContinuousSpellChecking();
if (isContinuousSpellCheckingEnabled())
return;
for (LocalFrame* frame = m_frame.page()->mainFrame(); frame && frame->document(); frame = frame->tree().traverseNext()) {
for (Node* node = &frame->document()->rootNode(); node; node = NodeTraversal::next(*node)) {
node->setAlreadySpellChecked(false);
}
}
}
bool SpellChecker::isGrammarCheckingEnabled()
{
return spellCheckerClient().isGrammarCheckingEnabled();
}
void SpellChecker::didBeginEditing(Element* element)
{
if (isContinuousSpellCheckingEnabled() && unifiedTextCheckerEnabled()) {
bool isTextField = false;
HTMLTextFormControlElement* enclosingHTMLTextFormControlElement = 0;
if (!isHTMLTextFormControlElement(*element))
enclosingHTMLTextFormControlElement = enclosingTextFormControl(firstPositionInNode(element));
element = enclosingHTMLTextFormControlElement ? enclosingHTMLTextFormControlElement : element;
Element* parent = element;
if (isHTMLTextFormControlElement(*element)) {
HTMLTextFormControlElement* textControl = toHTMLTextFormControlElement(element);
parent = textControl;
element = textControl->innerTextElement();
isTextField = isHTMLInputElement(*textControl) && toHTMLInputElement(*textControl).isTextField();
}
if (isTextField || !parent->isAlreadySpellChecked()) {
VisibleSelection selection = VisibleSelection::selectionFromContentsOfNode(element);
markMisspellingsAndBadGrammar(selection);
if (!isTextField)
parent->setAlreadySpellChecked(true);
}
}
}
void SpellChecker::ignoreSpelling()
{
if (RefPtrWillBeRawPtr<Range> selectedRange = m_frame.selection().toNormalizedRange())
m_frame.document()->markers().removeMarkers(selectedRange.get(), DocumentMarker::Spelling);
}
void SpellChecker::advanceToNextMisspelling(bool startBeforeSelection)
{
VisibleSelection selection(m_frame.selection().selection());
RefPtrWillBeRawPtr<Range> spellingSearchRange(rangeOfContents(m_frame.document()));
bool startedWithSelection = false;
if (selection.start().deprecatedNode()) {
startedWithSelection = true;
if (startBeforeSelection) {
VisiblePosition start(selection.visibleStart());
VisiblePosition oneBeforeStart = start.previous();
setStart(spellingSearchRange.get(), oneBeforeStart.isNotNull() ? oneBeforeStart : start);
} else {
setStart(spellingSearchRange.get(), selection.visibleEnd());
}
}
Position position = spellingSearchRange->startPosition();
if (!isEditablePosition(position)) {
position = firstEditablePositionAfterPositionInRoot(position, m_frame.document()->documentElement()).deepEquivalent();
if (position.isNull())
return;
Position rangeCompliantPosition = position.parentAnchoredEquivalent();
spellingSearchRange->setStart(rangeCompliantPosition.deprecatedNode(), rangeCompliantPosition.deprecatedEditingOffset(), IGNORE_EXCEPTION);
startedWithSelection = false;
}
Node* topNode = highestEditableRoot(position);
spellingSearchRange->setEnd(topNode, lastOffsetForEditing(topNode), IGNORE_EXCEPTION);
if (startedWithSelection) {
VisiblePosition oneBeforeStart = startVisiblePosition(spellingSearchRange.get(), DOWNSTREAM).previous();
if (oneBeforeStart.isNotNull())
setStart(spellingSearchRange.get(), endOfWord(oneBeforeStart));
}
if (spellingSearchRange->collapsed(IGNORE_EXCEPTION))
return;
Node* searchEndNodeAfterWrap = spellingSearchRange->endContainer();
int searchEndOffsetAfterWrap = spellingSearchRange->endOffset();
int misspellingOffset = 0;
GrammarDetail grammarDetail;
int grammarPhraseOffset = 0;
RefPtrWillBeRawPtr<Range> grammarSearchRange = nullptr;
String badGrammarPhrase;
String misspelledWord;
bool isSpelling = true;
int foundOffset = 0;
String foundItem;
RefPtrWillBeRawPtr<Range> firstMisspellingRange = nullptr;
if (unifiedTextCheckerEnabled()) {
grammarSearchRange = spellingSearchRange->cloneRange(IGNORE_EXCEPTION);
foundItem = TextCheckingHelper(spellCheckerClient(), spellingSearchRange).findFirstMisspellingOrBadGrammar(isGrammarCheckingEnabled(), isSpelling, foundOffset, grammarDetail);
if (isSpelling) {
misspelledWord = foundItem;
misspellingOffset = foundOffset;
} else {
badGrammarPhrase = foundItem;
grammarPhraseOffset = foundOffset;
}
} else {
misspelledWord = TextCheckingHelper(spellCheckerClient(), spellingSearchRange).findFirstMisspelling(misspellingOffset, false, firstMisspellingRange);
grammarSearchRange = spellingSearchRange->cloneRange(IGNORE_EXCEPTION);
if (!misspelledWord.isEmpty()) {
CharacterIterator chars(grammarSearchRange.get());
chars.advance(misspellingOffset);
grammarSearchRange->setEnd(chars.range()->startContainer(), chars.range()->startOffset(), IGNORE_EXCEPTION);
}
if (isGrammarCheckingEnabled())
badGrammarPhrase = TextCheckingHelper(spellCheckerClient(), grammarSearchRange).findFirstBadGrammar(grammarDetail, grammarPhraseOffset, false);
}
if (startedWithSelection && !misspelledWord && !badGrammarPhrase) {
spellingSearchRange->setStart(topNode, 0, IGNORE_EXCEPTION);
spellingSearchRange->setEnd(searchEndNodeAfterWrap, searchEndOffsetAfterWrap, IGNORE_EXCEPTION);
if (unifiedTextCheckerEnabled()) {
grammarSearchRange = spellingSearchRange->cloneRange(IGNORE_EXCEPTION);
foundItem = TextCheckingHelper(spellCheckerClient(), spellingSearchRange).findFirstMisspellingOrBadGrammar(isGrammarCheckingEnabled(), isSpelling, foundOffset, grammarDetail);
if (isSpelling) {
misspelledWord = foundItem;
misspellingOffset = foundOffset;
} else {
badGrammarPhrase = foundItem;
grammarPhraseOffset = foundOffset;
}
} else {
misspelledWord = TextCheckingHelper(spellCheckerClient(), spellingSearchRange).findFirstMisspelling(misspellingOffset, false, firstMisspellingRange);
grammarSearchRange = spellingSearchRange->cloneRange(IGNORE_EXCEPTION);
if (!misspelledWord.isEmpty()) {
CharacterIterator chars(grammarSearchRange.get());
chars.advance(misspellingOffset);
grammarSearchRange->setEnd(chars.range()->startContainer(), chars.range()->startOffset(), IGNORE_EXCEPTION);
}
if (isGrammarCheckingEnabled())
badGrammarPhrase = TextCheckingHelper(spellCheckerClient(), grammarSearchRange).findFirstBadGrammar(grammarDetail, grammarPhraseOffset, false);
}
}
if (!badGrammarPhrase.isEmpty()) {
ASSERT(badGrammarPhrase.length() > 0);
ASSERT(grammarDetail.location != -1 && grammarDetail.length > 0);
RefPtrWillBeRawPtr<Range> badGrammarRange = TextIterator::subrange(grammarSearchRange.get(), grammarPhraseOffset + grammarDetail.location, grammarDetail.length);
m_frame.selection().setSelection(VisibleSelection(badGrammarRange.get(), SEL_DEFAULT_AFFINITY));
m_frame.selection().revealSelection();
m_frame.document()->markers().addMarker(badGrammarRange.get(), DocumentMarker::Grammar, grammarDetail.userDescription);
} else if (!misspelledWord.isEmpty()) {
RefPtrWillBeRawPtr<Range> misspellingRange = TextIterator::subrange(spellingSearchRange.get(), misspellingOffset, misspelledWord.length());
m_frame.selection().setSelection(VisibleSelection(misspellingRange.get(), DOWNSTREAM));
m_frame.selection().revealSelection();
spellCheckerClient().updateSpellingUIWithMisspelledWord(misspelledWord);
m_frame.document()->markers().addMarker(misspellingRange.get(), DocumentMarker::Spelling);
}
}
void SpellChecker::showSpellingGuessPanel()
{
if (spellCheckerClient().spellingUIIsShowing()) {
spellCheckerClient().showSpellingUI(false);
return;
}
advanceToNextMisspelling(true);
spellCheckerClient().showSpellingUI(true);
}
void SpellChecker::clearMisspellingsAndBadGrammar(const VisibleSelection &movingSelection)
{
RefPtrWillBeRawPtr<Range> selectedRange = movingSelection.toNormalizedRange();
if (selectedRange)
m_frame.document()->markers().removeMarkers(selectedRange.get(), DocumentMarker::MisspellingMarkers());
}
void SpellChecker::markMisspellingsAndBadGrammar(const VisibleSelection &movingSelection)
{
markMisspellingsAndBadGrammar(movingSelection, isContinuousSpellCheckingEnabled() && isGrammarCheckingEnabled(), movingSelection);
}
void SpellChecker::markMisspellingsAfterTypingToWord(const VisiblePosition &wordStart, const VisibleSelection& selectionAfterTyping)
{
if (unifiedTextCheckerEnabled()) {
TextCheckingTypeMask textCheckingOptions = 0;
if (isContinuousSpellCheckingEnabled())
textCheckingOptions |= TextCheckingTypeSpelling;
if (!(textCheckingOptions & TextCheckingTypeSpelling))
return;
if (isGrammarCheckingEnabled())
textCheckingOptions |= TextCheckingTypeGrammar;
VisibleSelection adjacentWords = VisibleSelection(startOfWord(wordStart, LeftWordIfOnBoundary), endOfWord(wordStart, RightWordIfOnBoundary));
if (textCheckingOptions & TextCheckingTypeGrammar) {
VisibleSelection selectedSentence = VisibleSelection(startOfSentence(wordStart), endOfSentence(wordStart));
markAllMisspellingsAndBadGrammarInRanges(textCheckingOptions, adjacentWords.toNormalizedRange().get(), selectedSentence.toNormalizedRange().get());
} else {
markAllMisspellingsAndBadGrammarInRanges(textCheckingOptions, adjacentWords.toNormalizedRange().get(), adjacentWords.toNormalizedRange().get());
}
return;
}
if (!isContinuousSpellCheckingEnabled())
return;
RefPtrWillBeRawPtr<Range> misspellingRange = nullptr;
markMisspellings(VisibleSelection(startOfWord(wordStart, LeftWordIfOnBoundary), endOfWord(wordStart, RightWordIfOnBoundary)), misspellingRange);
if (!misspellingRange)
return;
const String misspelledWord = plainText(misspellingRange.get());
String autocorrectedString = textChecker().getAutoCorrectSuggestionForMisspelledWord(misspelledWord);
if (!autocorrectedString.isEmpty()) {
VisibleSelection newSelection(misspellingRange.get(), DOWNSTREAM);
if (newSelection != m_frame.selection().selection()) {
m_frame.selection().setSelection(newSelection);
}
m_frame.editor().replaceSelectionWithText(autocorrectedString, false, false);
m_frame.selection().moveTo(m_frame.selection().selection().visibleEnd());
m_frame.selection().modify(FrameSelection::AlterationMove, DirectionForward, CharacterGranularity);
}
if (!isGrammarCheckingEnabled())
return;
markBadGrammar(VisibleSelection(startOfSentence(wordStart), endOfSentence(wordStart)));
}
void SpellChecker::markMisspellingsOrBadGrammar(const VisibleSelection& selection, bool checkSpelling, RefPtrWillBeRawPtr<Range>& firstMisspellingRange)
{
if (!isContinuousSpellCheckingEnabled())
return;
RefPtrWillBeRawPtr<Range> searchRange(selection.toNormalizedRange());
if (!searchRange)
return;
Node* editableNode = searchRange->startContainer();
if (!editableNode || !editableNode->rendererIsEditable())
return;
if (!isSpellCheckingEnabledFor(editableNode))
return;
TextCheckingHelper checker(spellCheckerClient(), searchRange);
if (checkSpelling)
checker.markAllMisspellings(firstMisspellingRange);
else if (isGrammarCheckingEnabled())
checker.markAllBadGrammar();
}
bool SpellChecker::isSpellCheckingEnabledFor(Node* node) const
{
if (!node)
return false;
const Element* focusedElement = node->isElementNode() ? toElement(node) : node->parentElement();
if (!focusedElement)
return false;
return focusedElement->isSpellCheckingEnabled();
}
bool SpellChecker::isSpellCheckingEnabledInFocusedNode() const
{
return isSpellCheckingEnabledFor(m_frame.selection().start().deprecatedNode());
}
void SpellChecker::markMisspellings(const VisibleSelection& selection, RefPtrWillBeRawPtr<Range>& firstMisspellingRange)
{
markMisspellingsOrBadGrammar(selection, true, firstMisspellingRange);
}
void SpellChecker::markBadGrammar(const VisibleSelection& selection)
{
RefPtrWillBeRawPtr<Range> firstMisspellingRange = nullptr;
markMisspellingsOrBadGrammar(selection, false, firstMisspellingRange);
}
void SpellChecker::markAllMisspellingsAndBadGrammarInRanges(TextCheckingTypeMask textCheckingOptions, Range* spellingRange, Range* grammarRange)
{
ASSERT(unifiedTextCheckerEnabled());
bool shouldMarkGrammar = textCheckingOptions & TextCheckingTypeGrammar;
if (!spellingRange || (shouldMarkGrammar && !grammarRange))
return;
Node* editableNode = spellingRange->startContainer();
if (!editableNode || !editableNode->rendererIsEditable())
return;
if (!isSpellCheckingEnabledFor(editableNode))
return;
Range* rangeToCheck = shouldMarkGrammar ? grammarRange : spellingRange;
TextCheckingParagraph fullParagraphToCheck(rangeToCheck);
bool asynchronous = m_frame.settings() && m_frame.settings()->asynchronousSpellCheckingEnabled();
chunkAndMarkAllMisspellingsAndBadGrammar(textCheckingOptions, fullParagraphToCheck, asynchronous);
}
void SpellChecker::chunkAndMarkAllMisspellingsAndBadGrammar(Node* node)
{
if (!node)
return;
RefPtrWillBeRawPtr<Range> rangeToCheck = Range::create(*m_frame.document(), firstPositionInNode(node), lastPositionInNode(node));
TextCheckingParagraph textToCheck(rangeToCheck, rangeToCheck);
bool asynchronous = true;
chunkAndMarkAllMisspellingsAndBadGrammar(resolveTextCheckingTypeMask(TextCheckingTypeSpelling | TextCheckingTypeGrammar), textToCheck, asynchronous);
}
void SpellChecker::chunkAndMarkAllMisspellingsAndBadGrammar(TextCheckingTypeMask textCheckingOptions, const TextCheckingParagraph& fullParagraphToCheck, bool asynchronous)
{
if (fullParagraphToCheck.isRangeEmpty() || fullParagraphToCheck.isEmpty())
return;
const int kChunkSize = 16 * 1024;
int start = fullParagraphToCheck.checkingStart();
int end = fullParagraphToCheck.checkingEnd();
start = std::min(start, end);
end = std::max(start, end);
const int kNumChunksToCheck = asynchronous ? (end - start + kChunkSize - 1) / (kChunkSize) : 1;
int currentChunkStart = start;
RefPtrWillBeRawPtr<Range> checkRange = fullParagraphToCheck.checkingRange();
if (kNumChunksToCheck == 1 && asynchronous) {
markAllMisspellingsAndBadGrammarInRanges(textCheckingOptions, checkRange.get(), checkRange.get(), asynchronous, 0);
return;
}
for (int iter = 0; iter < kNumChunksToCheck; ++iter) {
checkRange = fullParagraphToCheck.subrange(currentChunkStart, kChunkSize);
setStart(checkRange.get(), startOfSentence(VisiblePosition(checkRange->startPosition())));
setEnd(checkRange.get(), endOfSentence(VisiblePosition(checkRange->endPosition())));
int checkingLength = 0;
markAllMisspellingsAndBadGrammarInRanges(textCheckingOptions, checkRange.get(), checkRange.get(), asynchronous, iter, &checkingLength);
currentChunkStart += checkingLength;
}
}
void SpellChecker::markAllMisspellingsAndBadGrammarInRanges(TextCheckingTypeMask textCheckingOptions, Range* checkRange, Range* paragraphRange, bool asynchronous, int requestNumber, int* checkingLength)
{
TextCheckingParagraph sentenceToCheck(checkRange, paragraphRange);
if (checkingLength)
*checkingLength = sentenceToCheck.checkingLength();
RefPtr<SpellCheckRequest> request = SpellCheckRequest::create(resolveTextCheckingTypeMask(textCheckingOptions), TextCheckingProcessBatch, checkRange, paragraphRange, requestNumber);
if (asynchronous) {
m_spellCheckRequester->requestCheckingFor(request);
} else {
Vector<TextCheckingResult> results;
checkTextOfParagraph(textChecker(), sentenceToCheck.text(), resolveTextCheckingTypeMask(textCheckingOptions), results);
markAndReplaceFor(request, results);
}
}
void SpellChecker::markAndReplaceFor(PassRefPtr<SpellCheckRequest> request, const Vector<TextCheckingResult>& results)
{
ASSERT(request);
TextCheckingTypeMask textCheckingOptions = request->data().mask();
TextCheckingParagraph paragraph(request->checkingRange(), request->paragraphRange());
bool shouldMarkSpelling = textCheckingOptions & TextCheckingTypeSpelling;
bool shouldMarkGrammar = textCheckingOptions & TextCheckingTypeGrammar;
int selectionOffset = 0;
int ambiguousBoundaryOffset = -1;
bool selectionChanged = false;
bool restoreSelectionAfterChange = false;
bool adjustSelectionForParagraphBoundaries = false;
if (shouldMarkSpelling) {
if (m_frame.selection().isCaret()) {
Position caretPosition = m_frame.selection().end();
selectionOffset = paragraph.offsetTo(caretPosition, ASSERT_NO_EXCEPTION);
restoreSelectionAfterChange = true;
if (selectionOffset > 0 && (static_cast<unsigned>(selectionOffset) > paragraph.text().length() || paragraph.textCharAt(selectionOffset - 1) == newlineCharacter))
adjustSelectionForParagraphBoundaries = true;
if (selectionOffset > 0 && static_cast<unsigned>(selectionOffset) <= paragraph.text().length() && isAmbiguousBoundaryCharacter(paragraph.textCharAt(selectionOffset - 1)))
ambiguousBoundaryOffset = selectionOffset - 1;
}
}
for (unsigned i = 0; i < results.size(); i++) {
int spellingRangeEndOffset = paragraph.checkingEnd();
const TextCheckingResult* result = &results[i];
int resultLocation = result->location + paragraph.checkingStart();
int resultLength = result->length;
bool resultEndsAtAmbiguousBoundary = ambiguousBoundaryOffset >= 0 && resultLocation + resultLength == ambiguousBoundaryOffset;
if (shouldMarkSpelling && result->decoration == TextDecorationTypeSpelling && resultLocation >= paragraph.checkingStart() && resultLocation + resultLength <= spellingRangeEndOffset && !resultEndsAtAmbiguousBoundary) {
ASSERT(resultLength > 0 && resultLocation >= 0);
RefPtrWillBeRawPtr<Range> misspellingRange = paragraph.subrange(resultLocation, resultLength);
misspellingRange->startContainer()->document().markers().addMarker(misspellingRange.get(), DocumentMarker::Spelling, result->replacement, result->hash);
} else if (shouldMarkGrammar && result->decoration == TextDecorationTypeGrammar && paragraph.checkingRangeCovers(resultLocation, resultLength)) {
ASSERT(resultLength > 0 && resultLocation >= 0);
for (unsigned j = 0; j < result->details.size(); j++) {
const GrammarDetail* detail = &result->details[j];
ASSERT(detail->length > 0 && detail->location >= 0);
if (paragraph.checkingRangeCovers(resultLocation + detail->location, detail->length)) {
RefPtrWillBeRawPtr<Range> badGrammarRange = paragraph.subrange(resultLocation + detail->location, detail->length);
badGrammarRange->startContainer()->document().markers().addMarker(badGrammarRange.get(), DocumentMarker::Grammar, detail->userDescription, result->hash);
}
}
} else if (result->decoration == TextDecorationTypeInvisibleSpellcheck && resultLocation >= paragraph.checkingStart() && resultLocation + resultLength <= spellingRangeEndOffset) {
ASSERT(resultLength > 0 && resultLocation >= 0);
RefPtrWillBeRawPtr<Range> invisibleSpellcheckRange = paragraph.subrange(resultLocation, resultLength);
invisibleSpellcheckRange->startContainer()->document().markers().addMarker(invisibleSpellcheckRange.get(), DocumentMarker::InvisibleSpellcheck, result->replacement, result->hash);
}
}
if (selectionChanged) {
TextCheckingParagraph extendedParagraph(paragraph);
extendedParagraph.expandRangeToNextEnd();
if (restoreSelectionAfterChange && selectionOffset >= 0 && selectionOffset <= extendedParagraph.rangeLength()) {
RefPtrWillBeRawPtr<Range> selectionRange = extendedParagraph.subrange(0, selectionOffset);
m_frame.selection().moveTo(selectionRange->endPosition(), DOWNSTREAM);
if (adjustSelectionForParagraphBoundaries)
m_frame.selection().modify(FrameSelection::AlterationMove, DirectionForward, CharacterGranularity);
} else {
m_frame.selection().moveTo(m_frame.selection().selection().visibleEnd());
m_frame.selection().modify(FrameSelection::AlterationMove, DirectionForward, CharacterGranularity);
}
}
}
void SpellChecker::markMisspellingsAndBadGrammar(const VisibleSelection& spellingSelection, bool markGrammar, const VisibleSelection& grammarSelection)
{
if (unifiedTextCheckerEnabled()) {
if (!isContinuousSpellCheckingEnabled())
return;
TextCheckingTypeMask textCheckingOptions = TextCheckingTypeSpelling;
if (markGrammar && isGrammarCheckingEnabled())
textCheckingOptions |= TextCheckingTypeGrammar;
markAllMisspellingsAndBadGrammarInRanges(textCheckingOptions, spellingSelection.toNormalizedRange().get(), grammarSelection.toNormalizedRange().get());
return;
}
RefPtrWillBeRawPtr<Range> firstMisspellingRange = nullptr;
markMisspellings(spellingSelection, firstMisspellingRange);
if (markGrammar)
markBadGrammar(grammarSelection);
}
void SpellChecker::updateMarkersForWordsAffectedByEditing(bool doNotRemoveIfSelectionAtWordBoundary)
{
if (textChecker().shouldEraseMarkersAfterChangeSelection(TextCheckingTypeSpelling))
return;
VisiblePosition startOfSelection = m_frame.selection().selection().visibleStart();
VisiblePosition endOfSelection = m_frame.selection().selection().visibleEnd();
if (startOfSelection.isNull())
return;
VisiblePosition startOfFirstWord = startOfWord(startOfSelection, LeftWordIfOnBoundary);
VisiblePosition endOfFirstWord = endOfWord(startOfSelection, LeftWordIfOnBoundary);
VisiblePosition startOfLastWord = startOfWord(endOfSelection, RightWordIfOnBoundary);
VisiblePosition endOfLastWord = endOfWord(endOfSelection, RightWordIfOnBoundary);
if (startOfFirstWord.isNull()) {
startOfFirstWord = startOfWord(startOfSelection, RightWordIfOnBoundary);
endOfFirstWord = endOfWord(startOfSelection, RightWordIfOnBoundary);
}
if (endOfLastWord.isNull()) {
startOfLastWord = startOfWord(endOfSelection, LeftWordIfOnBoundary);
endOfLastWord = endOfWord(endOfSelection, LeftWordIfOnBoundary);
}
if (doNotRemoveIfSelectionAtWordBoundary && endOfFirstWord == startOfSelection) {
startOfFirstWord = nextWordPosition(startOfFirstWord);
endOfFirstWord = endOfWord(startOfFirstWord, RightWordIfOnBoundary);
if (startOfFirstWord == endOfSelection)
return;
}
if (doNotRemoveIfSelectionAtWordBoundary && startOfLastWord == endOfSelection) {
startOfLastWord = previousWordPosition(startOfLastWord);
endOfLastWord = endOfWord(startOfLastWord, RightWordIfOnBoundary);
if (endOfLastWord == startOfSelection)
return;
}
if (startOfFirstWord.isNull() || endOfFirstWord.isNull() || startOfLastWord.isNull() || endOfLastWord.isNull())
return;
Document* document = m_frame.document();
ASSERT(document);
RefPtrWillBeRawPtr<Range> wordRange = Range::create(*document, startOfFirstWord.deepEquivalent(), endOfLastWord.deepEquivalent());
document->markers().removeMarkers(wordRange.get(), DocumentMarker::MisspellingMarkers(), DocumentMarkerController::RemovePartiallyOverlappingMarker);
}
void SpellChecker::didEndEditingOnTextField(Element* e)
{
m_spellCheckRequester->cancelCheck();
HTMLTextFormControlElement* textFormControlElement = toHTMLTextFormControlElement(e);
HTMLElement* innerText = textFormControlElement->innerTextElement();
DocumentMarker::MarkerTypes markerTypes(DocumentMarker::Spelling);
if (isGrammarCheckingEnabled() || unifiedTextCheckerEnabled())
markerTypes.add(DocumentMarker::Grammar);
for (Node* node = innerText; node; node = NodeTraversal::next(*node, innerText)) {
m_frame.document()->markers().removeMarkers(node, markerTypes);
}
}
void SpellChecker::respondToChangedSelection(const VisibleSelection& oldSelection, FrameSelection::SetSelectionOptions options)
{
bool closeTyping = options & FrameSelection::CloseTyping;
bool isContinuousSpellCheckingEnabled = this->isContinuousSpellCheckingEnabled();
bool isContinuousGrammarCheckingEnabled = isContinuousSpellCheckingEnabled && isGrammarCheckingEnabled();
if (isContinuousSpellCheckingEnabled) {
VisibleSelection newAdjacentWords;
VisibleSelection newSelectedSentence;
bool caretBrowsing = m_frame.settings() && m_frame.settings()->caretBrowsingEnabled();
if (m_frame.selection().selection().isContentEditable() || caretBrowsing) {
VisiblePosition newStart(m_frame.selection().selection().visibleStart());
newAdjacentWords = VisibleSelection(startOfWord(newStart, LeftWordIfOnBoundary), endOfWord(newStart, RightWordIfOnBoundary));
if (isContinuousGrammarCheckingEnabled)
newSelectedSentence = VisibleSelection(startOfSentence(newStart), endOfSentence(newStart));
}
bool shouldCheckSpellingAndGrammar = !(options & FrameSelection::SpellCorrectionTriggered);
if (shouldCheckSpellingAndGrammar
&& closeTyping
&& oldSelection.isContentEditable()
&& oldSelection.start().inDocument()
&& !isSelectionInTextField(oldSelection)) {
spellCheckOldSelection(oldSelection, newAdjacentWords, newSelectedSentence);
}
if (textChecker().shouldEraseMarkersAfterChangeSelection(TextCheckingTypeSpelling)) {
if (RefPtrWillBeRawPtr<Range> wordRange = newAdjacentWords.toNormalizedRange())
m_frame.document()->markers().removeMarkers(wordRange.get(), DocumentMarker::Spelling);
}
if (textChecker().shouldEraseMarkersAfterChangeSelection(TextCheckingTypeGrammar)) {
if (RefPtrWillBeRawPtr<Range> sentenceRange = newSelectedSentence.toNormalizedRange())
m_frame.document()->markers().removeMarkers(sentenceRange.get(), DocumentMarker::Grammar);
}
}
if (!isContinuousSpellCheckingEnabled)
m_frame.document()->markers().removeMarkers(DocumentMarker::Spelling);
if (!isContinuousGrammarCheckingEnabled)
m_frame.document()->markers().removeMarkers(DocumentMarker::Grammar);
}
void SpellChecker::spellCheckAfterBlur()
{
if (!m_frame.selection().selection().isContentEditable())
return;
if (isSelectionInTextField(m_frame.selection().selection())) {
return;
}
VisibleSelection empty;
spellCheckOldSelection(m_frame.selection().selection(), empty, empty);
}
void SpellChecker::spellCheckOldSelection(const VisibleSelection& oldSelection, const VisibleSelection& newAdjacentWords, const VisibleSelection& newSelectedSentence)
{
VisiblePosition oldStart(oldSelection.visibleStart());
VisibleSelection oldAdjacentWords = VisibleSelection(startOfWord(oldStart, LeftWordIfOnBoundary), endOfWord(oldStart, RightWordIfOnBoundary));
if (oldAdjacentWords != newAdjacentWords) {
if (isContinuousSpellCheckingEnabled() && isGrammarCheckingEnabled()) {
VisibleSelection selectedSentence = VisibleSelection(startOfSentence(oldStart), endOfSentence(oldStart));
markMisspellingsAndBadGrammar(oldAdjacentWords, true, selectedSentence);
} else {
markMisspellingsAndBadGrammar(oldAdjacentWords, false, oldAdjacentWords);
}
}
}
static Node* findFirstMarkable(Node* node)
{
while (node) {
if (!node->renderer())
return 0;
if (node->renderer()->isText())
return node;
if (node->renderer()->isTextControl())
node = toRenderTextControl(node->renderer())->textFormControlElement()->visiblePositionForIndex(1).deepEquivalent().deprecatedNode();
else if (node->firstChild())
node = node->firstChild();
else
node = node->nextSibling();
}
return 0;
}
bool SpellChecker::selectionStartHasMarkerFor(DocumentMarker::MarkerType markerType, int from, int length) const
{
Node* node = findFirstMarkable(m_frame.selection().start().deprecatedNode());
if (!node)
return false;
unsigned startOffset = static_cast<unsigned>(from);
unsigned endOffset = static_cast<unsigned>(from + length);
Vector<DocumentMarker*> markers = m_frame.document()->markers().markersFor(node);
for (size_t i = 0; i < markers.size(); ++i) {
DocumentMarker* marker = markers[i];
if (marker->startOffset() <= startOffset && endOffset <= marker->endOffset() && marker->type() == markerType)
return true;
}
return false;
}
TextCheckingTypeMask SpellChecker::resolveTextCheckingTypeMask(TextCheckingTypeMask textCheckingOptions)
{
bool shouldMarkSpelling = textCheckingOptions & TextCheckingTypeSpelling;
bool shouldMarkGrammar = textCheckingOptions & TextCheckingTypeGrammar;
TextCheckingTypeMask checkingTypes = 0;
if (shouldMarkSpelling)
checkingTypes |= TextCheckingTypeSpelling;
if (shouldMarkGrammar)
checkingTypes |= TextCheckingTypeGrammar;
return checkingTypes;
}
bool SpellChecker::unifiedTextCheckerEnabled() const
{
return WebCore::unifiedTextCheckerEnabled(&m_frame);
}
void SpellChecker::cancelCheck()
{
m_spellCheckRequester->cancelCheck();
}
void SpellChecker::requestTextChecking(const Element& element)
{
RefPtrWillBeRawPtr<Range> rangeToCheck = rangeOfContents(const_cast<Element*>(&element));
m_spellCheckRequester->requestCheckingFor(SpellCheckRequest::create(TextCheckingTypeSpelling | TextCheckingTypeGrammar, TextCheckingProcessBatch, rangeToCheck, rangeToCheck));
}
}