_SpellCheckParser.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.editor.plugins._SpellCheckParser"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.editor.plugins._SpellCheckParser"] = true;
  8. dojo.provide("dojox.editor.plugins._SpellCheckParser");
  9. dojo.declare("dojox.editor.plugins._SpellCheckParser", null, {
  10. lang: "english",
  11. parseIntoWords: function(/*String*/ text){
  12. // summary:
  13. // Parse the text into words
  14. // text:
  15. // Plain text without html tags
  16. // tags:
  17. // public
  18. // returns:
  19. // Array holding all the words
  20. function isCharExt(c){
  21. var ch = c.charCodeAt(0);
  22. return 48 <= ch && ch <= 57 || 65 <= ch && ch <= 90 || 97 <= ch && ch <= 122;
  23. }
  24. var words = this.words = [],
  25. indices = this.indices = [],
  26. index = 0,
  27. length = text && text.length,
  28. start = 0;
  29. while(index < length){
  30. var ch;
  31. // Skip the white charactor and need to treat HTML entity respectively
  32. while(index < length && !isCharExt(ch = text.charAt(index)) && ch != "&"){ index++; }
  33. if(ch == "&"){ // An HTML entity, skip it
  34. while(++index < length && (ch = text.charAt(index)) != ";" && isCharExt(ch)){}
  35. }else{ // A word
  36. start = index;
  37. while(++index < length && isCharExt(text.charAt(index))){}
  38. if(start < length){
  39. words.push(text.substring(start, index));
  40. indices.push(start);
  41. }
  42. }
  43. }
  44. return words;
  45. },
  46. getIndices: function(){
  47. // summary:
  48. // Get the indices of the words. They are in one-to-one correspondence
  49. // tags:
  50. // public
  51. // returns:
  52. // Index array
  53. return this.indices;
  54. }
  55. });
  56. // Register this parser in the SpellCheck plugin.
  57. dojo.subscribe(dijit._scopeName + ".Editor.plugin.SpellCheck.getParser", null, function(sp){
  58. if(sp.parser){ return; }
  59. sp.parser = new dojox.editor.plugins._SpellCheckParser();
  60. });
  61. }