UNPKG

@biscuitpants/codemirror-spell-checker

Version:
7 lines 9.77 kB
/** * @biscuitpants/codemirror-spell-checker v0.0.12 * Copyright * @link https://github.com/biscuitpants/codemirror-spell-checker * @license MIT */ !function(e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).CodeMirrorSpellChecker=e()}(function(){return function r(o,i,s){function a(t,e){if(!i[t]){if(!o[t]){var n="function"==typeof require&&require;if(!e&&n)return n(t,!0);if(u)return u(t,!0);throw(n=new Error("Cannot find module '"+t+"'")).code="MODULE_NOT_FOUND",n}n=i[t]={exports:{}},o[t][0].call(n.exports,function(e){return a(o[t][1][e]||e)},n,n.exports,r,o,i,s)}return i[t].exports}for(var u="function"==typeof require&&require,e=0;e<s.length;e++)a(s[e]);return a}({1:[function(e,t,n){},{}],2:[function(i,t,e){!function(d){!function(){var e;!function(){"use strict";(e=function(e,o,i,s){s=s||{},this.dictionary=null,this.rules={},this.dictionaryTable={},this.compoundRules=[],this.compoundRuleCodes={},this.replacementTable=[],this.flags=s.flags||{},this.memoized={},this.loaded=!1;var t,a,u,l,c,p=this;function n(e,t){e=p._readFile(e,null,s.asyncLoad);s.asyncLoad?e.then(function(e){t(e)}):t(e)}function r(e){o=e,i&&h()}function f(e){i=e,o&&h()}function h(){for(p.rules=p._parseAFF(o),p.compoundRuleCodes={},a=0,l=p.compoundRules.length;a<l;a++){var e=p.compoundRules[a];for(u=0,c=e.length;u<c;u++)p.compoundRuleCodes[e[u]]=[]}for(a in"ONLYINCOMPOUND"in p.flags&&(p.compoundRuleCodes[p.flags.ONLYINCOMPOUND]=[]),p.dictionaryTable=p._parseDIC(i),p.compoundRuleCodes)0===p.compoundRuleCodes[a].length&&delete p.compoundRuleCodes[a];for(a=0,l=p.compoundRules.length;a<l;a++){var t=p.compoundRules[a],n="";for(u=0,c=t.length;u<c;u++){var r=t[u];r in p.compoundRuleCodes?n+="("+p.compoundRuleCodes[r].join("|")+")":n+=r}p.compoundRules[a]=new RegExp(n,"i")}p.loaded=!0,s.asyncLoad&&s.loadedCallback&&s.loadedCallback(p)}return e&&(p.dictionary=e,o&&i?h():"undefined"!=typeof window&&"chrome"in window&&"extension"in window.chrome&&"getURL"in window.chrome.extension?(t=s.dictionaryPath||"typo/dictionaries",o||n(chrome.extension.getURL(t+"/"+e+"/"+e+".aff"),r),i||n(chrome.extension.getURL(t+"/"+e+"/"+e+".dic"),f)):(t=s.dictionaryPath||(void 0!==d?d+"/dictionaries":"./dictionaries"),o||n(t+"/"+e+"/"+e+".aff",r),i||n(t+"/"+e+"/"+e+".dic",f))),this}).prototype={load:function(e){for(var t in e)e.hasOwnProperty(t)&&(this[t]=e[t]);return this},_readFile:function(e,t,n){if(t=t||"utf8","undefined"!=typeof XMLHttpRequest){var r=new XMLHttpRequest;return r.open("GET",e,n),n&&(o=new Promise(function(e,t){r.onload=function(){200===r.status?e(r.responseText):t(r.statusText)},r.onerror=function(){t(r.statusText)}})),r.overrideMimeType&&r.overrideMimeType("text/plain; charset="+t),r.send(null),n?o:r.responseText}if(void 0!==i){var o=i("fs");try{if(o.existsSync(e))return o.readFileSync(e,t);console.log("Path "+e+" does not exist.")}catch(e){return console.log(e),""}}},_parseAFF:function(e){for(var t={},n=(e=this._removeAffixComments(e)).split(/\r?\n/),r=0,o=n.length;r<o;r++){var i,s=(i=n[r]).split(/\s+/),a=s[0];if("PFX"==a||"SFX"==a){for(var u,l=s[1],c=s[2],p=[],f=r+1,h=r+1+(u=parseInt(s[3],10));f<h;f++){var d,g=(d=n[f].split(/\s+/))[2],m=d[3].split("/"),v=m[0];"0"===v&&(v="");var y=this.parseRuleCodes(m[1]),C=d[4],m={};m.add=v,0<y.length&&(m.continuationClasses=y),"."!==C&&(m.match="SFX"===a?new RegExp(C+"$"):new RegExp("^"+C)),"0"!=g&&(m.remove="SFX"===a?new RegExp(g+"$"):g),p.push(m)}t[l]={type:a,combineable:"Y"==c,entries:p},r+=u}else if("COMPOUNDRULE"===a){for(h=(f=r+1)+(u=parseInt(s[1],10));f<h;f++)d=(i=n[f]).split(/\s+/),this.compoundRules.push(d[1]);r+=u}else"REP"===a?3===(d=i.split(/\s+/)).length&&this.replacementTable.push([d[1],d[2]]):this.flags[a]=s[1]}return t},_removeAffixComments:function(e){return e=(e=(e=(e=e.replace(/^\s*#.*$/gm,"")).replace(/^\s\s*/m,"").replace(/\s\s*$/m,"")).replace(/\n{2,}/g,"\n")).replace(/^\s\s*/,"").replace(/\s\s*$/,"")},_parseDIC:function(e){var t=(e=this._removeDicComments(e)).split(/\r?\n/),n={};function r(e,t){n.hasOwnProperty(e)||(n[e]=null),0<t.length&&(null===n[e]&&(n[e]=[]),n[e].push(t))}for(var o=1,i=t.length;o<i;o++){var s=t[o];if(s){var s=s.split("/",2),a=s[0];if(1<s.length){var u=this.parseRuleCodes(s[1]);"NEEDAFFIX"in this.flags&&-1!=u.indexOf(this.flags.NEEDAFFIX)||r(a,u);for(var l=0,c=u.length;l<c;l++){var p=u[l],f=this.rules[p];if(f)for(var h=this._applyRule(a,f),d=0,g=h.length;d<g;d++){var m=h[d];if(r(m,[]),f.combineable)for(var v=l+1;v<c;v++){var y=u[v],y=this.rules[y];if(y&&y.combineable&&f.type!=y.type)for(var C=this._applyRule(m,y),x=0,R=C.length;x<R;x++)r(C[x],[])}}p in this.compoundRuleCodes&&this.compoundRuleCodes[p].push(a)}}else r(a.trim(),[])}}return n},_removeDicComments:function(e){return e=e.replace(/^\t.*$/gm,"")},parseRuleCodes:function(e){if(e){if(!("FLAG"in this.flags))return e.split("");if("long"!==this.flags.FLAG)return"num"===this.flags.FLAG?e.split(","):void 0;for(var t=[],n=0,r=e.length;n<r;n+=2)t.push(e.substr(n,2));return t}return[]},_applyRule:function(e,t){for(var n=t.entries,r=[],o=0,i=n.length;o<i;o++){var s=n[o];if(!s.match||e.match(s.match)){var a=e;if(s.remove&&(a=a.replace(s.remove,"")),"SFX"===t.type?a+=s.add:a=s.add+a,r.push(a),"continuationClasses"in s)for(var u=0,l=s.continuationClasses.length;u<l;u++){var c=this.rules[s.continuationClasses[u]];c&&(r=r.concat(this._applyRule(a,c)))}}}return r},check:function(e){if(!this.loaded)throw"Dictionary not loaded.";e=e.replace(/^\s\s*/,"").replace(/\s\s*$/,"");if(this.checkExact(e))return!0;if(e.toUpperCase()===e){var t=e[0]+e.substring(1).toLowerCase();if(this.hasFlag(t,"KEEPCASE"))return!1;if(this.checkExact(t))return!0;if(this.checkExact(e.toLowerCase()))return!0}t=e[0].toLowerCase()+e.substring(1);if(t!==e){if(this.hasFlag(t,"KEEPCASE"))return!1;if(this.checkExact(t))return!0}return!1},checkExact:function(e){if(!this.loaded)throw"Dictionary not loaded.";var t,n,r=this.dictionaryTable[e];if(void 0===r){if("COMPOUNDMIN"in this.flags&&e.length>=this.flags.COMPOUNDMIN)for(t=0,n=this.compoundRules.length;t<n;t++)if(e.match(this.compoundRules[t]))return!0}else{if(null===r)return!0;if("object"==typeof r)for(t=0,n=r.length;t<n;t++)if(!this.hasFlag(e,"ONLYINCOMPOUND",r[t]))return!0}return!1},hasFlag:function(e,t,n){if(!this.loaded)throw"Dictionary not loaded.";return!!(t in this.flags&&(n=void 0===n?Array.prototype.concat.apply([],this.dictionaryTable[e]):n)&&-1!==n.indexOf(this.flags[t]))},alphabet:"",suggest:function(e,l){if(!this.loaded)throw"Dictionary not loaded.";if(l=l||5,this.memoized.hasOwnProperty(e)){var t=this.memoized[e].limit;if(l<=t||this.memoized[e].suggestions.length<t)return this.memoized[e].suggestions.slice(0,l)}if(this.check(e))return[];for(var n=0,r=this.replacementTable.length;n<r;n++){var o=this.replacementTable[n];if(-1!==e.indexOf(o[0])){o=e.replace(o[0],o[1]);if(this.check(o))return[o]}}var f=this;function c(e,t){var n,r,o,i,s={},a=f.alphabet.length;for(i in"string"==typeof e&&(i=e,(e={})[i]=!0),e)for(n=0,r=i.length+1;n<r;n++){var u=[i.substring(0,n),i.substring(n)];if(u[1]&&(o=u[0]+u[1].substring(1),t&&!f.check(o)||(o in s?s[o]+=1:s[o]=1)),1<u[1].length&&u[1][1]!==u[1][0]&&(o=u[0]+u[1][1]+u[1][0]+u[1].substring(2),t&&!f.check(o)||(o in s?s[o]+=1:s[o]=1)),u[1])for(var l=u[1].substring(0,1).toUpperCase()===u[1].substring(0,1)?"uppercase":"lowercase",c=0;c<a;c++){var p=f.alphabet[c];(p="uppercase"===l?p.toUpperCase():p)!=u[1].substring(0,1)&&(o=u[0]+p+u[1].substring(1),t&&!f.check(o)||(o in s?s[o]+=1:s[o]=1))}if(u[1])for(c=0;c<a;c++){l=u[0].substring(-1).toUpperCase()===u[0].substring(-1)&&u[1].substring(0,1).toUpperCase()===u[1].substring(0,1)?"uppercase":"lowercase",p=f.alphabet[c];"uppercase"===l&&(p=p.toUpperCase()),o=u[0]+p+u[1],t&&!f.check(o)||(o in s?s[o]+=1:s[o]=1)}}return s}return f.alphabet="abcdefghijklmnopqrstuvwxyz",this.memoized[e]={suggestions:function(e){var t,n=c(e),r=c(n,!0);for(t in n)f.check(t)&&(t in r?r[t]+=n[t]:r[t]=n[t]);var o=[];for(u in r)r.hasOwnProperty(u)&&o.push([u,r[u]]);o.sort(function(e,t){var n=e[1],r=t[1];return n<r?-1:r<n?1:t[0].localeCompare(e[0])}).reverse();var i=[],s="lowercase";e.toUpperCase()===e?s="uppercase":e.substr(0,1).toUpperCase()+e.substr(1).toLowerCase()===e&&(s="capitalized");for(var a=l,u=0;u<Math.min(a,o.length);u++)"uppercase"===s?o[u][0]=o[u][0].toUpperCase():"capitalized"===s&&(o[u][0]=o[u][0].substr(0,1).toUpperCase()+o[u][0].substr(1)),f.hasFlag(o[u][0],"NOSUGGEST")||-1!=i.indexOf(o[u][0])?a++:i.push(o[u][0]);return i}(e),limit:l},this.memoized[e].suggestions}}}(),void 0!==t&&(t.exports=e)}.call(this)}.call(this,"/node_modules/typo-js")},{fs:1}],3:[function(e,t,n){"use strict";var s=e("typo-js");function a(e){var n,r,o,i,t="en_US";(e=e||{}).dictionaryLanguage&&(t=e.dictionaryLanguage),null!=e.editorInstance?(a.typo=new s(t,void 0,void 0,{platform:"any",dictionaryPath:"https://spellcheck-dictionaries.github.io/"}),n=/^[^!"#$%&()*+,\-./:;<=>?@[\\\]^_`{|}~\s]+/,e.matchRegex&&e.matchRegex instanceof RegExp&&(n=e.matchRegex),r=/[0-9'_-]+/,e.ignoreRegex&&e.ignoreRegex instanceof RegExp&&(r=e.ignoreRegex),o=[],e.customWords&&(o=e.customWords instanceof Function?e.customWords():e.customWords),e.commentStart&&(i=new RegExp("\\s*"+e.commentStart)),t={token:function(e){if(i&&e.string.match(i))return e.next(),null;var t=e.match(n,!0);return t?(t=t[0]).match(r)||!a.typo||a.typo.check(t)||~o.indexOf(t)?void 0:"spell-error":(e.next(),null)}},e.editorInstance.addOverlay(t)):console.error("CodeMirror Spell Checker: You must provide an instance of a CodeMirror editor via the option `editorInstance`")}a.typo,t.exports=a},{"typo-js":2}]},{},[3])(3)});