2016-12-20 12:34:20 +00:00
|
|
|
"use strict";var Typo=function(e,t,r,n){if(n=n||{},this.platform=n.platform||"chrome",this.dictionary=null,this.rules={},this.dictionaryTable={},this.compoundRules=[],this.compoundRuleCodes={},this.replacementTable=[],this.flags=n.flags||{},e){if(this.dictionary=e,"chrome"==this.platform)t||(t=this._readFile(chrome.extension.getURL("lib/typo/dictionaries/"+e+"/"+e+".aff"))),r||(r=this._readFile(chrome.extension.getURL("lib/typo/dictionaries/"+e+"/"+e+".dic")));else{var i=n.dictionaryPath||"";t||(t=this._readFile(i+"/"+e+"/"+e+".aff")),r||(r=this._readFile(i+"/"+e+"/"+e+".dic"))}this.rules=this._parseAFF(t),this.compoundRuleCodes={};for(var a=0,s=this.compoundRules.length;s>a;a++)for(var o=this.compoundRules[a],l=0,u=o.length;u>l;l++)this.compoundRuleCodes[o[l]]=[];"ONLYINCOMPOUND"in this.flags&&(this.compoundRuleCodes[this.flags.ONLYINCOMPOUND]=[]),this.dictionaryTable=this._parseDIC(r);for(var a in this.compoundRuleCodes)0==this.compoundRuleCodes[a].length&&delete this.compoundRuleCodes[a];for(var a=0,s=this.compoundRules.length;s>a;a++){for(var h=this.compoundRules[a],c="",l=0,u=h.length;u>l;l++){var p=h[l];c+=p in this.compoundRuleCodes?"("+this.compoundRuleCodes[p].join("|")+")":p}this.compoundRules[a]=RegExp(c,"i")}}return this};Typo.prototype={load:function(e){for(var t in e)this[t]=e[t];return this},_readFile:function(e,t){t||(t="ISO8859-1");var r=new XMLHttpRequest;return r.open("GET",e,!1),r.overrideMimeType&&r.overrideMimeType("text/plain; charset="+t),r.send(null),r.responseText},_parseAFF:function(e){var t={};e=this._removeAffixComments(e);for(var r=e.split("\n"),n=0,i=r.length;i>n;n++){var a=r[n],s=a.split(/\s+/),o=s[0];if("PFX"==o||"SFX"==o){for(var l=s[1],u=s[2],h=parseInt(s[3],10),c=[],p=n+1,f=n+1+h;f>p;p++){var a=r[p],d=a.split(/\s+/),v=d[2],g=d[3].split("/"),m=g[0];"0"===m&&(m="");var y=this.parseRuleCodes(g[1]),_=d[4],C={};C.add=m,y.length>0&&(C.continuationClasses=y),"."!==_&&(C.match=RegExp("SFX"===o?_+"$":"^"+_)),"0"!=v&&(C.remove="SFX"===o?RegExp(v+"$"):v),c.push(C)}t[l]={type:o,combineable:"Y"==u,entries:c},n+=h}else if("COMPOUNDRULE"===o){for(var h=parseInt(s[1],10),p=n+1,f=n+1+h;f>p;p++){var a=r[p],d=a.split(/\s+/);this.compoundRules.push(d[1])}n+=h}else if("REP"===o){var d=a.split(/\s+/);3===d.length&&this.replacementTable.push([d[1],d[2]])}else this.flags[o]=s[1]}return t},_removeAffixComments:function(e){return e=e.replace(/#.*$/gm,""),e=e.replace(/^\s\s*/m,"").replace(/\s\s*$/m,""),e=e.replace(/\n{2,}/g,"\n"),e=e.replace(/^\s\s*/,"").replace(/\s\s*$/,"")},_parseDIC:function(e){function t(e,t){e in n&&"object"==typeof n[e]||(n[e]=[]),n[e].push(t)}e=this._removeDicComments(e);for(var r=e.split("\n"),n={},i=1,a=r.length;a>i;i++){var s=r[i],o=s.split("/",2),l=o[0];if(o.length>1){var u=this.parseRuleCodes(o[1]);"NEEDAFFIX"in this.flags&&-1!=u.indexOf(this.flags.NEEDAFFIX)||t(l,u);for(var h=0,c=u.length;c>h;h++){var p=u[h],f=this.rules[p];if(f)for(var d=this._applyRule(l,f),v=0,g=d.length;g>v;v++){var m=d[v];if(t(m,[]),f.combineable)for(var y=h+1;c>y;y++){var _=u[y],C=this.rules[_];if(C&&C.combineable&&f.type!=C.type)for(var R=this._applyRule(m,C),b=0,F=R.length;F>b;b++){var x=R[b];t(x,[])}}}p in this.compoundRuleCodes&&this.compoundRuleCodes[p].push(l)}}else t(l.trim(),[])}return n},_removeDicComments:function(e){return e=e.replace(/^\t.*$/gm,"")},parseRuleCodes:function(e){if(!e)return[];if(!("FLAG"in this.flags))return e.split("");if("long"===this.flags.FLAG){for(var t=[],r=0,n=e.length;n>r;r+=2)t.push(e.substr(r,2));return t}return"num"===this.flags.FLAG?textCode.split(","):void 0},_applyRule:function(e,t){for(var r=t.entries,n=[],i=0,a=r.length;a>i;i++){var s=r[i];if(!s.match||e.match(s.match)){var o=e;if(s.remove&&(o=o.replace(s.remove,"")),"SFX"===t.type?o+=s.add:o=s.add+o,n.push(o),"continuationClasses"in s)for(var l=0,u=s.continuationClasses.length;u>l;l++){var h=this.rules[s.continuationClasses[l]];h&&(n=n.concat(this._applyRule(o,h)))}}}return n},check:function(e){var t=e.replace(/^\s\s*/,"").replace(/\s\s*$/,"");if(this.checkExact(t))return!0;if(t.toUpperCase()===t){var r=t[0]+t.subst
|