summaryrefslogtreecommitdiff
path: root/Source/WebKit/gtk/webkit
diff options
context:
space:
mode:
authorSimon Hausmann <simon.hausmann@nokia.com>2012-07-30 11:37:48 +0200
committerSimon Hausmann <simon.hausmann@nokia.com>2012-07-30 11:38:52 +0200
commit89e2486a48b739f8d771d69ede5a6a1b244a10fc (patch)
tree503b1a7812cf97d93704c32437eb5f62dc1a1ff9 /Source/WebKit/gtk/webkit
parent625f028249cb37c55bbbd153f3902afd0b0756d9 (diff)
downloadqtwebkit-89e2486a48b739f8d771d69ede5a6a1b244a10fc.tar.gz
Imported WebKit commit 0282df8ca7c11d8c8a66ea18543695c69f545a27 (http://svn.webkit.org/repository/webkit/trunk@124002)
New snapshot with prospective Mountain Lion build fix
Diffstat (limited to 'Source/WebKit/gtk/webkit')
-rw-r--r--Source/WebKit/gtk/webkit/webkitspellcheckerenchant.cpp182
1 files changed, 21 insertions, 161 deletions
diff --git a/Source/WebKit/gtk/webkit/webkitspellcheckerenchant.cpp b/Source/WebKit/gtk/webkit/webkitspellcheckerenchant.cpp
index 0eb4986d1..0a226928b 100644
--- a/Source/WebKit/gtk/webkit/webkitspellcheckerenchant.cpp
+++ b/Source/WebKit/gtk/webkit/webkitspellcheckerenchant.cpp
@@ -21,12 +21,15 @@
#if ENABLE(SPELLCHECK)
+#include "TextCheckerEnchant.h"
#include "webkitspellchecker.h"
-#include <enchant.h>
#include <gtk/gtk.h>
+#include <wtf/OwnPtr.h>
#include <wtf/gobject/GOwnPtr.h>
#include <wtf/text/CString.h>
+using namespace WebCore;
+
/**
* SECTION:webkitspellcheckerenchant
* @short_description: the default spell checking implementation for WebKitGTK+.
@@ -35,10 +38,9 @@
* WebKitGTK+. It uses the Enchant dictionaries installed on the system to
* correct spelling.
*/
-static EnchantBroker* broker = 0;
struct _WebKitSpellCheckerEnchantPrivate {
- GSList* enchantDicts;
+ OwnPtr<TextCheckerEnchant> textCheckerEnchant;
};
static void webkit_spell_checker_enchant_spell_checker_interface_init(WebKitSpellCheckerInterface* interface);
@@ -47,35 +49,16 @@ G_DEFINE_TYPE_WITH_CODE(WebKitSpellCheckerEnchant, webkit_spell_checker_enchant,
G_IMPLEMENT_INTERFACE(WEBKIT_TYPE_SPELL_CHECKER,
webkit_spell_checker_enchant_spell_checker_interface_init))
-static void createEnchantBrokerIfNeeded()
-{
- if (!broker)
- broker = enchant_broker_init();
-}
-
-static void freeSpellCheckingLanguage(gpointer data, gpointer)
-{
- createEnchantBrokerIfNeeded();
-
- enchant_broker_free_dict(broker, static_cast<EnchantDict*>(data));
-}
-
static void webkit_spell_checker_enchant_finalize(GObject* object)
{
WebKitSpellCheckerEnchantPrivate* priv = WEBKIT_SPELL_CHECKER_ENCHANT(object)->priv;
-
- g_slist_foreach(priv->enchantDicts, freeSpellCheckingLanguage, 0);
- g_slist_free(priv->enchantDicts);
-
- WEBKIT_SPELL_CHECKER_ENCHANT(object)->priv->~WebKitSpellCheckerEnchantPrivate();
+ priv->~WebKitSpellCheckerEnchantPrivate();
}
static void webkit_spell_checker_enchant_class_init(WebKitSpellCheckerEnchantClass* klass)
{
GObjectClass* objectClass = G_OBJECT_CLASS(klass);
-
objectClass->finalize = webkit_spell_checker_enchant_finalize;
-
g_type_class_add_private(klass, sizeof(WebKitSpellCheckerEnchantPrivate));
}
@@ -85,149 +68,38 @@ static void webkit_spell_checker_enchant_init(WebKitSpellCheckerEnchant* checker
checker->priv = priv;
new (priv) WebKitSpellCheckerEnchantPrivate();
- priv->enchantDicts = 0;
-}
-
-static bool wordEndIsAContractionApostrophe(const char* string, long offset)
-{
- if (g_utf8_get_char(g_utf8_offset_to_pointer(string, offset)) != g_utf8_get_char("'"))
- return false;
-
- // If this is the last character in the string, it cannot be the apostrophe part of a contraction.
- if (offset == g_utf8_strlen(string, -1))
- return false;
-
- return g_unichar_isalpha(g_utf8_get_char(g_utf8_offset_to_pointer(string, offset + 1)));
+ priv->textCheckerEnchant = TextCheckerEnchant::create();
}
static void checkSpellingOfString(WebKitSpellChecker* checker, const char* string, int* misspellingLocation, int* misspellingLength)
{
WebKitSpellCheckerEnchantPrivate* priv = WEBKIT_SPELL_CHECKER_ENCHANT(checker)->priv;
-
- GSList* dicts = priv->enchantDicts;
- if (!dicts)
- return;
-
- int length = g_utf8_strlen(string, -1);
-
- PangoLanguage* language(pango_language_get_default());
- GOwnPtr<PangoLogAttr> attrs(g_new(PangoLogAttr, length + 1));
-
- // pango_get_log_attrs uses an aditional position at the end of the text.
- pango_get_log_attrs(string, -1, -1, language, attrs.get(), length + 1);
-
- for (int i = 0; i < length + 1; i++) {
- // We go through each character until we find an is_word_start,
- // then we get into an inner loop to find the is_word_end corresponding
- // to it.
- if (attrs.get()[i].is_word_start) {
- int start = i;
- int end = i;
- int wordLength;
-
- while (attrs.get()[end].is_word_end < 1 || wordEndIsAContractionApostrophe(string, end))
- end++;
-
- wordLength = end - start;
- // Set the iterator to be at the current word end, so we don't
- // check characters twice.
- i = end;
-
- gchar* cstart = g_utf8_offset_to_pointer(string, start);
- gint bytes = static_cast<gint>(g_utf8_offset_to_pointer(string, end) - cstart);
- GOwnPtr<gchar> word(g_new0(gchar, bytes + 1));
-
- g_utf8_strncpy(word.get(), cstart, wordLength);
-
- for (; dicts; dicts = dicts->next) {
- EnchantDict* dict = static_cast<EnchantDict*>(dicts->data);
- if (enchant_dict_check(dict, word.get(), wordLength)) {
- *misspellingLocation = start;
- *misspellingLength = wordLength;
- } else {
- // Stop checking, this word is ok in at least one dict.
- *misspellingLocation = -1;
- *misspellingLength = 0;
- break;
- }
- }
- }
- }
+ priv->textCheckerEnchant->checkSpellingOfString(String(string), *misspellingLocation, *misspellingLength);
}
static char** getGuessesForWord(WebKitSpellChecker* checker, const char* word, const char* context)
{
WebKitSpellCheckerEnchantPrivate* priv = WEBKIT_SPELL_CHECKER_ENCHANT(checker)->priv;
- GSList* dicts = priv->enchantDicts;
- char** guesses = 0;
+ Vector<String> guesses = priv->textCheckerEnchant->getGuessesForWord(String(word));
- for (; dicts; dicts = dicts->next) {
- size_t numberOfSuggestions;
- size_t i;
+ if (guesses.isEmpty())
+ return 0;
- EnchantDict* dict = static_cast<EnchantDict*>(dicts->data);
- gchar** suggestions = enchant_dict_suggest(dict, word, -1, &numberOfSuggestions);
+ int i = 0;
+ int numberOfGuesses = guesses.size();
+ char** guessesArray = static_cast<char**>(g_malloc0((numberOfGuesses + 1) * sizeof(char*)));
+ for (Vector<String>::const_iterator iter = guesses.begin(); iter != guesses.end(); ++iter)
+ guessesArray[i++] = g_strdup(iter->utf8().data());
+ guessesArray[i] = 0;
- if (numberOfSuggestions > 0) {
- if (numberOfSuggestions > 10)
- numberOfSuggestions = 10;
-
- guesses = static_cast<char**>(g_malloc0((numberOfSuggestions + 1) * sizeof(char*)));
- for (i = 0; i < numberOfSuggestions && i < 10; i++)
- guesses[i] = g_strdup(suggestions[i]);
-
- guesses[i] = 0;
-
- enchant_dict_free_suggestions(dict, suggestions);
- }
- }
-
- return guesses;
-}
-
-static void getAvailableDictionariesCallback(const char* const languageTag, const char* const, const char* const, const char* const, void* data)
-{
- Vector<CString>* dicts = static_cast<Vector<CString>*>(data);
-
- dicts->append(languageTag);
+ return guessesArray;
}
static void updateSpellCheckingLanguages(WebKitSpellChecker* checker, const char* languages)
{
- GSList* spellDictionaries = 0;
-
WebKitSpellCheckerEnchantPrivate* priv = WEBKIT_SPELL_CHECKER_ENCHANT(checker)->priv;
-
- createEnchantBrokerIfNeeded();
-
- if (languages) {
- char** langs = g_strsplit(languages, ",", -1);
- for (int i = 0; langs[i]; i++) {
- if (enchant_broker_dict_exists(broker, langs[i])) {
- EnchantDict* dict = enchant_broker_request_dict(broker, langs[i]);
- spellDictionaries = g_slist_append(spellDictionaries, dict);
- }
- }
- g_strfreev(langs);
- } else {
- const char* language = pango_language_to_string(gtk_get_default_language());
- if (enchant_broker_dict_exists(broker, language)) {
- EnchantDict* dict = enchant_broker_request_dict(broker, language);
- spellDictionaries = g_slist_append(spellDictionaries, dict);
- } else {
- // No dictionaries selected, we get one from the list.
- Vector<CString> allDictionaries;
- enchant_broker_list_dicts(broker, getAvailableDictionariesCallback, &allDictionaries);
- if (!allDictionaries.isEmpty()) {
- EnchantDict* dict = enchant_broker_request_dict(broker, allDictionaries[0].data());
- spellDictionaries = g_slist_append(spellDictionaries, dict);
- }
- }
- }
- g_slist_foreach(priv->enchantDicts, freeSpellCheckingLanguage, 0);
- g_slist_free(priv->enchantDicts);
- priv->enchantDicts = spellDictionaries;
+ priv->textCheckerEnchant->updateSpellCheckingLanguages(String(languages));
}
static char* getAutocorrectSuggestionsForMisspelledWord(WebKitSpellChecker* checker, const char* word)
@@ -238,25 +110,13 @@ static char* getAutocorrectSuggestionsForMisspelledWord(WebKitSpellChecker* chec
static void learnWord(WebKitSpellChecker* checker, const char* word)
{
WebKitSpellCheckerEnchantPrivate* priv = WEBKIT_SPELL_CHECKER_ENCHANT(checker)->priv;
- GSList* dicts = priv->enchantDicts;
-
- for (; dicts; dicts = dicts->next) {
- EnchantDict* dict = static_cast<EnchantDict*>(dicts->data);
-
- enchant_dict_add_to_personal(dict, word, -1);
- }
+ priv->textCheckerEnchant->learnWord(String(word));
}
static void ignoreWord(WebKitSpellChecker* checker, const char* word)
{
WebKitSpellCheckerEnchantPrivate* priv = WEBKIT_SPELL_CHECKER_ENCHANT(checker)->priv;
- GSList* dicts = priv->enchantDicts;
-
- for (; dicts; dicts = dicts->next) {
- EnchantDict* dict = static_cast<EnchantDict*>(dicts->data);
-
- enchant_dict_add_to_session(dict, word, -1);
- }
+ priv->textCheckerEnchant->ignoreWord(String(word));
}
static void webkit_spell_checker_enchant_spell_checker_interface_init(WebKitSpellCheckerInterface* interface)