Using spell checking in XUL

  • Revision slug: Using_spell_checking_in_XUL
  • Revision title: Using spell checking in XUL
  • Revision id: 81222
  • Created:
  • Creator: Nickolay
  • Is current revision? No
  • Comment

Revision Content

The new spell checking functionality is available in Firefox 2. This document describes how to use the mozISpellCheckingEngine component to add spell checking capabilities to your Firefox extension.

See Controlling spell checking in HTML forms for details on adding spell check support to HTML forms for your website.

Checking the spelling of a word

To check the spelling of a word, you must first create an interface to the mozISpellCheckingEngine component using @mozilla.org/spellchecker/myspell;1 as the contract ID, and then call the check() method on the string you wish to test. This method returns true if the word is correctly spelled, or false if it's not.

gSpellCheckEngine = Components.classes["@mozilla.org/spellchecker/myspell;1"]
                    .getService(Components.interfaces.mozISpellCheckingEngine); 
gSpellCheckEngine.dictionary = 'en-US';

if (gSpellCheckEngine.check("kat") {
    // It's spelled correctly    
} else {
    // It's spelled incorrectly
}

Getting a list of suggestions

To get a list of suggestions for a misspelled word, you call the suggest() method, specifying the word and an object to be filled with an array of possible suggestions.

var suggestions = {};
gSpellCheckEngine.suggest("kat", suggestions); 

if (suggestions.value) {
   // suggestions.value is a JavaScript Array of strings
   // there were suggestions.value.length suggestions found
}

Revision Source

<p>The new spell checking functionality is available in <a href="en/Firefox_2">Firefox 2</a>. This document describes how to use the <code><a class="external" href="http://www.xulplanet.com/references/xpcomref/ifaces/mozISpellCheckingEngine.html">mozISpellCheckingEngine</a></code> component to add spell checking capabilities to your Firefox extension.  
</p><p>See <a href="en/Controlling_spell_checking_in_HTML_forms">Controlling spell checking in HTML forms</a> for details on adding spell check support to HTML forms for your website.
</p>
<h2 name="Checking_the_spelling_of_a_word">Checking the spelling of a word</h2>
<p>To check the spelling of a word, you must first create an interface to the <code><a class="external" href="http://www.xulplanet.com/references/xpcomref/ifaces/mozISpellCheckingEngine.html">mozISpellCheckingEngine</a></code> component using <code>@mozilla.org/spellchecker/myspell;1</code> as the contract ID, and then call the <code>check()</code> method on the string you wish to test.  This method returns <code>true</code> if the word is correctly spelled, or <code>false</code> if it's not.
</p>
<pre>gSpellCheckEngine = Components.classes["@mozilla.org/spellchecker/myspell;1"]
                    .getService(Components.interfaces.mozISpellCheckingEngine); 
gSpellCheckEngine.dictionary = 'en-US';

if (gSpellCheckEngine.check("kat") {
    // It's spelled correctly    
} else {
    // It's spelled incorrectly
}
</pre>
<h2 name="Getting_a_list_of_suggestions">Getting a list of suggestions</h2>
<p>To get a list of suggestions for a misspelled word, you call the <code>suggest()</code> method, specifying the word and an object to be filled with an array of possible suggestions.
</p>
<pre class="eval">var suggestions = {};
gSpellCheckEngine.suggest("kat", suggestions); 

if (suggestions.value) {
   // suggestions.value is a JavaScript <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array">Array</a> of strings
   // there were suggestions.value.length suggestions found
}
</pre>
Revert to this revision