Re: SpellCheck API?

2011/5/9 Hironori Bono (坊野 博典) <hbono@google.com>:
> function CheckTextOfNode(node) {
>   // Remove all the previous spellchecking results.
>   window.spellCheckController.removeMarkers(node);
>
>   // Check the text in the specified node.
>   var result = CheckText(node.innerText ? node.innerText : node.value);
>  for (var i = 0; i < result.length; i++) {
>    // Add a misspelled underline and suggestions to the specified word.
>     window.spellCheckController.addMarker(
>         node, result[i].start, result[i].length, result[i].suggestions);
>   }
> }
>
> . . .
>
> function CheckTextOfNode(node) {
>  // Reset all the previous spellcheck results.
>  Window.spellCheckController.removeMarkers(node);
>
>   // Check the text with our custom spellchecker.
>   var result = CheckText(node.innerText ? node.innerText : node.value);
>  for (var i = 0; i < result.length; i++) {
>    // Use the intergrated spellchecker to check a misspelled word.
>     if (!window.spellCheckController.checkWord(result.word)) {
>      result[i].suggestions.concat(
>           window.spellCheckController.getSuggestionsForWord(result.text));
>       window.spellCheckController.addMarker(
>           node, result[i].start, result[i].length, result[i].suggestions);
>     }
>   }
> }

It would be much simpler for authors if the UA just fired an event
every time it did a spellcheck.  The event might work like this:

* Every time the UA would normally invoke its spellchecker on a word,
it fires a spellcheck event at the element in question, which bubbles
(so authors can set a handler on the body if they like).  This has to
occur when a spellcheckable element first loads, if an element becomes
spellcheckable when it wasn't before, or whenever the user modifies a
spellcheckable element such that the spellchecker would normally fire
(e.g., when they finish typing a word).
* The event object should provide the text of the word whose spelling
needs to be checked.  It should give the node and start/end offsets,
either of the input/textarea or the text node.  (Not sure what should
happen for a misspelled word that's not all in one text node.)
* The event object should have a member variable that the script can
assign a list of suggestions to, and other members to specify what
behavior the script wants: mark the word as spelled correctly, mark it
as misspelled with only the provided suggestions, mark it as
misspelled with the provided suggestions plus the UA's suggestions, or
let the UA make the decision.
* Nothing should expose what the built-in spellchecker's decision was,
neither whether it was misspelled nor the list of suggestions.
* If authors want to re-spellcheck something that's already been
spellchecked, no special function is needed.  They should set
spellcheck="false" on the element and then restore spellcheck="true".

This means authors wouldn't have to do word-breaking themselves, which
is a big advantage, since word-breaking can be very complicated.  It
would be *much* simpler to just plug in a spell-checker, without
having to write a lot of scaffolding code to track what text the user
is entering.  The only downside I can see is that it does force you to
use the browser's word-breaking behavior, but I don't think that's a
big disadvantage compared to the advantages.

Here's some sample code for how to do roughly the same thing as your
sample code:

function CheckTextOfNode(node) {
  node.onspellcheck = function(event) {
    // Set suggestions
    event.suggestions = MySpellCheckFunction(event.word);
    // If there were no suggestions, the word is spelled right.  I
want to totally ignore the built-in spellchecker.
    // By default, event.misspelled might be null, which means "mark
it as misspelled only if the built-in
    // spellchecker thinks it's misspelled".  True or false overrides
the built-in spellchecker.
    event.misspelled = event.suggestions.length > 0;
    // I don't want the built-in suggestions to combine with mine.
    event.combineSuggestions = false;
  }
  node.spellcheck = false;
  node.spellcheck = true;
}

But this doesn't even capture how much simpler it is, because your
CheckText() function is what would have to contain all the
word-breaking logic, which isn't needed here.  MySpellCheckFunction()
just needs to take a word as input and return a list of suggestions as
output, that's it.

Received on Monday, 9 May 2011 19:40:19 UTC