diff options
author | shinyak@chromium.org <shinyak@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-26 08:30:08 +0000 |
---|---|---|
committer | shinyak@chromium.org <shinyak@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-26 08:30:08 +0000 |
commit | 9d56027a1bd54cdf472fbc115688ebe24078e8d7 (patch) | |
tree | 2bdd277f5a69068aa1bfad154367766c3c98e020 /chrome | |
parent | a51dfe5f1b7601538cf1234b3d2b96f33a16ba35 (diff) | |
download | chromium_src-9d56027a1bd54cdf472fbc115688ebe24078e8d7.zip chromium_src-9d56027a1bd54cdf472fbc115688ebe24078e8d7.tar.gz chromium_src-9d56027a1bd54cdf472fbc115688ebe24078e8d7.tar.bz2 |
In http://trac.webkit.org/changeset/105491, WebKit introduced an interface WebSpellCheckClient::checkTextOfParagraph to spellcheck.
This patch introduces the Chromium side implementation of WebSpellCheckClient::checkTextOfParagraph.
This function is currently enabled only when USE_UNIFIED_TEXT_CHECKING is enable. It is currently enabled on Mac Snow Leopard or Lion, though this function will not be used in such ports. I will enable it in chromium port later.
BUG=106941
TEST=SpellCheckTest.SpellCheckParagraph*
Review URL: http://codereview.chromium.org/8912006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@119210 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/renderer/spellchecker/spellcheck.cc | 47 | ||||
-rw-r--r-- | chrome/renderer/spellchecker/spellcheck.h | 15 | ||||
-rw-r--r-- | chrome/renderer/spellchecker/spellcheck_provider.cc | 29 | ||||
-rw-r--r-- | chrome/renderer/spellchecker/spellcheck_provider.h | 16 | ||||
-rw-r--r-- | chrome/renderer/spellchecker/spellcheck_unittest.cc | 105 |
5 files changed, 203 insertions, 9 deletions
diff --git a/chrome/renderer/spellchecker/spellcheck.cc b/chrome/renderer/spellchecker/spellcheck.cc index fc4da90..a423dff 100644 --- a/chrome/renderer/spellchecker/spellcheck.cc +++ b/chrome/renderer/spellchecker/spellcheck.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -13,6 +13,7 @@ #include "chrome/common/spellcheck_messages.h" #include "content/public/renderer/render_thread.h" #include "third_party/hunspell/src/hunspell/hunspell.hxx" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingResult.h" using base::TimeTicks; using content::RenderThread; @@ -144,6 +145,50 @@ bool SpellCheck::SpellCheckWord( return true; } +bool SpellCheck::SpellCheckParagraph( + const string16& text, + int tag, + std::vector<WebKit::WebTextCheckingResult>* results) { +#if !defined(OS_MACOSX) + // Mac has its own spell checker, so this method will not be used. + + DCHECK(results); + + size_t length = text.length(); + size_t offset = 0; + + // Spellcheck::SpellCheckWord() automatically breaks text into words and + // checks the spellings of the extracted words. This function sets the + // position and length of the first misspelled word and returns false when + // the text includes misspelled words. Therefore, we just repeat calling the + // function until it returns true to check the whole text. + int misspelling_start = 0; + int misspelling_length = 0; + while (offset <= length) { + if (SpellCheckWord(&text[offset], + length - offset, + 0, + &misspelling_start, + &misspelling_length, + NULL)) { + return true; + } + + if (results) { + results->push_back(WebKit::WebTextCheckingResult( + WebKit::WebTextCheckingTypeSpelling, + misspelling_start + offset, + misspelling_length)); + } + offset += misspelling_start + misspelling_length; + } + + return false; +#else + return true; +#endif +} + string16 SpellCheck::GetAutoCorrectionWord(const string16& word, int tag) { string16 autocorrect_word; if (!auto_spell_correct_turned_on_) diff --git a/chrome/renderer/spellchecker/spellcheck.h b/chrome/renderer/spellchecker/spellcheck.h index c046c1b..c4c0e87 100644 --- a/chrome/renderer/spellchecker/spellcheck.h +++ b/chrome/renderer/spellchecker/spellcheck.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -25,6 +25,10 @@ namespace file_util { class MemoryMappedFile; } +namespace WebKit { +struct WebTextCheckingResult; +} + // TODO(morrita): Needs reorg with SpellCheckProvider. // See http://crbug.com/73699. class SpellCheck : public content::RenderProcessObserver { @@ -53,6 +57,15 @@ class SpellCheck : public content::RenderProcessObserver { int* misspelling_len, std::vector<string16>* optional_suggestions); + // SpellCheck a paragrpah. + // Returns true if |text| is correctly spelled, false otherwise. + // If the spellchecker failed to initialize, always returns true. + // The |tag| parameter should either be a unique identifier for the document, + // or 0. + bool SpellCheckParagraph(const string16& text, + int tag, + std::vector<WebKit::WebTextCheckingResult>* results); + // Find a possible correctly spelled word for a misspelled word. Computes an // empty string if input misspelled word is too long, there is ambiguity, or // the correct spelling cannot be determined. diff --git a/chrome/renderer/spellchecker/spellcheck_provider.cc b/chrome/renderer/spellchecker/spellcheck_provider.cc index 7a5cd76..a523d22 100644 --- a/chrome/renderer/spellchecker/spellcheck_provider.cc +++ b/chrome/renderer/spellchecker/spellcheck_provider.cc @@ -13,6 +13,7 @@ #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingCompletion.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingResult.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingType.h" #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" @@ -121,6 +122,34 @@ void SpellCheckProvider::spellCheck( } } +void SpellCheckProvider::checkTextOfParagraph( + const WebKit::WebString& text, + WebKit::WebTextCheckingTypeMask mask, + WebKit::WebVector<WebKit::WebTextCheckingResult>* results) { +#if !defined(OS_MACOSX) + // Since Mac has its own spell checker, this method will not be used on Mac. + + if (!results) + return; + + if (!(mask & WebKit::WebTextCheckingTypeSpelling)) + return; + + EnsureDocumentTag(); + + // Will be NULL during unit tets. + if (!chrome_content_renderer_client_) + return; + + std::vector<WebKit::WebTextCheckingResult> tmp_results; + chrome_content_renderer_client_->spellcheck()->SpellCheckParagraph( + string16(text), + document_tag_, + &tmp_results); + *results = tmp_results; +#endif +} + void SpellCheckProvider::requestCheckingOfText( const WebString& text, WebTextCheckingCompletion* completion) { diff --git a/chrome/renderer/spellchecker/spellcheck_provider.h b/chrome/renderer/spellchecker/spellcheck_provider.h index a785b12..ecd98ea 100644 --- a/chrome/renderer/spellchecker/spellcheck_provider.h +++ b/chrome/renderer/spellchecker/spellcheck_provider.h @@ -59,16 +59,20 @@ class SpellCheckProvider : public content::RenderViewObserver, const WebKit::WebString& text, int& offset, int& length, - WebKit::WebVector<WebKit::WebString>* optional_suggestions); + WebKit::WebVector<WebKit::WebString>* optional_suggestions) OVERRIDE; + virtual void checkTextOfParagraph( + const WebKit::WebString& text, + WebKit::WebTextCheckingTypeMask mask, + WebKit::WebVector<WebKit::WebTextCheckingResult>* results) OVERRIDE; virtual void requestCheckingOfText( const WebKit::WebString& text, - WebKit::WebTextCheckingCompletion* completion); + WebKit::WebTextCheckingCompletion* completion) OVERRIDE; virtual WebKit::WebString autoCorrectWord( - const WebKit::WebString& misspelled_word); - virtual void showSpellingUI(bool show); - virtual bool isShowingSpellingUI(); + const WebKit::WebString& misspelled_word) OVERRIDE; + virtual void showSpellingUI(bool show) OVERRIDE; + virtual bool isShowingSpellingUI() OVERRIDE; virtual void updateSpellingUIWithMisspelledWord( - const WebKit::WebString& word); + const WebKit::WebString& word) OVERRIDE; void OnAdvanceToNextMisspelling(); void OnRespondTextCheck( diff --git a/chrome/renderer/spellchecker/spellcheck_unittest.cc b/chrome/renderer/spellchecker/spellcheck_unittest.cc index 9b31383..ed06323 100644 --- a/chrome/renderer/spellchecker/spellcheck_unittest.cc +++ b/chrome/renderer/spellchecker/spellcheck_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -14,6 +14,7 @@ #include "chrome/common/chrome_paths.h" #include "chrome/common/spellcheck_common.h" #include "testing/gtest/include/gtest/gtest.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingResult.h" namespace { @@ -52,6 +53,26 @@ class SpellCheckTest : public testing::Test { SpellCheck* spell_check() { return spell_check_.get(); } +#if !defined(OS_MACOSX) + protected: + void TestSpellCheckParagraph( + const string16& input, + const std::vector<WebKit::WebTextCheckingResult>& expected) { + std::vector<WebKit::WebTextCheckingResult> results; + spell_check()->SpellCheckParagraph(input, + 0, + &results); + + EXPECT_EQ(results.size(), expected.size()); + size_t size = std::min(results.size(), expected.size()); + for (size_t j = 0; j < size; ++j) { + EXPECT_EQ(results[j].type, WebKit::WebTextCheckingTypeSpelling); + EXPECT_EQ(results[j].location, expected[j].location); + EXPECT_EQ(results[j].length, expected[j].length); + } + } +#endif + private: scoped_ptr<SpellCheck> spell_check_; }; @@ -705,3 +726,85 @@ TEST_F(SpellCheckTest, GetAutoCorrectionWord_EN_US) { EXPECT_EQ(expected_autocorrect_word, autocorrect_word); } } + +// Since SpellCheck::SpellCheckParagraph is not implemented on Mac, +// we skip these SpellCheckParagraph tests on Mac. +#if !defined(OS_MACOSX) + +// Make sure SpellCheckParagraph does not crash if the input is empty. +TEST_F(SpellCheckTest, SpellCheckParagraphEmptyParagraph) { + std::vector<WebKit::WebTextCheckingResult> expected; + TestSpellCheckParagraph(UTF8ToUTF16(""), expected); +} + +// A simple test case having no misspellings. +TEST_F(SpellCheckTest, SpellCheckParagraphNoMisspellings) { + const string16 text = UTF8ToUTF16("apple"); + std::vector<WebKit::WebTextCheckingResult> expected; + TestSpellCheckParagraph(text, expected); +} + +// A simple test case having one misspelling. +TEST_F(SpellCheckTest, SpellCheckParagraphSingleMisspellings) { + const string16 text = UTF8ToUTF16("zz"); + std::vector<WebKit::WebTextCheckingResult> expected; + expected.push_back(WebKit::WebTextCheckingResult( + WebKit::WebTextCheckingTypeSpelling, 0, 2)); + + TestSpellCheckParagraph(text, expected); +} + +// A simple test case having multiple misspellings. +TEST_F(SpellCheckTest, SpellCheckParagraphMultipleMisspellings) { + const string16 text = UTF8ToUTF16("zz, zz"); + std::vector<WebKit::WebTextCheckingResult> expected; + expected.push_back(WebKit::WebTextCheckingResult( + WebKit::WebTextCheckingTypeSpelling, 0, 2)); + expected.push_back(WebKit::WebTextCheckingResult( + WebKit::WebTextCheckingTypeSpelling, 4, 2)); + + TestSpellCheckParagraph(text, expected); +} + +// Make sure a relatively long (correct) sentence can be spellchecked. +TEST_F(SpellCheckTest, SpellCheckParagraphLongSentence) { + std::vector<WebKit::WebTextCheckingResult> expected; + // The text is taken from US constitution preamble. + const string16 text = UTF8ToUTF16( + "We the people of the United States, in order to form a more perfect " + "union, establish justice, insure domestic tranquility, provide for " + "the common defense, promote the general welfare, and secure the " + "blessings of liberty to ourselves and our posterity, do ordain and " + "establish this Constitution for the United States of America."); + + TestSpellCheckParagraph(text, expected); +} + +// Make sure all misspellings can be found in a relatively long sentence. +TEST_F(SpellCheckTest, SpellCheckParagraphLongSentenceMultipleMisspellings) { + std::vector<WebKit::WebTextCheckingResult> expected; + + // All 'the' are converted to 'hte' in US consitition preamble. + const string16 text = UTF8ToUTF16( + "We hte people of hte United States, in order to form a more perfect " + "union, establish justice, insure domestic tranquility, provide for " + "hte common defense, promote hte general welfare, and secure hte " + "blessings of liberty to ourselves and our posterity, do ordain and " + "establish this Constitution for hte United States of America."); + + expected.push_back(WebKit::WebTextCheckingResult( + WebKit::WebTextCheckingTypeSpelling, 3, 3)); + expected.push_back(WebKit::WebTextCheckingResult( + WebKit::WebTextCheckingTypeSpelling, 17, 3)); + expected.push_back(WebKit::WebTextCheckingResult( + WebKit::WebTextCheckingTypeSpelling, 135, 3)); + expected.push_back(WebKit::WebTextCheckingResult( + WebKit::WebTextCheckingTypeSpelling, 163, 3)); + expected.push_back(WebKit::WebTextCheckingResult( + WebKit::WebTextCheckingTypeSpelling, 195, 3)); + expected.push_back(WebKit::WebTextCheckingResult( + WebKit::WebTextCheckingTypeSpelling, 298, 3)); + + TestSpellCheckParagraph(text, expected); +} +#endif |