Refactoring: SpellChecker::requestCheckingFor should take Range instead of Node.

https://bugs.webkit.org/show_bug.cgi?id=72847

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

Covered by existing test.

* editing/Editor.cpp:
(WebCore::Editor::replaceSelectionWithFragment):
  Passes Range to requestCheckingFor instead of Node.
* editing/SpellChecker.cpp:
  Changed argument type from Node to Range.
  The corresponding changes are also done in dependent methods.
(WebCore::SpellChecker::initRequest):
(WebCore::SpellChecker::clearRequest):
(WebCore::SpellChecker::canCheckAsynchronously):
(WebCore::SpellChecker::isBusy):
(WebCore::SpellChecker::isValid):
(WebCore::SpellChecker::isCheckable):
(WebCore::SpellChecker::requestCheckingFor):
  Changed argument type from Node to Range.
(WebCore::SpellChecker::doRequestCheckingFor):
(WebCore::SpellChecker::didCheck):
* editing/SpellChecker.h:

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@100890 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent fc2c6f38
2011-11-21 Shinya Kawanaka <shinyak@google.com>
Refactoring: SpellChecker::requestCheckingFor should take Range instead of Node.
https://bugs.webkit.org/show_bug.cgi?id=72847
Reviewed by Hajime Morita.
Covered by existing test.
* editing/Editor.cpp:
(WebCore::Editor::replaceSelectionWithFragment):
Passes Range to requestCheckingFor instead of Node.
* editing/SpellChecker.cpp:
Changed argument type from Node to Range.
The corresponding changes are also done in dependent methods.
(WebCore::SpellChecker::initRequest):
(WebCore::SpellChecker::clearRequest):
(WebCore::SpellChecker::canCheckAsynchronously):
(WebCore::SpellChecker::isBusy):
(WebCore::SpellChecker::isValid):
(WebCore::SpellChecker::isCheckable):
(WebCore::SpellChecker::requestCheckingFor):
Changed argument type from Node to Range.
(WebCore::SpellChecker::doRequestCheckingFor):
(WebCore::SpellChecker::didCheck):
* editing/SpellChecker.h:
2011-11-20 Kenichi Ishibashi <bashi@chromium.org>
[Chromium] Remove old getFontFamilyForCharacters() and familyForChars() APIs.
......@@ -413,8 +413,11 @@ void Editor::replaceSelectionWithFragment(PassRefPtr<DocumentFragment> fragment,
revealSelectionAfterEditingOperation();
Node* nodeToCheck = m_frame->selection()->rootEditableElement();
if (m_spellChecker->canCheckAsynchronously(nodeToCheck))
m_spellChecker->requestCheckingFor(resolveTextCheckingTypeMask(TextCheckingTypeSpelling | TextCheckingTypeGrammar), nodeToCheck);
if (!nodeToCheck)
return;
m_spellChecker->requestCheckingFor(resolveTextCheckingTypeMask(TextCheckingTypeSpelling | TextCheckingTypeGrammar),
Range::create(m_frame->document(), firstPositionInNode(nodeToCheck), lastPositionInNode(nodeToCheck)));
}
void Editor::replaceSelectionWithText(const String& text, bool selectReplacement, bool smartReplace)
......
......@@ -39,6 +39,7 @@
#include "RenderObject.h"
#include "Settings.h"
#include "TextCheckerClient.h"
#include "TextCheckingHelper.h"
#include "TextIterator.h"
#include "htmlediting.h"
......@@ -62,15 +63,15 @@ TextCheckerClient* SpellChecker::client() const
return page->editorClient()->textChecker();
}
bool SpellChecker::initRequest(Node* node)
bool SpellChecker::initRequest(PassRefPtr<Range> range)
{
ASSERT(canCheckAsynchronously(node));
ASSERT(canCheckAsynchronously(range.get()));
String text = node->textContent();
String text = range->text();
if (!text.length())
return false;
m_requestNode = node;
m_requestRange = range;
m_requestText = text;
m_requestSequence++;
......@@ -79,7 +80,7 @@ bool SpellChecker::initRequest(Node* node)
void SpellChecker::clearRequest()
{
m_requestNode.clear();
m_requestRange.clear();
m_requestText = String();
}
......@@ -88,31 +89,39 @@ bool SpellChecker::isAsynchronousEnabled() const
return m_frame->settings() && m_frame->settings()->asynchronousSpellCheckingEnabled();
}
bool SpellChecker::canCheckAsynchronously(Node* node) const
bool SpellChecker::canCheckAsynchronously(Range* range) const
{
return client() && isCheckable(node) && isAsynchronousEnabled() && !isBusy();
return client() && isCheckable(range) && isAsynchronousEnabled() && !isBusy();
}
bool SpellChecker::isBusy() const
{
return m_requestNode.get();
return m_requestRange.get();
}
bool SpellChecker::isValid(int sequence) const
{
return m_requestNode.get() && m_requestText.length() && m_requestSequence == sequence;
return m_requestRange.get() && m_requestText.length() && m_requestSequence == sequence;
}
bool SpellChecker::isCheckable(Node* node) const
bool SpellChecker::isCheckable(Range* range) const
{
return node && node->renderer();
return range && range->firstNode() && range->firstNode()->renderer();
}
void SpellChecker::requestCheckingFor(TextCheckingTypeMask mask, Node* node)
void SpellChecker::requestCheckingFor(TextCheckingTypeMask mask, PassRefPtr<Range> range)
{
ASSERT(canCheckAsynchronously(node));
if (!canCheckAsynchronously(range.get()))
return;
doRequestCheckingFor(mask, range);
}
void SpellChecker::doRequestCheckingFor(TextCheckingTypeMask mask, PassRefPtr<Range> range)
{
ASSERT(canCheckAsynchronously(range.get()));
if (!initRequest(node))
if (!initRequest(range))
return;
client()->requestCheckingOfString(this, m_requestSequence, mask, m_requestText);
}
......@@ -153,13 +162,13 @@ void SpellChecker::didCheck(int sequence, const Vector<TextCheckingResult>& resu
if (!isValid(sequence))
return;
if (!m_requestNode->renderer()) {
if (!isCheckable(m_requestRange.get())) {
clearRequest();
return;
}
int startOffset = 0;
PositionIterator start = firstPositionInOrBeforeNode(m_requestNode.get());
PositionIterator start = m_requestRange->startPosition();
for (size_t i = 0; i < results.size(); ++i) {
if (results[i].type != TextCheckingTypeSpelling && results[i].type != TextCheckingTypeGrammar)
continue;
......@@ -177,12 +186,12 @@ void SpellChecker::didCheck(int sequence, const Vector<TextCheckingResult>& resu
// spellings in the background. To avoid adding markers to the words modified by users or
// JavaScript applications, retrieve the words in the specified region and compare them with
// the original ones.
RefPtr<Range> range = Range::create(m_requestNode->document(), start, end);
RefPtr<Range> range = Range::create(m_requestRange->ownerDocument(), start, end);
// FIXME: Use textContent() compatible string conversion.
String destination = range->text();
String source = m_requestText.substring(results[i].location, results[i].length);
if (destination == source)
m_requestNode->document()->markers()->addMarker(range.get(), toMarkerType(results[i].type));
m_requestRange->ownerDocument()->markers()->addMarker(range.get(), toMarkerType(results[i].type));
startOffset = results[i].location;
}
......
......@@ -38,6 +38,7 @@ class Frame;
class Node;
class TextCheckerClient;
struct TextCheckingResult;
class Range;
class SpellChecker {
WTF_MAKE_NONCOPYABLE(SpellChecker);
......@@ -46,21 +47,22 @@ public:
~SpellChecker();
bool isAsynchronousEnabled() const;
bool canCheckAsynchronously(Node*) const;
bool canCheckAsynchronously(Range*) const;
bool isBusy() const;
bool isValid(int sequence) const;
bool isCheckable(Node*) const;
void requestCheckingFor(TextCheckingTypeMask, Node*);
bool isCheckable(Range*) const;
void requestCheckingFor(TextCheckingTypeMask, PassRefPtr<Range>);
void didCheck(int sequence, const Vector<TextCheckingResult>&);
private:
bool initRequest(Node*);
bool initRequest(PassRefPtr<Range>);
void clearRequest();
void doRequestCheckingFor(TextCheckingTypeMask, PassRefPtr<Range>);
TextCheckerClient* client() const;
Frame* m_frame;
RefPtr<Node> m_requestNode;
RefPtr<Range> m_requestRange;
String m_requestText;
int m_requestSequence;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment