mozilla

Revision 81219 of Using spell checking in XUL

  • Revision slug: Using_spell_checking_in_XUL
  • Revision title: Using spell checking in XUL
  • Revision id: 81219
  • Created:
  • Creator: Danbeck
  • Is current revision? No
  • Comment Initial Creation - Feel free to flesh this out more

Revision Content

Getting Started

The new spell checking functionality requires Firefox version 2.0 or later and this document describes how to use the mozISpellCheckingEngine component to add spell check capabilities to your Firefox extension.

See Controlling_spell_checking_in_HTML_forms for details on adding spellcheck support to HTML forms for your website.

Check the spelling of a word

To check the spelling of a word, you must first create an interface to the mozISpellCheckingEngine component using <tt>@mozilla.org/spellchecker/myspell;1</tt> as the contract ID, and then call the <tt>check()</tt> method on the string you wish to test. This method returns true if the word is correctly spelled, or false if 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 pass the <tt>suggest()</tt> method 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

<h3 name="Getting_Started">Getting Started</h3>
<p>The new spell checking functionality requires Firefox version 2.0 or later and this document describes how to use the <a class="external" href="http://www.xulplanet.com/references/xpcomref/ifaces/mozISpellCheckingEngine.html">mozISpellCheckingEngine</a> component to add spell check 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 spellcheck support to HTML forms for your website.
</p>
<h4 name="Check_the_spelling_of_a_word">Check the spelling of a word</h4>
<p>To check the spelling of a word, you must first create an interface to the <a class="external" href="http://www.xulplanet.com/references/xpcomref/ifaces/mozISpellCheckingEngine.html">mozISpellCheckingEngine</a> component using <tt>@mozilla.org/spellchecker/myspell;1</tt> as the contract ID, and then call the <tt>check()</tt> method on the string you wish to test.  This method returns true if the word is correctly spelled, or false if 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>
<h4 name="Getting_a_list_of_suggestions">Getting a list of suggestions</h4>
<p>To get a list of suggestions for a misspelled word, you pass the <tt>suggest()</tt> method 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