_SpellCheckParser.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. define("dojox/editor/plugins/_SpellCheckParser", [
  2. "dojo",
  3. "dojox",
  4. "dojo/_base/connect",
  5. "dojo/_base/declare"
  6. ], function(dojo, dojox) {
  7. dojo.declare("dojox.editor.plugins._SpellCheckParser", null, {
  8. lang: "english",
  9. parseIntoWords: function(/*String*/ text){
  10. // summary:
  11. // Parse the text into words
  12. // text:
  13. // Plain text without html tags
  14. // tags:
  15. // public
  16. // returns:
  17. // Array holding all the words
  18. function isCharExt(c){
  19. var ch = c.charCodeAt(0);
  20. return 48 <= ch && ch <= 57 || 65 <= ch && ch <= 90 || 97 <= ch && ch <= 122;
  21. }
  22. var words = this.words = [],
  23. indices = this.indices = [],
  24. index = 0,
  25. length = text && text.length,
  26. start = 0;
  27. while(index < length){
  28. var ch;
  29. // Skip the white charactor and need to treat HTML entity respectively
  30. while(index < length && !isCharExt(ch = text.charAt(index)) && ch != "&"){ index++; }
  31. if(ch == "&"){ // An HTML entity, skip it
  32. while(++index < length && (ch = text.charAt(index)) != ";" && isCharExt(ch)){}
  33. }else{ // A word
  34. start = index;
  35. while(++index < length && isCharExt(text.charAt(index))){}
  36. if(start < length){
  37. words.push(text.substring(start, index));
  38. indices.push(start);
  39. }
  40. }
  41. }
  42. return words;
  43. },
  44. getIndices: function(){
  45. // summary:
  46. // Get the indices of the words. They are in one-to-one correspondence
  47. // tags:
  48. // public
  49. // returns:
  50. // Index array
  51. return this.indices;
  52. }
  53. });
  54. // Register this parser in the SpellCheck plugin.
  55. dojo.subscribe(dijit._scopeName + ".Editor.plugin.SpellCheck.getParser", null, function(sp){
  56. if(sp.parser){ return; }
  57. sp.parser = new dojox.editor.plugins._SpellCheckParser();
  58. });
  59. return dojox.editor.plugins._SpellCheckParser;
  60. });